fix: 吧页面点击刷新后不自动回顶

This commit is contained in:
HuanCheng65 2023-09-23 12:19:03 +08:00
parent c4e96c4031
commit b6737f85d6
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
4 changed files with 28 additions and 19 deletions

View File

@ -43,7 +43,7 @@ sealed interface GlobalEvent : UiEvent {
} }
private val globalEventSharedFlow: MutableSharedFlow<UiEvent> by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { private val globalEventSharedFlow: MutableSharedFlow<UiEvent> by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
MutableSharedFlow(0, 1, BufferOverflow.DROP_OLDEST) MutableSharedFlow(0, 2, BufferOverflow.DROP_OLDEST)
} }
val GlobalEventFlow = globalEventSharedFlow.asSharedFlow() 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) globalEventSharedFlow.emit(event)
} }
@ -69,7 +69,6 @@ inline fun <reified Event : UiEvent> onGlobalEvent(
GlobalEventFlow GlobalEventFlow
.filterIsInstance<Event>() .filterIsInstance<Event>()
.filter { .filter {
Log.i("GlobalEvent", "onGlobalEvent: $it")
filter(it) filter(it)
} }
.cancellable() .cancellable()

View File

@ -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.ImmutableHolder
import com.huanchengfly.tieba.post.arch.collectPartialAsState import com.huanchengfly.tieba.post.arch.collectPartialAsState
import com.huanchengfly.tieba.post.arch.emitGlobalEvent 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.onEvent
import com.huanchengfly.tieba.post.arch.pageViewModel import com.huanchengfly.tieba.post.arch.pageViewModel
import com.huanchengfly.tieba.post.dataStore import com.huanchengfly.tieba.post.dataStore
@ -593,12 +594,12 @@ fun ForumPage(
when (context.appPreferences.forumFabFunction) { when (context.appPreferences.forumFabFunction) {
"refresh" -> { "refresh" -> {
coroutineScope.launch { coroutineScope.launch {
emitGlobalEvent( emitGlobalEventSuspend(
ForumThreadListUiEvent.BackToTop( ForumThreadListUiEvent.BackToTop(
pagerState.currentPage == 1 pagerState.currentPage == 1
) )
) )
emitGlobalEvent( emitGlobalEventSuspend(
ForumThreadListUiEvent.Refresh( ForumThreadListUiEvent.Refresh(
pagerState.currentPage == 1, pagerState.currentPage == 1,
getSortType( getSortType(
@ -838,7 +839,7 @@ fun ForumPage(
forumId = forumInfo!!.get { id }, forumId = forumInfo!!.get { id },
forumName = forumInfo!!.get { name }, forumName = forumInfo!!.get { name },
isGood = it == 1, isGood = it == 1,
lazyListState = lazyListStates[it] lazyListState = remember { lazyListStates[it] }
) )
} }
} }

View File

@ -271,10 +271,14 @@ fun ForumThreadListPage(
viewModel.send(getFirstLoadIntent(context, forumName, isGood)) viewModel.send(getFirstLoadIntent(context, forumName, isGood))
viewModel.initialized = true viewModel.initialized = true
} }
onGlobalEvent<ForumThreadListUiEvent.Refresh> { onGlobalEvent<ForumThreadListUiEvent.Refresh>(
filter = { it.isGood == isGood },
) {
viewModel.send(getRefreshIntent(context, forumName, isGood, it.sortType)) viewModel.send(getRefreshIntent(context, forumName, isGood, it.sortType))
} }
onGlobalEvent<ForumThreadListUiEvent.BackToTop> { onGlobalEvent<ForumThreadListUiEvent.BackToTop>(
filter = { it.isGood == isGood },
) {
lazyListState.animateScrollToItem(0) lazyListState.animateScrollToItem(0)
} }
viewModel.onEvent<ForumThreadListUiEvent.AgreeFail> { viewModel.onEvent<ForumThreadListUiEvent.AgreeFail> {

View File

@ -718,10 +718,14 @@ sealed interface ThreadPartialChange : PartialChange<ThreadUiState> {
sealed class LoadMore : ThreadPartialChange { sealed class LoadMore : ThreadPartialChange {
override fun reduce(oldState: ThreadUiState): ThreadUiState = when (this) { override fun reduce(oldState: ThreadUiState): ThreadUiState = when (this) {
is Start -> oldState.copy(isLoadingMore = true) is Start -> oldState.copy(isLoadingMore = true)
is Success -> oldState.copy( is Success -> {
val uniqueData = data.filterNot { item ->
oldState.data.any { it.post.get { id } == item.post.get { id } }
}
oldState.copy(
isLoadingMore = false, isLoadingMore = false,
author = wrapImmutable(author), author = wrapImmutable(author),
data = (oldState.data + data).toImmutableList(), data = (oldState.data + uniqueData).toImmutableList(),
threadInfo = threadInfo.wrapImmutable(), threadInfo = threadInfo.wrapImmutable(),
currentPageMax = currentPage, currentPageMax = currentPage,
totalPage = totalPage, totalPage = totalPage,
@ -729,6 +733,7 @@ sealed interface ThreadPartialChange : PartialChange<ThreadUiState> {
nextPagePostId = nextPagePostId, nextPagePostId = nextPagePostId,
latestPosts = persistentListOf(), latestPosts = persistentListOf(),
) )
}
is Failure -> oldState.copy(isLoadingMore = false) is Failure -> oldState.copy(isLoadingMore = false)
} }