From a9dc88134d04d423604e059d88f89b6a22f062eb Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Thu, 20 Jul 2023 15:33:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B1=8F=E8=94=BD=E5=90=A7=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E7=9A=84=E7=9B=B4=E6=92=AD=E5=B9=BF=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/repository/FrsPageRepository.kt | 34 +++++++ .../threadlist/ForumThreadListViewModel.kt | 95 +++++++------------ .../tieba/post/ui/page/thread/ThreadPage.kt | 7 +- 3 files changed, 68 insertions(+), 68 deletions(-) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/repository/FrsPageRepository.kt b/app/src/main/java/com/huanchengfly/tieba/post/repository/FrsPageRepository.kt index e01dd8e0..cbba952f 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/repository/FrsPageRepository.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/repository/FrsPageRepository.kt @@ -1,9 +1,14 @@ package com.huanchengfly.tieba.post.repository +import com.huanchengfly.tieba.post.App import com.huanchengfly.tieba.post.api.TiebaApi import com.huanchengfly.tieba.post.api.models.protos.frsPage.FrsPageResponse +import com.huanchengfly.tieba.post.api.models.protos.threadList.ThreadListResponse +import com.huanchengfly.tieba.post.api.retrofit.exception.TiebaUnknownException +import com.huanchengfly.tieba.post.utils.appPreferences import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach object FrsPageRepository { @@ -24,6 +29,35 @@ object FrsPageRepository { } lastHash = hash return TiebaApi.getInstance().frsPage(forumName, page, loadType, sortType, goodClassifyId) + .map { response -> + if (response.data_ == null) throw TiebaUnknownException + val userList = response.data_.user_list + val threadList = response.data_.thread_list + .map { threadInfo -> + threadInfo.copy(author = userList.find { it.id == threadInfo.authorId }) + } + .filter { !App.INSTANCE.appPreferences.blockVideo || it.videoInfo == null } + .filter { it.threadTypes != 3072 } // 去他妈的直播 + response.copy(data_ = response.data_.copy(thread_list = threadList)) + } .onEach { lastResponse = it } } + + fun threadList( + forumId: Long, + forumName: String, + page: Int, + sortType: Int, + threadIds: String = "", + ): Flow = + TiebaApi.getInstance() + .threadList(forumId, forumName, page, sortType, threadIds) + .map { response -> + if (response.data_ == null) throw TiebaUnknownException + val userList = response.data_.user_list + val threadList = response.data_.thread_list.map { threadInfo -> + threadInfo.copy(author = userList.find { it.id == threadInfo.authorId }) + } + response.copy(data_ = response.data_.copy(thread_list = threadList)) + } } \ No newline at end of file diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/threadlist/ForumThreadListViewModel.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/threadlist/ForumThreadListViewModel.kt index e052993d..dcf2e98e 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/threadlist/ForumThreadListViewModel.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/forum/threadlist/ForumThreadListViewModel.kt @@ -1,11 +1,12 @@ package com.huanchengfly.tieba.post.ui.page.forum.threadlist import androidx.compose.runtime.Stable -import com.huanchengfly.tieba.post.App import com.huanchengfly.tieba.post.api.TiebaApi import com.huanchengfly.tieba.post.api.models.AgreeBean import com.huanchengfly.tieba.post.api.models.protos.ThreadInfo import com.huanchengfly.tieba.post.api.models.protos.frsPage.Classify +import com.huanchengfly.tieba.post.api.models.protos.frsPage.FrsPageResponse +import com.huanchengfly.tieba.post.api.retrofit.exception.TiebaUnknownException import com.huanchengfly.tieba.post.api.retrofit.exception.getErrorCode import com.huanchengfly.tieba.post.api.retrofit.exception.getErrorMessage import com.huanchengfly.tieba.post.api.updateAgreeStatus @@ -19,7 +20,6 @@ import com.huanchengfly.tieba.post.arch.UiIntent import com.huanchengfly.tieba.post.arch.UiState import com.huanchengfly.tieba.post.arch.wrapImmutable import com.huanchengfly.tieba.post.repository.FrsPageRepository -import com.huanchengfly.tieba.post.utils.appPreferences import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow @@ -96,52 +96,36 @@ private class ForumThreadListPartialChangeProducer(val type: ForumThreadListType sortType.takeIf { type == ForumThreadListType.Latest } ?: -1, goodClassifyId.takeIf { type == ForumThreadListType.Good } ) - .map { response -> - if (response.data_?.page == null) ForumThreadListPartialChange.FirstLoad.Failure( - NullPointerException("未知错误") + .map { response -> + if (response.data_?.page == null) throw TiebaUnknownException + ForumThreadListPartialChange.FirstLoad.Success( + response.data_.thread_list.wrapImmutable(), + response.data_.thread_id_list, + (response.data_.forum?.good_classify ?: emptyList()).wrapImmutable(), + goodClassifyId.takeIf { type == ForumThreadListType.Good }, + response.data_.page.has_more == 1 ) - else { - val userList = response.data_.user_list - val threadList = response.data_.thread_list.map { threadInfo -> - threadInfo.copy(author = userList.find { it.id == threadInfo.authorId }) - }.filter { !App.INSTANCE.appPreferences.blockVideo || it.videoInfo == null } - ForumThreadListPartialChange.FirstLoad.Success( - threadList.wrapImmutable(), - response.data_.thread_id_list, - (response.data_.forum?.good_classify ?: emptyList()).wrapImmutable(), - goodClassifyId.takeIf { type == ForumThreadListType.Good }, - response.data_.page.has_more == 1 - ) - } } .onStart { emit(ForumThreadListPartialChange.FirstLoad.Start) } .catch { emit(ForumThreadListPartialChange.FirstLoad.Failure(it)) } private fun ForumThreadListUiIntent.Refresh.producePartialChange() = - TiebaApi.getInstance().frsPage( + FrsPageRepository.frsPage( forumName, 1, 1, sortType.takeIf { type == ForumThreadListType.Latest } ?: -1, goodClassifyId.takeIf { type == ForumThreadListType.Good } ) - .map { response -> - if (response.data_?.page == null) ForumThreadListPartialChange.Refresh.Failure( - NullPointerException("未知错误") + .map { response -> + if (response.data_?.page == null) throw TiebaUnknownException + ForumThreadListPartialChange.Refresh.Success( + response.data_.thread_list.wrapImmutable(), + response.data_.thread_id_list, + (response.data_.forum?.good_classify ?: emptyList()).wrapImmutable(), + goodClassifyId.takeIf { type == ForumThreadListType.Good }, + response.data_.page.has_more == 1 ) - else { - val userList = response.data_.user_list - val threadList = response.data_.thread_list.map { threadInfo -> - threadInfo.copy(author = userList.find { it.id == threadInfo.authorId }) - } - ForumThreadListPartialChange.Refresh.Success( - threadList.wrapImmutable(), - response.data_.thread_id_list, - (response.data_.forum?.good_classify ?: emptyList()).wrapImmutable(), - goodClassifyId.takeIf { type == ForumThreadListType.Good }, - response.data_.page.has_more == 1 - ) - } } .onStart { emit(ForumThreadListPartialChange.Refresh.Start) } .catch { emit(ForumThreadListPartialChange.Refresh.Failure(it)) } @@ -149,53 +133,38 @@ private class ForumThreadListPartialChangeProducer(val type: ForumThreadListType private fun ForumThreadListUiIntent.LoadMore.producePartialChange(): Flow { val flow = if (threadListIds.isNotEmpty()) { val size = min(threadListIds.size, 30) - TiebaApi.getInstance().threadList( + FrsPageRepository.threadList( forumId, forumName, currentPage, sortType, threadListIds.subList(0, size).joinToString(separator = ",") { "$it" } ).map { response -> - if (response.data_ == null) ForumThreadListPartialChange.LoadMore.Failure( - NullPointerException("未知错误") + if (response.data_ == null) throw TiebaUnknownException + ForumThreadListPartialChange.LoadMore.Success( + threadList = response.data_.thread_list.wrapImmutable(), + threadListIds = threadListIds.drop(size), + currentPage = currentPage, + hasMore = response.data_.thread_list.isNotEmpty() ) - else { - val userList = response.data_.user_list - val threadList = response.data_.thread_list.map { threadInfo -> - threadInfo.copy(author = userList.find { it.id == threadInfo.authorId }) - } - ForumThreadListPartialChange.LoadMore.Success( - threadList = threadList.wrapImmutable(), - threadListIds = threadListIds.drop(size), - currentPage = currentPage, - hasMore = threadList.isNotEmpty() - ) - } } } else { - TiebaApi.getInstance().frsPage( + FrsPageRepository.frsPage( forumName, currentPage + 1, 2, sortType.takeIf { type == ForumThreadListType.Latest } ?: -1, goodClassifyId.takeIf { type == ForumThreadListType.Good } - ).map { response -> - if (response.data_ == null) ForumThreadListPartialChange.LoadMore.Failure( - NullPointerException("未知错误") - ) - else { - val userList = response.data_.user_list - val threadList = response.data_.thread_list.map { threadInfo -> - threadInfo.copy(author = userList.find { it.id == threadInfo.authorId }) - } + ) + .map { response -> + if (response.data_?.page == null) throw TiebaUnknownException ForumThreadListPartialChange.LoadMore.Success( - threadList = threadList.wrapImmutable(), + threadList = response.data_.thread_list.wrapImmutable(), threadListIds = response.data_.thread_id_list, currentPage = currentPage + 1, - hasMore = threadList.isNotEmpty() + response.data_.page.has_more == 1 ) } - } } return flow .onStart { emit(ForumThreadListPartialChange.LoadMore.Start) } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/thread/ThreadPage.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/thread/ThreadPage.kt index a4062304..b6f0c9b2 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/thread/ThreadPage.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/thread/ThreadPage.kt @@ -504,11 +504,8 @@ fun ThreadPage( initial = false ) - val showFirstPostContent by remember { - derivedStateOf { firstPostContentRenders.isNotEmpty() && author != null } - } val isEmpty by remember { - derivedStateOf { data.isEmpty() && !showFirstPostContent } + derivedStateOf { data.isEmpty() && firstPost == null } } val isCollected = remember(thread) { thread?.get { collectStatus != 0 } == true @@ -890,7 +887,7 @@ fun ThreadPage( modifier = Modifier.fillMaxWidth() ) { item(key = "FirstPost") { - if (showFirstPostContent && firstPost != null) { + if (firstPost != null) { Column { PostCard( postHolder = firstPost!!,