Modern Apps git

← Modern-Apps.git

sudoku: tolerate corrupt persisted stats

Commit
96e606452ea923ed070cb60a6e10f839c1d8f5d9
Author
Vayun Mathur <[email protected]>
When
2026-11-19 02:29 UTC (17 days ago)
Parents
723391a
readKey decoded the explicitly cloud-restored sudoku_stats prefs
value with no guard. A malformed or cross-version GameStats value
threw SerializationException in SudokuViewModel init (via the
achievements manager) and in HomeScreen composition, crash-looping
every launch.

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

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

Diff

games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuStatsRepository.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuStatsRepository.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuStatsRepository.ktindex 3a8fbba80b..8df193510a 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuStatsRepository.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/data/SudokuStatsRepository.kt@@ -40,8 +40,12 @@ class SudokuStatsRepository(context: Context) {      private fun sizePrefix(size: BoardSize) = "stats_${size.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()