pref: 输入法高度持久化

This commit is contained in:
HuanCheng65 2023-07-21 13:47:31 +08:00
parent 6e3c43b498
commit e8a83b1ac1
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
3 changed files with 37 additions and 2 deletions

View File

@ -3,8 +3,13 @@ package com.huanchengfly.tieba.post
import android.annotation.SuppressLint
import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.datastore.core.DataMigration
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.SharedPreferencesMigration
@ -48,6 +53,25 @@ private val dataStoreInstance by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED)
val Context.dataStore: DataStore<Preferences> by dataStoreInstance
@Composable
fun <T> rememberPreferenceAsMutableState(
key: Preferences.Key<T>,
defaultValue: T
): MutableState<T> {
val dataStore = LocalContext.current.dataStore
val state = remember { mutableStateOf(defaultValue) }
LaunchedEffect(Unit) {
dataStore.data.map { it[key] ?: defaultValue }.collect { state.value = it }
}
LaunchedEffect(state.value) {
dataStore.edit { it[key] = state.value }
}
return state
}
@SuppressLint("FlowOperatorInvokedInComposition")
@Composable
fun <T> DataStore<Preferences>.collectPreferenceAsState(

View File

@ -101,6 +101,7 @@ import com.huanchengfly.tieba.post.utils.Emoticon
import com.huanchengfly.tieba.post.utils.EmoticonManager
import com.huanchengfly.tieba.post.utils.PickMediasRequest
import com.huanchengfly.tieba.post.utils.StringUtil
import com.huanchengfly.tieba.post.utils.appPreferences
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.spec.DestinationStyle
@ -308,11 +309,19 @@ fun ReplyPage(
.collect { value = it }
}
val imeAnimationEnd by remember { derivedStateOf { imeCurrentHeight == imeAnimationTargetHeight } }
val imeVisibleHeight by produceState(initialValue = 0, imeAnimationTargetInset, density) {
val imeVisibleHeight by produceState(
initialValue = remember { context.appPreferences.imeHeight },
imeAnimationTargetInset,
density
) {
snapshotFlow { imeAnimationTargetInset.getBottom(density) }
.filter { it > 0 }
.distinctUntilChanged()
.collect { value = it }
.collect {
Log.i("ReplyPage", "imeVisibleHeight: $it")
context.appPreferences.imeHeight = it
value = it
}
}
val textMeasurer = rememberTextMeasurer()

View File

@ -118,6 +118,8 @@ open class AppPreferencesUtils private constructor(ctx: Context) {
defaultValue = "0"
)
var imeHeight by DataStoreDelegates.int(defaultValue = 800)
var listItemsBackgroundIntermixed by DataStoreDelegates.boolean(defaultValue = true)
var listSingle by DataStoreDelegates.boolean(defaultValue = false)