Skip to content

Commit

Permalink
Merge pull request #3 from Minsky/mvvm
Browse files Browse the repository at this point in the history
Added handling of activity recreation. For instance on screen rotation
  • Loading branch information
ericmaxwell2003 authored Apr 6, 2017
2 parents a0660af + e22d680 commit 96b3eb7
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".view.TicTacToeActivity" android:screenOrientation="portrait">
<activity android:name=".view.TicTacToeActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
53 changes: 51 additions & 2 deletions app/src/main/java/com/acme/tictactoe/model/Board.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,71 @@
package com.acme.tictactoe.model;

import android.os.Parcel;
import android.os.Parcelable;

import static com.acme.tictactoe.model.Player.O;
import static com.acme.tictactoe.model.Player.X;

public class Board {
public class Board implements Parcelable {

private Cell[][] cells = new Cell[3][3];

private Player winner;
private GameState state;
private Player currentTurn;

private enum GameState { IN_PROGRESS, FINISHED };
private enum GameState { IN_PROGRESS, FINISHED }

public Board() {
restart();
}

private Board(Parcel in)
{
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cells[i][j].setValue(Player.fromInt(in.readInt()));
}
}
winner = Player.fromInt(in.readInt());
state = (in.readInt() == 0 ? GameState.IN_PROGRESS : GameState.FINISHED);
currentTurn = Player.fromInt(in.readInt());
}

@Override
public void writeToParcel(Parcel dest, int flags)
{
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
dest.writeInt(cells[i][j].getValue().id);
}
}
dest.writeInt(winner.id);
dest.writeInt(state.ordinal());
dest.writeInt(currentTurn.id);
}

@Override
public int describeContents()
{
return 0;
}

public static final Creator<Board> CREATOR = new Creator<Board>()
{
@Override
public Board createFromParcel(Parcel in)
{
return new Board(in);
}

@Override
public Board[] newArray(int size)
{
return new Board[size];
}
};

/**
* Restart or start a new game, will clear the board and win status
*/
Expand Down
44 changes: 42 additions & 2 deletions app/src/main/java/com/acme/tictactoe/model/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,55 @@
* Created by ericmaxwell on 1/19/17.
*/

public class Cell {
import android.os.Parcel;
import android.os.Parcelable;

public class Cell implements Parcelable
{

private Player value;

protected Cell(Parcel in)
{
value = Player.fromInt(in.readInt());
}

public static final Creator<Cell> CREATOR = new Creator<Cell>()
{
@Override
public Cell createFromParcel(Parcel in)
{
return new Cell(in);
}

@Override
public Cell[] newArray(int size)
{
return new Cell[size];
}
};

public Cell()
{
}

public Player getValue() {
return value;
}

public void setValue(Player value) {
this.value = value;
}
}

@Override
public int describeContents()
{
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags)
{
dest.writeInt(value.id);
}
}
28 changes: 27 additions & 1 deletion app/src/main/java/com/acme/tictactoe/model/Player.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
package com.acme.tictactoe.model;

public enum Player { X , O }
import android.util.SparseArray;

public enum Player {
X(0),
O(1),
NO(2);

int id;

Player(int id)
{
this.id = id;
}

static SparseArray<Player> cache;

static Player fromInt(int id) {
if (cache == null) {
cache = new SparseArray<>();
Player[] players = Player.values();
for (Player value : players) {
cache.put(value.id, value);
}
}
return cache.get(id);
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/acme/tictactoe/view/TicTacToeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ protected void onCreate(Bundle savedInstanceState) {
TictactoeBinding binding = DataBindingUtil.setContentView(this, R.layout.tictactoe);
binding.setViewModel(viewModel);
viewModel.onCreate();

if (savedInstanceState != null) {
viewModel.loadFromSavedInstanceState(savedInstanceState);
}
}

@Override
Expand All @@ -35,6 +39,13 @@ protected void onResume() {
viewModel.onResume();
}

@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
viewModel.onSaveInstanceState(outState);
}

@Override
protected void onDestroy() {
super.onDestroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import android.databinding.ObservableArrayMap;
import android.databinding.ObservableField;
import android.os.Bundle;

import com.acme.tictactoe.model.Board;
import com.acme.tictactoe.model.Player;

public class TicTacToeViewModel implements ViewModel {

private static final String SAVED_TTT_VIEWMODEL = "TicTacToeViewModel_SaveBundle";

private Board model;

public final ObservableArrayMap<String, String> cells = new ObservableArrayMap<>();
Expand All @@ -32,6 +35,29 @@ public void onResume() {

}

@Override
public void onSaveInstanceState(Bundle bundle)
{
bundle.putParcelable(SAVED_TTT_VIEWMODEL, model);
}

@Override
public void loadFromSavedInstanceState(Bundle bundle)
{
model = bundle.getParcelable(SAVED_TTT_VIEWMODEL);
// update the view
if (model != null) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (model.valueAtCell(row, col) != null) {
cells.put("" + row + col, model.valueAtCell(row, col).toString());
}
}
}
winner.set(model.getWinner() == null ? null : model.getWinner().toString());
}
}

@Override
public void onDestroy() {

Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/acme/tictactoe/viewmodel/ViewModel.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package com.acme.tictactoe.viewmodel;

import android.os.Bundle;

public interface ViewModel {

void onCreate();
void onPause();
void onResume();

void onSaveInstanceState(Bundle bundle);

void loadFromSavedInstanceState(Bundle bundle);

void onDestroy();

}

0 comments on commit 96b3eb7

Please sign in to comment.