dark mode-v18
This commit is contained in:
parent
5f02dc7642
commit
e9125fc46f
2 changed files with 39 additions and 1 deletions
|
|
@ -0,0 +1,38 @@
|
|||
package com.example.login_v3.data.local.UserPreferences
|
||||
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences // 確保是這個 Preferences
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.emptyPreferences
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.map
|
||||
import java.io.IOException
|
||||
|
||||
|
||||
class UserPreferencesRepository(private val dataStore: DataStore<Preferences>) {
|
||||
|
||||
private object PreferencesKeys {
|
||||
val THEME_MODE = stringPreferencesKey("theme_mode")
|
||||
}
|
||||
|
||||
// 讀取設定:直接在宣告時賦值,簡潔又安全
|
||||
val themeModeFlow: Flow<String> = dataStore.data
|
||||
.catch { exception ->
|
||||
if (exception is IOException) {
|
||||
emit(emptyPreferences())
|
||||
} else {
|
||||
throw exception
|
||||
}
|
||||
}
|
||||
.map { preferences ->
|
||||
preferences[PreferencesKeys.THEME_MODE] ?: "system"
|
||||
}
|
||||
|
||||
suspend fun saveThemeMode(mode: String) {
|
||||
dataStore.edit { preferences ->
|
||||
preferences[PreferencesKeys.THEME_MODE] = mode
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue