重写主题系统
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package io.legato.kazusa.lib.prefs
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.preference.PreferenceViewHolder
|
||||
import com.google.android.material.button.MaterialButtonToggleGroup
|
||||
import io.legato.kazusa.R
|
||||
import androidx.core.content.withStyledAttributes
|
||||
|
||||
|
||||
class ThemeModePreference(context: Context, attrs: AttributeSet) : Preference(context, attrs) {
|
||||
|
||||
private var currentValue: String? = null
|
||||
private var isBottomBackground: Boolean = false
|
||||
|
||||
init {
|
||||
layoutResource = R.layout.view_pref
|
||||
widgetLayoutResource = R.layout.view_theme_mode
|
||||
context.withStyledAttributes(attrs, R.styleable.Preference) {
|
||||
isBottomBackground = getBoolean(R.styleable.Preference_isBottomBackground, false)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: PreferenceViewHolder) {
|
||||
super.onBindViewHolder(holder)
|
||||
|
||||
val toggleGroup = holder.itemView.findViewById<View>(R.id.theme_toggle_group)
|
||||
if (toggleGroup == null) {
|
||||
val rootView = holder.itemView as ViewGroup
|
||||
val inflater = LayoutInflater.from(context)
|
||||
val themeToggleView = inflater.inflate(R.layout.view_theme_mode, rootView, false)
|
||||
rootView.addView(themeToggleView)
|
||||
|
||||
setupToggleGroup(themeToggleView.findViewById(R.id.theme_toggle_group))
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupToggleGroup(toggleGroup: MaterialButtonToggleGroup) {
|
||||
|
||||
when (currentValue) {
|
||||
"0" -> toggleGroup.check(R.id.btn_system)
|
||||
"1" -> toggleGroup.check(R.id.btn_light)
|
||||
"2" -> toggleGroup.check(R.id.btn_dark)
|
||||
}
|
||||
|
||||
// 监听选择变化
|
||||
toggleGroup.addOnButtonCheckedListener { group, checkedId, isChecked ->
|
||||
if (isChecked) {
|
||||
val newValue = when (checkedId) {
|
||||
R.id.btn_system -> "0"
|
||||
R.id.btn_light -> "1"
|
||||
R.id.btn_dark -> "2"
|
||||
else -> null
|
||||
}
|
||||
|
||||
if (newValue != null && callChangeListener(newValue)) {
|
||||
currentValue = newValue
|
||||
persistString(newValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSetInitialValue(defaultValue: Any?) {
|
||||
super.onSetInitialValue(defaultValue)
|
||||
currentValue = getPersistedString(defaultValue as? String ?: "0")
|
||||
}
|
||||
|
||||
fun getDefaultValue(): Any {
|
||||
return "0"
|
||||
}
|
||||
|
||||
override fun shouldDisableDependents(): Boolean {
|
||||
return currentValue == null || super.shouldDisableDependents()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.legato.kazusa.utils
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.preferences.core.booleanPreferencesKey
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.floatPreferencesKey
|
||||
import androidx.datastore.preferences.core.intPreferencesKey
|
||||
import androidx.datastore.preferences.core.longPreferencesKey
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
object DataStoreUtil {
|
||||
|
||||
suspend fun <T> save(context: Context, key: String, value: T) {
|
||||
context.dataStore.edit { preferences ->
|
||||
when (value) {
|
||||
is String -> preferences[stringPreferencesKey(key)] = value
|
||||
is Int -> preferences[intPreferencesKey(key)] = value
|
||||
is Boolean -> preferences[booleanPreferencesKey(key)] = value
|
||||
is Float -> preferences[floatPreferencesKey(key)] = value
|
||||
is Long -> preferences[longPreferencesKey(key)] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun <T> read(context: Context, key: String, defaultValue: T): T {
|
||||
return context.dataStore.data.map { preferences ->
|
||||
when (defaultValue) {
|
||||
is String -> preferences[stringPreferencesKey(key)] ?: defaultValue
|
||||
is Int -> preferences[intPreferencesKey(key)] ?: defaultValue
|
||||
is Boolean -> preferences[booleanPreferencesKey(key)] ?: defaultValue
|
||||
is Float -> preferences[floatPreferencesKey(key)] ?: defaultValue
|
||||
is Long -> preferences[longPreferencesKey(key)] ?: defaultValue
|
||||
else -> defaultValue
|
||||
} as T
|
||||
}.first()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user