Added timer
This commit is contained in:
@@ -58,13 +58,17 @@ class GameFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
viewModel.score.observe(this, Observer {
|
viewModel.score.observe(this, Observer {
|
||||||
binding.scoreText.text = it.toString()
|
binding.scoreText.text = getString(R.string.score_format, it)
|
||||||
})
|
})
|
||||||
|
|
||||||
viewModel.word.observe(this, Observer {
|
viewModel.word.observe(this, Observer {
|
||||||
binding.wordText.text = it
|
binding.wordText.text = it
|
||||||
})
|
})
|
||||||
|
|
||||||
|
viewModel.gameTimer.observe(this, Observer {
|
||||||
|
binding.timerText.text = getString(R.string.time_format, it)
|
||||||
|
})
|
||||||
|
|
||||||
viewModel.eventGameFinished.observe(this, Observer {
|
viewModel.eventGameFinished.observe(this, Observer {
|
||||||
if (it) {
|
if (it) {
|
||||||
gameFinished()
|
gameFinished()
|
||||||
|
|||||||
@@ -1,10 +1,26 @@
|
|||||||
package com.example.android.guesstheword.screens.game
|
package com.example.android.guesstheword.screens.game
|
||||||
|
|
||||||
|
import android.os.CountDownTimer
|
||||||
|
import android.text.format.DateUtils
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
import androidx.lifecycle.Transformations
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
|
|
||||||
class GameViewModel(): 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
|
// The current word
|
||||||
private val _word = MutableLiveData<String>()
|
private val _word = MutableLiveData<String>()
|
||||||
val word: LiveData<String>
|
val word: LiveData<String>
|
||||||
@@ -15,6 +31,12 @@ class GameViewModel(): ViewModel() {
|
|||||||
val score: LiveData<Int>
|
val score: LiveData<Int>
|
||||||
get() = _score
|
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>()
|
private val _eventGameFinished = MutableLiveData<Boolean>()
|
||||||
val eventGameFinished: LiveData<Boolean>
|
val eventGameFinished: LiveData<Boolean>
|
||||||
get() = _eventGameFinished
|
get() = _eventGameFinished
|
||||||
@@ -28,6 +50,24 @@ class GameViewModel(): ViewModel() {
|
|||||||
_eventGameFinished.value = false
|
_eventGameFinished.value = false
|
||||||
_word.value = ""
|
_word.value = ""
|
||||||
_score.value = 0
|
_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() {
|
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()) {
|
||||||
_eventGameFinished.value = true
|
endGame()
|
||||||
} else {
|
} else {
|
||||||
_word.value = wordList.removeAt(0)
|
_word.value = wordList.removeAt(0)
|
||||||
}
|
}
|
||||||
@@ -84,6 +124,13 @@ class GameViewModel(): ViewModel() {
|
|||||||
nextWord()
|
nextWord()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun endGame() {
|
||||||
|
if (_gameTimer.value?.toInt() == 0) {
|
||||||
|
timer.cancel()
|
||||||
|
}
|
||||||
|
_eventGameFinished.value = true
|
||||||
|
}
|
||||||
|
|
||||||
fun onGameFinishComplete() {
|
fun onGameFinishComplete() {
|
||||||
_eventGameFinished.value = false
|
_eventGameFinished.value = false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,4 +26,5 @@
|
|||||||
<string name="word_is">The word is…</string>
|
<string name="word_is">The word is…</string>
|
||||||
<string name="quote_format">\"%s\"</string>
|
<string name="quote_format">\"%s\"</string>
|
||||||
<string name="score_format">Current Score: %d</string>
|
<string name="score_format">Current Score: %d</string>
|
||||||
|
<string name="time_format">Time Left: %s</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Reference in New Issue
Block a user