//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! (answer with only the parts that need to be fixed
- Assistant
Sure, I'll adjust the relevant parts of your code snippet to ensure that the current user is not duplicated if they happen to be the last user on the leaderboard.
// ... boolean currentUserDisplayed = false; boolean isCurrentUserLast = false; // Add a flag to check if current user is last // 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; // Check if the current user is the last one isCurrentUserLast = !documentIterator.hasNext(); // Assuming there's an iterator you can use to check this // We exit from the for-loop as we found our current user. break; } lastUserDoc = document; rank++; } // Reset the rank counter rank = 1; // ... if (!currentUserDisplayed && currentUserDoc != null) { // If the current user is not in the top 10, display their rank, unless they are the last user if (!isCurrentUserLast) { // Only add the placeholder if the current user isn't the last user placeholder(leaderboardLayout); } createLeaderboardEntry(currentUserDoc, leaderboardLayout, currentUserRank, true); // Use currentUserRank which has the accurate rank of the current user } if (lastUserDoc != null && rank > 10 && !isCurrentUserLast) { // Display the last user with the worst score if there are more than 10 users // and the current user is not the last user placeholder(leaderboardLayout); createLeaderboardEntry(lastUserDoc, leaderboardLayout, rank - 1, false); } // ...Here, I've introduced a new boolean variable
isCurrentUserLastto check whether the current user is the last one in the list. We then use this flag to determine whether we should display the placeholder for the current user as well as the last entry in the leaderboard. If the current user is last, we do not add the placeholder again and only display the current user's entry without adding them as the last user again.