Skip to content
This repository has been archived by the owner on Feb 17, 2024. It is now read-only.

Commit

Permalink
pref: 优化性能
Browse files Browse the repository at this point in the history
  • Loading branch information
HuanCheng65 committed Jul 23, 2023
1 parent dbd07ca commit b224836
Show file tree
Hide file tree
Showing 15 changed files with 454 additions and 390 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.huanchengfly.tieba.post.api.models

import com.google.gson.annotations.SerializedName
import com.huanchengfly.tieba.post.models.BaseBean
import javax.annotation.concurrent.Immutable

data class ThreadStoreBean(
@SerializedName("error_code")
Expand All @@ -10,6 +11,7 @@ data class ThreadStoreBean(
@SerializedName("store_thread")
val storeThread: List<ThreadStoreInfo>? = null
) : BaseBean() {
@Immutable
data class ThreadStoreInfo(
@SerializedName("thread_id")
val threadId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fun <T : UiState, A> Flow<T>.collectPartialAsState(
this@collectPartialAsState
.map { prop1.get(it) }
.distinctUntilChanged()
.flowOn(Dispatchers.IO)
.collect {
value = it
}
Expand All @@ -71,6 +72,7 @@ inline fun <reified Event : UiEvent> Flow<UiEvent>.onEvent(
this@onEvent
.filterIsInstance<Event>()
.cancellable()
.flowOn(Dispatchers.IO)
.collect {
launch {
listener(it)
Expand All @@ -95,6 +97,7 @@ inline fun <reified Event : UiEvent> BaseViewModel<*, *, *, *>.onEvent(
uiEventFlow
.filterIsInstance<Event>()
.cancellable()
.flowOn(Dispatchers.IO)
.collect {
coroutineScope.launch {
listener(it)
Expand All @@ -119,6 +122,7 @@ inline fun <reified VM : BaseViewModel<*, *, *, *>> pageViewModel(): VM {
uiEventFlow
.filterIsInstance<CommonUiEvent>()
.cancellable()
.flowOn(Dispatchers.IO)
.collectIn(context) {
context.handleCommonEvent(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import com.huanchengfly.tieba.post.ui.widgets.compose.Dialog
import com.huanchengfly.tieba.post.ui.widgets.compose.DialogNegativeButton
import com.huanchengfly.tieba.post.ui.widgets.compose.picker.ListSinglePicker
import com.huanchengfly.tieba.post.ui.widgets.compose.rememberDialogState
import kotlinx.collections.immutable.toImmutableList
import kotlinx.collections.immutable.toImmutableMap
import kotlinx.coroutines.launch

/**
Expand Down Expand Up @@ -92,6 +94,9 @@ fun ListPref(
onClick = { if (enabled) dialogState.show() },
)

val itemTitle = remember(entries) { entries.map { it.value }.toImmutableList() }
val itemValues = remember(entries) { entries.map { it.key }.toImmutableList() }

Dialog(
dialogState = dialogState,
title = { Text(text = title) },
Expand All @@ -100,15 +105,15 @@ fun ListPref(
}
) {
ListSinglePicker(
itemTitles = entries.map { it.value },
itemValues = entries.map { it.key },
itemTitles = itemTitle,
itemValues = itemValues,
selectedPosition = entries.keys.indexOf(selected),
onItemSelected = { _, title, value, _ ->
edit(current = value to title)
dismiss()
},
itemIcons = icons,
modifier = Modifier.padding(bottom = 16.dp)
modifier = Modifier.padding(bottom = 16.dp),
itemIcons = icons.toImmutableMap()
)
}
// if (showDialog) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -24,6 +23,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.huanchengfly.tieba.post.R
import com.huanchengfly.tieba.post.arch.emitGlobalEvent
import com.huanchengfly.tieba.post.ui.common.theme.compose.ExtendedTheme
import com.huanchengfly.tieba.post.ui.page.ProvideNavigator
import com.huanchengfly.tieba.post.ui.page.history.list.HistoryListPage
Expand All @@ -36,7 +36,6 @@ import com.huanchengfly.tieba.post.utils.HistoryUtil
import com.ramcosta.composedestinations.annotation.DeepLink
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch

@OptIn(ExperimentalFoundationApi::class)
Expand All @@ -55,8 +54,6 @@ fun HistoryPage(

val context = LocalContext.current

val eventFlow = remember { MutableSharedFlow<HistoryListUiEvent>() }

MyScaffold(
backgroundColor = Color.Transparent,
scaffoldState = scaffoldState,
Expand All @@ -70,7 +67,7 @@ fun HistoryPage(
IconButton(onClick = {
coroutineScope.launch {
HistoryUtil.deleteAll()
eventFlow.emit(HistoryListUiEvent.DeleteAll)
emitGlobalEvent(HistoryListUiEvent.DeleteAll)
launch {
scaffoldState.snackbarHostState.showSnackbar(
context.getString(
Expand Down Expand Up @@ -148,9 +145,9 @@ fun HistoryPage(
userScrollEnabled = true,
) {
if (it == 0) {
HistoryListPage(type = HistoryUtil.TYPE_THREAD, eventFlow = eventFlow)
HistoryListPage(type = HistoryUtil.TYPE_THREAD)
} else {
HistoryListPage(type = HistoryUtil.TYPE_FORUM, eventFlow = eventFlow)
HistoryListPage(type = HistoryUtil.TYPE_FORUM)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand All @@ -25,6 +24,7 @@ import com.huanchengfly.tieba.post.activities.ForumActivity
import com.huanchengfly.tieba.post.activities.ThreadActivity
import com.huanchengfly.tieba.post.arch.collectPartialAsState
import com.huanchengfly.tieba.post.arch.onEvent
import com.huanchengfly.tieba.post.arch.onGlobalEvent
import com.huanchengfly.tieba.post.arch.pageViewModel
import com.huanchengfly.tieba.post.fromJson
import com.huanchengfly.tieba.post.models.ThreadHistoryInfoBean
Expand All @@ -45,29 +45,19 @@ import com.huanchengfly.tieba.post.ui.widgets.compose.UserHeader
import com.huanchengfly.tieba.post.ui.widgets.compose.rememberMenuState
import com.huanchengfly.tieba.post.utils.DateTimeUtils
import com.huanchengfly.tieba.post.utils.HistoryUtil
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.launch

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun HistoryListPage(
type: Int,
eventFlow: Flow<HistoryListUiEvent>,
viewModel: HistoryListViewModel = if (type == HistoryUtil.TYPE_THREAD) pageViewModel<ThreadHistoryListViewModel>() else pageViewModel<ForumHistoryListViewModel>()
) {
LazyLoad(loaded = viewModel.initialized) {
viewModel.send(HistoryListUiIntent.Refresh)
viewModel.initialized = true
}
LaunchedEffect(null) {
launch {
eventFlow
.filterIsInstance<HistoryListUiEvent.DeleteAll>()
.collect {
viewModel.send(HistoryListUiIntent.DeleteAll)
}
}
onGlobalEvent<HistoryListUiEvent.DeleteAll> {
viewModel.send(HistoryListUiIntent.DeleteAll)
}
val isLoadingMore by viewModel.uiState.collectPartialAsState(
prop1 = HistoryListUiState::isLoadingMore,
Expand Down
Loading

0 comments on commit b224836

Please sign in to comment.