-
Notifications
You must be signed in to change notification settings - Fork 243
Mvvm #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ShilpaSarawagi
wants to merge
14
commits into
master
Choose a base branch
from
mvvm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Mvvm #8
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5ed3c8b
View complete
ericmaxwell2003 d365f37
Finished working MVC
ericmaxwell2003 2d24a4c
Re arrange method name.
ericmaxwell2003 4eab20b
MVP Changes complete
ericmaxwell2003 a8f2d30
Finish View Model example
ericmaxwell2003 0e0a346
Update second test desc
ericmaxwell2003 a723fc3
Update TicTacToeTests.java
ericmaxwell2003 44e9c1d
Make the observable fields public final and remove setters.
ericmaxwell2003 1071255
reverse public and final modifiers on the ViewModel to match the post
ericmaxwell2003 e22d680
Added handling of activity recreation. For instance on screenrotation
Minsky 4db6fd8
setContentView() not required.
EurigJones a0660af
Merge pull request #4 from EurigJones/patch-1
ericmaxwell2003 96b3eb7
Merge pull request #3 from Minsky/mvvm
ericmaxwell2003 f383a7e
Reverted merge 96b3eb763118b01a9e3e73d792fe3362afe3078c
ericmaxwell2003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
15 changes: 0 additions & 15 deletions
15
app/src/main/java/com/acme/tictactoe/controller/TicTacToeActivity.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ public Board() { | |
| */ | ||
| public void restart() { | ||
| clearCells(); | ||
| winner = null; | ||
| currentTurn = Player.X; | ||
| state = GameState.IN_PROGRESS; | ||
| } | ||
|
|
@@ -33,11 +34,17 @@ public void restart() { | |
| * | ||
| * @param row 0..2 | ||
| * @param col 0..2 | ||
| * @return the player that moved or null if we did not move anything. | ||
| * | ||
| */ | ||
| public void mark( int row, int col ) { | ||
| public Player mark( int row, int col ) { | ||
|
|
||
| Player playerThatMoved = null; | ||
|
|
||
| if(isValid(row, col)) { | ||
|
|
||
| cells[row][col].setValue(currentTurn); | ||
| playerThatMoved = currentTurn; | ||
|
|
||
| if(isWinningMoveByPlayer(currentTurn, row, col)) { | ||
| state = GameState.FINISHED; | ||
|
|
@@ -48,6 +55,12 @@ public void mark( int row, int col ) { | |
| flipCurrentTurn(); | ||
| } | ||
| } | ||
|
|
||
| return playerThatMoved; | ||
| } | ||
|
|
||
| public Player valueAtCell(int row, int col) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the point of that method? |
||
| return cells[row][col].getValue(); | ||
| } | ||
|
|
||
| public Player getWinner() { | ||
|
|
||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| package com.acme.tictactoe.model; | ||
|
|
||
| enum Player { X , O } | ||
| public enum Player { X , O } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it not only used on |
||
61 changes: 61 additions & 0 deletions
61
app/src/main/java/com/acme/tictactoe/view/TicTacToeActivity.java
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.acme.tictactoe.view; | ||
|
|
||
| import android.databinding.DataBindingUtil; | ||
| import android.os.Bundle; | ||
| import android.support.v7.app.AppCompatActivity; | ||
| import android.view.Menu; | ||
| import android.view.MenuInflater; | ||
| import android.view.MenuItem; | ||
|
|
||
| import com.acme.tictactoe.R; | ||
| import com.acme.tictactoe.databinding.TictactoeBinding; | ||
| import com.acme.tictactoe.viewmodel.TicTacToeViewModel; | ||
|
|
||
| public class TicTacToeActivity extends AppCompatActivity { | ||
|
|
||
| TicTacToeViewModel viewModel = new TicTacToeViewModel(); | ||
|
|
||
| @Override | ||
| protected void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| TictactoeBinding binding = DataBindingUtil.setContentView(this, R.layout.tictactoe); | ||
| binding.setViewModel(viewModel); | ||
| viewModel.onCreate(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void onPause() { | ||
| super.onPause(); | ||
| viewModel.onPause(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void onResume() { | ||
| super.onResume(); | ||
| viewModel.onResume(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void onDestroy() { | ||
| super.onDestroy(); | ||
| viewModel.onDestroy(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean onCreateOptionsMenu(Menu menu) { | ||
| MenuInflater inflater = getMenuInflater(); | ||
| inflater.inflate(R.menu.menu_tictactoe, menu); | ||
| return true; | ||
| } | ||
| @Override | ||
| public boolean onOptionsItemSelected(MenuItem item) { | ||
| switch (item.getItemId()) { | ||
| case R.id.action_reset: | ||
| viewModel.onResetSelected(); | ||
| return true; | ||
| default: | ||
| return super.onOptionsItemSelected(item); | ||
| } | ||
| } | ||
|
|
||
| } |
52 changes: 52 additions & 0 deletions
52
app/src/main/java/com/acme/tictactoe/viewmodel/TicTacToeViewModel.java
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package com.acme.tictactoe.viewmodel; | ||
|
|
||
| import android.databinding.ObservableArrayMap; | ||
| import android.databinding.ObservableField; | ||
|
|
||
| import com.acme.tictactoe.model.Board; | ||
| import com.acme.tictactoe.model.Player; | ||
|
|
||
| public class TicTacToeViewModel implements ViewModel { | ||
|
|
||
| private Board model; | ||
|
|
||
| public final ObservableArrayMap<String, String> cells = new ObservableArrayMap<>(); | ||
| public final ObservableField<String> winner = new ObservableField<>(); | ||
|
|
||
| public TicTacToeViewModel() { | ||
| model = new Board(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onCreate() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onPause() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onResume() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onDestroy() { | ||
|
|
||
| } | ||
|
|
||
| public void onResetSelected() { | ||
| model.restart(); | ||
| winner.set(null); | ||
| cells.clear(); | ||
| } | ||
|
|
||
| public void onClickedCellAt(int row, int col) { | ||
| Player playerThatMoved = model.mark(row, col); | ||
| cells.put("" + row + col, playerThatMoved == null ? null : playerThatMoved.toString()); | ||
| winner.set(model.getWinner() == null ? null : model.getWinner().toString()); | ||
| } | ||
|
|
||
| } |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/acme/tictactoe/viewmodel/ViewModel.java
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.acme.tictactoe.viewmodel; | ||
|
|
||
| public interface ViewModel { | ||
|
|
||
| void onCreate(); | ||
| void onPause(); | ||
| void onResume(); | ||
| void onDestroy(); | ||
|
|
||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,104 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| <layout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:tools="http://schemas.android.com/tools" | ||
| android:id="@+id/tictactoe" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| android:paddingBottom="@dimen/activity_vertical_margin" | ||
| android:paddingLeft="@dimen/activity_horizontal_margin" | ||
| android:paddingRight="@dimen/activity_horizontal_margin" | ||
| android:paddingTop="@dimen/activity_vertical_margin" | ||
| tools:context="com.acme.tictactoe.controller.TicTacToeActivity"> | ||
|
|
||
| <TextView | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="Hello World!" /> | ||
| </RelativeLayout> | ||
| xmlns:app="http://schemas.android.com/apk/res-auto"> | ||
|
|
||
| <data> | ||
| <import type="android.view.View" /> | ||
| <variable name="viewModel" type="com.acme.tictactoe.viewmodel.TicTacToeViewModel" /> | ||
| </data> | ||
|
|
||
| <LinearLayout | ||
| android:id="@+id/tictactoe" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| android:gravity="center_horizontal" | ||
| android:orientation="vertical" | ||
| android:paddingBottom="@dimen/activity_vertical_margin" | ||
| android:paddingLeft="@dimen/activity_horizontal_margin" | ||
| android:paddingRight="@dimen/activity_horizontal_margin" | ||
| android:paddingTop="@dimen/activity_vertical_margin" | ||
| tools:context="com.acme.tictactoe.view.TicTacToeActivity"> | ||
|
|
||
| <GridLayout | ||
| android:id="@+id/buttonGrid" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:columnCount="3" | ||
| android:rowCount="3"> | ||
|
|
||
| <Button | ||
| style="@style/tictactoebutton" | ||
| android:onClick="@{() -> viewModel.onClickedCellAt(0,0)}" | ||
| android:text='@{viewModel.cells["00"]}' /> | ||
|
|
||
| <Button | ||
| style="@style/tictactoebutton" | ||
| android:onClick="@{() -> viewModel.onClickedCellAt(0,1)}" | ||
| android:text='@{viewModel.cells["01"]}' /> | ||
|
|
||
| <Button | ||
| style="@style/tictactoebutton" | ||
| android:onClick="@{() -> viewModel.onClickedCellAt(0,2)}" | ||
| android:text='@{viewModel.cells["02"]}' /> | ||
|
|
||
| <Button | ||
| style="@style/tictactoebutton" | ||
| android:onClick="@{() -> viewModel.onClickedCellAt(1,0)}" | ||
| android:text='@{viewModel.cells["10"]}' /> | ||
|
|
||
| <Button | ||
| style="@style/tictactoebutton" | ||
| android:onClick="@{() -> viewModel.onClickedCellAt(1,1)}" | ||
| android:text='@{viewModel.cells["11"]}' /> | ||
|
|
||
| <Button | ||
| style="@style/tictactoebutton" | ||
| android:onClick="@{() -> viewModel.onClickedCellAt(1,2)}" | ||
| android:text='@{viewModel.cells["12"]}' /> | ||
|
|
||
| <Button | ||
| style="@style/tictactoebutton" | ||
| android:onClick="@{() -> viewModel.onClickedCellAt(2,0)}" | ||
| android:text='@{viewModel.cells["20"]}' /> | ||
|
|
||
| <Button | ||
| style="@style/tictactoebutton" | ||
| android:onClick="@{() -> viewModel.onClickedCellAt(2,1)}" | ||
| android:text='@{viewModel.cells["21"]}' /> | ||
|
|
||
| <Button | ||
| style="@style/tictactoebutton" | ||
| android:onClick="@{() -> viewModel.onClickedCellAt(2,2)}" | ||
| android:text='@{viewModel.cells["22"]}' /> | ||
|
|
||
| </GridLayout> | ||
|
|
||
| <LinearLayout | ||
| android:id="@+id/winnerPlayerViewGroup" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="match_parent" | ||
| android:gravity="center" | ||
| android:orientation="vertical" | ||
| android:visibility="@{viewModel.winner != null ? View.VISIBLE : View.GONE}" | ||
| tools:visibility="visible"> | ||
|
|
||
| <TextView | ||
| android:id="@+id/winnerPlayerLabel" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:layout_margin="20dp" | ||
| android:textSize="40sp" | ||
| android:text="@{viewModel.winner}" | ||
| tools:text="X" /> | ||
|
|
||
| <TextView | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="@string/winner" | ||
| android:textSize="30sp" /> | ||
|
|
||
| </LinearLayout> | ||
|
|
||
| </LinearLayout> | ||
| </layout> |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <menu xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:app="http://schemas.android.com/apk/res-auto" | ||
| xmlns:tools="http://schemas.android.com/tools" | ||
| tools:context=".view.TicTacToeActivity"> | ||
|
|
||
| <item android:id="@+id/action_reset" | ||
| android:title="@string/action_reset" | ||
| android:orderInCategory="100" | ||
| app:showAsAction="ifRoom" /> | ||
|
|
||
| </menu> |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| <resources> | ||
| <!-- Default screen margins, per the Android Design guidelines. --> | ||
| <dimen name="activity_horizontal_margin">16dp</dimen> | ||
| <dimen name="activity_vertical_margin">16dp</dimen> | ||
| <dimen name="activity_vertical_margin">44dp</dimen> | ||
| </resources> |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| <resources> | ||
| <string name="app_name">TicTacToe</string> | ||
| <string name="winner">Winner</string> | ||
| <string name="action_reset">Reset</string> | ||
| </resources> |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was really nice!