Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 4ca98441 authored by 62160052's avatar 62160052
Browse files

Attach observer to the LiveData object

parent 1bec2397
No related branches found
No related tags found
No related merge requests found
...@@ -66,10 +66,14 @@ class GameFragment : Fragment() { ...@@ -66,10 +66,14 @@ class GameFragment : Fragment() {
binding.submit.setOnClickListener { onSubmitWord() } binding.submit.setOnClickListener { onSubmitWord() }
binding.skip.setOnClickListener { onSkipWord() } binding.skip.setOnClickListener { onSkipWord() }
// Update the UI // Update the UI
updateNextWordOnScreen()
binding.score.text = getString(R.string.score, 0) binding.score.text = getString(R.string.score, 0)
binding.wordCount.text = getString( binding.wordCount.text = getString(
R.string.word_count, 0, MAX_NO_OF_WORDS) R.string.word_count, 0, MAX_NO_OF_WORDS)
// Observe the scrambledCharArray LiveData, passing in the LifecycleOwner and the observer.
viewModel.currentScrambledWord.observe(viewLifecycleOwner) {
newWord -> binding.textViewUnscrambledWord.text = newWord
}
} }
/* /*
...@@ -81,9 +85,7 @@ class GameFragment : Fragment() { ...@@ -81,9 +85,7 @@ class GameFragment : Fragment() {
if (viewModel.isUserWordCorrect(playerWord)) { if (viewModel.isUserWordCorrect(playerWord)) {
setErrorTextField(false) setErrorTextField(false)
if (viewModel.nextWord()) { if (!viewModel.nextWord()) {
updateNextWordOnScreen()
} else {
showFinalScoreDialog() showFinalScoreDialog()
} }
} else { } else {
...@@ -98,7 +100,7 @@ class GameFragment : Fragment() { ...@@ -98,7 +100,7 @@ class GameFragment : Fragment() {
private fun onSkipWord() { private fun onSkipWord() {
if (viewModel.nextWord()) { if (viewModel.nextWord()) {
setErrorTextField(false) setErrorTextField(false)
updateNextWordOnScreen()
} else { } else {
showFinalScoreDialog() showFinalScoreDialog()
} }
...@@ -120,7 +122,7 @@ class GameFragment : Fragment() { ...@@ -120,7 +122,7 @@ class GameFragment : Fragment() {
private fun restartGame() { private fun restartGame() {
viewModel.reinitializeData() viewModel.reinitializeData()
setErrorTextField(false) setErrorTextField(false)
updateNextWordOnScreen()
} }
/* /*
...@@ -160,7 +162,4 @@ class GameFragment : Fragment() { ...@@ -160,7 +162,4 @@ class GameFragment : Fragment() {
/* /*
* Displays the next scrambled word on screen. * Displays the next scrambled word on screen.
*/ */
private fun updateNextWordOnScreen() {
binding.textViewUnscrambledWord.text = viewModel.currentScrambledWord
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment