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:
1 parent
4bdd185434
commit
03b706c9f7
3 files changed
+26
-4
No files matched your search
+24
-2
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user