pref: 修改设置卡顿

This commit is contained in:
HuanCheng65 2023-01-26 14:54:47 +08:00
parent 442a6cd74f
commit ada00cfed8
No known key found for this signature in database
GPG Key ID: E9031EF91A805148
2 changed files with 13 additions and 4 deletions

View File

@ -65,12 +65,15 @@ fun ListPref(
prefs?.get(selectionKey)?.also { selected = it } // starting value if it exists in datastore prefs?.get(selectionKey)?.also { selected = it } // starting value if it exists in datastore
fun edit(current: Pair<String, String>) = run { fun edit(current: Pair<String, String>) = run {
scope.launch {
selected = current.first
onValueChange?.invoke(current.first)
}
scope.launch { scope.launch {
try { try {
datastore.edit { preferences -> datastore.edit { preferences ->
preferences[selectionKey] = current.first preferences[selectionKey] = current.first
} }
onValueChange?.invoke(current.first)
} catch (e: Exception) { } catch (e: Exception) {
Log.e("ListPref", "Could not write pref $key to database. ${e.printStackTrace()}") Log.e("ListPref", "Could not write pref $key to database. ${e.printStackTrace()}")
} }

View File

@ -3,7 +3,11 @@ package com.huanchengfly.tieba.post.ui.common.prefs.widgets
import android.util.Log import android.util.Log
import androidx.compose.material.ExperimentalMaterialApi import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.MaterialTheme import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.* import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.datastore.preferences.core.booleanPreferencesKey import androidx.datastore.preferences.core.booleanPreferencesKey
@ -35,13 +39,15 @@ fun SwitchPref(
prefs?.get(selectionKey)?.also { checked = it } // starting value if it exists in datastore prefs?.get(selectionKey)?.also { checked = it } // starting value if it exists in datastore
fun edit(newState: Boolean) = run { fun edit(newState: Boolean) = run {
scope.launch {
checked = newState
onCheckedChange?.invoke(newState)
}
scope.launch { scope.launch {
try { try {
datastore.edit { preferences -> datastore.edit { preferences ->
preferences[selectionKey] = newState preferences[selectionKey] = newState
} }
checked = newState
onCheckedChange?.invoke(newState)
} catch (e: Exception) { } catch (e: Exception) {
Log.e("SwitchPref", "Could not write pref $key to database. ${e.printStackTrace()}") Log.e("SwitchPref", "Could not write pref $key to database. ${e.printStackTrace()}")
} }