git.iko.soy/opencode-android
on 8bc2cce help sign in
50 lines 2.0 kB history raw
1package soy.iko.opencode.data.repo
3import android.content.Context
4import androidx.datastore.preferences.core.booleanPreferencesKey
5import androidx.datastore.preferences.core.edit
6import androidx.datastore.preferences.core.stringPreferencesKey
7import androidx.datastore.preferences.preferencesDataStore
8import kotlinx.coroutines.flow.Flow
9import kotlinx.coroutines.flow.map
11enum class ThemeMode { SYSTEM, LIGHT, DARK }
13private val Context.settingsDataStore by preferencesDataStore(name = "settings")
15/** App-preferences store: theme mode, dynamic color (Material You), input behavior. */
16class SettingsStore(context: Context) {
18 private val appContext = context.applicationContext
19 private val themeKey = stringPreferencesKey("theme_mode")
20 private val dynamicColorKey = booleanPreferencesKey("dynamic_color")
21 private val sendOnEnterKey = booleanPreferencesKey("send_on_enter")
23 val themeMode: Flow<ThemeMode> = appContext.settingsDataStore.data.map { prefs ->
24 runCatching { ThemeMode.valueOf(prefs[themeKey] ?: ThemeMode.SYSTEM.name) }
25 .getOrDefault(ThemeMode.SYSTEM)
26 }
28 val dynamicColor: Flow<Boolean> = appContext.settingsDataStore.data.map { prefs ->
29 prefs[dynamicColorKey] ?: true
30 }
32 /** When true, the hardware Enter key sends the prompt; when false, Enter inserts a
33 * newline and Ctrl+Enter sends. Soft-keyboard IME action always sends. Defaults to
34 * true to match the prior hardcoded behavior. */
35 val sendOnEnter: Flow<Boolean> = appContext.settingsDataStore.data.map { prefs ->
36 prefs[sendOnEnterKey] ?: true
37 }
39 suspend fun setThemeMode(mode: ThemeMode) {
40 appContext.settingsDataStore.edit { it[themeKey] = mode.name }
41 }
43 suspend fun setDynamicColor(enabled: Boolean) {
44 appContext.settingsDataStore.edit { it[dynamicColorKey] = enabled }
45 }
47 suspend fun setSendOnEnter(enabled: Boolean) {
48 appContext.settingsDataStore.edit { it[sendOnEnterKey] = enabled }
49 }