refactor(prefs): create the settings store through DataStore.Builder

The store is now built through DataStore.Builder instead of the
preferencesDataStore delegate.
This commit is contained in:
OneWay committed 2026-10-22 21:48:01 +08:00
1 parent 4bdd185434
commit 03b706c9f7
3 files changed
+26 -4

No files matched your search

+1 -1
View File
@@ -89,7 +89,7 @@ The main search screen remains usable as long as at least one indexed dictionary
- Jetpack Compose (BOM 2026.09.00)
- Material 3 1.5.0-beta01
- Coroutines and Flow 1.11.0
- DataStore Preferences 1.2.1
- DataStore Preferences 1.3.0
- Paging 3.5.3
- OkHttp 5.5.0
- kotlinx.serialization 1.12.0
@@ -2,14 +2,20 @@ package com.example.research.data.local.preferences
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.core.FileStorage
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.PreferencesFileSerializer
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.core.stringSetPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import androidx.datastore.preferences.preferencesDataStoreFile
import com.example.research.core.domain.model.DictionarySource
import com.example.research.core.domain.model.SearchBarPosition
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.first
@@ -17,7 +23,23 @@ import kotlinx.coroutines.flow.map
import kotlinx.serialization.json.Json
import java.io.File
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
private const val SETTINGS_STORE_NAME = "settings"
private val storeLock = Any()
@Volatile
private var settingsStore: DataStore<Preferences>? = null
private val Context.dataStore: DataStore<Preferences>
get() = settingsStore ?: synchronized(storeLock) {
settingsStore ?: DataStore.Builder(
storage = FileStorage(PreferencesFileSerializer) {
applicationContext.preferencesDataStoreFile(SETTINGS_STORE_NAME)
},
context = Dispatchers.IO + SupervisorJob(),
).setCorruptionHandler(ReplaceFileCorruptionHandler { emptyPreferences() }).build()
.also { settingsStore = it }
}
data class SectionExpansionStates(
val isThemeExpanded: Boolean = false,
+1 -1
View File
@@ -4,7 +4,7 @@ activity_compose = "1.13.0"
agp = "9.4.1"
compose_bom = "2026.09.00"
core = "1.19.1"
datastore_preferences = "1.2.1"
datastore_preferences = "1.3.0"
documentfile = "1.1.0"
kotlin = "2.4.21"
lifecycle_runtime_ktx = "2.11.0"