Modern Apps git

← Modern-Apps.git

minesweeper: tolerate corrupt persisted stats

Commit
fa3a834a7ed2f5af13277122d82ce6982bbab763
Author
Vayun Mathur <[email protected]>
When
2026-11-19 02:29 UTC (17 days ago)
Parents
e2ec614
readKey decoded the backup-restored minesweeper_stats prefs value
with no guard. A malformed or cross-version GameStats value threw
SerializationException in the ViewModel init/achievements path on
every launch, crash-looping until data was cleared.

Corrupt values now read as no record (fresh GameStats defaults).

Verified: ./gradlew :games:minesweeper:testDevUnitTest (pass),
./check games/minesweeper (pass).

Diff

games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/MinesweeperStatsRepository.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/MinesweeperStatsRepository.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/MinesweeperStatsRepository.ktindex 071a4ed09f..ad959e704b 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/MinesweeperStatsRepository.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/data/MinesweeperStatsRepository.kt@@ -36,8 +36,12 @@ class MinesweeperStatsRepository(context: Context) {      private fun key(size: BoardSize, difficulty: Difficulty) = "stats_${size.name}__${difficulty.name}" +    // The prefs file is backup-restored, so a corrupt or cross-version+    // value must read as "no record" instead of crash-looping at startup.     private fun readKey(key: String): GameStats? =-        prefs.getString(key, null)?.let { json.decodeFromString<GameStats>(it) }+        prefs.getString(key, null)?.let {+            runCatching { json.decodeFromString<GameStats>(it) }.getOrNull()+        }      fun getStats(size: BoardSize, difficulty: Difficulty): GameStats =         readKey(key(size, difficulty)) ?: GameStats()