Reset preferences if file is corrupted (#92)

This commit is contained in:
timklge 2025-04-17 18:12:43 +02:00 committed by GitHub
parent 28e2afeda7
commit 2e5b0d6dbd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 6 deletions

View File

@ -2,8 +2,13 @@ package de.timklge.karooheadwind
import android.content.Context
import android.util.Log
import androidx.datastore.core.DataStore
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
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 com.mapbox.geojson.LineString
import com.mapbox.geojson.Point
import com.mapbox.turf.TurfConstants
@ -34,6 +39,10 @@ import kotlinx.serialization.json.Json
import kotlin.math.absoluteValue
import kotlin.time.Duration.Companion.minutes
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings", corruptionHandler = ReplaceFileCorruptionHandler {
Log.w(KarooHeadwindExtension.TAG, "Error reading settings, using default values")
emptyPreferences()
})
val jsonWithUnknownKeys = Json { ignoreUnknownKeys = true }

View File

@ -1,17 +1,11 @@
package de.timklge.karooheadwind
import android.content.Context
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import de.timklge.karooheadwind.screens.MainScreen
import de.timklge.karooheadwind.theme.AppTheme
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)