Modern Apps git

← Modern-Apps.git

games: confirm before destructive reset (#700)

Commit
5971f9e4fbfdfeff57f5f0db038e5d392364bbd2
Author
vayun-mathur <[email protected]>
When
2026-09-12 23:52 UTC (2 months ago)
Parents
8376974

Diff

games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.kt
diff --git a/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.kt b/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.ktindex 89cfd986d8..022c23b446 100644--- a/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.kt+++ b/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.kt@@ -10,6 +10,10 @@ import androidx.compose.foundation.layout.widthIn import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource@@ -25,8 +29,10 @@ import com.vayunmathur.library.ui.AppBarAlignment import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.CircularProgressIndicator+import com.vayunmathur.library.ui.ConfirmDialog import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.OutlinedButton+import com.vayunmathur.library.ui.R as UiR import com.vayunmathur.library.ui.Spacing import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.appBarScrollBehavior@@ -50,6 +56,7 @@ fun ArrowsGameScreen(     onOpenGameCenter: () -> Unit, ) {     val daily = state.mode == GameMode.DAILY+    var confirmRestart by remember { mutableStateOf(false) }     AppScaffold(         title = {             GameModeChooser(@@ -175,7 +182,14 @@ fun ArrowsGameScreen(                                     textAlign = TextAlign.Center,                                 )                             }-                            OutlinedButton(onClick = { actions.restartLevel() }) {+                            // Restart puts every arrow back, so only ask when the+                            // board has moved: a fresh board restarts into itself.+                            val hasProgress = game.removed.isNotEmpty() ||+                                game.hearts != STARTING_HEARTS+                            OutlinedButton(onClick = {+                                if (hasProgress) confirmRestart = true+                                else actions.restartLevel()+                            }) {                                 Text(stringResource(R.string.restart))                             }                         }@@ -184,4 +198,16 @@ fun ArrowsGameScreen(             }         }     }++    if (confirmRestart) {+        ConfirmDialog(+            title = stringResource(R.string.confirm_restart_title),+            message = stringResource(R.string.confirm_restart_message),+            confirmLabel = stringResource(R.string.restart),+            dismissLabel = stringResource(UiR.string.cancel),+            destructive = true,+            onConfirm = { actions.restartLevel() },+            onDismiss = { confirmRestart = false },+        )+    } }
games/arrows/src/main/res/values/strings.xml
diff --git a/games/arrows/src/main/res/values/strings.xml b/games/arrows/src/main/res/values/strings.xmlindex 7950ea9bbc..c668071cef 100644--- a/games/arrows/src/main/res/values/strings.xml+++ b/games/arrows/src/main/res/values/strings.xml@@ -14,6 +14,10 @@     <string name="next_level">Next Level</string>     <!-- ArrowsGameScreen: Puts every arrow back and restores the hearts. Verb -->     <string name="restart">Restart</string>+    <!-- ArrowsGameScreen restart confirmation: Title asking whether to reset the board. Question -->+    <string name="confirm_restart_title">Restart this board?</string>+    <!-- ArrowsGameScreen restart confirmation: Explains progress on the board is lost. Sentence -->+    <string name="confirm_restart_message">Your progress on this board will be lost.</string>     <!-- ArrowsGameScreen: Shown after clearing the daily, since there is only one per day. Sentence -->     <string name="daily_come_back_tomorrow">Cleared! Come back tomorrow for a new board.</string>     <!-- ArrowsGameScreen: Celebratory line shown when every arrow has flown. Declarative -->
games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.ktindex a9f895d50c..86437e2e23 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameBoardScreen.kt@@ -11,6 +11,10 @@ import androidx.compose.foundation.layout.widthIn import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.pluralStringResource@@ -26,6 +30,7 @@ import com.vayunmathur.library.ui.AppBarAlignment import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.CircularProgressIndicator+import com.vayunmathur.library.ui.ConfirmDialog import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.IconFlag import com.vayunmathur.library.ui.MaterialTheme@@ -55,6 +60,12 @@ fun GameBoardScreen( ) {     val game = state.game     val mineCount = state.config.difficulty.mineCount(state.config.size)+    // The game is only worth confirming over once the first dig has laid the+    // mines: a fresh field restarts into an identical blank one, and giving up+    // on it records no loss.+    val hasProgress = game?.let { it.started && !it.isOver } == true+    var confirmRestart by remember { mutableStateOf(false) }+    var confirmGiveUp by remember { mutableStateOf(false) }     AppScaffold(         // Size name plus the mine count rather than size plus density name: the two enums share         // labels ("Medium" and "Medium"), and the raw count is what a player actually wants to know.@@ -129,19 +140,50 @@ fun GameBoardScreen(                     horizontalArrangement = Arrangement.spacedBy(Spacing.sm),                 ) {                     Button(-                        onClick = { actions.restart() },+                        onClick = { if (hasProgress) confirmRestart = true else actions.restart() },                         modifier = Modifier.weight(1f),                     ) { Text(stringResource(R.string.new_field_button)) }                     TextButton(                         onClick = {-                            actions.giveUp()-                            onExit()+                            if (hasProgress) {+                                confirmGiveUp = true+                            } else {+                                actions.giveUp()+                                onExit()+                            }                         },                         modifier = Modifier.weight(1f),                     ) { Text(stringResource(R.string.give_up)) }                 }             } +            if (confirmRestart) {+                ConfirmDialog(+                    title = stringResource(R.string.confirm_new_field_title),+                    message = stringResource(R.string.confirm_new_field_message),+                    confirmLabel = stringResource(R.string.new_field_button),+                    dismissLabel = stringResource(UiR.string.cancel),+                    destructive = true,+                    onConfirm = { actions.restart() },+                    onDismiss = { confirmRestart = false },+                )+            }++            if (confirmGiveUp) {+                ConfirmDialog(+                    title = stringResource(R.string.confirm_give_up_title),+                    message = stringResource(R.string.confirm_give_up_message),+                    confirmLabel = stringResource(R.string.give_up),+                    dismissLabel = stringResource(UiR.string.cancel),+                    destructive = true,+                    onConfirm = {+                        actions.giveUp()+                        onExit()+                    },+                    onDismiss = { confirmGiveUp = false },+                )+            }+             if (game.isOver) {                 val won = game.outcome == GameOutcome.WON                 GameResultOverlay(
games/minesweeper/src/main/res/values/strings.xml
diff --git a/games/minesweeper/src/main/res/values/strings.xml b/games/minesweeper/src/main/res/values/strings.xmlindex 332f1269a8..af55b15470 100644--- a/games/minesweeper/src/main/res/values/strings.xml+++ b/games/minesweeper/src/main/res/values/strings.xml@@ -48,6 +48,14 @@     <string name="give_up">Give Up</string>     <!-- GameBoardScreen: Starts a fresh field with the same settings. Verb phrase -->     <string name="new_field_button">New Field</string>+    <!-- GameBoardScreen restart confirmation: Title asking whether to throw away the field in progress. Question -->+    <string name="confirm_new_field_title">Start a new field?</string>+    <!-- GameBoardScreen restart confirmation: Explains the current field is discarded. Sentence -->+    <string name="confirm_new_field_message">Your current field will be discarded.</string>+    <!-- GameBoardScreen give-up confirmation: Title asking whether to abandon the field. Question -->+    <string name="confirm_give_up_title">Give up this field?</string>+    <!-- GameBoardScreen give-up confirmation: Explains abandoning counts as a loss. Sentence -->+    <string name="confirm_give_up_message">Giving up counts as a loss and ends this field.</string>      <!-- WinOverlay: Celebratory title shown when the field is cleared. Declarative -->     <string name="cleared">Field Cleared!</string>
games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt
diff --git a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.ktindex 0b4fb8fa62..fbaa26e8dc 100644--- a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt+++ b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt@@ -11,6 +11,10 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.width import androidx.compose.foundation.layout.widthIn import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource@@ -28,6 +32,7 @@ import com.vayunmathur.library.ui.AppBarAlignment import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.CircularProgressIndicator+import com.vayunmathur.library.ui.ConfirmDialog import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.OutlinedButton@@ -36,6 +41,7 @@ import com.vayunmathur.library.ui.SegmentedButtonDefaults import com.vayunmathur.library.ui.SingleChoiceSegmentedButtonRow import com.vayunmathur.library.ui.Spacing import com.vayunmathur.library.ui.Text+import com.vayunmathur.library.ui.R as UiR import com.vayunmathur.library.ui.appBarScrollBehavior import com.vayunmathur.library.ui.game.DailyStreakText import com.vayunmathur.library.ui.game.GameModeChooser@@ -231,6 +237,11 @@ private fun NonogramControls(     daily: Boolean,     actions: NonogramGameActions, ) {+    var confirmRestart by remember { mutableStateOf(false) }+    // Restart clears every mark, so only ask when there is something to lose:+    // an untouched board restarts into itself.+    val hasProgress = game.filled.isNotEmpty() || game.crossed.isNotEmpty() ||+        game.revealedBlanks.isNotEmpty()     when {         game.isWon -> {             Text(@@ -276,9 +287,21 @@ private fun NonogramControls(                     textAlign = TextAlign.Center,                 )             }-            OutlinedButton(onClick = { actions.restartLevel() }) {+            OutlinedButton(onClick = { if (hasProgress) confirmRestart = true else actions.restartLevel() }) {                 Text(stringResource(R.string.restart))             }         }     }++    if (confirmRestart) {+        ConfirmDialog(+            title = stringResource(R.string.confirm_restart_title),+            message = stringResource(R.string.confirm_restart_message),+            confirmLabel = stringResource(R.string.restart),+            dismissLabel = stringResource(UiR.string.cancel),+            destructive = true,+            onConfirm = { actions.restartLevel() },+            onDismiss = { confirmRestart = false },+        )+    } }
games/nonogram/src/main/res/values/strings.xml
diff --git a/games/nonogram/src/main/res/values/strings.xml b/games/nonogram/src/main/res/values/strings.xmlindex 20cbea9473..b5a9733a15 100644--- a/games/nonogram/src/main/res/values/strings.xml+++ b/games/nonogram/src/main/res/values/strings.xml@@ -14,6 +14,10 @@     <string name="next_level">Next Level</string>     <!-- NonogramGameScreen: Clears every mark on the current puzzle. Verb -->     <string name="restart">Restart</string>+    <!-- NonogramGameScreen restart confirmation: Title asking whether to clear the board's marks. Question -->+    <string name="confirm_restart_title">Restart this puzzle?</string>+    <!-- NonogramGameScreen restart confirmation: Explains every mark is cleared. Sentence -->+    <string name="confirm_restart_message">All your marks on this puzzle will be cleared.</string>     <!-- NonogramGameScreen: Shown after finishing the daily, since there is only one per day. Sentence -->     <string name="daily_come_back_tomorrow">Solved! Come back tomorrow for a new puzzle.</string>     <!-- NonogramGameScreen: Celebratory line shown when a level is completed. Declarative -->
games/solitaire/src/main/java/com/vayunmathur/games/solitaire/ui/GameBoardScreen.kt
diff --git a/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/ui/GameBoardScreen.kt b/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/ui/GameBoardScreen.ktindex f054c726a5..8a0dd85e24 100644--- a/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/ui/GameBoardScreen.kt+++ b/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/ui/GameBoardScreen.kt@@ -12,8 +12,14 @@ import androidx.compose.foundation.verticalScroll import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.Text+import com.vayunmathur.library.ui.ConfirmDialog+import com.vayunmathur.library.ui.R as UiR import com.vayunmathur.library.ui.AppScaffold import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource@@ -56,10 +62,16 @@ fun GameBoardScreen(state: SolitaireUiState, mode: GameMode, actions: SolitaireA         },         scrollBehavior = appBarScrollBehavior(),     ) { innerPadding ->+        var confirmGiveUp by remember { mutableStateOf(false) }+        val giveUp = {+            // Giving up records a loss, so confirm while the game is still+            // winnable; a finished board has nothing left to lose.+            if (!isWon) confirmGiveUp = true else { actions.giveUp(); onExit() }+        }         Box(Modifier.fillMaxSize()) {             DesktopMaxWidthContainer(modifier = Modifier.padding(innerPadding)) {             Column(Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(8.dp)) {-                GameActionBar(onUndo = { actions.undo() }, onGiveUp = { actions.giveUp(); onExit() }, undoEnabled = state.history.isNotEmpty() && !isWon)+                GameActionBar(onUndo = { actions.undo() }, onGiveUp = giveUp, undoEnabled = state.history.isNotEmpty() && !isWon)                 when (mode) {                     GameMode.KLONDIKE -> state.klondike?.let {                         KlondikeBoard(it, actions, Modifier.align(Alignment.CenterHorizontally).widthIn(max = boardMaxWidth).fillMaxWidth())@@ -74,6 +86,17 @@ fun GameBoardScreen(state: SolitaireUiState, mode: GameMode, actions: SolitaireA             }             }             if (isWon) { WinOverlay(elapsedSeconds = elapsed, moveCount = moveCount, onNewGame = { actions.restart() }, onBack = onExit) }+            if (confirmGiveUp) {+                ConfirmDialog(+                    title = stringResource(R.string.confirm_give_up_title),+                    message = stringResource(R.string.confirm_give_up_message),+                    confirmLabel = stringResource(R.string.give_up),+                    dismissLabel = stringResource(UiR.string.cancel),+                    destructive = true,+                    onConfirm = { actions.giveUp(); onExit() },+                    onDismiss = { confirmGiveUp = false },+                )+            }         }     } }
games/solitaire/src/main/res/values/strings.xml
diff --git a/games/solitaire/src/main/res/values/strings.xml b/games/solitaire/src/main/res/values/strings.xmlindex c95ddb3e3c..c2e8629688 100644--- a/games/solitaire/src/main/res/values/strings.xml+++ b/games/solitaire/src/main/res/values/strings.xml@@ -15,6 +15,10 @@     <string name="continue_game">Continue</string>     <!-- GameActionBar: Button to abandon current game (records loss). Verb phrase -->     <string name="give_up">Give Up</string>+    <!-- GameBoardScreen give-up confirmation: Title asking whether to abandon the deal. Question -->+    <string name="confirm_give_up_title">Give up this game?</string>+    <!-- GameBoardScreen give-up confirmation: Explains abandoning counts as a loss. Sentence -->+    <string name="confirm_give_up_message">Giving up counts as a loss and ends this game.</string>     <!-- GameScreen TopAppBar / WinOverlay: Label for move counter HUD. Noun -->     <string name="moves">Moves</string>     <!-- WinOverlay: Label for elapsed time shown in win dialog, e.g. Time: 02:34. Noun -->
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.ktindex 7d207dab3b..862e73f2b8 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/GameBoardScreen.kt@@ -9,6 +9,10 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.widthIn import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource@@ -21,6 +25,7 @@ import com.vayunmathur.games.sudoku.ui.components.SudokuGrid import com.vayunmathur.library.ui.AppBarAlignment import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.CircularProgressIndicator+import com.vayunmathur.library.ui.ConfirmDialog import com.vayunmathur.library.ui.FilledTonalButton import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.OutlinedButton@@ -133,6 +138,7 @@ private fun ActionBar(     onExit: () -> Unit, ) {     val game = state.game ?: return+    var confirmGiveUp by remember { mutableStateOf(false) }     Column(verticalArrangement = Arrangement.spacedBy(Spacing.sm)) {         Row(             Modifier.fillMaxWidth(),@@ -168,10 +174,31 @@ private fun ActionBar(         }         TextButton(             onClick = {-                actions.giveUp()-                onExit()+                // A finished puzzle has nothing to lose; only confirm mid-game,+                // when giving up records a loss.+                if (!game.isWon) {+                    confirmGiveUp = true+                } else {+                    actions.giveUp()+                    onExit()+                }             },             modifier = Modifier.fillMaxWidth(),         ) { Text(stringResource(R.string.give_up)) }     }++    if (confirmGiveUp) {+        ConfirmDialog(+            title = stringResource(R.string.confirm_give_up_title),+            message = stringResource(R.string.confirm_give_up_message),+            confirmLabel = stringResource(R.string.give_up),+            dismissLabel = stringResource(UiR.string.cancel),+            destructive = true,+            onConfirm = {+                actions.giveUp()+                onExit()+            },+            onDismiss = { confirmGiveUp = false },+        )+    } }
games/sudoku/src/main/res/values/strings.xml
diff --git a/games/sudoku/src/main/res/values/strings.xml b/games/sudoku/src/main/res/values/strings.xmlindex f5ba32f6ac..77be48bb8a 100644--- a/games/sudoku/src/main/res/values/strings.xml+++ b/games/sudoku/src/main/res/values/strings.xml@@ -40,6 +40,10 @@     <string name="erase">Erase</string>     <!-- GameBoardScreen action bar: Abandons the puzzle, recorded as a loss. Verb phrase -->     <string name="give_up">Give Up</string>+    <!-- GameBoardScreen give-up confirmation: Title asking whether to abandon the puzzle. Question -->+    <string name="confirm_give_up_title">Give up this puzzle?</string>+    <!-- GameBoardScreen give-up confirmation: Explains abandoning counts as a loss. Sentence -->+    <string name="confirm_give_up_message">Giving up counts as a loss and ends this puzzle.</string>     <!-- GameBoardScreen TopAppBar: Label preceding the hint counter, e.g. Hints: 2. Noun plural -->     <string name="hints_used">Hints</string>