Modern Apps git

← Modern-Apps.git

games-nonogram: count each completed puzzle once across relaunches

Commit
7896a9e3515e59ae3e9a3051eac71b71bf978db6
Author
Vayun Mathur <[email protected]>
When
2026-11-08 04:26 UTC (28 days ago)
Parents
06a2430
The lifetime completed counter was guarded only by the ViewModel's
in-memory key, so reopening an already-won board before advancing
(killed app before next-level, completed daily reopened the same day)
replayed the win and inflated the count that feeds the puzzles_10 /
puzzles_50 progress achievements.

NonogramDataStore.recordCompleted() is replaced by
recordCompletedOnce(key): the win key (negated level for casual, epoch
day for daily, which cannot collide) is banked in a stored string set
in the same edit that bumps the counter, so a replay is a no-op and
returns the current total for the progress updates. A set, not a single
last-key slot: distinct wins must each survive (level 3, then 4, then 3
reopened). The ViewModel's in-memory guard stays as the fast path for
re-emitted flows within a session.

User-visible effect: the completed-puzzle count and its achievements no
longer inflate when an already-won board is reopened.

Verified with :games:nonogram:testDevUnitTest (68 tests green),
including the new recordCompletedOnceBanksEachWinKeyOnce test covering
distinct keys counting once each and replays reporting the total
unchanged.

Diff

games/nonogram/src/main/java/com/vayunmathur/games/nonogram/data/NonogramDataStore.kt
diff --git a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/data/NonogramDataStore.kt b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/data/NonogramDataStore.ktindex 64fad3ff20..fb28d58cbe 100644--- a/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/data/NonogramDataStore.kt+++ b/games/nonogram/src/main/java/com/vayunmathur/games/nonogram/data/NonogramDataStore.kt@@ -131,12 +131,27 @@ class NonogramDataStore(private val context: Context) {         }     } -    /** Bumps and returns the lifetime completed count. */-    suspend fun recordCompleted(): Int {+    /**+     * Banks the win identified by [key] and returns the lifetime completed count.+     *+     * The first call for a key bumps the count; repeats return the stored total unchanged, all in+     * one edit. The ViewModel's in-memory guard covers re-emitted flows within a session, but only+     * these stored keys survive a relaunch: without them, reopening an already-won board (killed+     * app before advancing, completed daily reopened the same day) replays the win and inflates+     * the count that feeds the `puzzles_10` / `puzzles_50` progress achievements. A set, not a+     * single last-key slot, because distinct wins (level 3, then level 4, then level 3 reopened)+     * must each survive.+     */+    suspend fun recordCompletedOnce(key: Long): Int {         var total = 0+        val marker = key.toString()         context.dataStore.edit { prefs ->-            total = (prefs[COMPLETED_KEY] ?: 0) + 1+            val banked = prefs[COMPLETED_WIN_KEYS] ?: emptySet()+            total = prefs[COMPLETED_KEY] ?: 0+            if (marker in banked) return@edit+            total += 1             prefs[COMPLETED_KEY] = total+            prefs[COMPLETED_WIN_KEYS] = banked + marker         }         return total     }@@ -167,6 +182,7 @@ class NonogramDataStore(private val context: Context) {         val GAME_MODE_KEY = stringPreferencesKey("game_mode")         val MARK_MODE_KEY = stringPreferencesKey("mark_mode")         val COMPLETED_KEY = intPreferencesKey("puzzles_completed")+        val COMPLETED_WIN_KEYS = stringSetPreferencesKey("puzzles_completed_win_keys")          val DAILY_DAY_KEY = longPreferencesKey("daily_day")         val DAILY_FILLED_KEY = stringSetPreferencesKey("daily_filled_cells")
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 7b134993a5..01e5ac7c8b 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@@ -287,7 +287,8 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application      * Reached from the marks collector, so it can fire more than once for the same win as unrelated      * flows re-emit. Everything it touches is idempotent for a given day: [DailyChallengeStore] treats      * a repeat of the same day as a no-op, and unlocking an already-unlocked achievement does nothing.-     * The lifetime counter is the exception, so it is guarded.+     * The lifetime counter is banked under the win key in the store, so even a relaunch replaying an+     * already-recorded win cannot count it twice; [recordedWin] is just the in-memory fast path.      */     private fun onWin(game: NonogramGameState, daily: Boolean, day: Long) {         // Daily wins are keyed by day and casual wins by negated level, which cannot collide.@@ -296,7 +297,7 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application         recordedWin = key          viewModelScope.launch {-            val total = dataStore.recordCompleted()+            val total = dataStore.recordCompletedOnce(key)             val manager = achievementsManager             manager.onAchievementUnlocked("first_puzzle")             if (game.size >= BIG_PICTURE_SIZE) manager.onAchievementUnlocked("big_picture")
games/nonogram/src/test/java/com/vayunmathur/games/nonogram/data/NonogramDataStoreTest.kt
diff --git a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/data/NonogramDataStoreTest.kt b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/data/NonogramDataStoreTest.ktindex 4528844ee8..ce7a69e058 100644--- a/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/data/NonogramDataStoreTest.kt+++ b/games/nonogram/src/test/java/com/vayunmathur/games/nonogram/data/NonogramDataStoreTest.kt@@ -72,10 +72,15 @@ class NonogramDataStoreTest {         assertEquals(emptySet(), s.dailyFilledCells.first()); assertEquals(STARTING_HEARTS, s.dailyHearts.first())     } -    @Test fun recordCompletedIncrements() = runBlocking {+    @Test fun recordCompletedOnceBanksEachWinKeyOnce() = runBlocking {         val s = store()-        val a = s.recordCompleted(); val b = s.recordCompleted()+        // Synthetic keys far outside the ViewModel's win namespace (casual: -level, daily: epoch+        // day), so this stays meaningful even if production keying ever changes.+        val a = s.recordCompletedOnce(-100_001L); val b = s.recordCompletedOnce(-100_002L)         assertEquals(a + 1, b); assertEquals(b, s.puzzlesCompleted.first())+        // A replay reports the lifetime total unchanged: it banks nothing new.+        assertEquals(b, s.recordCompletedOnce(-100_001L), "replaying a banked win must not count again")+        assertEquals(b, s.puzzlesCompleted.first())     }      @Test fun dailyRolloverClearsOnlyOnDayChange() = runBlocking {