Setup viewmodel
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
package="com.example.android.guesstheword">
|
||||
|
||||
<application
|
||||
android:name=".GuessItApplication"
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_guess_it"
|
||||
android:label="@string/app_name"
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import androidx.navigation.fragment.NavHostFragment.findNavController
|
||||
import com.example.android.guesstheword.R
|
||||
import com.example.android.guesstheword.databinding.GameFragmentBinding
|
||||
@@ -31,14 +32,7 @@ import com.example.android.guesstheword.databinding.GameFragmentBinding
|
||||
*/
|
||||
class GameFragment : Fragment() {
|
||||
|
||||
// The current word
|
||||
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 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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user