Refine observed usage

This commit is contained in:
tarsin committed 2023-06-05 01:14:53 +08:00
1 parent ae2f7204fa
commit 866ea6369e
2 files changed
+3 -4

No files matched your search

@@ -111,7 +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 language by stringPref("app_language", "system").observed { updateWhenLocaleChanges() }
var lastDawnDay by longPref("last_dawn_day", 0)
init {
@@ -5,12 +5,11 @@ 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
import splitties.preferences.PrefDelegate
val collectScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
fun Flow<Unit>.observe(func: (Unit) -> Unit) = collectScope.launch { collect(func) }
fun <T, R : PrefDelegate<T>> R.observed(func: (Unit) -> Unit) = apply { collectScope.launch { changesFlow().collect(func) } }
fun updateWhenLocaleChanges() {
val newValue = Settings.language