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) }