//FirebaseDB private void leaderboard() { // Query to get all users ordered by score in…
- User
//FirebaseDB private void leaderboard() { // Query to get all users ordered by score in descending order Query allUsersQuery = usersRef.orderBy(“score”, Query.Direction.DESCENDING);
allUsersQuery.get().addOnCompleteListener(task -> { if (task.isSuccessful()) { LinearLayout leaderboardLayout = findViewById(R.id.leaderboardLayout); leaderboardLayout.removeAllViews(); // Clear the layout
String currentUserId = mAuth.getUid(); // Get current user id int rank = 1; // Start counting ranks from the top player int currentUserRank = 0; // Added a variable to keep track of the current user rank
boolean currentUserDisplayed = false; QueryDocumentSnapshot currentUserDoc = null; QueryDocumentSnapshot lastUserDoc = null;
// Iterate through all documents to populate leaderboard and find current and last user for (QueryDocumentSnapshot document : task.getResult()) { if (currentUserId.equals(document.getId())) { currentUserDoc = document; currentUserRank = rank; // We exit from the for-loop as we found our current user. break; } lastUserDoc = document; rank++; }
// Reset the rank counter rank = 1;
// Iterate and display for the top 10 again, now that we have the current user document for (QueryDocumentSnapshot document : task.getResult()) { if (rank <= 10) { createLeaderboardEntry(document, leaderboardLayout, rank, currentUserId.equals(document.getId())); if (currentUserId.equals(document.getId())) { currentUserDisplayed = true; // The current user is within the top 10 } } lastUserDoc = document; // Keep track of the last user document for the worse score rank++; }
if (!currentUserDisplayed && currentUserDoc != null) { // If the current user is not in the top 10, display their rank placeholder(leaderboardLayout); createLeaderboardEntry(currentUserDoc, leaderboardLayout, currentUserRank, true); // Use currentUserRank which has the accurate rank of the current user }
if (lastUserDoc != null && rank > 10) { // Display the last user with the worst score if there are more than 10 users placeholder(leaderboardLayout); createLeaderboardEntry(lastUserDoc, leaderboardLayout, rank - 1, false); }
} else { Log.w(“TagDB”, “Error getting documents.”, task.getException()); } }); }
If the current user is the last, it becomes duplicate. One time as highlighted current user, and one time as last user. Fix it!
- Assistant
To fix the duplication issue when the current user is also the last user on the leaderboard, you need to set some condition to check if the current user rank matches the final rank in the list. If it does, you should not add the last user again to the leaderboard UI since it will have already been added when the current user was displayed. Here is the revised code snippet:
// ... previous code ... if (!currentUserDisplayed && currentUserDoc != null) { // If the current user is not in the top 10, display their rank only if they are not the last one boolean isCurrentUserLast = (currentUserRank == rank - 1); // Display placeholder only if the current user is not the last if (!isCurrentUserLast) { placeholder(leaderboardLayout); } createLeaderboardEntry(currentUserDoc, leaderboardLayout, currentUserRank, true); // Use currentUserRank which has the accurate rank of the current user } if (lastUserDoc != null && rank > 10 && currentUserRank != rank - 1) { // Display the last user with the worst score only if they are not the current user placeholder(leaderboardLayout); createLeaderboardEntry(lastUserDoc, leaderboardLayout, rank - 1, false); } // ... rest of your code ...