Implementing live data in viewmodel

This commit is contained in:
2020-03-09 18:45:52 -04:00
parent 4e6d3bc678
commit be20efca5f
2 changed files with 43 additions and 30 deletions

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.Observer
import androidx.lifecycle.ViewModelProviders 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
@@ -51,16 +52,26 @@ class GameFragment : Fragment() {
binding.correctButton.setOnClickListener { binding.correctButton.setOnClickListener {
viewModel.onCorrect() viewModel.onCorrect()
updateScoreText()
updateWordText()
} }
binding.skipButton.setOnClickListener { binding.skipButton.setOnClickListener {
viewModel.onSkip() 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 return binding.root
} }
@@ -69,18 +80,8 @@ class GameFragment : Fragment() {
* Called when the game is finished * Called when the game is finished
*/ */
private fun gameFinished() { private fun gameFinished() {
val action = GameFragmentDirections.actionGameToScore(viewModel.score) val action = GameFragmentDirections.actionGameToScore(viewModel.score.value ?: 0)
findNavController(this).navigate(action) 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()
}
} }

View File

@@ -1,27 +1,33 @@
package com.example.android.guesstheword.screens.game package com.example.android.guesstheword.screens.game
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import timber.log.Timber
class GameViewModel(): ViewModel() { class GameViewModel(): ViewModel() {
// The current word // The current word
var word = "" private val _word = MutableLiveData<String>()
val word: LiveData<String>
get() = _word
// The current score // 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 // The list of words - the front of the list is the next word to guess
private lateinit var wordList: MutableList<String> private lateinit var wordList: MutableList<String>
init { init {
Timber.i("GameViewModel created")
resetList() resetList()
nextWord() nextWord()
} _eventGameFinished.value = false
_word.value = ""
override fun onCleared() { _score.value = 0
super.onCleared()
Timber.i("GameViewModel destroyed")
} }
/** /**
@@ -60,19 +66,25 @@ class GameViewModel(): ViewModel() {
private fun nextWord() { private fun nextWord() {
//Select and remove a word from the list //Select and remove a word from the list
if (wordList.isEmpty()) { if (wordList.isEmpty()) {
// gameFinished() _eventGameFinished.value = true
} else { } else {
word = wordList.removeAt(0) _word.value = wordList.removeAt(0)
} }
} }
fun onSkip() { fun onSkip() {
score-- if (score.value != 0) {
_score.value = (score.value)?.dec()
}
nextWord() nextWord()
} }
fun onCorrect() { fun onCorrect() {
score++ _score.value = (score.value)?.inc()
nextWord() nextWord()
} }
fun onGameFinishComplete() {
_eventGameFinished.value = false
}
} }