Added timer
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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<String>()
|
||||
val word: LiveData<String>
|
||||
@@ -15,6 +31,12 @@ class GameViewModel(): ViewModel() {
|
||||
val score: LiveData<Int>
|
||||
get() = _score
|
||||
|
||||
private val _gameTimer = MutableLiveData<Long>()
|
||||
val gameTimer: LiveData<String>
|
||||
get() = Transformations.map(_gameTimer) {
|
||||
DateUtils.formatElapsedTime(it / ONE_SECOND)
|
||||
};
|
||||
|
||||
private val _eventGameFinished = MutableLiveData<Boolean>()
|
||||
val eventGameFinished: LiveData<Boolean>
|
||||
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
|
||||
}
|
||||
|
||||
@@ -26,4 +26,5 @@
|
||||
<string name="word_is">The word is…</string>
|
||||
<string name="quote_format">\"%s\"</string>
|
||||
<string name="score_format">Current Score: %d</string>
|
||||
<string name="time_format">Time Left: %s</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user