From 4e6d3bc678657f43f1e0be77bf54969b80ee04aa Mon Sep 17 00:00:00 2001 From: Andrew Kemp Date: Mon, 9 Mar 2020 18:17:39 -0400 Subject: [PATCH] Setup viewmodel --- app/build.gradle | 11 ++- app/src/main/AndroidManifest.xml | 1 + .../guesstheword/GuessItApplication.kt | 11 +++ .../guesstheword/screens/game/GameFragment.kt | 87 ++++--------------- .../screens/game/GameViewModel.kt | 78 +++++++++++++++++ build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 7 files changed, 116 insertions(+), 78 deletions(-) create mode 100644 app/src/main/java/com/example/android/guesstheword/GuessItApplication.kt create mode 100644 app/src/main/java/com/example/android/guesstheword/screens/game/GameViewModel.kt diff --git a/app/build.gradle b/app/build.gradle index 981b558..5d1aa32 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -45,17 +45,20 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation 'androidx.appcompat:appcompat:1.0.2' + implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.legacy:legacy-support-v4:1.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' + implementation 'com.jakewharton.timber:timber:4.7.1' // KTX - implementation 'androidx.core:core-ktx:1.0.1' + implementation 'androidx.core:core-ktx:1.2.0' // Navigation - implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0-rc02" - implementation "android.arch.navigation:navigation-ui-ktx:1.0.0-rc02" + implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0" + implementation "android.arch.navigation:navigation-ui-ktx:1.0.0" + + implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ebac427..149563e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -19,6 +19,7 @@ package="com.example.android.guesstheword"> + private lateinit var viewModel: GameViewModel private lateinit var binding: GameFragmentBinding @@ -53,89 +47,40 @@ class GameFragment : Fragment() { false ) - resetList() - nextWord() + viewModel = ViewModelProviders.of(this).get(GameViewModel::class.java) - binding.correctButton.setOnClickListener { onCorrect() } - binding.skipButton.setOnClickListener { onSkip() } + binding.correctButton.setOnClickListener { + viewModel.onCorrect() + updateScoreText() + updateWordText() + } + binding.skipButton.setOnClickListener { + viewModel.onSkip() + updateScoreText() + updateWordText() + } updateScoreText() updateWordText() return binding.root } - /** - * Resets the list of words and randomizes the order - */ - private fun resetList() { - wordList = mutableListOf( - "queen", - "hospital", - "basketball", - "cat", - "change", - "snail", - "soup", - "calendar", - "sad", - "desk", - "guitar", - "home", - "railway", - "zebra", - "jelly", - "car", - "crow", - "trade", - "bag", - "roll", - "bubble" - ) - wordList.shuffle() - } - /** * Called when the game is finished */ private fun gameFinished() { - val action = GameFragmentDirections.actionGameToScore(score) + val action = GameFragmentDirections.actionGameToScore(viewModel.score) findNavController(this).navigate(action) } - /** - * Moves to the next word in the list - */ - private fun nextWord() { - //Select and remove a word from the list - if (wordList.isEmpty()) { - gameFinished() - } else { - word = wordList.removeAt(0) - } - updateWordText() - updateScoreText() - } - - /** Methods for buttons presses **/ - - private fun onSkip() { - score-- - nextWord() - } - - private fun onCorrect() { - score++ - nextWord() - } - /** Methods for updating the UI **/ private fun updateWordText() { - binding.wordText.text = word + binding.wordText.text = viewModel.word } private fun updateScoreText() { - binding.scoreText.text = score.toString() + binding.scoreText.text = viewModel.score.toString() } } diff --git a/app/src/main/java/com/example/android/guesstheword/screens/game/GameViewModel.kt b/app/src/main/java/com/example/android/guesstheword/screens/game/GameViewModel.kt new file mode 100644 index 0000000..b229663 --- /dev/null +++ b/app/src/main/java/com/example/android/guesstheword/screens/game/GameViewModel.kt @@ -0,0 +1,78 @@ +package com.example.android.guesstheword.screens.game + +import androidx.lifecycle.ViewModel +import timber.log.Timber + +class GameViewModel(): ViewModel() { + // The current word + var word = "" + + // The current score + var score = 0 + + // The list of words - the front of the list is the next word to guess + private lateinit var wordList: MutableList + + init { + Timber.i("GameViewModel created") + resetList() + nextWord() + } + + override fun onCleared() { + super.onCleared() + Timber.i("GameViewModel destroyed") + } + + /** + * Resets the list of words and randomizes the order + */ + private fun resetList() { + wordList = mutableListOf( + "queen", + "hospital", + "basketball", + "cat", + "change", + "snail", + "soup", + "calendar", + "sad", + "desk", + "guitar", + "home", + "railway", + "zebra", + "jelly", + "car", + "crow", + "trade", + "bag", + "roll", + "bubble" + ) + wordList.shuffle() + } + + /** + * Moves to the next word in the list + */ + private fun nextWord() { + //Select and remove a word from the list + if (wordList.isEmpty()) { +// gameFinished() + } else { + word = wordList.removeAt(0) + } + } + + fun onSkip() { + score-- + nextWord() + } + + fun onCorrect() { + score++ + nextWord() + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index d2f6ae1..0a7a515 100644 --- a/build.gradle +++ b/build.gradle @@ -23,7 +23,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.3.2' + classpath 'com.android.tools.build:gradle:3.6.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-rc02" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 9b75a91..a5dae34 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Oct 01 14:14:39 PDT 2018 +#Mon Mar 09 17:38:57 EDT 2020 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip