Modern Apps git

← Modern-Apps.git

chess: fix uncancelable new game dialog and duplicate win count (#667, #668)

Commit
21608c0ed396b25163fbeebf1a1c39ce571d539d
Author
vayun-mathur <[email protected]>
When
2026-09-16 01:56 UTC (2 months ago)
Parents
3ce476b

Diff

games/chess/src/main/java/com/vayunmathur/games/chess/ChessGameScreens.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/ChessGameScreens.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/ChessGameScreens.ktindex 3c0c2ef42c..74d2817d54 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/ChessGameScreens.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/ChessGameScreens.kt@@ -92,7 +92,7 @@ internal val ChessSidePanelWidth = 300.dp  * disabled when the model cannot run: a mode that can never move is not worth explaining.  */ @Composable-fun NewGameDialog(onNewGame: (GameMode) -> Unit, aiAvailable: Boolean = true) {+fun NewGameDialog(onNewGame: (GameMode) -> Unit, onDismiss: () -> Unit = {}, aiAvailable: Boolean = true) {     var showSettings by remember { mutableStateOf<((PieceColor, Difficulty) -> Unit)?>(null) }      showSettings?.let { startGame ->@@ -150,7 +150,7 @@ fun NewGameDialog(onNewGame: (GameMode) -> Unit, aiAvailable: Boolean = true) {         )     } ?:         AlertDialog(-            onDismissRequest = { },+            onDismissRequest = onDismiss,             title = { Text(text = stringResource(R.string.new_game)) },             text = {                 Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {@@ -213,11 +213,6 @@ fun ChessGame(          if (playerWins) {             achievementsManager.onAchievementUnlocked("first_mate")-            val ds = DataStoreUtils.getInstance(context)-            val currentWins = (ds.getLong("chess_wins_count") ?: 0L) + 1-            ds.setLong("chess_wins_count", currentWins)-            achievementsManager.onProgressUpdated("win_10", currentWins.toInt())-            achievementsManager.onProgressUpdated("win_50", currentWins.toInt())              if (uiState.board.moves.size <= 40) {                 achievementsManager.onAchievementUnlocked("won_fast")@@ -226,6 +221,17 @@ fun ChessGame(             if (mode is GameMode.VsAI && mode.difficulty >= Difficulty.ADVANCED) {                 achievementsManager.onAchievementUnlocked("win_vs_ai_hard")             }++            // Only tick the persistent win counter once per game. This effect re-runs when the+            // Game screen re-enters composition (e.g. returning from the achievements screen), and+            // the outcome is still a win, so without this guard the count would climb each time.+            if (viewModel.claimWinScoring()) {+                val ds = DataStoreUtils.getInstance(context)+                val currentWins = (ds.getLong("chess_wins_count") ?: 0L) + 1+                ds.setLong("chess_wins_count", currentWins)+                achievementsManager.onProgressUpdated("win_10", currentWins.toInt())+                achievementsManager.onProgressUpdated("win_50", currentWins.toInt())+            }         }     } 
games/chess/src/main/java/com/vayunmathur/games/chess/MainActivity.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/MainActivity.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/MainActivity.ktindex 41f0e22da8..46db5d9681 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/MainActivity.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/MainActivity.kt@@ -94,6 +94,7 @@ class MainActivity : ComponentActivity() {                                         viewModel.onNewGame(it)                                         showNewGameDialog = false                                     },+                                    onDismiss = { showNewGameDialog = false },                                     aiAvailable = aiAvailable                                 )                             }
games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.ktindex ddf5fc111b..f2ababdeec 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt@@ -55,11 +55,24 @@ class ChessViewModel(application: Application) : AndroidViewModel(application),     private val _aiAvailable = MutableStateFlow(false)     val aiAvailable: StateFlow<Boolean> = _aiAvailable.asStateFlow() +    // Guards one-time scoring of a finished game. The win-counting effect lives in composition and+    // re-runs whenever the Game screen re-enters composition (e.g. returning from the achievements+    // screen), which must not tick the counter again (#668). Reset on every new game.+    private var winScored = false+     init {         viewModelScope.launch { _aiAvailable.value = chessApi.isAvailable() }     } +    /** Returns true only the first time it is called for the current game; false thereafter. */+    fun claimWinScoring(): Boolean {+        if (winScored) return false+        winScored = true+        return true+    }+     fun onNewGame(gameMode: GameMode) {+        winScored = false         val isFlipped = gameMode is GameMode.VsAI && gameMode.playerColor == PieceColor.BLACK         _uiState.value = ChessUiState(gameMode = gameMode, isBoardFlipped = isFlipped)         if (gameMode is GameMode.VsAI && gameMode.playerColor == PieceColor.BLACK) {