Skip to content

Commit

Permalink
Refactored Results activity and retrieve best scores on first connect…
Browse files Browse the repository at this point in the history
…, because should be sync after that.
  • Loading branch information
chrisjluc committed Mar 1, 2015
1 parent 1b40612 commit e77c93d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

public class BaseGooglePlayServicesActivity extends BaseActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {

protected static int RC_SIGN_IN = 9001;
private static String BGP_PREF_MAME = "base_google_play";
private static String FIRST_CONNECT = "first_connect";
protected boolean mInSignInFlow = false;
protected boolean mResolvingConnectionFailure = false;
protected boolean mAutoStartSignInflow = true;
Expand Down Expand Up @@ -84,30 +85,39 @@ protected void onActivityResult(int requestCode, int resultCode,

@Override
public void onConnected(Bundle bundle) {
// Push high scores
SharedPreferences prefs = getSharedPreferences(ResultsActivity.PREF_NAME, MODE_PRIVATE);
String easyLeaderboardId = getString(R.string.leaderboard_highest_scores__easy);
String mediumLeaderboardId = getString(R.string.leaderboard_highest_scores__medium);
String hardLeaderboardId = getString(R.string.leaderboard_highest_scores__hard);

SharedPreferences prefs = getSharedPreferences(BGP_PREF_MAME, MODE_PRIVATE);
boolean isFirstTime = prefs.getBoolean(FIRST_CONNECT, true);
if (isFirstTime) {
SharedPreferences.Editor editor = getSharedPreferences(BGP_PREF_MAME, MODE_PRIVATE).edit();
editor.putBoolean(FIRST_CONNECT, false);
editor.commit();

// Push high scores
prefs = getSharedPreferences(ResultsActivity.PREF_NAME, MODE_PRIVATE);
String easyLeaderboardId = getString(R.string.leaderboard_highest_scores__easy);
String mediumLeaderboardId = getString(R.string.leaderboard_highest_scores__medium);
String hardLeaderboardId = getString(R.string.leaderboard_highest_scores__hard);
// String advancedLeaderboardId = getString(R.string.leaderboard_highest_scores__advanced);

int easyScore = prefs.getInt(ResultsActivity.SCORE_PREFIX + GameDifficulty.Easy, 0);
int mediumScore = prefs.getInt(ResultsActivity.SCORE_PREFIX + GameDifficulty.Medium, 0);
int hardScore = prefs.getInt(ResultsActivity.SCORE_PREFIX + GameDifficulty.Hard, 0);
int easyScore = prefs.getInt(ResultsActivity.SCORE_PREFIX + GameDifficulty.Easy, 0);
int mediumScore = prefs.getInt(ResultsActivity.SCORE_PREFIX + GameDifficulty.Medium, 0);
int hardScore = prefs.getInt(ResultsActivity.SCORE_PREFIX + GameDifficulty.Hard, 0);
// int advancedScore = prefs.getInt(ResultsActivity.SCORE_PREFIX + GameDifficulty.Advanced, 0);

if (easyScore > 0)
Games.Leaderboards.submitScore(mGoogleApiClient, easyLeaderboardId, easyScore);
if (mediumScore > 0)
Games.Leaderboards.submitScore(mGoogleApiClient, mediumLeaderboardId, mediumScore);
if (hardScore > 0)
Games.Leaderboards.submitScore(mGoogleApiClient, hardLeaderboardId, hardScore);
if (easyScore > 0)
Games.Leaderboards.submitScore(mGoogleApiClient, easyLeaderboardId, easyScore);
if (mediumScore > 0)
Games.Leaderboards.submitScore(mGoogleApiClient, mediumLeaderboardId, mediumScore);
if (hardScore > 0)
Games.Leaderboards.submitScore(mGoogleApiClient, hardLeaderboardId, hardScore);
// if (advancedScore > 0)
// Games.Leaderboards.submitScore(mGoogleApiClient, advancedLeaderboardId, advancedScore);

loadScoreOfLeaderBoardIfLarger(easyLeaderboardId, easyScore, GameDifficulty.Easy);
loadScoreOfLeaderBoardIfLarger(mediumLeaderboardId, mediumScore, GameDifficulty.Medium);
loadScoreOfLeaderBoardIfLarger(hardLeaderboardId, hardScore, GameDifficulty.Hard);
loadScoreOfLeaderBoardIfLarger(easyLeaderboardId, easyScore, GameDifficulty.Easy);
loadScoreOfLeaderBoardIfLarger(mediumLeaderboardId, mediumScore, GameDifficulty.Medium);
loadScoreOfLeaderBoardIfLarger(hardLeaderboardId, hardScore, GameDifficulty.Hard);
}
}

private void loadScoreOfLeaderBoardIfLarger(final String leaderboardId, final int currentScore, final String gameDifficulty) {
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/chrisjluc/onesearch/ui/ResultsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,26 @@ protected void onCreate(Bundle savedInstanceState) {
editor.commit();
}

updateSavedScoreAndRenderViews(mScore);
updateSavedScoreAndRenderViews();
}
}

private void updateSavedScoreAndRenderViews(int score) {
private void updateSavedScoreAndRenderViews() {

TextView scoreTextView = (TextView) findViewById(R.id.tvScoreResult);
scoreTextView.setText(Integer.toString(score));
scoreTextView.setText(Integer.toString(mScore));

if (mGameMode != null) {
SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
int bestScore = prefs.getInt(SCORE_PREFIX + mGameMode.getDifficulty(), 0);
mPreviousBestScore = bestScore;
if (score > bestScore) {
if (mScore > bestScore) {
SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME, MODE_PRIVATE).edit();
editor.putInt(SCORE_PREFIX + mGameMode.getDifficulty(), score);
editor.putInt(SCORE_PREFIX + mGameMode.getDifficulty(), mScore);
editor.commit();

findViewById(R.id.tvBestScoreResultNotify).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.tvBestScoreResult)).setText(Integer.toString(score));
((TextView) findViewById(R.id.tvBestScoreResult)).setText(Integer.toString(mScore));
} else {
((TextView) findViewById(R.id.tvBestScoreResult)).setText(Integer.toString(bestScore));
}
Expand Down

0 comments on commit e77c93d

Please sign in to comment.