Modern Apps git

← Modern-Apps.git

pipes: confirm before restarting level (#728)

Commit
faec05a2e1297010cc666fcdc4dca1796d9f4e94
Author
vayun-mathur <[email protected]>
When
2026-10-13 19:37 UTC (1 month ago)
Parents
aa8d1d0

Diff

games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoardScreen.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoardScreen.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoardScreen.ktindex 614168fb89..5b23a53669 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoardScreen.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoardScreen.kt@@ -10,16 +10,22 @@ import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding 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 import androidx.compose.ui.unit.dp+import com.vayunmathur.games.pipes.R import com.vayunmathur.games.pipes.platform.GameBoardUiState import com.vayunmathur.games.pipes.platform.PipesActions import com.vayunmathur.library.ui.game.LevelPickerBox import com.vayunmathur.library.ui.game.MovesBox import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.Button+import com.vayunmathur.library.ui.ConfirmDialog import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.Surface@@ -30,10 +36,12 @@ import com.vayunmathur.library.ui.R as UiR @OptIn(ExperimentalMaterial3Api::class) @Composable fun GameBoardScreen(state: GameBoardUiState, actions: PipesActions, onBack: () -> Unit, onLevelChange: (Int) -> Unit) {+    var showRestartConfirm by remember { mutableStateOf(false) }+    if (showRestartConfirm) { RestartConfirmDialog(onConfirm = { actions.onRestart() }, onDismiss = { showRestartConfirm = false }) }     AppScaffold(title = "", onNavigateBack = onBack, scrollBehavior = appBarScrollBehavior()) { innerPadding ->         Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {             val infoBoxes = @Composable { LevelPickerBox(levelIndex = state.levelIndex, onLevelChange = onLevelChange, isCompleted = state.isCompleted, maxLevelIndex = state.maxLevelIndex); MovesBox(moves = state.moves, bestScore = state.bestScore, optimalMoves = state.levelData.optimalMoves) }-            val actionButtons = @Composable { if (!state.isLevelWon) { Button(onClick = { actions.onUndo() }, enabled = state.canUndo) { Text(stringResource(UiR.string.undo)) }; Button(onClick = { actions.onRestart() }, enabled = state.canUndo) { Text(stringResource(UiR.string.restart)) } } else if (state.levelIndex < state.maxLevelIndex) { Button(onClick = { onLevelChange(state.levelIndex + 1) }) { Text(stringResource(UiR.string.next_level)) } } }+            val actionButtons = @Composable { if (!state.isLevelWon) { Button(onClick = { actions.onUndo() }, enabled = state.canUndo) { Text(stringResource(UiR.string.undo)) }; Button(onClick = { showRestartConfirm = true }, enabled = state.canUndo) { Text(stringResource(UiR.string.restart)) } } else if (state.levelIndex < state.maxLevelIndex) { Button(onClick = { onLevelChange(state.levelIndex + 1) }) { Text(stringResource(UiR.string.next_level)) } } }             val board = @Composable { boardModifier: Modifier -> GameBoard(levelData = state.levelData, gameState = state.gameState, activeColor = state.activeColor, activePath = state.activePath, onStartDraw = actions::startDraw, onExtendPath = actions::extendPath, onCommitDraw = actions::commitDraw, isLevelWon = state.isLevelWon, colorblind = state.colorblind, modifier = boardModifier) }             BoxWithConstraints(modifier = Modifier.fillMaxSize().padding(innerPadding).padding(16.dp)) {                 if (maxWidth > maxHeight) { Row(modifier = Modifier.fillMaxSize(), horizontalArrangement = Arrangement.spacedBy(16.dp), verticalAlignment = Alignment.CenterVertically) { Box(modifier = Modifier.weight(1f).fillMaxHeight(), contentAlignment = Alignment.Center) { board(Modifier.fillMaxSize()) }; Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(16.dp)) { infoBoxes(); Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { actionButtons() } } } }@@ -42,3 +50,8 @@ fun GameBoardScreen(state: GameBoardUiState, actions: PipesActions, onBack: () -         }     } }++@Composable+private fun RestartConfirmDialog(onConfirm: () -> Unit, onDismiss: () -> Unit) {+    ConfirmDialog(title = stringResource(R.string.restart_confirm_title), message = stringResource(R.string.restart_confirm_message), confirmLabel = stringResource(UiR.string.restart), dismissLabel = stringResource(UiR.string.cancel), destructive = true, onConfirm = onConfirm, onDismiss = onDismiss)+}
games/pipes/src/main/res/values/strings.xml
diff --git a/games/pipes/src/main/res/values/strings.xml b/games/pipes/src/main/res/values/strings.xmlindex b52d749d9f..3cbc877e35 100644--- a/games/pipes/src/main/res/values/strings.xml+++ b/games/pipes/src/main/res/values/strings.xml@@ -9,4 +9,8 @@     <string name="colorblind_mode">Colorblind mode</string>     <!-- SettingsScreen: Subtitle explaining colorblind mode labels each flow with letter A-Z. Sentence -->     <string name="colorblind_mode_desc">Label each flow with a letter (A–Z)</string>+    <!-- GameBoardScreen: Restart confirmation dialog title. Sentence, question -->+    <string name="restart_confirm_title">Restart level?</string>+    <!-- GameBoardScreen: Restart confirmation dialog message. Sentence -->+    <string name="restart_confirm_message">This will clear your current progress on this level.</string> </resources>