Set Dark theme in Settings

Add MyViewModel to keep ThemeSettings state
Use ListItem to display SSIDs in WiFi SSID Policy
Use green color scheme as default color scheme
Add Compose BOM dependency, update dependencies
This commit is contained in:
BinTianqi
2024-11-16 12:48:01 +08:00
parent 66d7e80c8c
commit 2546a9c3c8
16 changed files with 316 additions and 259 deletions

View File

@@ -0,0 +1,37 @@
package com.bintianqi.owndroid
import android.content.Context
import android.os.Build
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
class MyViewModel: ViewModel() {
val theme = MutableStateFlow(ThemeSettings())
var initialized = false
fun initialize(context: Context) {
val sharedPrefs = context.getSharedPreferences("data", Context.MODE_PRIVATE)
theme.value = ThemeSettings(
materialYou = sharedPrefs.getBoolean("material_you", Build.VERSION.SDK_INT >= 31),
darkTheme = if(sharedPrefs.contains("dark_theme")) sharedPrefs.getBoolean("dark_theme", false) else null,
blackTheme = sharedPrefs.getBoolean("black_theme", false)
)
viewModelScope.launch {
theme.collect {
val editor = sharedPrefs.edit()
editor.putBoolean("material_you", it.materialYou)
if(it.darkTheme == null) editor.remove("dark_theme") else editor.putBoolean("dark_theme", it.darkTheme)
editor.putBoolean("black_theme", it.blackTheme)
editor.commit()
}
}
}
}
data class ThemeSettings(
val materialYou: Boolean = false,
val darkTheme: Boolean? = null,
val blackTheme: Boolean = false
)