Use game finished setting to display victory dialog.
This commit is contained in:
@@ -21,6 +21,7 @@ enum class Language(val value: LocaleListCompat) {
|
|||||||
enum class KeepScreenOn(val value: Boolean) { ON(true), OFF(false) }
|
enum class KeepScreenOn(val value: Boolean) { ON(true), OFF(false) }
|
||||||
|
|
||||||
typealias VictoryPoints = Int
|
typealias VictoryPoints = Int
|
||||||
|
typealias GameWon = Boolean
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class SettingsAdapter @Inject constructor(@ApplicationContext private val context: Context) {
|
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 victoryPoints = MutableStateFlow(0)
|
||||||
|
|
||||||
|
val gameFinished = MutableStateFlow(false)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
language.value = try {
|
language.value = try {
|
||||||
enumValueOf(sharedPreferences.getString(Language::class.simpleName, null)!!)
|
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)
|
victoryPoints.value = sharedPreferences.getInt(VictoryPoints::class.simpleName, 1000)
|
||||||
|
|
||||||
|
gameFinished.value = sharedPreferences.getBoolean(GameWon::class.simpleName, false)
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
language.collect {
|
language.collect {
|
||||||
@@ -80,6 +84,12 @@ class SettingsAdapter @Inject constructor(@ApplicationContext private val contex
|
|||||||
updatePreference(VictoryPoints::class.simpleName, it)
|
updatePreference(VictoryPoints::class.simpleName, it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
|
gameFinished.collect {
|
||||||
|
updatePreference(GameWon::class.simpleName, it)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updatePreference(name: String?, value: String) {
|
private fun updatePreference(name: String?, value: String) {
|
||||||
@@ -88,9 +98,9 @@ class SettingsAdapter @Inject constructor(@ApplicationContext private val contex
|
|||||||
editor.apply()
|
editor.apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updatePreference(name: String?, value: Long) {
|
private fun updatePreference(name: String?, value: Boolean) {
|
||||||
val editor = sharedPreferences.edit()
|
val editor = sharedPreferences.edit()
|
||||||
editor.putLong(name, value)
|
editor.putBoolean(name, value)
|
||||||
editor.apply()
|
editor.apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,10 +6,12 @@ import androidx.compose.runtime.mutableStateOf
|
|||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
import androidx.compose.ui.focus.FocusRequester
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.asLiveData
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.flow.collect
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import me.zobrist.tichucounter.data.entity.Game
|
import me.zobrist.tichucounter.data.entity.Game
|
||||||
import me.zobrist.tichucounter.data.entity.Round
|
import me.zobrist.tichucounter.data.entity.Round
|
||||||
@@ -162,9 +164,11 @@ class CounterViewModel @Inject constructor(
|
|||||||
|
|
||||||
private var distinctTeamNames = listOf<String>()
|
private var distinctTeamNames = listOf<String>()
|
||||||
|
|
||||||
private var victoryDialogShown = false
|
|
||||||
|
|
||||||
private var lastGame: Game? = null
|
private var lastGame: Game? = null
|
||||||
|
private var lastVictoryPoints: Int? = null
|
||||||
|
|
||||||
|
private val gameWon: Boolean
|
||||||
|
get() = totalScoreA >= settings.victoryPoints.value || totalScoreB >= settings.victoryPoints.value
|
||||||
|
|
||||||
init {
|
init {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
@@ -182,12 +186,15 @@ class CounterViewModel @Inject constructor(
|
|||||||
buildTeamNameSuggestions()
|
buildTeamNameSuggestions()
|
||||||
|
|
||||||
if (it.game.uid != lastGame?.uid) {
|
if (it.game.uid != lastGame?.uid) {
|
||||||
victoryDialogShown = false
|
if(lastGame != null)
|
||||||
|
{
|
||||||
|
settings.gameFinished.value = false
|
||||||
|
}
|
||||||
lastGame = it.game
|
lastGame = it.game
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!victoryDialogShown) {
|
if (!settings.gameFinished.value) {
|
||||||
if (totalScoreA >= settings.victoryPoints.value || totalScoreB >= settings.victoryPoints.value) {
|
if (gameWon) {
|
||||||
showVictoryDialog = true
|
showVictoryDialog = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -201,9 +208,17 @@ class CounterViewModel @Inject constructor(
|
|||||||
|
|
||||||
buildTeamNameSuggestions()
|
buildTeamNameSuggestions()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
viewModelScope.launch {
|
||||||
settings.victoryPoints.collect {
|
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) {
|
override fun victoryDialogExecuted(result: Boolean) {
|
||||||
showVictoryDialog = false
|
showVictoryDialog = false
|
||||||
victoryDialogShown = true
|
settings.gameFinished.value = true
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
|
|||||||
Reference in New Issue
Block a user