From b6737f85d6bc7eaf40ff07352069a815a70dce01 Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Sat, 23 Sep 2023 12:19:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=90=A7=E9=A1=B5=E9=9D=A2=E7=82=B9?= =?UTF-8?q?=E5=87=BB=E5=88=B7=E6=96=B0=E5=90=8E=E4=B8=8D=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=9B=9E=E9=A1=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tieba/post/arch/GlobalEvent.kt | 5 ++-- .../tieba/post/ui/page/forum/ForumPage.kt | 7 ++--- .../forum/threadlist/ForumThreadListPage.kt | 8 ++++-- .../post/ui/page/thread/ThreadViewModel.kt | 27 +++++++++++-------- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/arch/GlobalEvent.kt b/app/src/main/java/com/huanchengfly/tieba/post/arch/GlobalEvent.kt index c9570a68..57c2d1b8 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/arch/GlobalEvent.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/arch/GlobalEvent.kt @@ -43,7 +43,7 @@ sealed interface GlobalEvent : UiEvent { } private val globalEventSharedFlow: MutableSharedFlow by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { - MutableSharedFlow(0, 1, BufferOverflow.DROP_OLDEST) + MutableSharedFlow(0, 2, BufferOverflow.DROP_OLDEST) } val GlobalEventFlow = globalEventSharedFlow.asSharedFlow() @@ -54,7 +54,7 @@ fun CoroutineScope.emitGlobalEvent(event: UiEvent) { } } -suspend fun emitGlobalEvent(event: UiEvent) { +suspend fun emitGlobalEventSuspend(event: UiEvent) { globalEventSharedFlow.emit(event) } @@ -69,7 +69,6 @@ inline fun onGlobalEvent( GlobalEventFlow .filterIsInstance() .filter { - Log.i("GlobalEvent", "onGlobalEvent: $it") filter(it) } .cancellable() diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/ForumPage.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/ForumPage.kt index dde494bd..ef077ade 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/ForumPage.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/ForumPage.kt @@ -92,6 +92,7 @@ import com.huanchengfly.tieba.post.api.models.protos.frsPage.ForumInfo import com.huanchengfly.tieba.post.arch.ImmutableHolder import com.huanchengfly.tieba.post.arch.collectPartialAsState import com.huanchengfly.tieba.post.arch.emitGlobalEvent +import com.huanchengfly.tieba.post.arch.emitGlobalEventSuspend import com.huanchengfly.tieba.post.arch.onEvent import com.huanchengfly.tieba.post.arch.pageViewModel import com.huanchengfly.tieba.post.dataStore @@ -593,12 +594,12 @@ fun ForumPage( when (context.appPreferences.forumFabFunction) { "refresh" -> { coroutineScope.launch { - emitGlobalEvent( + emitGlobalEventSuspend( ForumThreadListUiEvent.BackToTop( pagerState.currentPage == 1 ) ) - emitGlobalEvent( + emitGlobalEventSuspend( ForumThreadListUiEvent.Refresh( pagerState.currentPage == 1, getSortType( @@ -838,7 +839,7 @@ fun ForumPage( forumId = forumInfo!!.get { id }, forumName = forumInfo!!.get { name }, isGood = it == 1, - lazyListState = lazyListStates[it] + lazyListState = remember { lazyListStates[it] } ) } } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/threadlist/ForumThreadListPage.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/threadlist/ForumThreadListPage.kt index 102838ed..a9f970c3 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/threadlist/ForumThreadListPage.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/threadlist/ForumThreadListPage.kt @@ -271,10 +271,14 @@ fun ForumThreadListPage( viewModel.send(getFirstLoadIntent(context, forumName, isGood)) viewModel.initialized = true } - onGlobalEvent { + onGlobalEvent( + filter = { it.isGood == isGood }, + ) { viewModel.send(getRefreshIntent(context, forumName, isGood, it.sortType)) } - onGlobalEvent { + onGlobalEvent( + filter = { it.isGood == isGood }, + ) { lazyListState.animateScrollToItem(0) } viewModel.onEvent { diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/thread/ThreadViewModel.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/thread/ThreadViewModel.kt index e0128cf9..e0916d60 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/thread/ThreadViewModel.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/thread/ThreadViewModel.kt @@ -718,17 +718,22 @@ sealed interface ThreadPartialChange : PartialChange { sealed class LoadMore : ThreadPartialChange { override fun reduce(oldState: ThreadUiState): ThreadUiState = when (this) { is Start -> oldState.copy(isLoadingMore = true) - is Success -> oldState.copy( - isLoadingMore = false, - author = wrapImmutable(author), - data = (oldState.data + data).toImmutableList(), - threadInfo = threadInfo.wrapImmutable(), - currentPageMax = currentPage, - totalPage = totalPage, - hasMore = hasMore, - nextPagePostId = nextPagePostId, - latestPosts = persistentListOf(), - ) + is Success -> { + val uniqueData = data.filterNot { item -> + oldState.data.any { it.post.get { id } == item.post.get { id } } + } + oldState.copy( + isLoadingMore = false, + author = wrapImmutable(author), + data = (oldState.data + uniqueData).toImmutableList(), + threadInfo = threadInfo.wrapImmutable(), + currentPageMax = currentPage, + totalPage = totalPage, + hasMore = hasMore, + nextPagePostId = nextPagePostId, + latestPosts = persistentListOf(), + ) + } is Failure -> oldState.copy(isLoadingMore = false) }