Skip to content

Commit

Permalink
Merge pull request #74 from lewismcgeary/pause-and-resume
Browse files Browse the repository at this point in the history
add automatic pause and resume behaviour when leaving a running game fixes #49
  • Loading branch information
lewismcgeary committed Feb 8, 2016
2 parents 58ab560 + 500ef90 commit 6d6207c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class GridPresenter {
LifeGridLayout worldGridLayout;
int moveDuration;
private CalculateUpdateTask calculateUpdateTask;
private boolean gamePaused = false;

public GridPresenter(LifeGridLayout newWorldGridLayout, int moveDuration) {
worldGridLayout = newWorldGridLayout;
Expand Down Expand Up @@ -49,6 +50,23 @@ public void startConstantUpdate() {
calculateUpdateTask.execute();
}

public void pauseGame(){
//check if there is an existing running async task
if(calculateUpdateTask !=null) {
if (!calculateUpdateTask.isCancelled()) {
calculateUpdateTask.cancel(true);
gamePaused = true;
}
}
}

public void resumeGame(){
if(gamePaused){
startConstantUpdate();
gamePaused = false;
}
}

public void resetGrid(){
if(calculateUpdateTask != null) {
calculateUpdateTask.cancel(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ public void onGlobalLayout() {
return view;
}

@Override
public void onPause() {
super.onPause();
worldGridPresenter.pauseGame();
}

@Override
public void onResume() {
super.onResume();
if(worldGridPresenter != null) {
worldGridPresenter.resumeGame();
}
}

private void setUpGrid() {
int densityOfScreen = DisplayMetrics.DENSITY_DEFAULT;
int cellSize = (int) getResources().getDimension(R.dimen.cell_size);
Expand Down

0 comments on commit 6d6207c

Please sign in to comment.