Skip to content

Commit

Permalink
Handling null cases better in result activity, deprecated serializabl…
Browse files Browse the repository at this point in the history
…e on GameMode
  • Loading branch information
chrisjluc committed Feb 25, 2015
1 parent 73a6345 commit 3d02e22
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 54 deletions.
4 changes: 1 addition & 3 deletions app/src/main/java/chrisjluc/onesearch/models/GameMode.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package chrisjluc.onesearch.models;

import java.io.Serializable;

@SuppressWarnings("serial")
public class GameMode implements Serializable {
public class GameMode {

private String type;
private String difficulty;
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/chrisjluc/onesearch/ui/MenuActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public void onClick(View view) {
mGoogleApiClient.connect();
return;
}
WordSearchManager.nullify();
String gd = null;
switch (view.getId()) {
case R.id.bMenuEasy:
Expand All @@ -76,6 +75,12 @@ public void onClick(View view) {
startActivity(i);
}

@Override
protected void onResume() {
super.onResume();
WordSearchManager.nullify();
}

@Override
public void onConnected(Bundle bundle) {
findViewById(R.id.bMenuSignIn).setVisibility(View.GONE);
Expand Down
87 changes: 47 additions & 40 deletions app/src/main/java/chrisjluc/onesearch/ui/ResultsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,48 +52,53 @@ protected void onCreate(Bundle savedInstanceState) {
mSkipped = extras.getInt("skipped");

mGameMode = WordSearchManager.getInstance().getGameMode();
switch (mGameMode.getDifficulty()) {
case GameDifficulty.Easy:
mLeaderboardId = getResources().getString(R.string.leaderboard_highest_scores__easy);
break;
case GameDifficulty.Medium:
mLeaderboardId = getResources().getString(R.string.leaderboard_highest_scores__medium);
break;
case GameDifficulty.Hard:
mLeaderboardId = getResources().getString(R.string.leaderboard_highest_scores__hard);
break;
case GameDifficulty.Advanced:
mLeaderboardId = getResources().getString(R.string.leaderboard_highest_scores__advanced);
break;
}
if (mGameMode != null) {
switch (mGameMode.getDifficulty()) {
case GameDifficulty.Easy:
mLeaderboardId = getResources().getString(R.string.leaderboard_highest_scores__easy);
break;
case GameDifficulty.Medium:
mLeaderboardId = getResources().getString(R.string.leaderboard_highest_scores__medium);
break;
case GameDifficulty.Hard:
mLeaderboardId = getResources().getString(R.string.leaderboard_highest_scores__hard);
break;
case GameDifficulty.Advanced:
mLeaderboardId = getResources().getString(R.string.leaderboard_highest_scores__advanced);
break;
}

SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
int numRounds = prefs.getInt(COMPLETED_ROUND_PREFIX + mGameMode.getDifficulty(), 0);
SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME, MODE_PRIVATE).edit();
editor.putInt(COMPLETED_ROUND_PREFIX + mGameMode.getDifficulty(), ++numRounds);
editor.commit();
// Track number of played rounds
SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
int numRounds = prefs.getInt(COMPLETED_ROUND_PREFIX + mGameMode.getDifficulty(), 0);
SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME, MODE_PRIVATE).edit();
editor.putInt(COMPLETED_ROUND_PREFIX + mGameMode.getDifficulty(), ++numRounds);
editor.commit();
}

updateSavedScoreAndRenderViews(mGameMode, mScore);
updateSavedScoreAndRenderViews(mScore);
}
}

private void updateSavedScoreAndRenderViews(GameMode gameMode, int score) {
private void updateSavedScoreAndRenderViews(int score) {

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

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

findViewById(R.id.tvBestScoreResultNotify).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.tvBestScoreResult)).setText(Integer.toString(score));
} else {
((TextView) findViewById(R.id.tvBestScoreResult)).setText(Integer.toString(bestScore));
if (mGameMode != null) {
SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
int bestScore = prefs.getInt(SCORE_PREFIX + mGameMode.getDifficulty(), 0);
mPreviousBestScore = bestScore;
if (score > bestScore) {
SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME, MODE_PRIVATE).edit();
editor.putInt(SCORE_PREFIX + mGameMode.getDifficulty(), score);
editor.commit();

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

Expand Down Expand Up @@ -185,13 +190,15 @@ public void onBackPressed() {
@Override
public void onConnected(Bundle bundle) {
super.onConnected(bundle);
findViewById(R.id.bResultSignIn).setVisibility(View.GONE);
findViewById(R.id.bShowLeaderBoards).setVisibility(View.VISIBLE);
findViewById(R.id.bShowLeaderBoards).setOnClickListener(this);
findViewById(R.id.bShowAchievements).setVisibility(View.VISIBLE);
findViewById(R.id.bShowAchievements).setOnClickListener(this);
updateLeaderboard();
updateAchievements();
if (mGameMode != null) {
findViewById(R.id.bResultSignIn).setVisibility(View.GONE);
findViewById(R.id.bShowLeaderBoards).setVisibility(View.VISIBLE);
findViewById(R.id.bShowLeaderBoards).setOnClickListener(this);
findViewById(R.id.bShowAchievements).setVisibility(View.VISIBLE);
findViewById(R.id.bShowAchievements).setOnClickListener(this);
updateLeaderboard();
updateAchievements();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import android.widget.TextView;

import chrisjluc.onesearch.R;
import chrisjluc.onesearch.framework.WordSearchManager;
import chrisjluc.onesearch.adapters.WordSearchPagerAdapter;
import chrisjluc.onesearch.base.BaseActivity;
import chrisjluc.onesearch.framework.WordSearchManager;
import chrisjluc.onesearch.ui.ResultsActivity;

public class WordSearchActivity extends BaseActivity implements WordSearchGridView.WordFoundListener, PauseDialogFragment.PauseDialogListener, View.OnClickListener {
Expand All @@ -38,7 +38,7 @@ public class WordSearchActivity extends BaseActivity implements WordSearchGridVi
private CountDownTimer mCountDownTimer;
private String mGameState;
private long mTimeRemaining;
private long mStartTime;
private long mRoundTime;
private int mScore;
private int mSkipped;
private WordSearchPagerAdapter mWordSearchPagerAdapter;
Expand All @@ -48,7 +48,6 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.wordsearch_activity);
mStartTime = WordSearchManager.getInstance().getGameMode().getTime();
mGameState = GameState.START;
Button skipButton = (Button) findViewById(R.id.bSkip);
Button pauseButton = (Button) findViewById(R.id.bPause);
Expand All @@ -57,6 +56,9 @@ protected void onCreate(Bundle savedInstanceState) {
mTimerTextView = (TextView) findViewById(R.id.tvTimer);
mScoreTextView = (TextView) findViewById(R.id.tvScore);
mScoreTextView.setText("0");
currentItem = 0;
mScore = 0;
mSkipped = 0;

// Create the adapter that will return a fragment for each of the
// primary sections of the activity.
Expand All @@ -73,10 +75,9 @@ protected void onCreate(Bundle savedInstanceState) {
// Set up the ViewPager with the sections adapter.
mViewPager = (WordSearchViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mWordSearchPagerAdapter);
currentItem = 0;
mScore = 0;
mSkipped = 0;
mTimeRemaining = mStartTime;

mRoundTime = WordSearchManager.getInstance().getGameMode().getTime();
mTimeRemaining = mRoundTime;
setupCountDownTimer(mTimeRemaining);
startCountDownTimer();
}
Expand Down Expand Up @@ -146,7 +147,7 @@ public void onDialogRestart() {
private void restart() {
mScore = 0;
mSkipped = 0;
mTimeRemaining = mStartTime;
mTimeRemaining = mRoundTime;
setupCountDownTimer(mTimeRemaining);
startCountDownTimer();
setFullscreen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import java.util.List;

import chrisjluc.onesearch.R;
import chrisjluc.onesearch.framework.WordSearchManager;
import chrisjluc.onesearch.adapters.WordSearchGridAdapter;
import chrisjluc.onesearch.framework.WordSearchManager;
import chrisjluc.onesearch.wordSearchGenerator.generators.StringUtils;
import chrisjluc.onesearch.wordSearchGenerator.generators.WordSearchGenerator;
import chrisjluc.onesearch.wordSearchGenerator.models.Node;
Expand Down Expand Up @@ -44,6 +44,9 @@ public WordSearchGridView(Context context, AttributeSet attrs) {

WordSearchManager manager = WordSearchManager.getInstance();
WordSearchGenerator wordSearch = manager.getWordSearch(WordSearchActivity.currentItem++);
while (wordSearch == null) {
wordSearch = manager.getWordSearch(WordSearchActivity.currentItem++);
}
mXLength = wordSearch.getnCol();
mYLength = wordSearch.getnRow();
mWord = wordSearch.getWord();
Expand Down Expand Up @@ -84,7 +87,7 @@ private void isWordFound() {

private String getSelectedString() {
// No need for validation because updateCurrentHighlightedNodes validates
if(mEndDrag == null || mStartDrag == null) return null;
if (mEndDrag == null || mStartDrag == null) return null;
int dX = mEndDrag.x - mStartDrag.x;
int dY = mEndDrag.y - mStartDrag.y;

Expand Down

0 comments on commit 3d02e22

Please sign in to comment.