Optimize code

New Privilege object, which follows Application lifecycle.
Add Privilege.DPM and Privilege.DAR variable, instead of creating DPM and DAR instance every time.
This commit is contained in:
BinTianqi
2025-08-29 20:58:39 +08:00
parent 10ac570818
commit 6f54bf576f
22 changed files with 496 additions and 687 deletions

View File

@@ -2,23 +2,15 @@ package com.bintianqi.owndroid
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
class MyViewModel(application: Application): AndroidViewModel(application) {
val theme = MutableStateFlow(ThemeSettings())
init {
val sp = SharedPrefs(application)
theme.value = ThemeSettings(sp.materialYou, sp.darkTheme, sp.blackTheme)
viewModelScope.launch {
theme.collect {
sp.materialYou = it.materialYou
sp.darkTheme = it.darkTheme
sp.blackTheme = it.blackTheme
}
}
val theme = MutableStateFlow(ThemeSettings(SP.materialYou, SP.darkTheme, SP.blackTheme))
fun changeTheme(newTheme: ThemeSettings) {
theme.value = newTheme
SP.materialYou = newTheme.materialYou
SP.darkTheme = newTheme.darkTheme
SP.blackTheme = newTheme.blackTheme
}
}