Last active
June 6, 2017 07:29
-
-
Save truizlop/22b7241714844f489f7c to your computer and use it in GitHub Desktop.
Retrieving leaderboards to display them in a custom view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class GetLeaderboard{ | |
private static final int MAX_RESULTS = 25; | |
/** | |
* Check documentation for LeaderboardVariant to see possible values for timeSpan and collection | |
*/ | |
public void getTopScores(GoogleApiClient googleApiClient, String leaderboardID, int timeSpan, int collection) { | |
PendingResult<Leaderboards.LoadScoresResult> pendingScores = Games. | |
Leaderboards.loadTopScores(googleApiClient, leaderboardID, | |
timeSpan, collection, MAX_RESULTS, true); | |
processPendingResult(pendingScores); | |
} | |
/** | |
* Check documentation for LeaderboardVariant to see possible values for timeSpan and collection | |
*/ | |
public void getPlayerCenteredScores(GoogleApiClient googleApiClient, String leaderboardID, int timeSpan, int collection) { | |
PendingResult<Leaderboards.LoadScoresResult> pendingScores = Games. | |
Leaderboards.loadPlayerCenteredScores(googleApiClient, leaderboardID, | |
timeSpan, collection, MAX_RESULTS, true); | |
processPendingResult(pendingScores); | |
} | |
protected void processPendingResult(PendingResult<Leaderboards.LoadScoresResult> pendingScores){ | |
pendingScores.setResultCallback(new ResultCallback<Leaderboards.LoadScoresResult>() { | |
@Override | |
public void onResult(Leaderboards.LoadScoresResult loadScoresResult) { | |
List<LeaderboardScore> leaderboardScores = extractScores(loadScoresResult); | |
notifyScoresLoaded(leaderboardScores); | |
} | |
}); | |
} | |
protected List<LeaderboardScore> extractScores(Leaderboards.LoadScoresResult loadScoresResult) { | |
LeaderboardScoreBuffer buffer = loadScoresResult.getScores(); | |
List<LeaderboardScore> leaderboardScores = new ArrayList<>(); | |
for(int i = 0; i < buffer.getCount(); i++){ | |
leaderboardScores.add(buffer.get(i)); | |
} | |
return leaderboardScores; | |
} | |
public abstract void notifyScoresLoaded(List<LeaderboardScore> scoreList); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment