From 2e5b0d6dbdcf8cf5868c909fb114b2412aa279d1 Mon Sep 17 00:00:00 2001 From: timklge <2026103+timklge@users.noreply.github.com> Date: Thu, 17 Apr 2025 18:12:43 +0200 Subject: [PATCH] Reset preferences if file is corrupted (#92) --- .../main/kotlin/de/timklge/karooheadwind/DataStore.kt | 9 +++++++++ .../main/kotlin/de/timklge/karooheadwind/MainActivity.kt | 6 ------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/DataStore.kt b/app/src/main/kotlin/de/timklge/karooheadwind/DataStore.kt index f956a63..86fa1d4 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/DataStore.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/DataStore.kt @@ -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 by preferencesDataStore(name = "settings", corruptionHandler = ReplaceFileCorruptionHandler { + Log.w(KarooHeadwindExtension.TAG, "Error reading settings, using default values") + emptyPreferences() +}) val jsonWithUnknownKeys = Json { ignoreUnknownKeys = true } diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/MainActivity.kt b/app/src/main/kotlin/de/timklge/karooheadwind/MainActivity.kt index 894ea51..e06abda 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/MainActivity.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/MainActivity.kt @@ -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 by preferencesDataStore(name = "settings") - class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)