Setup viewmodel

This commit is contained in:
2020-03-09 18:17:39 -04:00
parent 12e39969c0
commit 4e6d3bc678
7 changed files with 116 additions and 78 deletions

View File

@@ -45,17 +45,20 @@ android {
dependencies { dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 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.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'com.jakewharton.timber:timber:4.7.1'
// KTX // KTX
implementation 'androidx.core:core-ktx:1.0.1' implementation 'androidx.core:core-ktx:1.2.0'
// Navigation // Navigation
implementation "android.arch.navigation:navigation-fragment-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-rc02" implementation "android.arch.navigation:navigation-ui-ktx:1.0.0"
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
} }

View File

@@ -19,6 +19,7 @@
package="com.example.android.guesstheword"> package="com.example.android.guesstheword">
<application <application
android:name=".GuessItApplication"
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_guess_it" android:icon="@mipmap/ic_guess_it"
android:label="@string/app_name" android:label="@string/app_name"

View File

@@ -0,0 +1,11 @@
package com.example.android.guesstheword
import android.app.Application
import timber.log.Timber
class GuessItApplication: Application() {
override fun onCreate() {
super.onCreate()
Timber.plant(Timber.DebugTree())
}
}

View File

@@ -22,6 +22,7 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.databinding.DataBindingUtil import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import androidx.navigation.fragment.NavHostFragment.findNavController import androidx.navigation.fragment.NavHostFragment.findNavController
import com.example.android.guesstheword.R import com.example.android.guesstheword.R
import com.example.android.guesstheword.databinding.GameFragmentBinding import com.example.android.guesstheword.databinding.GameFragmentBinding
@@ -31,14 +32,7 @@ import com.example.android.guesstheword.databinding.GameFragmentBinding
*/ */
class GameFragment : Fragment() { class GameFragment : Fragment() {
// The current word private lateinit var viewModel: GameViewModel
private var word = ""
// The current score
private var score = 0
// The list of words - the front of the list is the next word to guess
private lateinit var wordList: MutableList<String>
private lateinit var binding: GameFragmentBinding private lateinit var binding: GameFragmentBinding
@@ -53,89 +47,40 @@ class GameFragment : Fragment() {
false false
) )
resetList() viewModel = ViewModelProviders.of(this).get(GameViewModel::class.java)
nextWord()
binding.correctButton.setOnClickListener { onCorrect() } binding.correctButton.setOnClickListener {
binding.skipButton.setOnClickListener { onSkip() } viewModel.onCorrect()
updateScoreText()
updateWordText()
}
binding.skipButton.setOnClickListener {
viewModel.onSkip()
updateScoreText()
updateWordText()
}
updateScoreText() updateScoreText()
updateWordText() updateWordText()
return binding.root 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 * Called when the game is finished
*/ */
private fun gameFinished() { private fun gameFinished() {
val action = GameFragmentDirections.actionGameToScore(score) val action = GameFragmentDirections.actionGameToScore(viewModel.score)
findNavController(this).navigate(action) 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 **/ /** Methods for updating the UI **/
private fun updateWordText() { private fun updateWordText() {
binding.wordText.text = word binding.wordText.text = viewModel.word
} }
private fun updateScoreText() { private fun updateScoreText() {
binding.scoreText.text = score.toString() binding.scoreText.text = viewModel.score.toString()
} }
} }

View File

@@ -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<String>
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()
}
}

View File

@@ -23,7 +23,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { 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 "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-rc02" classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-rc02"

View File

@@ -1,6 +1,6 @@
#Mon Oct 01 14:14:39 PDT 2018 #Mon Mar 09 17:38:57 EDT 2020
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists 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