Refactor
This commit is contained in:
@@ -9,7 +9,8 @@
|
|||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/AppTheme">
|
android:theme="@style/AppTheme">
|
||||||
<activity android:name=".LoginActivity">
|
<activity android:name=".LoginActivity"
|
||||||
|
android:exported="true">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ public class ChatFragment extends Fragment {
|
|||||||
|
|
||||||
// Convert epoch timestamp to formatted date string
|
// Convert epoch timestamp to formatted date string
|
||||||
Date date = new Date(timestamp * 1000L);
|
Date date = new Date(timestamp * 1000L);
|
||||||
this.date = new SimpleDateFormat("dd.MM.YYYY HH:mm", Locale.getDefault()).format(date);
|
this.date = new SimpleDateFormat("dd.MM.yyyy HH:mm", Locale.getDefault()).format(date);
|
||||||
|
|
||||||
// Check if the sender's user id still exist in the database.
|
// Check if the sender's user id still exist in the database.
|
||||||
// If so, extract their nickname
|
// If so, extract their nickname
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public class HomeFragment extends Fragment {
|
public class HomeFragment extends Fragment {
|
||||||
private View root;
|
private View root;
|
||||||
@@ -44,7 +43,6 @@ public class HomeFragment extends Fragment {
|
|||||||
private DataSnapshot dsUsers, dsGames;
|
private DataSnapshot dsUsers, dsGames;
|
||||||
private ProgressBar progressBar;
|
private ProgressBar progressBar;
|
||||||
private TextView welcome, nextDate, nextHost;
|
private TextView welcome, nextDate, nextHost;
|
||||||
private Button btnSignOut, btnVote, btnSuggest;
|
|
||||||
private Spinner spinnerGames;
|
private Spinner spinnerGames;
|
||||||
private Map<Long, Integer> games = new HashMap<>();
|
private Map<Long, Integer> games = new HashMap<>();
|
||||||
private String userNickname, nextDateID;
|
private String userNickname, nextDateID;
|
||||||
@@ -55,11 +53,11 @@ public class HomeFragment extends Fragment {
|
|||||||
welcome = root.findViewById(R.id.text_home);
|
welcome = root.findViewById(R.id.text_home);
|
||||||
nextDate = root.findViewById(R.id.text_home2);
|
nextDate = root.findViewById(R.id.text_home2);
|
||||||
nextHost = root.findViewById(R.id.text_home5);
|
nextHost = root.findViewById(R.id.text_home5);
|
||||||
btnSignOut = root.findViewById(R.id.logout);
|
|
||||||
btnVote = root.findViewById(R.id.vote);
|
|
||||||
btnSuggest = root.findViewById(R.id.suggest);
|
|
||||||
spinnerGames = root.findViewById(R.id.spinner_games);
|
spinnerGames = root.findViewById(R.id.spinner_games);
|
||||||
progressBar = root.findViewById(R.id.progressBar);
|
progressBar = root.findViewById(R.id.progressBar);
|
||||||
|
Button btnSignOut = root.findViewById(R.id.logout);
|
||||||
|
Button btnVote = root.findViewById(R.id.vote);
|
||||||
|
Button btnSuggest = root.findViewById(R.id.suggest);
|
||||||
|
|
||||||
// Set button listeners
|
// Set button listeners
|
||||||
btnSignOut.setOnClickListener(v -> signOutUser());
|
btnSignOut.setOnClickListener(v -> signOutUser());
|
||||||
@@ -76,28 +74,41 @@ public class HomeFragment extends Fragment {
|
|||||||
refGames = FirebaseDatabase.getInstance().getReference("spiele");
|
refGames = FirebaseDatabase.getInstance().getReference("spiele");
|
||||||
refUsers = FirebaseDatabase.getInstance().getReference("spieler");
|
refUsers = FirebaseDatabase.getInstance().getReference("spieler");
|
||||||
|
|
||||||
// Get list of registered users from database
|
// Get a list of all players from the database and store as DataSnapshot,
|
||||||
refUsers.addValueEventListener(new ValueEventListener() {
|
// then check whether a new event needs to be added to the database.
|
||||||
|
refUsers.addListenerForSingleValueEvent(new ValueEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
|
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
|
||||||
dsUsers = dataSnapshot;
|
dsUsers = dataSnapshot;
|
||||||
setValues();
|
updateUI();
|
||||||
|
checkUpcomingEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCancelled(@NonNull DatabaseError databaseError) {
|
public void onCancelled(@NonNull DatabaseError databaseError) {
|
||||||
Toast.makeText(getContext(), R.string.db_comm_err, Toast.LENGTH_SHORT).show();
|
Toast.makeText(getContext(), R.string.db_comm_err, Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Get list of available games from database
|
// Update the UI whenever the list of players changes in the database
|
||||||
|
refUsers.addValueEventListener(new ValueEventListener() {
|
||||||
|
@Override
|
||||||
|
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
|
||||||
|
dsUsers = dataSnapshot;
|
||||||
|
updateUI();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onCancelled(@NonNull DatabaseError databaseError) {
|
||||||
|
Toast.makeText(getContext(), R.string.db_comm_err, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update the UI whenever the list of games changes in the database
|
||||||
refGames.addValueEventListener(new ValueEventListener() {
|
refGames.addValueEventListener(new ValueEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
|
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
|
||||||
dsGames = dataSnapshot;
|
dsGames = dataSnapshot;
|
||||||
updateGamesList();
|
updateGamesList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCancelled(@NonNull DatabaseError databaseError) {
|
public void onCancelled(@NonNull DatabaseError databaseError) {
|
||||||
Toast.makeText(getContext(), R.string.db_comm_err, Toast.LENGTH_SHORT).show();
|
Toast.makeText(getContext(), R.string.db_comm_err, Toast.LENGTH_SHORT).show();
|
||||||
@@ -116,15 +127,90 @@ public class HomeFragment extends Fragment {
|
|||||||
if(isActive) {
|
if(isActive) {
|
||||||
progressBar.setVisibility(View.VISIBLE);
|
progressBar.setVisibility(View.VISIBLE);
|
||||||
// Disable user interaction while progress bar is visible
|
// Disable user interaction while progress bar is visible
|
||||||
Objects.requireNonNull(getActivity()).getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
|
requireActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
|
||||||
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
|
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
|
||||||
} else if(progressBar.getVisibility() == View.VISIBLE) {
|
} else if(progressBar.getVisibility() == View.VISIBLE) {
|
||||||
progressBar.setVisibility(View.GONE);
|
progressBar.setVisibility(View.GONE);
|
||||||
// Re-enable user interaction when progress bar is gone
|
// Re-enable user interaction when progress bar is gone
|
||||||
Objects.requireNonNull(getActivity()).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
|
requireActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Then get the most recent event next and check if .
|
||||||
|
* If so, a new event is added.
|
||||||
|
*/
|
||||||
|
private void checkUpcomingEvent() {
|
||||||
|
refEventDates.orderByKey().limitToLast(1).addValueEventListener(new ValueEventListener() {
|
||||||
|
@Override
|
||||||
|
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
|
||||||
|
for(DataSnapshot ds : dataSnapshot.getChildren()) {
|
||||||
|
nextDateID = ds.getKey();
|
||||||
|
long epoch = Long.parseLong(nextDateID);
|
||||||
|
|
||||||
|
if(epoch < (System.currentTimeMillis() / 1000L)) {
|
||||||
|
addUpcomingEvent(epoch);
|
||||||
|
}
|
||||||
|
|
||||||
|
Date date = new Date(epoch * 1000L);
|
||||||
|
String s = new SimpleDateFormat("dd. MMMM yyyy", Locale.getDefault()).format(date);
|
||||||
|
nextDate.setText(s);
|
||||||
|
|
||||||
|
String sNextHostUID = (String) ds.child("gastgeber").getValue();
|
||||||
|
String sNextHost = (String)dsUsers.child(sNextHostUID).child("nickname").getValue();
|
||||||
|
nextHost.setText(sNextHost);
|
||||||
|
|
||||||
|
if(ds.hasChild("abstimmung_spiele")) {
|
||||||
|
games = new HashMap<>();
|
||||||
|
for(DataSnapshot dataSnapshot1 : ds.child("abstimmung_spiele").getChildren()) {
|
||||||
|
long curInt = (long)dataSnapshot1.getValue();
|
||||||
|
|
||||||
|
if(games.containsKey(curInt)) games.put(curInt, games.get(curInt)+1);
|
||||||
|
else games.put(curInt, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setProgressBar(false);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onCancelled(@NonNull DatabaseError databaseError) {
|
||||||
|
Toast.makeText(getContext(), R.string.db_comm_err, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines the date and host of the next event and writes the details to the database.
|
||||||
|
* If this was a real life app and not just an example project, it would be advisable to handle
|
||||||
|
* event setup automatically with scheduled functions running on the server (a possible
|
||||||
|
* solution could be Google Cloud Functions for Firebase).
|
||||||
|
* @param prevEventTimestamp Unix epoch time of the previous event
|
||||||
|
*/
|
||||||
|
private void addUpcomingEvent(long prevEventTimestamp) {
|
||||||
|
// Programmatically determine the host of the upcoming event by iterating through the list
|
||||||
|
// of registered users and comparing the epoch timestamps of their most recently hosted event.
|
||||||
|
// The lowest timestamp will determine the new host.
|
||||||
|
String nextHostUID = null;
|
||||||
|
long ll = 0;
|
||||||
|
for(DataSnapshot dataSnapshot : dsUsers.getChildren()) {
|
||||||
|
long ts = (long)dataSnapshot.child("zuletzt_gehostet").getValue();
|
||||||
|
if(ll == 0 || ts < ll) {
|
||||||
|
ll = ts;
|
||||||
|
nextHostUID = dataSnapshot.getKey();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate epoch timestamp of upcoming event
|
||||||
|
// It will take place exactly 1 week after the last event.
|
||||||
|
// 7 days * 24 hours * 60 minutes * 60 seconds = 604800
|
||||||
|
long nextEventTimestamp = prevEventTimestamp + 604800;
|
||||||
|
|
||||||
|
// Write to database
|
||||||
|
refEventDates.child(String.valueOf(nextEventTimestamp)).child("gastgeber").setValue(nextHostUID);
|
||||||
|
refUsers.child(nextHostUID).child("zuletzt_gehostet").setValue(nextEventTimestamp);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Displays a popup dialog with an text input field. The user is supposed to type in the title
|
* Displays a popup dialog with an text input field. The user is supposed to type in the title
|
||||||
* of a game that he would like to add to the database.
|
* of a game that he would like to add to the database.
|
||||||
@@ -167,90 +253,18 @@ public class HomeFragment extends Fragment {
|
|||||||
spinnerGames.setAdapter(adapter);
|
spinnerGames.setAdapter(adapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setValues() {
|
private void updateUI() {
|
||||||
// Get details of upcoming event from database
|
|
||||||
refEventDates.orderByKey().limitToLast(1).addValueEventListener(new ValueEventListener() {
|
|
||||||
@Override
|
|
||||||
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
|
|
||||||
for(DataSnapshot ds : dataSnapshot.getChildren()) {
|
|
||||||
nextDateID = ds.getKey();
|
|
||||||
long epoch = Long.parseLong(nextDateID);
|
|
||||||
|
|
||||||
if(epoch < (System.currentTimeMillis() / 1000L)) {
|
|
||||||
addUpcomingEvent(epoch);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Date date = new Date(epoch * 1000L);
|
|
||||||
String s = new SimpleDateFormat("dd. MMMM YYYY", Locale.getDefault()).format(date);
|
|
||||||
nextDate.setText(s);
|
|
||||||
|
|
||||||
String sNextHostUID = (String) ds.child("gastgeber").getValue();
|
|
||||||
String sNextHost = (String)dsUsers.child(sNextHostUID).child("nickname").getValue();
|
|
||||||
nextHost.setText(sNextHost + "'s place");
|
|
||||||
|
|
||||||
if(ds.hasChild("abstimmung_spiele")) {
|
|
||||||
games = new HashMap<>();
|
|
||||||
for(DataSnapshot dataSnapshot1 : ds.child("abstimmung_spiele").getChildren()) {
|
|
||||||
long curInt = (long)dataSnapshot1.getValue();
|
|
||||||
|
|
||||||
if(games.containsKey(curInt)) games.put(curInt, games.get(curInt)+1);
|
|
||||||
else games.put(curInt, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
updateGamesList();
|
|
||||||
setProgressBar(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCancelled(@NonNull DatabaseError databaseError) {
|
|
||||||
Toast.makeText(getContext(), R.string.db_comm_err, Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
userNickname = (String)dsUsers.child(mUser.getUid()).child("nickname").getValue();
|
userNickname = (String)dsUsers.child(mUser.getUid()).child("nickname").getValue();
|
||||||
|
|
||||||
String sWelcome = userNickname + getString(R.string.welcome_back);
|
String sWelcome = userNickname + getString(R.string.welcome_back);
|
||||||
welcome.setText(sWelcome);
|
welcome.setText(sWelcome);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Determines the date and host of the next event and writes the details to the database.
|
|
||||||
* If this was a real life app and not just an example project, it would be advisable to handle
|
|
||||||
* event setup automatically with scheduled functions running on the server (a possible
|
|
||||||
* solution could be Google Cloud Functions for Firebase).
|
|
||||||
* @param prevEventTimestamp Unix epoch time of the previous event
|
|
||||||
*/
|
|
||||||
private void addUpcomingEvent(long prevEventTimestamp) {
|
|
||||||
// Programmatically determine the host of the upcoming event by iterating through the list
|
|
||||||
// of registered users and comparing the epoch timestamps of their most recently hosted event.
|
|
||||||
// The lowest timestamp will determine the new host.
|
|
||||||
String nextHostUID = null;
|
|
||||||
long ll = 0;
|
|
||||||
for(DataSnapshot dataSnapshot : dsUsers.getChildren()) {
|
|
||||||
long ts = (long)dataSnapshot.child("zuletzt_gehostet").getValue();
|
|
||||||
if(ll == 0 || ts < ll) {
|
|
||||||
ll = ts;
|
|
||||||
nextHostUID = dataSnapshot.getKey();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate epoch timestamp of upcoming event
|
|
||||||
// It will take place exactly 1 week after the last event.
|
|
||||||
// 7 days * 24 hours * 60 minutes * 60 seconds = 604800
|
|
||||||
long nextEventTimestamp = prevEventTimestamp + 604800;
|
|
||||||
|
|
||||||
// Write to database
|
|
||||||
refEventDates.child(String.valueOf(nextEventTimestamp)).child("gastgeber").setValue(nextHostUID);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Signs out the current Firebase user and switches to the login interface.
|
* Signs out the current Firebase user and switches to the login interface.
|
||||||
*/
|
*/
|
||||||
private void signOutUser() {
|
private void signOutUser() {
|
||||||
FirebaseAuth.getInstance().signOut();
|
FirebaseAuth.getInstance().signOut();
|
||||||
startActivity(new Intent(getActivity(), LoginActivity.class));
|
startActivity(new Intent(getActivity(), LoginActivity.class));
|
||||||
Objects.requireNonNull(getActivity()).finish();
|
requireActivity().finish();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,7 @@ import java.util.List;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class RateFragment extends Fragment {
|
public class RatingFragment extends Fragment {
|
||||||
private EventDate selectedEvent;
|
private EventDate selectedEvent;
|
||||||
private FirebaseUser mUser;
|
private FirebaseUser mUser;
|
||||||
private DatabaseReference refUsers, refEvents;
|
private DatabaseReference refUsers, refEvents;
|
||||||
@@ -43,11 +43,10 @@ public class RateFragment extends Fragment {
|
|||||||
private RatingBar rbOverall, rbFood, rbHost;
|
private RatingBar rbOverall, rbFood, rbHost;
|
||||||
private TextView tvHost;
|
private TextView tvHost;
|
||||||
private TextView tvOverallRatings, tvFoodRatings, tvHostRatings;
|
private TextView tvOverallRatings, tvFoodRatings, tvHostRatings;
|
||||||
private Button btnRate;
|
|
||||||
|
|
||||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||||
ViewGroup container, Bundle savedInstanceState) {
|
ViewGroup container, Bundle savedInstanceState) {
|
||||||
root = inflater.inflate(R.layout.fragment_rate, container, false);
|
root = inflater.inflate(R.layout.fragment_rating, container, false);
|
||||||
spinnerPastEvents = root.findViewById(R.id.spinner_rating);
|
spinnerPastEvents = root.findViewById(R.id.spinner_rating);
|
||||||
rbOverall = root.findViewById(R.id.ratingBar_overall);
|
rbOverall = root.findViewById(R.id.ratingBar_overall);
|
||||||
rbFood = root.findViewById(R.id.ratingBar_food);
|
rbFood = root.findViewById(R.id.ratingBar_food);
|
||||||
@@ -56,7 +55,7 @@ public class RateFragment extends Fragment {
|
|||||||
tvOverallRatings = root.findViewById(R.id.textView_rate1);
|
tvOverallRatings = root.findViewById(R.id.textView_rate1);
|
||||||
tvFoodRatings = root.findViewById(R.id.textView_rate2);
|
tvFoodRatings = root.findViewById(R.id.textView_rate2);
|
||||||
tvHostRatings = root.findViewById(R.id.textView_rate3);
|
tvHostRatings = root.findViewById(R.id.textView_rate3);
|
||||||
btnRate = root.findViewById(R.id.rate);
|
Button btnRate = root.findViewById(R.id.rate);
|
||||||
|
|
||||||
// Set rate button listener
|
// Set rate button listener
|
||||||
btnRate.setOnClickListener(v -> ratePastEvent());
|
btnRate.setOnClickListener(v -> ratePastEvent());
|
||||||
@@ -234,7 +233,7 @@ public class RateFragment extends Fragment {
|
|||||||
|
|
||||||
String getFormattedDate() {
|
String getFormattedDate() {
|
||||||
Date date = new Date(this.epochTimestamp * 1000L);
|
Date date = new Date(this.epochTimestamp * 1000L);
|
||||||
return new SimpleDateFormat("EEEE, dd. MMMM YYYY", Locale.getDefault()).format(date);
|
return new SimpleDateFormat("EEEE, dd. MMMM yyyy", Locale.getDefault()).format(date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,7 +67,7 @@
|
|||||||
android:id="@+id/text_home4"
|
android:id="@+id/text_home4"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="The next event will be held on"
|
android:text="The next event will be on"
|
||||||
android:textAlignment="center"
|
android:textAlignment="center"
|
||||||
android:textSize="14sp" />
|
android:textSize="14sp" />
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@
|
|||||||
android:id="@+id/text_home3"
|
android:id="@+id/text_home3"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="at"
|
android:text="hosted by"
|
||||||
android:textAlignment="center"
|
android:textAlignment="center"
|
||||||
android:textSize="14sp" />
|
android:textSize="14sp" />
|
||||||
|
|
||||||
|
|||||||
0
app/src/main/res/layout/fragment_rate.xml → app/src/main/res/layout/fragment_rating.xml
Executable file → Normal file
0
app/src/main/res/layout/fragment_rate.xml → app/src/main/res/layout/fragment_rating.xml
Executable file → Normal file
@@ -13,9 +13,9 @@
|
|||||||
|
|
||||||
<fragment
|
<fragment
|
||||||
android:id="@+id/navigation_rate"
|
android:id="@+id/navigation_rate"
|
||||||
android:name="com.example.iubhgamerapp.ui.RateFragment"
|
android:name="com.example.iubhgamerapp.ui.RatingFragment"
|
||||||
android:label="@string/title_rate"
|
android:label="@string/title_rate"
|
||||||
tools:layout="@layout/fragment_rate" />
|
tools:layout="@layout/fragment_rating" />
|
||||||
|
|
||||||
<fragment
|
<fragment
|
||||||
android:id="@+id/navigation_chat"
|
android:id="@+id/navigation_chat"
|
||||||
|
|||||||
Reference in New Issue
Block a user