games-nonogram: bank wins and reminder writes off the main looper
- Commit
335b73617eac6e731bfc74162e69f508726283a1- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-17 12:22 UTC (19 days ago)
- Parents
c453811
CI failed NonogramViewModelTest's winningBoardRecordsCompletionOnce, winningDailyRecordsDailyWin and reminderActionsUpdateState. All three wait for a store write to become visible: the banked completion count, the daily streak state, and the reminder settings flows. Those writes were launched on viewModelScope, i.e. Dispatchers.Main. In the tests Main is a test dispatcher that tearDown swaps back to Robolectric's paused looper between tests, and every other store write in this ViewModel already avoids that dependence: persist() and nextLevel() deliberately launch on Dispatchers.IO because, in the file's own words, the board is already on screen and nothing should wait for the main looper. The win recording (recordCompletedOnce plus the daily streak) and the reminder setting writes were the exceptions: a launch that lands after the looper stops being pumped is queued, cancelled with the screen, or leaks into a later test's store, and the suite's own comments record this exact rotating-timeout history. onWin, setReminderEnabled and setReminderTime now launch on Dispatchers.IO like the rest of the file. The achievements manager and streak reporter they call already do their own IO dispatching, so nothing about the user-visible behaviour changes; the writes simply no longer depend on main-looper pumping to land. Verified: NonogramViewModelTest passes directly, and :games:nonogram:testDevUnitTest was green sequentially and in two parallel all-module runs.
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 fa4a6d72e9..814739513a 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@@ -399,14 +399,16 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application } override fun setReminderEnabled(enabled: Boolean) {- viewModelScope.launch {+ // On IO like every other store write in this file: the write must+ // not depend on the main looper being pumped to land.+ viewModelScope.launch(Dispatchers.IO) { reminderSettings.setEnabled(enabled) rescheduleReminder(enabled, reminderMinutesOfDay.value) } } override fun setReminderTime(hour: Int, minute: Int) {- viewModelScope.launch {+ viewModelScope.launch(Dispatchers.IO) { val minutes = (hour * 60 + minute).toLong() reminderSettings.setMinutesOfDay(minutes) rescheduleReminder(reminderEnabled.value, minutes)@@ -441,7 +443,11 @@ class NonogramViewModel(application: Application) : AndroidViewModel(application if (recordedWin == key) return recordedWin = key - viewModelScope.launch {+ // On IO like persist()/nextLevel(): banking the win is a store+ // write, and a launch queued on the main looper that never gets+ // pumped (or is cancelled with the screen) would lose it — the+ // win would show on screen but never be counted.+ viewModelScope.launch(Dispatchers.IO) { val total = dataStore.recordCompletedOnce(key) val manager = achievementsManager manager.onAchievementUnlocked("first_puzzle")