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 64d1d8846..1d828a08e 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 @@ -17,6 +17,7 @@ package com.example.android.guesstheword.screens.game import android.os.Bundle +import android.text.format.DateUtils import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -59,16 +60,17 @@ class GameFragment : Fragment() { } /** Setting up LiveData observation relationship **/ - viewModel.word.observe(this, Observer { newWord -> + viewModel.word.observe(this.viewLifecycleOwner, Observer { newWord -> binding.wordText.text = newWord }) - viewModel.score.observe(this, Observer { newScore -> + viewModel.score.observe(viewLifecycleOwner, Observer { newScore -> binding.scoreText.text = newScore.toString() }) - // TODO (07) Setup an observer relationship to update binding.timerText - // You can use DateUtils.formatElapsedTime to correctly format the long to a time string + viewModel.currentTime.observe(viewLifecycleOwner, Observer { newTime -> + binding.timerText.text = DateUtils.formatElapsedTime(newTime / 1000) + }) // Sets up event listening to navigate the player when the game is finished viewModel.eventGameFinish.observe(viewLifecycleOwner, Observer { isFinished -> @@ -79,9 +81,6 @@ class GameFragment : Fragment() { viewModel.onGameFinishComplete() } }) - return binding.root - } - } 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 21ad0fad5..74371352a 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 @@ -16,6 +16,7 @@ package com.example.android.guesstheword.screens.game +import android.os.CountDownTimer import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel @@ -26,12 +27,17 @@ import androidx.lifecycle.ViewModel */ class GameViewModel : ViewModel() { - // TODO (01) Copy over the provided companion object with the timer constants + companion object { + private const val DONE = 0L + private const val SECOND = 1000L + private const val COUNTDOWN_TIME = 10000L + } - // TODO (02) Create a timer field of type CountDownTimer + private val timer: CountDownTimer - // TODO (03) Create a properly encapsulated LiveData for the current time called currentTime - // Its type should be Long + private val _currentTime = MutableLiveData() + val currentTime: LiveData + get() = _currentTime // The current word private val _word = MutableLiveData() @@ -57,9 +63,18 @@ class GameViewModel : ViewModel() { resetList() nextWord() _score.value = 0 + _currentTime.value = COUNTDOWN_TIME + + timer = object : CountDownTimer(COUNTDOWN_TIME, SECOND) { + override fun onTick(millisUntilFinished: Long) { + _currentTime.value = _currentTime.value?.minus(SECOND) + } - // TODO (04) Copy over the CountDownTimer code and then update currentTime and - // eventGameFinish appropriately as the timer ticks and finishes + override fun onFinish() { + _eventGameFinish.value = true + } + } + timer.start() } /** @@ -96,14 +111,10 @@ class GameViewModel : ViewModel() { * Moves to the next word in the list */ private fun nextWord() { - //Select and remove a word from the list if (wordList.isEmpty()) { - // TODO (05) Update this logic so that the game doesn't finish; - // Instead the list is reset and re-shuffled when you run out of words - _eventGameFinish.value = true - } else { - _word.value = wordList.removeAt(0) + resetList() } + _word.value = wordList.removeAt(0) } /** Methods for buttons presses **/ @@ -124,6 +135,8 @@ class GameViewModel : ViewModel() { _eventGameFinish.value = false } - // TODO (06) Cancel the timer in onCleared - + override fun onCleared() { + super.onCleared() + timer.cancel() + } }