Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand All @@ -79,9 +81,6 @@ class GameFragment : Fragment() {
viewModel.onGameFinishComplete()
}
})

return binding.root

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Long>()
val currentTime: LiveData<Long>
get() = _currentTime

// The current word
private val _word = MutableLiveData<String>()
Expand All @@ -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()
}

/**
Expand Down Expand Up @@ -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 **/
Expand All @@ -124,6 +135,8 @@ class GameViewModel : ViewModel() {
_eventGameFinish.value = false
}

// TODO (06) Cancel the timer in onCleared

override fun onCleared() {
super.onCleared()
timer.cancel()
}
}