Modern Apps git

← Modern-Apps.git

nonogram: double-tapping Next no longer skips a level (#ux-audit)

Commit
74544f4b1b196b7374d1faa30d9afe143a69ed15
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:12 UTC (23 days ago)
Parents
881108f
Symptom: tapping Next level twice in quick succession on a solved casual
board jumped two levels (e.g. 4 -> 6), silently skipping a puzzle the
player never saw.

Root cause: nextLevel() wrote `uiState.level + 1`, but load() raises
uiState.level as soon as DataStore reports the new level, before the
next puzzle has been generated. The second tap read the already-raised
counter and wrote level + 2. The button also stayed enabled for the
whole generation.

Fix:
- nextLevel() derives the target from the loaded game's own level
  (`game.level + 1`), so a repeated tap can only ever write the same
  value.
- It refuses unless the board on screen is solved, casual, and not
  already generating, and flags `generating = true` synchronously so
  the next tap is refused before any coroutine runs.
- The Next level button is disabled while `generating` is true.

User-visible effect: Next level always advances exactly one level, and
the button greys out until the next puzzle is ready.

Verification: new Robolectric tests doubleTappingNextAdvancesExactlyOne
Level (two taps + a late third tap -> level 2 stored) and
nextLevelIgnoresAnUnsolvedBoard; Compose test that the button is
disabled and inert while generating. The existing next-level test now
solves the board first, since Next only exists on a solved board.

Diff

games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.kt
diff --git a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.kt b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.ktindex 61c78c6954..c1270b9961 100644--- a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.kt+++ b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/platform/NonogramViewModel.kt@@ -255,10 +255,23 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application         viewModelScope.launch { dataStore.setMarkMode(mode) }     } +    /**+     * Advances from the solved board on screen to the level after it.+     *+     * The target comes from the loaded game's own level, not [NonogramUiState.level]: `load()` raises+     * that counter as soon as the stored level changes, before the next puzzle exists, so a second+     * tap on Next used to read the already-raised number and skip a level. Marking the state as+     * generating straight away disables the button for the rest of the load, and the solved-board+     * guard means a stray tap that lands after the new puzzle appears does nothing.+     */     override fun nextLevel() {         val state = _uiState.value-        if (state.mode == GameMode.DAILY) return-        viewModelScope.launch { dataStore.saveLevel(state.level + 1) }+        val game = state.game ?: return+        if (state.mode == GameMode.DAILY || game.mode == GameMode.DAILY) return+        if (state.generating || !game.isWon) return+        val target = game.level + 1+        _uiState.value = state.copy(generating = true)+        viewModelScope.launch { dataStore.saveLevel(target) }     }      override fun restartLevel() {
games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt
diff --git a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.ktindex cbc4476229..128c01fc25 100644--- a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt+++ b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/ui/NonogramGameScreen.kt@@ -194,6 +194,7 @@ fun NonogramGameScreen(                                 game = game,                                 markMode = state.markMode,                                 daily = daily,+                                generating = state.generating,                                 actions = actions,                             )                         }@@ -216,6 +217,7 @@ fun NonogramGameScreen(                         game = game,                         markMode = state.markMode,                         daily = daily,+                        generating = state.generating,                         actions = actions,                     )                 }@@ -235,6 +237,7 @@ private fun NonogramControls(     game: NonogramGameState,     markMode: MarkMode,     daily: Boolean,+    generating: Boolean,     actions: NonogramGameActions, ) {     var confirmRestart by remember { mutableStateOf(false) }@@ -256,8 +259,10 @@ private fun NonogramControls(                     textAlign = TextAlign.Center,                 )             } else {+                // Disabled while the next puzzle generates, so a double tap cannot advance twice.                 Button(                     onClick = { actions.nextLevel() },+                    enabled = !generating,                     modifier = Modifier.fillMaxWidth(),                 ) { Text(stringResource(UiR.string.next_level)) }             }
games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt
diff --git a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.ktindex dc34cd84db..173e4f48e8 100644--- a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt+++ b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/platform/NonogramViewModelTest.kt@@ -87,8 +87,41 @@ class NonogramViewModelTest {         await("restart clears") { vm.uiState.value.game?.crossed?.isEmpty() == true }     } +    private fun winBoard(vm: NonogramViewModel) {+        val g = vm.uiState.value.game!!+        for (i in g.puzzle.solution.indices.filter { g.puzzle.solution[it] }) vm.tapCell(i)+        await("won") { vm.uiState.value.game?.isWon == true }+    }++    @Test fun nextLevelIgnoresAnUnsolvedBoard() {+        val vm = loadedVm()+        vm.nextLevel()+        Thread.sleep(200)+        assertEquals(1, vm.uiState.value.level, "Next only exists on a solved board")+        assertEquals(1, runBlocking { vm.dataStore.currentLevel.first() })+    }++    @Test fun doubleTappingNextAdvancesExactlyOneLevel() {+        val vm = loadedVm()+        winBoard(vm)+        vm.nextLevel()+        // The first tap flags the load straight away, so the second is refused rather than+        // reading the already-raised level counter and writing level 3.+        assertTrue(vm.uiState.value.generating)+        vm.nextLevel()+        await("level 2 loaded") {+            vm.uiState.value.game?.level == 2 && !vm.uiState.value.generating+        }+        // A late tap that lands on the fresh, unsolved board is ignored too.+        vm.nextLevel()+        Thread.sleep(200)+        assertEquals(2, runBlocking { vm.dataStore.currentLevel.first() })+        assertEquals(2, vm.uiState.value.level)+    }+     @Test fun nextLevelAdvancesAndDailyModeLoads() {         val vm = loadedVm()+        winBoard(vm)         vm.nextLevel()         await("level 2") { vm.uiState.value.level == 2 && vm.uiState.value.game != null && !vm.uiState.value.generating }         vm.setGameMode(GameMode.DAILY)
games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt
diff --git a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.ktindex b96419be60..a8adb6abac 100644--- a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt+++ b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/ui/NonogramScreensTest.kt@@ -1,6 +1,7 @@ package com.vayunmathur.games.nonogram.ui  import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.assertIsNotEnabled import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText@@ -106,6 +107,13 @@ class NonogramScreensTest {         assertEquals(1, a.next)     } +    @Test fun nextLevelIsDisabledWhileTheNextPuzzleGenerates() {+        val a = Recording()+        rule.setContent { NonogramGameScreen(NonogramUiState(game = game(filled = answer()), level = 3, generating = true), a, {}, {}) }+        rule.onNodeWithText(str(UiR.string.next_level)).assertIsNotEnabled().performClick()+        assertEquals(0, a.next, "a second tap while generating must not advance again")+    }+     @Test fun wonDailyShowsComeBack() {         val a = Recording()         rule.setContent { NonogramGameScreen(NonogramUiState(game = game(filled = answer(), mode = GameMode.DAILY), mode = GameMode.DAILY, dailyStreak = 3), a, {}, {}) }