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 58615a3..ee573d2 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 @@ -58,13 +58,17 @@ class GameFragment : Fragment() { } viewModel.score.observe(this, Observer { - binding.scoreText.text = it.toString() + binding.scoreText.text = getString(R.string.score_format, it) }) viewModel.word.observe(this, Observer { binding.wordText.text = it }) + viewModel.gameTimer.observe(this, Observer { + binding.timerText.text = getString(R.string.time_format, it) + }) + viewModel.eventGameFinished.observe(this, Observer { if (it) { gameFinished() 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 45992e4..cfe46d3 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,10 +1,26 @@ package com.example.android.guesstheword.screens.game +import android.os.CountDownTimer +import android.text.format.DateUtils import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.Transformations import androidx.lifecycle.ViewModel class GameViewModel(): ViewModel() { + + companion object { + // These represent different important times + // This is when the game is over + const val DONE = 0L + // This is the number of milliseconds in a second + const val ONE_SECOND = 1000L + // This is the total time of the game + const val COUNTDOWN_TIME = 60000L + } + + private val timer: CountDownTimer + // The current word private val _word = MutableLiveData() val word: LiveData @@ -15,6 +31,12 @@ class GameViewModel(): ViewModel() { val score: LiveData get() = _score + private val _gameTimer = MutableLiveData() + val gameTimer: LiveData + get() = Transformations.map(_gameTimer) { + DateUtils.formatElapsedTime(it / ONE_SECOND) + }; + private val _eventGameFinished = MutableLiveData() val eventGameFinished: LiveData get() = _eventGameFinished @@ -28,6 +50,24 @@ class GameViewModel(): ViewModel() { _eventGameFinished.value = false _word.value = "" _score.value = 0 + + timer = object : CountDownTimer(COUNTDOWN_TIME, ONE_SECOND) { + + override fun onTick(millisUntilFinished: Long) { + _gameTimer.value = millisUntilFinished + } + + override fun onFinish() { + endGame() + } + } + + timer.start() + } + + override fun onCleared() { + super.onCleared() + timer.cancel() } /** @@ -66,7 +106,7 @@ class GameViewModel(): ViewModel() { private fun nextWord() { //Select and remove a word from the list if (wordList.isEmpty()) { - _eventGameFinished.value = true + endGame() } else { _word.value = wordList.removeAt(0) } @@ -84,6 +124,13 @@ class GameViewModel(): ViewModel() { nextWord() } + fun endGame() { + if (_gameTimer.value?.toInt() == 0) { + timer.cancel() + } + _eventGameFinished.value = true + } + fun onGameFinishComplete() { _eventGameFinished.value = false } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5dd9777..5a5302c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -26,4 +26,5 @@ The word is… \"%s\" Current Score: %d + Time Left: %s