Implement Compose SimpleMenuPreference 2

This commit is contained in:
tarsin committed 2023-06-05 01:09:41 +08:00
1 parent 5be2b77e42
commit ae2f7204fa
4 files changed
+45 -2

No files matched your search

@@ -111,6 +111,7 @@ object Settings : DefaultPreferences() {
var proxyIp by stringOrNullPref("proxy_ip", null)
var dohUrl by stringPref("doh_url", "")
var language by stringPref("app_language", "system").apply { changesFlow().observe { updateWhenLocaleChanges() } }
var lastDawnDay by longPref("last_dawn_day", 0)
init {
@@ -0,0 +1,22 @@
package com.hippo.ehviewer
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
val collectScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
fun Flow<Unit>.observe(func: (Unit) -> Unit) = collectScope.launch { collect(func) }
fun updateWhenLocaleChanges() {
val newValue = Settings.language
if ("system" == newValue) {
AppCompatDelegate.setApplicationLocales(LocaleListCompat.getEmptyLocaleList())
} else {
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(newValue))
}
}
@@ -50,12 +50,18 @@ fun AdvancedScreen() {
title = stringResource(id = R.string.settings_advanced_dump_logcat),
summary = stringResource(id = R.string.settings_advanced_dump_logcat_summary),
)
SimpleMenuPreference(
SimpleMenuPreferenceInt(
title = stringResource(id = R.string.settings_advanced_read_cache_size),
entry = R.array.read_cache_size_entries,
entryValueRes = R.array.read_cache_size_entry_values,
value = Settings::readCacheSize,
)
SimpleMenuPreference(
title = stringResource(id = R.string.settings_advanced_app_language_title),
entry = R.array.app_language_entries,
entryValueRes = R.array.app_language_entry_values,
value = Settings::language,
)
}
}
}
@@ -86,7 +86,7 @@ fun HtmlPreference(title: String, summary: AnnotatedString? = null, onClick: ()
}
@Composable
fun SimpleMenuPreference(title: String, @ArrayRes entry: Int, @ArrayRes entryValueRes: Int, value: KMutableProperty0<Int>) {
fun SimpleMenuPreferenceInt(title: String, @ArrayRes entry: Int, @ArrayRes entryValueRes: Int, value: KMutableProperty0<Int>) {
val entryArray = stringArrayResource(id = entry)
val valuesArray = stringArrayResource(id = entryValueRes)
val map = remember {
@@ -98,3 +98,17 @@ fun SimpleMenuPreference(title: String, @ArrayRes entry: Int, @ArrayRes entryVal
check(entryArray.size == valuesArray.size)
DropDownPref(title = title, defaultValue = v.toString(), onValueChange = ::set, useSelectedAsSummary = true, entries = map)
}
@Composable
fun SimpleMenuPreference(title: String, @ArrayRes entry: Int, @ArrayRes entryValueRes: Int, value: KMutableProperty0<String>) {
val entryArray = stringArrayResource(id = entry)
val valuesArray = stringArrayResource(id = entryValueRes)
val map = remember {
val iter = entryArray.iterator()
valuesArray.associateWith { iter.next() }
}
var v by remember { mutableStateOf(value.get()) }
fun set(new: String) = value.set(new.also { v = it })
check(entryArray.size == valuesArray.size)
DropDownPref(title = title, defaultValue = v, onValueChange = ::set, useSelectedAsSummary = true, entries = map)
}