Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 1bec2397 authored by 62160052's avatar 62160052
Browse files

Add LiveData to the current scrambled word

parent 3be6e672
No related branches found
No related tags found
No related merge requests found
......@@ -22,7 +22,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.example.android.unscramble.R
import com.example.android.unscramble.databinding.GameFragmentBinding
import com.google.android.material.dialog.MaterialAlertDialogBuilder
......
package com.example.android.unscramble.ui.game
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class GameViewModel: ViewModel() {
......@@ -11,8 +13,8 @@ class GameViewModel: ViewModel() {
val currentWordCount: Int
get() = _currentWordCount
private lateinit var _currentScrambledWord: String
val currentScrambledWord: String
private val _currentScrambledWord = MutableLiveData<String>()
val currentScrambledWord: LiveData<String>
get() = _currentScrambledWord
private var wordsList: MutableList<String> = mutableListOf()
private lateinit var currentWord: String
......@@ -27,7 +29,7 @@ class GameViewModel: ViewModel() {
Log.d("GameFragment", "GameViewModel destroyed!")
}
fun getNextWord() {
private fun getNextWord() {
currentWord = allWordsList.random()
val tempWord = currentWord.toCharArray()
tempWord.shuffle()
......@@ -37,7 +39,7 @@ class GameViewModel: ViewModel() {
if (wordsList.contains(currentWord)) {
getNextWord()
} else {
_currentScrambledWord = String(tempWord)
_currentScrambledWord.value = String(tempWord)
++_currentWordCount
wordsList.add(currentWord)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment