diff --git a/app/src/main/java/com/example/android/guesstheword/screens/game/GameFragment.kt b/app/src/main/java/com/example/android/guesstheword/screens/game/GameFragment.kt index 9f5ab67..58615a3 100644 --- a/app/src/main/java/com/example/android/guesstheword/screens/game/GameFragment.kt +++ b/app/src/main/java/com/example/android/guesstheword/screens/game/GameFragment.kt @@ -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() - } } 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 index b229663..45992e4 100644 --- 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 @@ -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() + val word: LiveData + get() = _word // The current score - var score = 0 + private val _score = MutableLiveData() + val score: LiveData + get() = _score + + private val _eventGameFinished = MutableLiveData() + val eventGameFinished: LiveData + get() = _eventGameFinished // 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") + _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 + } } \ No newline at end of file