Modern Apps git

← Modern-Apps.git

pipes: show a solved message and "Back to packs" on the last level (#ux-audit)

Commit
d1b43206bddde9d919273e2c78a6a6c3e3351026
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:27 UTC (23 days ago)
Parents
74f17b3
Symptom: solving a level gave almost no feedback (the board just
stopped accepting input), and on a pack's last level Undo and Restart
disappeared with nothing in their place, leaving an empty button row
and only the small top-bar back arrow.

Root cause: GameBoardScreen's action row rendered Undo/Restart while
playing, Next Level after a win when a next level existed, and nothing
otherwise; there was no win text at all.

Fix:
- After a win, a "Solved in N moves!" line (plural resource, primary
  colour, polite live region so TalkBack announces it) sits between
  the board and the buttons, in both portrait and landscape layouts.
- On the last level the action row shows "Back to packs".
  GameBoardScreen takes a new onBackToPacks callback (defaulting to
  onBack so previews and other callers are unchanged); GameScreen pops
  the back stack down to the pack list, which also covers the daily
  pack (Pack list > Daily > game).
- The screen body is reformatted from single-line lambdas into
  readable blocks; layout is otherwise identical.

User-visible effect: a clear solved message on every win, and a way
on from the last level of each pack.

Verification: PipesScreensTest's won-state checks now assert the
solved message, that the last level shows "Back to packs" (and no
Next/Undo) and that tapping it calls onBackToPacks.

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 5b23a53669..4f1a9a68ca 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@@ -16,7 +16,13 @@ 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 import androidx.compose.ui.res.stringResource+import androidx.compose.ui.semantics.LiveRegionMode+import androidx.compose.ui.semantics.liveRegion+import androidx.compose.ui.semantics.semantics+import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import com.vayunmathur.games.pipes.R import com.vayunmathur.games.pipes.platform.GameBoardUiState@@ -33,24 +39,74 @@ import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.appBarScrollBehavior import com.vayunmathur.library.ui.R as UiR +/**+ * The game screen. [onBackToPacks] is offered after solving a pack's last level, where+ * there is no next level to go to; it defaults to plain back navigation.+ */ @OptIn(ExperimentalMaterial3Api::class) @Composable-fun GameBoardScreen(state: GameBoardUiState, actions: PipesActions, onBack: () -> Unit, onLevelChange: (Int) -> Unit) {+fun GameBoardScreen(+    state: GameBoardUiState,+    actions: PipesActions,+    onBack: () -> Unit,+    onLevelChange: (Int) -> Unit,+    onBackToPacks: () -> Unit = onBack,+) {     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 = { 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 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)) }+                } else {+                    // Last level of the pack: Undo/Restart are gone, so never leave this row empty.+                    Button(onClick = onBackToPacks) { Text(stringResource(R.string.back_to_packs)) }+                }+            }+            val status = @Composable { StatusLine(state) }             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() } } } }-                else { Column(modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(16.dp)) { Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceAround, verticalAlignment = Alignment.CenterVertically) { infoBoxes() }; Box(modifier = Modifier.weight(1f).fillMaxWidth(), contentAlignment = Alignment.Center) { board(Modifier.fillMaxSize()) }; Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly, verticalAlignment = Alignment.CenterVertically) { actionButtons() } } }+                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()+                            status()+                            Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { actionButtons() }+                        }+                    }+                } else {+                    Column(modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(16.dp)) {+                        Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceAround, verticalAlignment = Alignment.CenterVertically) { infoBoxes() }+                        Box(modifier = Modifier.weight(1f).fillMaxWidth(), contentAlignment = Alignment.Center) { board(Modifier.fillMaxSize()) }+                        status()+                        Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly, verticalAlignment = Alignment.CenterVertically) { actionButtons() }+                    }+                }             }         }     } } +/** The solved message once the level is won; announced by TalkBack as it appears. */+@Composable+private fun StatusLine(state: GameBoardUiState) {+    if (!state.isLevelWon) return+    Text(+        text = pluralStringResource(R.plurals.level_solved_moves, state.moves, state.moves),+        style = MaterialTheme.typography.titleMedium,+        fontWeight = FontWeight.Bold,+        color = MaterialTheme.colorScheme.primary,+        textAlign = TextAlign.Center,+        modifier = Modifier.semantics { liveRegion = LiveRegionMode.Polite },+    )+}+ @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/java/com/vayunmathur/games/pipes/ui/GameScreen.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameScreen.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameScreen.ktindex 1983e54cfb..dc8b3eda15 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameScreen.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameScreen.kt@@ -32,5 +32,5 @@ fun GameScreen(backStack: NavBackStack<Route>, viewModel: PipesViewModel, packIn     if (levels == null || levelData == null) { Box(Modifier.fillMaxSize(), Alignment.Center) { CircularProgressIndicator() }; return }     val isReady = uiState.packIndex == packIndex && uiState.levelIndex == levelIndex && uiState.levelData != null     val currentLevelStats = levelStats[levelData.id]-    GameBoardScreen(state = GameBoardUiState(levelData = if (isReady) uiState.levelData!! else levelData, levelIndex = levelIndex, maxLevelIndex = levels.lastIndex, gameState = if (isReady) uiState.gameState else PipesGameState(), activeColor = if (isReady && uiState.leftStart) uiState.activeColor else null, activePath = if (isReady) uiState.activePath else emptyList(), isLevelWon = isReady && uiState.isLevelWon, isCompleted = currentLevelStats != null, colorblind = colorblind, moves = if (isReady) viewModel.getCurrentMoves() else 0, bestScore = currentLevelStats?.bestScore, canUndo = isReady && uiState.history.isNotEmpty()), actions = viewModel, onBack = { backStack.pop() }, onLevelChange = { newIndex -> val clamped = newIndex.coerceIn(0, levels.lastIndex); backStack.setLast(if (isDaily) Route.DailyGame(clamped) else Route.Game(packIndex, clamped)) })+    GameBoardScreen(state = GameBoardUiState(levelData = if (isReady) uiState.levelData!! else levelData, levelIndex = levelIndex, maxLevelIndex = levels.lastIndex, gameState = if (isReady) uiState.gameState else PipesGameState(), activeColor = if (isReady && uiState.leftStart) uiState.activeColor else null, activePath = if (isReady) uiState.activePath else emptyList(), isLevelWon = isReady && uiState.isLevelWon, isCompleted = currentLevelStats != null, colorblind = colorblind, moves = if (isReady) viewModel.getCurrentMoves() else 0, bestScore = currentLevelStats?.bestScore, canUndo = isReady && uiState.history.isNotEmpty()), actions = viewModel, onBack = { backStack.pop() }, onBackToPacks = { while (backStack.backStack.size > 1 && backStack.last() !is Route.PackSelector) backStack.pop() }, onLevelChange = { newIndex -> val clamped = newIndex.coerceIn(0, levels.lastIndex); backStack.setLast(if (isDaily) Route.DailyGame(clamped) else Route.Game(packIndex, clamped)) }) }
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 3cbc877e35..3239dc6d60 100644--- a/games/pipes/src/main/res/values/strings.xml+++ b/games/pipes/src/main/res/values/strings.xml@@ -13,4 +13,11 @@     <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>+    <!-- GameBoardScreen: Message shown when the level is solved, with the number of moves used. Sentence, exclamation -->+    <plurals name="level_solved_moves">+        <item quantity="one">Solved in <xliff:g id="moves" example="1">%d</xliff:g> move!</item>+        <item quantity="other">Solved in <xliff:g id="moves" example="7">%d</xliff:g> moves!</item>+    </plurals>+    <!-- GameBoardScreen: Button shown after solving the last level of a pack; returns to the pack list. Verb phrase -->+    <string name="back_to_packs">Back to packs</string> </resources>
games/pipes/src/test/java/com/vayunmathur/games/pipes/ui/PipesScreensTest.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/ui/PipesScreensTest.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/ui/PipesScreensTest.ktindex bdaf904f9f..7656be5e7e 100644--- a/games/pipes/src/test/java/com/vayunmathur/games/pipes/ui/PipesScreensTest.kt+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/ui/PipesScreensTest.kt@@ -168,15 +168,21 @@ class PipesScreensTest {         }         rule.onNodeWithText(str(UiR.string.next_level)).performClick()         assertEquals(2, levelChange)-        // Won on the last level: no next button+        rule.onNodeWithText(ctx.resources.getQuantityString(R.plurals.level_solved_moves, 0, 0)).assertIsDisplayed()+        // Won on the last level: no next button, but a solved message and a way back to the packs+        var toPacks = 0         screen.value = {             PipesTheme {                 GameBoardScreen(-                    GameBoardUiState(level, levelIndex = 3, maxLevelIndex = 3, isLevelWon = true),-                    actions, onBack = {}, onLevelChange = {})+                    GameBoardUiState(level, levelIndex = 3, maxLevelIndex = 3, isLevelWon = true, moves = 7),+                    actions, onBack = {}, onLevelChange = {}, onBackToPacks = { toPacks++ })             }         }         rule.onNodeWithText(str(UiR.string.next_level)).assertDoesNotExist()+        rule.onNodeWithText(str(UiR.string.undo)).assertDoesNotExist()+        rule.onNodeWithText("Solved in 7 moves!").assertIsDisplayed()+        rule.onNodeWithText(str(R.string.back_to_packs)).assertIsDisplayed().performClick()+        assertEquals(1, toPacks)         assertEquals(0, backs)     }