This commit is contained in:
HapeLee
2026-06-01 00:35:04 +08:00
9 changed files with 125 additions and 38 deletions
@@ -17,6 +17,7 @@ import io.legado.app.utils.putPrefFloat
import io.legado.app.utils.putPrefInt
import io.legado.app.utils.putPrefLong
import io.legado.app.utils.putPrefString
import io.legado.app.utils.putPrefStringSync
import splitties.init.appCtx
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
@@ -34,6 +35,7 @@ fun <T> prefDelegate(
key: String,
defaultValue: T,
lifecycleOwner: LifecycleOwner? = null,
sync: Boolean = false,
onValueChange: ((T) -> Unit)? = null
): PrefDelegate<T> {
return object : PrefDelegate<T>, SharedPreferences.OnSharedPreferenceChangeListener, DefaultLifecycleObserver {
@@ -84,7 +86,7 @@ fun <T> prefDelegate(
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
if (_value.value != value) {
when (value) {
is String? -> appCtx.putPrefString(key, value)
is String? -> if (sync) appCtx.putPrefStringSync(key, value) else appCtx.putPrefString(key, value)
is Int -> appCtx.putPrefInt(key, value)
is Boolean -> appCtx.putPrefBoolean(key, value)
is Long -> appCtx.putPrefLong(key, value)
@@ -111,8 +113,9 @@ fun <T> prefStateDelegate(
key: String,
defaultValue: T,
lifecycleOwner: LifecycleOwner? = null,
sync: Boolean = false,
onValueChange: ((T) -> Unit)? = null
): PrefStateDelegate<T> {
val delegate = prefDelegate(key, defaultValue, lifecycleOwner, onValueChange)
val delegate = prefDelegate(key, defaultValue, lifecycleOwner, sync, onValueChange)
return PrefStateDelegate(delegate)
}
@@ -624,10 +624,8 @@ fun BackupConfigScreen(
AppAlertDialog(
show = showLoadingDialog,
onDismiss = {},
onConfirm = {},
title = loadingText,
onDismissRequest = { showLoadingDialog = false }
onDismissRequest = { showLoadingDialog = false },
title = loadingText
)
}
@@ -8,7 +8,8 @@ object OtherConfig {
var language by prefDelegate(
PreferKey.language,
"auto"
"auto",
sync = true
)
var updateToVariant by prefDelegate(
@@ -223,6 +223,9 @@ fun Context.getPrefString(key: String, defValue: String? = null) =
fun Context.putPrefString(key: String, value: String?) =
defaultSharedPreferences.edit { putString(key, value) }
fun Context.putPrefStringSync(key: String, value: String?) =
defaultSharedPreferences.edit(commit = true) { putString(key, value) }
fun Context.getPrefStringSet(
key: String,
defValue: MutableSet<String>? = null,