Implementing live data in viewmodel
This commit is contained in:
@@ -22,6 +22,7 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import androidx.navigation.fragment.NavHostFragment.findNavController
|
||||
import com.example.android.guesstheword.R
|
||||
@@ -51,16 +52,26 @@ class GameFragment : Fragment() {
|
||||
|
||||
binding.correctButton.setOnClickListener {
|
||||
viewModel.onCorrect()
|
||||
updateScoreText()
|
||||
updateWordText()
|
||||
}
|
||||
binding.skipButton.setOnClickListener {
|
||||
viewModel.onSkip()
|
||||
updateScoreText()
|
||||
updateWordText()
|
||||
}
|
||||
updateScoreText()
|
||||
updateWordText()
|
||||
|
||||
viewModel.score.observe(this, Observer {
|
||||
binding.scoreText.text = it.toString()
|
||||
})
|
||||
|
||||
viewModel.word.observe(this, Observer {
|
||||
binding.wordText.text = it
|
||||
})
|
||||
|
||||
viewModel.eventGameFinished.observe(this, Observer {
|
||||
if (it) {
|
||||
gameFinished()
|
||||
viewModel.onGameFinishComplete()
|
||||
}
|
||||
})
|
||||
|
||||
return binding.root
|
||||
|
||||
}
|
||||
@@ -69,18 +80,8 @@ class GameFragment : Fragment() {
|
||||
* Called when the game is finished
|
||||
*/
|
||||
private fun gameFinished() {
|
||||
val action = GameFragmentDirections.actionGameToScore(viewModel.score)
|
||||
val action = GameFragmentDirections.actionGameToScore(viewModel.score.value ?: 0)
|
||||
findNavController(this).navigate(action)
|
||||
}
|
||||
|
||||
/** Methods for updating the UI **/
|
||||
|
||||
private fun updateWordText() {
|
||||
binding.wordText.text = viewModel.word
|
||||
|
||||
}
|
||||
|
||||
private fun updateScoreText() {
|
||||
binding.scoreText.text = viewModel.score.toString()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,33 @@
|
||||
package com.example.android.guesstheword.screens.game
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import timber.log.Timber
|
||||
|
||||
class GameViewModel(): ViewModel() {
|
||||
// The current word
|
||||
var word = ""
|
||||
private val _word = MutableLiveData<String>()
|
||||
val word: LiveData<String>
|
||||
get() = _word
|
||||
|
||||
// The current score
|
||||
var score = 0
|
||||
private val _score = MutableLiveData<Int>()
|
||||
val score: LiveData<Int>
|
||||
get() = _score
|
||||
|
||||
private val _eventGameFinished = MutableLiveData<Boolean>()
|
||||
val eventGameFinished: LiveData<Boolean>
|
||||
get() = _eventGameFinished
|
||||
|
||||
// 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")
|
||||
_eventGameFinished.value = false
|
||||
_word.value = ""
|
||||
_score.value = 0
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,19 +66,25 @@ class GameViewModel(): ViewModel() {
|
||||
private fun nextWord() {
|
||||
//Select and remove a word from the list
|
||||
if (wordList.isEmpty()) {
|
||||
// gameFinished()
|
||||
_eventGameFinished.value = true
|
||||
} else {
|
||||
word = wordList.removeAt(0)
|
||||
_word.value = wordList.removeAt(0)
|
||||
}
|
||||
}
|
||||
|
||||
fun onSkip() {
|
||||
score--
|
||||
if (score.value != 0) {
|
||||
_score.value = (score.value)?.dec()
|
||||
}
|
||||
nextWord()
|
||||
}
|
||||
|
||||
fun onCorrect() {
|
||||
score++
|
||||
_score.value = (score.value)?.inc()
|
||||
nextWord()
|
||||
}
|
||||
|
||||
fun onGameFinishComplete() {
|
||||
_eventGameFinished.value = false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user