pref: 性能优化

This commit is contained in:
HuanCheng65 2023-01-28 23:34:44 +08:00
parent ce69d9e2c1
commit fe1a0f2105
No known key found for this signature in database
GPG Key ID: E9031EF91A805148
2 changed files with 35 additions and 16 deletions

View File

@ -1,17 +1,27 @@
package com.huanchengfly.tieba.post.arch package com.huanchengfly.tieba.post.arch
import android.annotation.SuppressLint
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.produceState
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.hilt.navigation.compose.hiltViewModel import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.* import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlin.reflect.KProperty1 import kotlin.reflect.KProperty1
@ -23,15 +33,24 @@ fun <T> Flow<T>.collectIn(
flowWithLifecycle(lifecycleOwner.lifecycle, minActiveState).collect(action) flowWithLifecycle(lifecycleOwner.lifecycle, minActiveState).collect(action)
} }
@SuppressLint("FlowOperatorInvokedInComposition")
@Composable @Composable
fun <T : UiState, A> Flow<T>.collectPartialAsState( fun <T : UiState, A> Flow<T>.collectPartialAsState(
prop1: KProperty1<T, A>, prop1: KProperty1<T, A>,
initial: A, initial: A,
): State<A> { ): State<A> {
return map { prop1.get(it) } return produceState(
initialValue = initial,
key1 = this,
key2 = prop1,
key3 = initial
) {
this@collectPartialAsState
.map { prop1.get(it) }
.distinctUntilChanged() .distinctUntilChanged()
.collectAsState(initial = initial) .collect {
value = it
}
}
} }
inline fun <reified Event : UiEvent> CoroutineScope.onEvent( inline fun <reified Event : UiEvent> CoroutineScope.onEvent(

View File

@ -4,14 +4,14 @@ import androidx.compose.runtime.Stable
import org.litepal.crud.LitePalSupport import org.litepal.crud.LitePalSupport
@Stable @Stable
data class Account( data class Account @JvmOverloads constructor(
var uid: String, var uid: String = "",
var name: String, var name: String = "",
var bduss: String, var bduss: String = "",
var tbs: String, var tbs: String = "",
var portrait: String, var portrait: String = "",
var sToken: String, var sToken: String = "",
var cookie: String, var cookie: String = "",
var nameShow: String? = null, var nameShow: String? = null,
var intro: String? = null, var intro: String? = null,
var sex: String? = null, var sex: String? = null,