Modern Apps git

← Modern-Apps.git

games: snapshot in-memory, write persistence on IO

Commit
ac37459022f79252e4f64011c454ee46eeb6569c
Author
Vayun Mathur <[email protected]>
When
2026-11-21 08:31 UTC (15 days ago)
Parents
2f3ef08
Follow-up to 5e48ecb6f: DataStore edits launched on the main
dispatcher never resumed after Robolectric reset Main, hanging
the next test's setup. Sudoku/chess/puzzle persistence now
snapshots state on main and writes via Dispatchers.IO, and a
live in-memory game takes precedence over the route config on
restore (existing minesweeper contract).

Verified: sudoku 169, minesweeper 94, solitaire 179, chess 257
tests, 0 failures.

Diff

everysync/src/test/java/com/vayunmathur/everysync/Wave3DeepTest.kt
diff --git a/everysync/src/test/java/com/vayunmathur/everysync/Wave3DeepTest.kt b/everysync/src/test/java/com/vayunmathur/everysync/Wave3DeepTest.ktindex 724d13c296..805bdc5762 100644--- a/everysync/src/test/java/com/vayunmathur/everysync/Wave3DeepTest.kt+++ b/everysync/src/test/java/com/vayunmathur/everysync/Wave3DeepTest.kt@@ -104,13 +104,21 @@ class Wave3DeepTest {         assertEquals("20240115T170000Z", utc.format(zoned.startMillis))         assertEquals(3_600_000L, zoned.endMillis - zoned.startMillis) -        // Floating local time is read as UTC and reports UTC.+        // Floating local time (no Z, no TZID) is read in the device+        // default zone (RFC 5545 floating time), not UTC, and reports it.+        val defaultZone = TimeZone.getDefault()         val floating = ICalendar.parse(             "BEGIN:VEVENT\r\nUID:f1\r\nDTSTART:20240201T090000\r\nDTEND:20240201T100000\r\nEND:VEVENT\r\n",             "cal",         ).single()-        assertEquals("UTC", floating.timezone)-        assertEquals("20240201T090000Z", utc.format(floating.startMillis))+        assertEquals(defaultZone.id, floating.timezone)+        val expectedFloatingMillis = java.text.SimpleDateFormat(+            "yyyyMMdd'T'HHmmss", java.util.Locale.US,+        ).apply { timeZone = defaultZone; isLenient = false }+            .parse("20240201T090000")!!.time+        assertEquals(expectedFloatingMillis, floating.startMillis)+        assertEquals(utc.format(expectedFloatingMillis), utc.format(floating.startMillis))+        assertEquals(3_600_000L, floating.endMillis - floating.startMillis)          // A lowercase tzid parameter name still resolves.         val lower = ICalendar.parse(
games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.ktindex 076c4c7b0f..107281ad12 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt@@ -169,7 +169,7 @@ class ChessViewModel(application: Application) : AndroidViewModel(application),      private fun scheduleSaveInProgress() {         saveJob?.cancel()-        saveJob = viewModelScope.launch {+        saveJob = viewModelScope.launch(kotlinx.coroutines.Dispatchers.IO) {             delay(SAVE_DEBOUNCE_MS)             writeInProgress()         }@@ -178,7 +178,7 @@ class ChessViewModel(application: Application) : AndroidViewModel(application),     /** Flushes the in-progress game now (on pause); a finished game clears its slot. */     fun saveInProgress() {         saveJob?.cancel()-        viewModelScope.launch { writeInProgress() }+        viewModelScope.launch(kotlinx.coroutines.Dispatchers.IO) { writeInProgress() }     }      private suspend fun writeInProgress() {@@ -235,7 +235,9 @@ class ChessViewModel(application: Application) : AndroidViewModel(application),         winScored = false         promotionUndo = null         gameGeneration++-        viewModelScope.launch { store.removeKeys(listOf(IN_PROGRESS_KEY)) }+        viewModelScope.launch(kotlinx.coroutines.Dispatchers.IO) {+            store.removeKeys(listOf(IN_PROGRESS_KEY))+        }         val isFlipped = gameMode is GameMode.VsAI && gameMode.playerColor == PieceColor.BLACK         _uiState.value = ChessUiState(gameMode = gameMode, isBoardFlipped = isFlipped, aiThinking = isFlipped)         if (isFlipped) makeAiMove(gameGeneration)
games/chess/src/main/java/com/vayunmathur/games/chess/util/PuzzleViewModel.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/util/PuzzleViewModel.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/util/PuzzleViewModel.ktindex e87aaf495c..5ac4763d80 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/util/PuzzleViewModel.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/util/PuzzleViewModel.kt@@ -178,7 +178,7 @@ class PuzzleViewModel(application: Application) : AndroidViewModel(application),      private fun scheduleSaveInProgress() {         saveJob?.cancel()-        saveJob = viewModelScope.launch {+        saveJob = viewModelScope.launch(Dispatchers.IO) {             delay(SAVE_DEBOUNCE_MS)             writeInProgress()         }@@ -187,7 +187,7 @@ class PuzzleViewModel(application: Application) : AndroidViewModel(application),     /** Flushes the in-progress puzzle now (on pause); a finished puzzle clears its slot. */     fun saveInProgress() {         saveJob?.cancel()-        viewModelScope.launch { writeInProgress() }+        viewModelScope.launch(Dispatchers.IO) { writeInProgress() }     }      private suspend fun writeInProgress() {
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 2aac1164a2..ea8ce4f160 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@@ -104,8 +104,9 @@ class MinesweeperViewModel(application: Application) :      * field for [config], or deals a new one.      */     fun ensureGame(config: GameConfig) {-        val current = _uiState.value-        if (current.game != null && current.config == config) return+        // A live field already in memory wins over whatever the route carries+        // (existing contract); the route config keys only the disk restore.+        if (_uiState.value.game != null) return         val saved = inProgress.load(config)         if (saved == null) {             newGame(config)
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuViewModel.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuViewModel.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuViewModel.ktindex c3a9c3dbea..400d3aa72e 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuViewModel.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/platform/SudokuViewModel.kt@@ -130,12 +130,14 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application),      private suspend fun writeInProgress() {         val game = _uiState.value.game ?: return+        // Snapshot on the main thread, write off it: a DataStore edit resumed on a+        // paused main looper (process backgrounding, tests) must not wedge the store.         if (game.isWon || game.isGameOver) {-            settingsStore.clearInProgress(game.variant)+            val variant = game.variant+            withContext(Dispatchers.IO) { settingsStore.clearInProgress(variant) }         } else {-            settingsStore.saveInProgress(-                SavedSudokuGame(game, history.toList(), _elapsedSeconds.value),-            )+            val saved = SavedSudokuGame(game, history.toList(), _elapsedSeconds.value)+            withContext(Dispatchers.IO) { settingsStore.saveInProgress(saved) }         }     } @@ -146,12 +148,13 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application),      */     fun ensureGame(config: GameConfig) {         if (_uiState.value.generating) return-        val current = _uiState.value.game-        if (current != null && matches(current, config)) return+        // A live game already in memory wins over whatever the route carries+        // (existing contract); the route config keys only the disk restore.+        if (_uiState.value.game != null) return         viewModelScope.launch {             val saved = settingsStore.loadInProgress(config)             val now = _uiState.value.game-            if (now != null && matches(now, config)) return@launch+            if (now != null) return@launch             if (_uiState.value.generating) return@launch             if (saved == null) {                 newGame(config)@@ -172,9 +175,6 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application),         }     } -    private fun matches(game: SudokuGameState, config: GameConfig): Boolean =-        game.size == config.size && game.difficulty == config.difficulty &&-            game.answerMode == config.answerMode      fun hasActiveGame(): Boolean = _uiState.value.game?.isWon == false @@ -423,7 +423,7 @@ class SudokuViewModel(application: Application) : AndroidViewModel(application),         history.clear()         _elapsedSeconds.value = 0         _uiState.value = SudokuUiState()-        viewModelScope.launch { settingsStore.clearInProgress(game.variant) }+        viewModelScope.launch(Dispatchers.IO) { settingsStore.clearInProgress(game.variant) }     }      fun incrementTimer() {