|
| 1 | +package com.appcontrolx.data.local |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import androidx.datastore.core.DataStore |
| 5 | +import androidx.datastore.preferences.core.Preferences |
| 6 | +import androidx.datastore.preferences.core.booleanPreferencesKey |
| 7 | +import androidx.datastore.preferences.core.edit |
| 8 | +import androidx.datastore.preferences.core.intPreferencesKey |
| 9 | +import androidx.datastore.preferences.core.stringPreferencesKey |
| 10 | +import androidx.datastore.preferences.preferencesDataStore |
| 11 | +import com.appcontrolx.utils.Constants |
| 12 | +import kotlinx.coroutines.flow.Flow |
| 13 | +import kotlinx.coroutines.flow.map |
| 14 | +import javax.inject.Inject |
| 15 | +import javax.inject.Singleton |
| 16 | + |
| 17 | +private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings") |
| 18 | + |
| 19 | +@Singleton |
| 20 | +class SettingsDataStore @Inject constructor( |
| 21 | + private val context: Context |
| 22 | +) { |
| 23 | + private object Keys { |
| 24 | + val EXECUTION_MODE = stringPreferencesKey(Constants.PREFS_EXECUTION_MODE) |
| 25 | + val THEME = intPreferencesKey(Constants.PREFS_THEME) |
| 26 | + val CONFIRM_ACTIONS = booleanPreferencesKey(Constants.PREFS_CONFIRM_ACTIONS) |
| 27 | + val PROTECT_SYSTEM = booleanPreferencesKey(Constants.PREFS_PROTECT_SYSTEM) |
| 28 | + val AUTO_SNAPSHOT = booleanPreferencesKey(Constants.PREFS_AUTO_SNAPSHOT) |
| 29 | + val SETUP_COMPLETE = booleanPreferencesKey(Constants.PREFS_SETUP_COMPLETE) |
| 30 | + val LAST_VERSION = intPreferencesKey("last_shown_version") |
| 31 | + } |
| 32 | + |
| 33 | + val executionMode: Flow<String> = context.dataStore.data |
| 34 | + .map { it[Keys.EXECUTION_MODE] ?: Constants.MODE_NONE } |
| 35 | + |
| 36 | + val theme: Flow<Int> = context.dataStore.data |
| 37 | + .map { it[Keys.THEME] ?: -1 } |
| 38 | + |
| 39 | + val confirmActions: Flow<Boolean> = context.dataStore.data |
| 40 | + .map { it[Keys.CONFIRM_ACTIONS] ?: true } |
| 41 | + |
| 42 | + val protectSystem: Flow<Boolean> = context.dataStore.data |
| 43 | + .map { it[Keys.PROTECT_SYSTEM] ?: true } |
| 44 | + |
| 45 | + val autoSnapshot: Flow<Boolean> = context.dataStore.data |
| 46 | + .map { it[Keys.AUTO_SNAPSHOT] ?: true } |
| 47 | + |
| 48 | + val setupComplete: Flow<Boolean> = context.dataStore.data |
| 49 | + .map { it[Keys.SETUP_COMPLETE] ?: false } |
| 50 | + |
| 51 | + val lastVersion: Flow<Int> = context.dataStore.data |
| 52 | + .map { it[Keys.LAST_VERSION] ?: 0 } |
| 53 | + |
| 54 | + suspend fun setExecutionMode(mode: String) { |
| 55 | + context.dataStore.edit { it[Keys.EXECUTION_MODE] = mode } |
| 56 | + } |
| 57 | + |
| 58 | + suspend fun setTheme(theme: Int) { |
| 59 | + context.dataStore.edit { it[Keys.THEME] = theme } |
| 60 | + } |
| 61 | + |
| 62 | + suspend fun setConfirmActions(confirm: Boolean) { |
| 63 | + context.dataStore.edit { it[Keys.CONFIRM_ACTIONS] = confirm } |
| 64 | + } |
| 65 | + |
| 66 | + suspend fun setProtectSystem(protect: Boolean) { |
| 67 | + context.dataStore.edit { it[Keys.PROTECT_SYSTEM] = protect } |
| 68 | + } |
| 69 | + |
| 70 | + suspend fun setAutoSnapshot(auto: Boolean) { |
| 71 | + context.dataStore.edit { it[Keys.AUTO_SNAPSHOT] = auto } |
| 72 | + } |
| 73 | + |
| 74 | + suspend fun setSetupComplete(complete: Boolean) { |
| 75 | + context.dataStore.edit { it[Keys.SETUP_COMPLETE] = complete } |
| 76 | + } |
| 77 | + |
| 78 | + suspend fun setLastVersion(version: Int) { |
| 79 | + context.dataStore.edit { it[Keys.LAST_VERSION] = version } |
| 80 | + } |
| 81 | +} |
0 commit comments