Use game finished setting to display victory dialog.

This commit is contained in:
2023-09-23 11:42:57 +02:00
parent 81a0d680e9
commit c3c0f09313
2 changed files with 35 additions and 10 deletions

View File

@@ -21,6 +21,7 @@ enum class Language(val value: LocaleListCompat) {
enum class KeepScreenOn(val value: Boolean) { ON(true), OFF(false) }
typealias VictoryPoints = Int
typealias GameWon = Boolean
@Singleton
class SettingsAdapter @Inject constructor(@ApplicationContext private val context: Context) {
@@ -35,6 +36,8 @@ class SettingsAdapter @Inject constructor(@ApplicationContext private val contex
val victoryPoints = MutableStateFlow(0)
val gameFinished = MutableStateFlow(false)
init {
language.value = try {
enumValueOf(sharedPreferences.getString(Language::class.simpleName, null)!!)
@@ -56,6 +59,7 @@ class SettingsAdapter @Inject constructor(@ApplicationContext private val contex
victoryPoints.value = sharedPreferences.getInt(VictoryPoints::class.simpleName, 1000)
gameFinished.value = sharedPreferences.getBoolean(GameWon::class.simpleName, false)
CoroutineScope(Dispatchers.IO).launch {
language.collect {
@@ -80,6 +84,12 @@ class SettingsAdapter @Inject constructor(@ApplicationContext private val contex
updatePreference(VictoryPoints::class.simpleName, it)
}
}
CoroutineScope(Dispatchers.IO).launch {
gameFinished.collect {
updatePreference(GameWon::class.simpleName, it)
}
}
}
private fun updatePreference(name: String?, value: String) {
@@ -88,9 +98,9 @@ class SettingsAdapter @Inject constructor(@ApplicationContext private val contex
editor.apply()
}
private fun updatePreference(name: String?, value: Long) {
private fun updatePreference(name: String?, value: Boolean) {
val editor = sharedPreferences.edit()
editor.putLong(name, value)
editor.putBoolean(name, value)
editor.apply()
}

View File

@@ -6,10 +6,12 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.focus.FocusRequester
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import me.zobrist.tichucounter.data.entity.Game
import me.zobrist.tichucounter.data.entity.Round
@@ -162,9 +164,11 @@ class CounterViewModel @Inject constructor(
private var distinctTeamNames = listOf<String>()
private var victoryDialogShown = false
private var lastGame: Game? = null
private var lastVictoryPoints: Int? = null
private val gameWon: Boolean
get() = totalScoreA >= settings.victoryPoints.value || totalScoreB >= settings.victoryPoints.value
init {
viewModelScope.launch {
@@ -182,12 +186,15 @@ class CounterViewModel @Inject constructor(
buildTeamNameSuggestions()
if (it.game.uid != lastGame?.uid) {
victoryDialogShown = false
if(lastGame != null)
{
settings.gameFinished.value = false
}
lastGame = it.game
}
if (!victoryDialogShown) {
if (totalScoreA >= settings.victoryPoints.value || totalScoreB >= settings.victoryPoints.value) {
if (!settings.gameFinished.value) {
if (gameWon) {
showVictoryDialog = true
}
}
@@ -201,9 +208,17 @@ class CounterViewModel @Inject constructor(
buildTeamNameSuggestions()
}
}
viewModelScope.launch {
settings.victoryPoints.collect {
victoryDialogShown = false
if(lastVictoryPoints == null)
{
lastVictoryPoints = it
return@collect
}
// Game was already won and will be won also with new settings
settings.gameFinished.value = settings.gameFinished.value && gameWon
}
}
}
@@ -316,7 +331,7 @@ class CounterViewModel @Inject constructor(
override fun victoryDialogExecuted(result: Boolean) {
showVictoryDialog = false
victoryDialogShown = true
settings.gameFinished.value = true
if (result) {
viewModelScope.launch {