translate: survive a corrupt DataStore file instead of crash-looping
- Commit
713278a1f7c8d6a1d900efa408f44047879b4b4b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-19 01:58 UTC (17 days ago)
- Parents
960df1c
TranslateSettings read the preferences DataStore with .first() and no catch. A corrupt translate_settings file makes that flow throw CorruptionException/IOException on every read; the throw escaped TranslateViewModel's init launch, crashed the app, and crashed again on every relaunch, and a later edit in setSource/setTarget could throw the same way on a language pick. Reads now go through a prefs() flow that catches and emits emptyPreferences (falling back to the AUTO/es defaults and empty recents), and writes are best-effort runCatching: the in-memory language change stands, only persistence is lost. Verified: ./gradlew :translate:testDevUnitTest (six-module run, BUILD SUCCESSFUL) and ./check todo translate travel tuner vpn (exit 0).
Diff
translate/src/main/java/com/vayunmathur/translate/data/TranslateSettings.kt
diff --git a/translate/src/main/java/com/vayunmathur/translate/data/TranslateSettings.kt b/translate/src/main/java/com/vayunmathur/translate/data/TranslateSettings.ktindex 6d0d0177ac..179a320532 100644--- a/translate/src/main/java/com/vayunmathur/translate/data/TranslateSettings.kt+++ b/translate/src/main/java/com/vayunmathur/translate/data/TranslateSettings.kt@@ -5,9 +5,13 @@ import com.vayunmathur.translate.domain.parseRecentLanguages import com.vayunmathur.translate.domain.pushRecentLanguage import android.content.Context+import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.core.edit+import androidx.datastore.preferences.core.emptyPreferences import androidx.datastore.preferences.core.stringPreferencesKey import androidx.datastore.preferences.preferencesDataStore+import kotlinx.coroutines.flow.Flow+import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map @@ -26,32 +30,44 @@ class TranslateSettings(private val context: Context) { private val recentSourceKey = stringPreferencesKey("recent_source_langs") private val recentTargetKey = stringPreferencesKey("recent_target_langs") + // A corrupt preferences file makes DataStore's data flow throw+ // (CorruptionException/IOException) on every read, which would crash+ // the ViewModel init read and then crash again on every relaunch.+ // Reading as empty preferences falls back to the defaults instead.+ private fun prefs(): Flow<Preferences> =+ context.dataStore.data.catch { emit(emptyPreferences()) }+ suspend fun source(): String =- context.dataStore.data.map { it[sourceKey] }.first() ?: Languages.AUTO.code+ prefs().map { it[sourceKey] }.first() ?: Languages.AUTO.code suspend fun target(): String =- context.dataStore.data.map { it[targetKey] }.first() ?: DEFAULT_TARGET+ prefs().map { it[targetKey] }.first() ?: DEFAULT_TARGET - suspend fun setSource(code: String) =- context.dataStore.edit { it[sourceKey] = code }.let { }+ // Writes are best-effort for the same reason reads are: a corrupt+ // file must not turn a language pick into a crash. The in-memory+ // state has already changed; only persistence is lost.+ suspend fun setSource(code: String) {+ runCatching { context.dataStore.edit { it[sourceKey] = code } }+ } - suspend fun setTarget(code: String) =- context.dataStore.edit { it[targetKey] = code }.let { }+ suspend fun setTarget(code: String) {+ runCatching { context.dataStore.edit { it[targetKey] = code } }+ } suspend fun recentSources(): List<String> =- context.dataStore.data.map { parseRecentLanguages(it[recentSourceKey]) }.first()+ prefs().map { parseRecentLanguages(it[recentSourceKey]) }.first() suspend fun recentTargets(): List<String> =- context.dataStore.data.map { parseRecentLanguages(it[recentTargetKey]) }.first()+ prefs().map { parseRecentLanguages(it[recentTargetKey]) }.first() suspend fun pushRecentSource(code: String) { val updated = pushRecentLanguage(recentSources(), code)- context.dataStore.edit { it[recentSourceKey] = updated.joinToString(",") }+ runCatching { context.dataStore.edit { it[recentSourceKey] = updated.joinToString(",") } } } suspend fun pushRecentTarget(code: String) { val updated = pushRecentLanguage(recentTargets(), code)- context.dataStore.edit { it[recentTargetKey] = updated.joinToString(",") }+ runCatching { context.dataStore.edit { it[recentTargetKey] = updated.joinToString(",") } } } /** Defaults for [TranslateSettings]. */