pipes: guard restored pack index in game and level screens
- Commit
6c47d0215396b34c23c6cc49e132edd5848016ac- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-19 02:29 UTC (17 days ago)
- Parents
57edbe3
Route.Game/LevelSelector are serializable back-stack entries, so a restored stack from a build with more or reordered packs could carry a packIndex outside LevelPack.PACKS. GameScreen and LevelScreen indexed PACKS directly and crashed in composition, before the ViewModel's existing bounds guard could run. Both screens now use getOrNull and fall back to the loading placeholder instead of crashing. Verified: ./gradlew :games:pipes:testDevUnitTest (pass), ./check games/pipes (pass).
Diff
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 ec16d30b03..7bb8024396 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@@ -25,7 +25,9 @@ fun GameScreen(backStack: NavBackStack<Route>, viewModel: PipesViewModel, packIn val dailyPack by viewModel.dailyPack.collectAsState() val colorblind by viewModel.colorblind.collectAsState() val isDaily = packIndex == PipesViewModel.DAILY_PACK_INDEX- val levels = if (isDaily) dailyPack?.levels else LevelPack.PACKS[packIndex].levels+ // A restored back stack can carry a pack index this build no longer+ // has; treat it like a not-yet-loaded pack instead of crashing.+ val levels = if (isDaily) dailyPack?.levels else LevelPack.PACKS.getOrNull(packIndex)?.levels val levelStats = if (isDaily) dailyStats else packStats LaunchedEffect(isDaily) { if (isDaily) viewModel.refreshDaily() } LaunchedEffect(packIndex, levelIndex, levels) { viewModel.loadLevel(packIndex, levelIndex) }
games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/LevelScreen.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/LevelScreen.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/LevelScreen.ktindex c3aa753af5..278b268f91 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/LevelScreen.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/LevelScreen.kt@@ -26,6 +26,7 @@ import com.vayunmathur.games.pipes.data.LevelPack import com.vayunmathur.games.pipes.platform.PipesViewModel import com.vayunmathur.games.pipes.ui.components.LevelThumbnail import com.vayunmathur.library.ui.AppScaffold+import com.vayunmathur.library.ui.CircularProgressIndicator import com.vayunmathur.library.ui.Card import com.vayunmathur.library.ui.CardDefaults import com.vayunmathur.library.ui.ExperimentalMaterial3Api@@ -39,7 +40,11 @@ import com.vayunmathur.library.util.NavBackStack @OptIn(ExperimentalMaterial3Api::class) @Composable fun LevelScreen(backStack: NavBackStack<Route>, viewModel: PipesViewModel, packIndex: Int) {- val pack = LevelPack.PACKS[packIndex]+ // See GameScreen: a stale restored pack index must not crash composition.+ val pack = LevelPack.PACKS.getOrNull(packIndex) ?: run {+ Box(Modifier.fillMaxSize(), Alignment.Center) { CircularProgressIndicator() }+ return+ } val levelStats by viewModel.levelStats.collectAsState() AppScaffold( title = stringResource(R.string.level_selector),