Yokan / rebuild
Course 1 8624561

SettingsStore.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/data/SettingsStore.kt

implementation76 lines
1package eu.friedcutlet.yokan.data23import eu.friedcutlet.yokan.database.DbSettings4import eu.friedcutlet.yokan.database.DbSyncPreference5import eu.friedcutlet.yokan.domain.UserSettings6import eu.friedcutlet.yokan.util.PlatformLock7import kotlinx.coroutines.CoroutineScope8import kotlinx.coroutines.flow.MutableStateFlow9import kotlinx.coroutines.flow.StateFlow10import kotlinx.coroutines.flow.asStateFlow11import kotlinx.coroutines.flow.distinctUntilChanged12import kotlinx.coroutines.launch1314/**15 * App-wide settings holder. Updates persist through [repository] and publish synchronously, so16 * text fields bound to a setting never lag; external writes (sync, restore, background jobs) are17 * picked up from the database snapshots. Safe to call from any thread.18 *19 * [onSchedulingChanged] runs after every [updateDb]/[updateSync]; the receiver decides whether the20 * periodic-task schedule actually changed.21 */22class SettingsStore(23    private val repository: YokanRepository,24    scope: CoroutineScope,25    private val onSchedulingChanged: () -> Unit,26) {27    // Serializes read-transform-write and publication, so a late snapshot can never overwrite a28    // newer value published by an update.29    private val lock = PlatformLock()3031    private val _db = MutableStateFlow(repository.dbSettings())32    val db: StateFlow<DbSettings> = _db.asStateFlow()3334    private val _user = MutableStateFlow(repository.settings())35    val user: StateFlow<UserSettings> = _user.asStateFlow()3637    private val _sync = MutableStateFlow(repository.syncPreference())38    val sync: StateFlow<DbSyncPreference> = _sync.asStateFlow()3940    init {41        scope.launch {42            repository.snapshots43                .distinctUntilChanged { old, new ->44                    old.settings === new.settings && old.syncPreferences === new.syncPreferences45                }46                .collect {47                    lock.withLock {48                        publishSettings(repository.dbSettings())49                        _sync.value = repository.syncPreference()50                    }51                }52        }53    }5455    fun updateDb(transform: (DbSettings) -> DbSettings) {56        lock.withLock { publishSettings(repository.updateDbSettings(transform)) }57        onSchedulingChanged()58    }5960    fun update(transform: (UserSettings) -> UserSettings) {61        lock.withLock {62            repository.updateSettings(transform)63            publishSettings(repository.dbSettings())64        }65    }6667    fun updateSync(transform: (DbSyncPreference) -> DbSyncPreference) {68        lock.withLock { _sync.value = repository.updateSyncPreference(transform = transform) }69        onSchedulingChanged()70    }7172    private fun publishSettings(settings: DbSettings) {73        _db.value = settings74        _user.value = settings.toDomain()75    }76}77

Search chapters, reference and source filenames.