Day68 of #100DaysOfCode
Hii folks 🙌
Today I will be continuing the same pathway in which we’ll Implement the Skip button logic and verify the ViewModel data.
Unit 3: Navigation
Pathway 2: Architecture componentsSource
https://developer.android.com/courses/android-basics-kotlin/course
Add the implementation for onSkipWord()
which handles when the Skip button is clicked.
- Similar to
onSubmitWord()
, we will add a condition to theonSkipWord()
method. Iftrue
, display the word on the screen and reset the text field. Iffalse
and there are no more words left in this round, we will show the alert dialog with the final score.
/*
* Skips the current word without changing the score.
*/
private fun onSkipWord() {
if (viewModel.nextWord()) {
setErrorTextField(false)
updateNextWordOnScreen()
} else {
showFinalScoreDialog()
}
}
- We will run our app. Play the game and will Notice that the Skip and Submit buttons are working as intended.
- Now we will verify the data preserved by ViewModel, In
GameViewModel
, right-click on the variablecurrentWordCount
, select Refactor > Rename... . Prefix the new name with an underscore,_currentWordCount
. - Now we will add a backing field.
private var _currentWordCount = 0
val currentWordCount: Int
get() = _currentWordCount
- In
GameFragment
insideonCreateView()
, above the return statement we will add another log to print the app data, word, score, and word count.
Log.d("GameFragment", "Word: ${viewModel.currentScrambledWord} " +
"Score: ${viewModel.score} WordCount: ${viewModel.currentWordCount}")
We will notice that the app data is preserved in the ViewModel
during orientation, changes will update score value and word count in the UI using LiveData
and Data Binding in later codelabs.
That is all for Day68 ✅
Thanks for reading, See you tomorrow!
If you are reading my #100Days Journey, feel free to drop by ;)