minesweeper: stop the clock from redrawing every cell each second (#ux-audit)
- Commit
c0ce3927d63263fbd0ad906ce747fb94b7c725a4- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 01:18 UTC (23 days ago)
- Parents
67e9ca9
Symptom: once the first cell was opened, the entire field - up to 216
cells on Large, each with its pointer-input and semantics blocks -
recomposed once a second for as long as the game ran.
Root cause: elapsedSeconds lived in MinesweeperGameState, so each 1 Hz
incrementTimer() produced a new game instance. MineFieldGrid and every
MineCell take that instance (its List fields make it unstable for
Compose), so all of them recomposed on every tick.
Fix:
- Removed elapsedSeconds from MinesweeperGameState. The ViewModel owns
the clock as its own StateFlow (elapsedSeconds), reset by newGame
(so restart/New Field too) and giveUp; onWin reads it for stats and
the speed_win achievement exactly as before.
- GameBoardScreen takes the clock as a lazily-read
`elapsedSeconds: () -> Int` (default { 0 }) and shows it in a small
private ClockText composable, so a tick invalidates only that text;
the win overlay reads the same lambda. GameScreen collects the flow
without delegating so the read happens inside the lambda.
- Store-listing previews pass their fixed times through the new
parameter (screenshots are not regenerated in this commit).
User-visible effect: none visually - the clock and the win time are
unchanged - but the field no longer redraws every second.
Verification: MinesweeperViewModelTest asserts incrementTimer() leaves
the game state instance untouched (assertSame), the clock counts in
vm.elapsedSeconds, does not run before the first dig, and resets on
restart. :games:minesweeper:testDevUnitTest in the toolchain container.Diff
games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/MinesweeperModels.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/MinesweeperModels.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/MinesweeperModels.ktindex d5378d0024..8f52f76b7d 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/MinesweeperModels.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/MinesweeperModels.kt@@ -92,7 +92,8 @@ data class MinesweeperGameState( val outcome: GameOutcome = GameOutcome.PLAYING, /** The mine that ended it, so the board can single it out from the ones merely shown. */ val explodedAt: Int = -1,- val elapsedSeconds: Int = 0,+ // The clock is deliberately not here: it ticks every second, and every tick would hand the+ // field a new state and redraw all 80-216 cells. MinesweeperViewModel.elapsedSeconds owns it. /** False until the first reveal, which is when the mines are laid and the clock starts. */ val started: Boolean = false, ) {
games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.ktindex 4056ef8707..417c26d45c 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.kt@@ -37,6 +37,16 @@ class MinesweeperViewModel(application: Application) : private val statsRepository = MinesweeperStatsRepository(application) + private val _elapsedSeconds = MutableStateFlow(0)++ /**+ * Seconds on the clock for the field in progress.+ *+ * Its own flow rather than a field of [MinesweeperGameState]: the game state feeds every cell,+ * so a 1 Hz tick inside it redrew the whole field every second. Only the clock text reads this.+ */+ val elapsedSeconds: StateFlow<Int> = _elapsedSeconds.asStateFlow()+ /** * Whether the player has placed a flag this game. *@@ -67,6 +77,7 @@ class MinesweeperViewModel(application: Application) : */ fun newGame(config: GameConfig) { placedAnyFlag = false+ _elapsedSeconds.value = 0 _uiState.value = MinesweeperUiState( config = config, game = MinesweeperGameState.empty(config),@@ -175,13 +186,14 @@ class MinesweeperViewModel(application: Application) : if (game.started && !game.isOver) { statsRepository.recordGameLost(state.config.size, state.config.difficulty) }+ _elapsedSeconds.value = 0 _uiState.value = MinesweeperUiState(config = state.config, tapMode = state.tapMode) } fun incrementTimer() { val game = _uiState.value.game ?: return if (!game.started || game.isOver) return- _uiState.update { it.copy(game = game.copy(elapsedSeconds = game.elapsedSeconds + 1)) }+ _elapsedSeconds.update { it + 1 } } fun dismissAchievementNotification() = achievementsManager.dismissNotification()@@ -201,22 +213,23 @@ class MinesweeperViewModel(application: Application) : val config = _uiState.value.config when (next.outcome) {- GameOutcome.WON -> onWin(next)+ GameOutcome.WON -> onWin() GameOutcome.LOST -> statsRepository.recordGameLost(config.size, config.difficulty) GameOutcome.PLAYING -> Unit } } - private fun onWin(game: MinesweeperGameState) {+ private fun onWin() { val config = _uiState.value.config- statsRepository.recordGameWon(config.size, config.difficulty, game.elapsedSeconds)+ val elapsed = _elapsedSeconds.value+ statsRepository.recordGameWon(config.size, config.difficulty, elapsed) val manager = achievementsManager manager.onAchievementUnlocked("first_win") if (config.size == BoardSize.LARGE) manager.onAchievementUnlocked("win_large") if (config.difficulty == Difficulty.EXPERT) manager.onAchievementUnlocked("win_expert") if (!placedAnyFlag) manager.onAchievementUnlocked("no_flags_needed")- if (game.elapsedSeconds < FAST_WIN_SECONDS) manager.onAchievementUnlocked("speed_win")+ if (elapsed < FAST_WIN_SECONDS) manager.onAchievementUnlocked("speed_win") val totalWins = statsRepository.getTotalGamesWon() manager.onProgressUpdated("wins_10", totalWins)
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 b214b6b5b0..d896ba7dc1 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@@ -51,12 +51,16 @@ private val FieldMaxWidth = 480.dp /** * The board, with no dependency on the ViewModel so it can be rendered from a `@Preview` — see * `src/screenshotTest`.+ *+ * @param elapsedSeconds the clock, read lazily so a 1 Hz tick recomposes only the text that+ * shows it rather than the field. */ @Composable fun GameBoardScreen( state: MinesweeperUiState, actions: MinesweeperActions, onExit: () -> Unit,+ elapsedSeconds: () -> Int = { 0 }, ) { val game = state.game val mineCount = state.config.difficulty.mineCount(state.config.size)@@ -74,13 +78,7 @@ fun GameBoardScreen( onNavigateBack = onExit, alignment = AppBarAlignment.Center, actions = {- if (game != null) {- Text(- formatDuration(game.elapsedSeconds),- style = MaterialTheme.typography.titleMedium,- modifier = Modifier.padding(end = Spacing.lg),- )- }+ if (game != null) ClockText(elapsedSeconds) }, scrollBehavior = appBarScrollBehavior(), ) { padding ->@@ -200,7 +198,7 @@ fun GameBoardScreen( if (won) { Text( "${stringResource(UiR.string.game_time)}: " +- formatDuration(game.elapsedSeconds)+ formatDuration(elapsedSeconds()) ) } }@@ -209,6 +207,16 @@ fun GameBoardScreen( } } +/** Its own restart scope, so the per-second tick recomposes this text and nothing else. */+@Composable+private fun ClockText(elapsedSeconds: () -> Int) {+ Text(+ formatDuration(elapsedSeconds()),+ style = MaterialTheme.typography.titleMedium,+ modifier = Modifier.padding(end = Spacing.lg),+ )+}+ /** * Chooses what a plain tap does. *
games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameScreen.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameScreen.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameScreen.ktindex f750ab0938..8fa25fd39a 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameScreen.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/GameScreen.kt@@ -24,6 +24,9 @@ fun GameScreen( config: GameConfig, ) { val uiState by viewModel.uiState.collectAsState()+ // Not delegated: the State is read inside the lambda below, so a tick recomposes only the+ // clock text instead of this whole screen and the field under it.+ val elapsedSeconds = viewModel.elapsedSeconds.collectAsState() LaunchedEffect(config) { if (uiState.game == null) viewModel.newGame(config)@@ -42,5 +45,6 @@ fun GameScreen( state = uiState, actions = viewModel, onExit = { backStack.pop() },+ elapsedSeconds = { elapsedSeconds.value }, ) }
games/minesweeper/src/screenshotTest/kotlin/com/vayunmathur/games/minesweeper/MetadataPreviews.kt
diff --git a/games/minesweeper/src/screenshotTest/kotlin/com/vayunmathur/games/minesweeper/MetadataPreviews.kt b/games/minesweeper/src/screenshotTest/kotlin/com/vayunmathur/games/minesweeper/MetadataPreviews.ktindex 6ef823a08c..eeb9e7493f 100644--- a/games/minesweeper/src/screenshotTest/kotlin/com/vayunmathur/games/minesweeper/MetadataPreviews.kt+++ b/games/minesweeper/src/screenshotTest/kotlin/com/vayunmathur/games/minesweeper/MetadataPreviews.kt@@ -42,7 +42,7 @@ class MetadataPreviews { for (index in game.mines.indices.filter { game.mines[it] }.take(4)) { game = MinesweeperRules.toggleFlag(game, index) }- Board(config, game.copy(elapsedSeconds = 74))+ Board(config, game, elapsed = 74) } @PreviewTest@@ -51,7 +51,7 @@ class MetadataPreviews { fun Preview2Large() { val config = GameConfig(BoardSize.LARGE, Difficulty.HARD) val game = opened(config, seed = 77)- Board(config, game.copy(elapsedSeconds = 142))+ Board(config, game, elapsed = 142) } @PreviewTest@@ -73,17 +73,18 @@ class MetadataPreviews { for (index in 0 until game.cellCount) { if (!game.mines[index]) game = MinesweeperRules.reveal(game, index) }- Board(config, game.copy(elapsedSeconds = 53))+ Board(config, game, elapsed = 53) } @Composable- private fun Board(config: GameConfig, game: MinesweeperGameState) {+ private fun Board(config: GameConfig, game: MinesweeperGameState, elapsed: Int = 0) { // Layoutlib has no wallpaper to sample, so Material You falls back to the default palette. DynamicTheme(darkTheme = true) { GameBoardScreen( state = MinesweeperUiState(config = config, game = game), actions = MinesweeperActions.Noop, onExit = {},+ elapsedSeconds = { elapsed }, ) } }
games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt
diff --git a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.ktindex cdd3a38cd0..bc468ce5f1 100644--- a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt+++ b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt@@ -13,6 +13,7 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertNull+import kotlin.test.assertSame import kotlin.test.assertTrue import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner@@ -96,8 +97,12 @@ class MinesweeperViewModelTest { assertTrue(game.revealed[0]) assertTrue(game.revealed.count { it } > 1) assertEquals(GameOutcome.PLAYING, game.outcome)+ val beforeTick = vm.uiState.value.game!! vm.incrementTimer()- assertEquals(1, vm.uiState.value.game!!.elapsedSeconds)+ assertEquals(1, vm.elapsedSeconds.value)+ assertSame(beforeTick, vm.uiState.value.game!!, "a tick must not hand the field a new state")+ vm.restart()+ assertEquals(0, vm.elapsedSeconds.value, "a new field starts the clock from zero") } @Test@@ -185,7 +190,7 @@ class MinesweeperViewModelTest { val vm = vm() vm.newGame(GameConfig()) vm.incrementTimer()- assertEquals(0, vm.uiState.value.game!!.elapsedSeconds)+ assertEquals(0, vm.elapsedSeconds.value) vm.tapCell(40) vm.giveUp() assertNull(vm.uiState.value.game)