nonogram: recompose only the tapped cell, hoist label lookups (#ux-audit)
- Commit
0b7cb4fd888d18e0765eb156f3cfe48b5b33f5fc- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 01:16 UTC (23 days ago)
- Parents
1e85b0f
Symptom: every tap recomposed all cells of the grid - 225 composables on a 15x15 - plus four string lookups and a formatted content description per cell, which is wasted work on every move (and on every cell crossed by a drag once drag-to-fill lands). Root cause: NonogramCell took the whole NonogramGameState and derived its own mark from it, so any change to the board changed every cell's parameters. Each cell also resolved all four state strings itself, and its pointerInput was keyed on game.isOver / isLocked, restarting the gesture detector across the grid whenever those changed. Fix: - Grid computes each cell's mark and passes the cell only that mark, its row/column and the one state word it needs; unchanged cells now skip. - The four state words are resolved once per grid (CellLabels). - The tap/long-press callbacks handed to cells are remembered once via rememberUpdatedState, so their identity is stable across recompositions. - pointerInput is keyed only on the index; the ViewModel already ignores taps on settled cells and finished boards. User-visible effect: none intended beyond smoother taps on big boards. Verification: existing board Compose tests (descriptions, taps, long press, empty lines, 15x15 render) still cover cell behaviour; the change is structural (parameters/stability), checked by review.
Diff
games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.kt
diff --git a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.kt b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.ktindex 9f7e749e12..347f362a92 100644--- a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.kt+++ b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/components/NonogramBoard.kt@@ -11,6 +11,10 @@ import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.runtime.Composable+import androidx.compose.runtime.Immutable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberUpdatedState import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.drawBehind@@ -169,17 +173,36 @@ private fun Grid( onLongPress: (Int) -> Unit, ) { val scheme = MaterialTheme.colorScheme+ // Resolved once per grid rather than once per cell per tap.+ val labels = CellLabels(+ filled = stringResource(R.string.cd_filled),+ crossed = stringResource(R.string.cd_crossed),+ revealed = stringResource(R.string.cd_revealed),+ blank = stringResource(R.string.cd_blank),+ )+ // Stable callbacks: the cells get the same lambda instance every recomposition, so a cell whose+ // mark did not change is skipped outright instead of being recomposed for a new closure.+ val currentTap by rememberUpdatedState(onTap)+ val currentLongPress by rememberUpdatedState(onLongPress)+ val tap = remember { { index: Int -> currentTap(index) } }+ val longPress = remember { { index: Int -> currentLongPress(index) } } Box { Column { for (row in 0 until game.size) { Row { for (col in 0 until game.size) { val index = row * game.size + col+ // Each cell sees only its own mark, so one tap recomposes one cell, not all+ // 225 of a 15x15.+ val mark = game.markAt(index) NonogramCell(- game = game,+ mark = mark, index = index,- onTap = { onTap(index) },- onLongPress = { onLongPress(index) },+ row = row,+ col = col,+ stateWord = labels.wordFor(mark),+ onTap = tap,+ onLongPress = longPress, modifier = Modifier.size(cell), ) }@@ -194,17 +217,35 @@ private fun Grid( } } +/** The four state words TalkBack reads, looked up once per grid. */+@Immutable+private data class CellLabels(+ val filled: String,+ val crossed: String,+ val revealed: String,+ val blank: String,+) {+ fun wordFor(mark: CellMark): String = when (mark) {+ CellMark.FILLED -> filled+ CellMark.CROSSED -> crossed+ CellMark.REVEALED -> revealed+ CellMark.BLANK -> blank+ }+}+ @Composable private fun NonogramCell(- game: NonogramGameState,+ mark: CellMark, index: Int,- onTap: () -> Unit,- onLongPress: () -> Unit,+ row: Int,+ col: Int,+ stateWord: String,+ onTap: (Int) -> Unit,+ onLongPress: (Int) -> Unit, modifier: Modifier = Modifier, ) { val scheme = MaterialTheme.colorScheme val haptics = LocalHapticFeedback.current- val mark = game.markAt(index) // A fill only ever lands on a cell that belongs to the picture, because a wrong guess is turned // into a revealed blank instead. That revealed blank gets a tinted cell and an error-coloured X so@@ -215,29 +256,19 @@ private fun NonogramCell( CellMark.REVEALED -> scheme.errorContainer else -> Color.Transparent }-- val stateWord = when (mark) {- CellMark.FILLED -> stringResource(R.string.cd_filled)- CellMark.CROSSED -> stringResource(R.string.cd_crossed)- CellMark.REVEALED -> stringResource(R.string.cd_revealed)- CellMark.BLANK -> stringResource(R.string.cd_blank)- }- val description = stringResource(- R.string.cd_cell,- index / game.size + 1,- index % game.size + 1,- stateWord,- )+ val description = stringResource(R.string.cd_cell, row + 1, col + 1, stateWord) Box( modifier .background(background)- .pointerInput(index, game.isOver, game.isLocked(index)) {+ .pointerInput(index) {+ // The ViewModel ignores taps on settled cells and finished boards, so the handler+ // does not need to restart whenever those change. detectTapGestures(- onTap = { onTap() },+ onTap = { onTap(index) }, onLongPress = { haptics.performHapticFeedback(HapticFeedbackType.LongPress)- onLongPress()+ onLongPress(index) }, ) }