From 647916fe6ccdbf5b6ad0d17c75ed11893eae3d5c Mon Sep 17 00:00:00 2001 From: Diego Beraldin Date: Sat, 7 Oct 2023 23:18:04 +0200 Subject: [PATCH] fix: pagination issues in case of network or API error --- .../core/commonui/chat/InboxChatViewModel.kt | 16 ++++++------ .../CommunityDetailViewModel.kt | 12 ++++----- .../commonui/drawer/ModalDrawerViewModel.kt | 12 ++++----- .../instanceinfo/InstanceInfoViewModel.kt | 12 ++++----- .../postdetail/PostDetailViewModel.kt | 21 ++++++++-------- .../saveditems/SavedItemsViewModel.kt | 25 +++++++++++-------- .../userdetail/UserDetailViewModel.kt | 20 +++++++-------- .../lemmy/repository/CommentRepository.kt | 8 +++--- .../lemmy/repository/CommunityRepository.kt | 8 +++--- .../domain/lemmy/repository/PostRepository.kt | 8 +++--- .../repository/PrivateMessageRepository.kt | 4 +-- .../domain/lemmy/repository/UserRepository.kt | 24 +++++++++--------- .../home/postlist/PostListViewModel.kt | 15 +++++------ .../inbox/mentions/InboxMentionsViewModel.kt | 15 +++++------ .../inbox/messages/InboxMessagesViewModel.kt | 20 +++++++-------- .../inbox/replies/InboxRepliesViewModel.kt | 13 +++++----- .../profile/logged/ProfileLoggedViewModel.kt | 20 +++++++-------- .../login/LoginBottomSheetViewModel.kt | 12 ++++----- .../feature/search/main/ExploreViewModel.kt | 9 +++---- .../utils/CommunityPaginator.kt | 4 +-- .../raccoonforlemmy/MainViewModel.kt | 4 +-- 21 files changed, 139 insertions(+), 143 deletions(-) diff --git a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/chat/InboxChatViewModel.kt b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/chat/InboxChatViewModel.kt index 36dd26b0d..b96448cf1 100644 --- a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/chat/InboxChatViewModel.kt +++ b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/chat/InboxChatViewModel.kt @@ -6,7 +6,6 @@ import com.github.diegoberaldin.raccoonforlemmy.core.architecture.MviModel import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationCenter import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationCenterContractKeys import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository -import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.CommentRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.PrivateMessageRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.UserRepository @@ -93,28 +92,29 @@ class InboxChatViewModel( auth = auth, page = currentPage, unreadOnly = false, - ).filter { + )?.filter { it.creator?.id == otherUserId || it.recipient?.id == otherUserId - }.onEach { + }?.onEach { if (!it.read) { launch { markAsRead(true, it.id) } } } - currentPage++ - val canFetchMore = itemList.size >= CommentRepository.DEFAULT_PAGE_SIZE + if (!itemList.isNullOrEmpty()) { + currentPage++ + } mvi.updateState { val newItems = if (refreshing) { - itemList + itemList.orEmpty() } else { - it.messages + itemList + it.messages + itemList.orEmpty() } it.copy( messages = newItems, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, ) } diff --git a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/communitydetail/CommunityDetailViewModel.kt b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/communitydetail/CommunityDetailViewModel.kt index b9221c54a..57cb66e0b 100644 --- a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/communitydetail/CommunityDetailViewModel.kt +++ b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/communitydetail/CommunityDetailViewModel.kt @@ -13,7 +13,6 @@ import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.PostModel import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.SortType import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.shareUrl import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.toSortType -import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.CommentRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.CommunityRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.PostRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository @@ -168,18 +167,19 @@ class CommunityDetailViewModel( sort = sort, ) } - currentPage++ - val canFetchMore = itemList.size >= CommentRepository.DEFAULT_PAGE_SIZE + if (!itemList.isNullOrEmpty()) { + currentPage++ + } mvi.updateState { val newItems = if (refreshing) { - itemList + itemList.orEmpty() } else { - it.posts + itemList + it.posts + itemList.orEmpty() } it.copy( posts = newItems, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, ) } diff --git a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/drawer/ModalDrawerViewModel.kt b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/drawer/ModalDrawerViewModel.kt index a397fd508..524438dfa 100644 --- a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/drawer/ModalDrawerViewModel.kt +++ b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/drawer/ModalDrawerViewModel.kt @@ -98,13 +98,11 @@ class ModalDrawerViewModel( mvi.scope?.launch(Dispatchers.IO) { mvi.updateState { it.copy(changeInstanceloading = true) } - val res = runCatching { - communityRepository.getAllInInstance( - instance = instanceName, - page = 1, - limit = 1 - ) - }.getOrElse { emptyList() } + val res = communityRepository.getAllInInstance( + instance = instanceName, + page = 1, + limit = 1 + ) ?: emptyList() if (res.isEmpty()) { mvi.updateState { it.copy( diff --git a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/instanceinfo/InstanceInfoViewModel.kt b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/instanceinfo/InstanceInfoViewModel.kt index 34008ff75..bab3eca5c 100644 --- a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/instanceinfo/InstanceInfoViewModel.kt +++ b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/instanceinfo/InstanceInfoViewModel.kt @@ -4,7 +4,6 @@ import cafe.adriel.voyager.core.model.ScreenModel import com.github.diegoberaldin.raccoonforlemmy.core.architecture.DefaultMviModel import com.github.diegoberaldin.raccoonforlemmy.core.architecture.MviModel import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository -import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.CommentRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.CommunityRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository import kotlinx.coroutines.Dispatchers @@ -72,18 +71,19 @@ class InstanceInfoViewModel( page = currentPage, limit = 50, ) - currentPage++ - val canFetchMore = itemList.size >= CommentRepository.DEFAULT_PAGE_SIZE + if (!itemList.isNullOrEmpty()) { + currentPage++ + } mvi.updateState { val newItems = if (refreshing) { - itemList.filter { e -> e.instanceUrl == url } + itemList?.filter { e -> e.instanceUrl == url }.orEmpty() } else { - it.communities + itemList.filter { e -> e.instanceUrl == url } + it.communities + itemList?.filter { e -> e.instanceUrl == url }.orEmpty() } it.copy( communities = newItems, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, ) } diff --git a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/postdetail/PostDetailViewModel.kt b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/postdetail/PostDetailViewModel.kt index 768e9f2ca..4e0a9cff2 100644 --- a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/postdetail/PostDetailViewModel.kt +++ b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/postdetail/PostDetailViewModel.kt @@ -225,13 +225,13 @@ class PostDetailViewModel( val auth = identityRepository.authToken.value val refreshing = currentState.refreshing val sort = currentState.sortType - val commentList = commentRepository.getAll( + val itemList = commentRepository.getAll( auth = auth, postId = post.id, page = currentPage, sort = sort, maxDepth = CommentRepository.MAX_COMMENT_DEPTH, - ).let { + )?.let { processCommentsToGetNestedOrder( items = it, ) @@ -239,24 +239,25 @@ class PostDetailViewModel( if (refreshing) { it } else { - it.filter { c1 -> + it?.filter { c1 -> // prevents accidental duplication currentState.comments.none { c2 -> c1.id == c2.id } } } } - currentPage++ - val canFetchMore = commentList.size >= CommentRepository.DEFAULT_PAGE_SIZE + if (!itemList.isNullOrEmpty()) { + currentPage++ + } mvi.updateState { val newcomments = if (refreshing) { - commentList + itemList.orEmpty() } else { - it.comments + commentList + it.comments + itemList.orEmpty() } it.copy( comments = newcomments, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, initial = false, ) @@ -288,7 +289,7 @@ class PostDetailViewModel( parentId = parentId, sort = sort, maxDepth = CommentRepository.MAX_COMMENT_DEPTH, - ).let { + )?.let { processCommentsToGetNestedOrder( items = it, ancestorId = parentId.toString(), @@ -297,7 +298,7 @@ class PostDetailViewModel( val newList = uiState.value.comments.let { list -> val index = list.indexOfFirst { c -> c.id == parentId } list.toMutableList().apply { - addAll(index + 1, fetchResult) + addAll(index + 1, fetchResult.orEmpty()) }.toList() } mvi.updateState { it.copy(comments = newList) } diff --git a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/saveditems/SavedItemsViewModel.kt b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/saveditems/SavedItemsViewModel.kt index 4939927bf..f13c7df4c 100644 --- a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/saveditems/SavedItemsViewModel.kt +++ b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/saveditems/SavedItemsViewModel.kt @@ -134,49 +134,52 @@ class SavedItemsViewModel( val section = currentState.section val sortType = currentState.sortType if (section == SavedItemsSection.Posts) { - val postList = userRepository.getSavedPosts( + val itemList = userRepository.getSavedPosts( auth = auth, id = user.id, page = currentPage, sort = sortType, ) - val canFetchMore = postList.size >= PostRepository.DEFAULT_PAGE_SIZE + if (!itemList.isNullOrEmpty()) { + currentPage++ + } mvi.updateState { val newPosts = if (refreshing) { - postList + itemList.orEmpty() } else { - it.posts + postList + it.posts + itemList.orEmpty() } it.copy( posts = newPosts, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, ) } } else { - val commentList = userRepository.getSavedComments( + val itemList = userRepository.getSavedComments( auth = auth, id = user.id, page = currentPage, sort = sortType, ) - val canFetchMore = commentList.size >= PostRepository.DEFAULT_PAGE_SIZE + if (!itemList.isNullOrEmpty()) { + currentPage++ + } mvi.updateState { val newComments = if (refreshing) { - commentList + itemList.orEmpty() } else { - it.comments + commentList + it.comments + itemList.orEmpty() } it.copy( comments = newComments, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, ) } } - currentPage++ } } diff --git a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/userdetail/UserDetailViewModel.kt b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/userdetail/UserDetailViewModel.kt index a20097a83..963b336ad 100644 --- a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/userdetail/UserDetailViewModel.kt +++ b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/userdetail/UserDetailViewModel.kt @@ -186,7 +186,7 @@ class UserDetailViewModel( val section = currentState.section val userId = currentState.user.id if (section == UserDetailSection.Posts) { - val postList = userRepository.getPosts( + val itemList = userRepository.getPosts( auth = auth, id = userId, page = currentPage, @@ -200,44 +200,42 @@ class UserDetailViewModel( id = userId, page = currentPage, sort = currentState.sortType, - ) + ).orEmpty() } else { currentState.comments } - val canFetchMore = postList.size >= PostRepository.DEFAULT_PAGE_SIZE mvi.updateState { val newPosts = if (refreshing) { - postList + itemList.orEmpty() } else { - it.posts + postList + it.posts + itemList.orEmpty() } it.copy( posts = newPosts, comments = comments, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, ) } } else { - val commentList = userRepository.getComments( + val itemList = userRepository.getComments( auth = auth, id = userId, page = currentPage, sort = currentState.sortType, ) - val canFetchMore = commentList.size >= PostRepository.DEFAULT_PAGE_SIZE mvi.updateState { val newcomments = if (refreshing) { - commentList + itemList.orEmpty() } else { - it.comments + commentList + it.comments + itemList.orEmpty() } it.copy( comments = newcomments, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, ) } diff --git a/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/CommentRepository.kt b/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/CommentRepository.kt index f8fe1dfc5..3f1c617a0 100644 --- a/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/CommentRepository.kt +++ b/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/CommentRepository.kt @@ -29,7 +29,7 @@ class CommentRepository( type: ListingType = ListingType.All, sort: SortType = SortType.New, maxDepth: Int = 1, - ): List = runCatching { + ): List? = runCatching { val response = services.comment.getAll( auth = auth, postId = postId, @@ -41,7 +41,7 @@ class CommentRepository( ) val dto = response.body()?.comments ?: emptyList() dto.map { it.toModel() } - }.getOrElse { emptyList() } + }.getOrNull() suspend fun getBy(id: Int, auth: String?): CommentModel? = runCatching { services.comment.getBy(id, auth).body()?.commentView?.toModel() @@ -54,7 +54,7 @@ class CommentRepository( type: ListingType = ListingType.All, sort: SortType = SortType.New, maxDepth: Int = 1, - ): List = runCatching { + ): List? = runCatching { val response = services.comment.getAll( auth = auth, parentId = parentId, @@ -65,7 +65,7 @@ class CommentRepository( ) val dto = response.body()?.comments ?: emptyList() dto.map { it.toModel() } - }.getOrElse { emptyList() } + }.getOrNull() fun asUpVoted(comment: CommentModel, voted: Boolean) = comment.copy( myVote = if (voted) 1 else 0, diff --git a/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/CommunityRepository.kt b/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/CommunityRepository.kt index 1da3a6baa..0fb76b74e 100644 --- a/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/CommunityRepository.kt +++ b/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/CommunityRepository.kt @@ -29,7 +29,7 @@ class CommunityRepository( listingType: ListingType = ListingType.All, sortType: SortType = SortType.Active, resultType: SearchResultType = SearchResultType.All, - ): List = runCatching { + ): List? = runCatching { val response = services.search.search( q = query, auth = auth, @@ -47,7 +47,7 @@ class CommunityRepository( // returns everything posts + comments + communities + users - }.getOrElse { emptyList() } + }.getOrNull() suspend fun getAllInInstance( instance: String = "", @@ -55,7 +55,7 @@ class CommunityRepository( page: Int, limit: Int = DEFAULT_PAGE_SIZE, sort: SortType = SortType.Active, - ): List = runCatching { + ): List? = runCatching { customServices.changeInstance(instance) val response = customServices.community.getAll( auth = auth, @@ -66,7 +66,7 @@ class CommunityRepository( response?.communities?.map { it.toModel() }.orEmpty() - }.getOrElse { emptyList() } + }.getOrNull() suspend fun getSubscribed( auth: String? = null, diff --git a/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/PostRepository.kt b/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/PostRepository.kt index c620c9925..004545d59 100644 --- a/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/PostRepository.kt +++ b/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/PostRepository.kt @@ -32,7 +32,7 @@ class PostRepository( type: ListingType = ListingType.Local, sort: SortType = SortType.Active, communityId: Int? = null, - ): List = runCatching { + ): List? = runCatching { val response = services.post.getAll( auth = auth, communityId = communityId, @@ -43,7 +43,7 @@ class PostRepository( ) val dto = response.body()?.posts ?: emptyList() dto.map { it.toModel() } - }.getOrElse { emptyList() } + }.getOrNull() suspend fun getAllInInstance( instance: String, @@ -52,7 +52,7 @@ class PostRepository( type: ListingType = ListingType.Local, sort: SortType = SortType.Active, communityId: Int? = null, - ): List = runCatching { + ): List? = runCatching { customServices.changeInstance(instance) val response = customServices.post.getAll( communityId = communityId, @@ -63,7 +63,7 @@ class PostRepository( ) val dto = response.body()?.posts ?: emptyList() dto.map { it.toModel() } - }.getOrElse { emptyList() } + }.getOrNull() suspend fun get(id: Int, auth: String? = null): PostModel? = runCatching { val dto = services.post.get(auth, id).body()?.postView diff --git a/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/PrivateMessageRepository.kt b/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/PrivateMessageRepository.kt index 2d198e286..b9d302444 100644 --- a/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/PrivateMessageRepository.kt +++ b/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/PrivateMessageRepository.kt @@ -14,7 +14,7 @@ class PrivateMessageRepository( page: Int, limit: Int = PostRepository.DEFAULT_PAGE_SIZE, unreadOnly: Boolean = true, - ): List = runCatching { + ): List? = runCatching { val response = serviceProvider.privateMessages.getPrivateMessages( auth = auth, limit = limit, @@ -23,7 +23,7 @@ class PrivateMessageRepository( ) val dto = response.body() ?: return@runCatching emptyList() dto.privateMessages.map { it.toModel() } - }.getOrElse { emptyList() } + }.getOrNull() suspend fun create( message: String, diff --git a/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/UserRepository.kt b/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/UserRepository.kt index 8481c570f..ffcdb8cc7 100644 --- a/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/UserRepository.kt +++ b/domain-lemmy/repository/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/lemmy/repository/UserRepository.kt @@ -69,7 +69,7 @@ class UserRepository( page: Int, limit: Int = PostRepository.DEFAULT_PAGE_SIZE, sort: SortType = SortType.Active, - ): List = runCatching { + ): List? = runCatching { val response = serviceProvider.user.getDetails( auth = auth, personId = id, @@ -79,7 +79,7 @@ class UserRepository( ) val dto = response.body() ?: return@runCatching emptyList() dto.posts.map { it.toModel() } - }.getOrElse { emptyList() } + }.getOrNull() suspend fun getSavedPosts( id: Int, @@ -87,7 +87,7 @@ class UserRepository( page: Int, limit: Int = PostRepository.DEFAULT_PAGE_SIZE, sort: SortType = SortType.Active, - ): List = runCatching { + ): List? = runCatching { val response = serviceProvider.user.getDetails( auth = auth, personId = id, @@ -98,7 +98,7 @@ class UserRepository( ) val dto = response.body() ?: return@runCatching emptyList() dto.posts.map { it.toModel() } - }.getOrElse { emptyList() } + }.getOrNull() suspend fun getComments( id: Int, @@ -106,7 +106,7 @@ class UserRepository( page: Int, limit: Int = PostRepository.DEFAULT_PAGE_SIZE, sort: SortType = SortType.Active, - ): List = runCatching { + ): List? = runCatching { val response = serviceProvider.user.getDetails( auth = auth, personId = id, @@ -116,7 +116,7 @@ class UserRepository( ) val dto = response.body() ?: return@runCatching emptyList() dto.comments.map { it.toModel() } - }.getOrElse { emptyList() } + }.getOrNull() suspend fun getSavedComments( id: Int, @@ -124,7 +124,7 @@ class UserRepository( page: Int, limit: Int = PostRepository.DEFAULT_PAGE_SIZE, sort: SortType = SortType.Active, - ): List = runCatching { + ): List? = runCatching { val response = serviceProvider.user.getDetails( auth = auth, personId = id, @@ -135,7 +135,7 @@ class UserRepository( ) val dto = response.body() ?: return@runCatching emptyList() dto.comments.map { it.toModel() } - }.getOrElse { emptyList() } + }.getOrNull() suspend fun getMentions( auth: String? = null, @@ -143,7 +143,7 @@ class UserRepository( limit: Int = PostRepository.DEFAULT_PAGE_SIZE, sort: SortType = SortType.New, unreadOnly: Boolean = true, - ): List = runCatching { + ): List? = runCatching { val response = serviceProvider.user.getMentions( auth = auth, limit = limit, @@ -153,7 +153,7 @@ class UserRepository( ) val dto = response.body() ?: return@runCatching emptyList() dto.mentions.map { it.toModel() } - }.getOrElse { emptyList() } + }.getOrNull() suspend fun getReplies( auth: String? = null, @@ -161,7 +161,7 @@ class UserRepository( limit: Int = PostRepository.DEFAULT_PAGE_SIZE, sort: SortType = SortType.New, unreadOnly: Boolean = true, - ): List = runCatching { + ): List? = runCatching { val response = serviceProvider.user.getReplies( auth = auth, limit = limit, @@ -171,7 +171,7 @@ class UserRepository( ) val dto = response.body() ?: return@runCatching emptyList() dto.replies.map { it.toModel() } - }.getOrElse { emptyList() } + }.getOrNull() suspend fun readAll( auth: String? = null, diff --git a/feature-home/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/home/postlist/PostListViewModel.kt b/feature-home/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/home/postlist/PostListViewModel.kt index f1c3dbefe..65f30eb3a 100644 --- a/feature-home/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/home/postlist/PostListViewModel.kt +++ b/feature-home/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/home/postlist/PostListViewModel.kt @@ -160,12 +160,12 @@ class PostListViewModel( val sort = currentState.sortType ?: SortType.Active val refreshing = currentState.refreshing val includeNsfw = settingsRepository.currentSettings.value.includeNsfw - val postList = postRepository.getAll( + val itemList = postRepository.getAll( auth = auth, page = currentPage, type = type, sort = sort, - ).let { + )?.let { if (refreshing) { it } else { @@ -175,13 +175,14 @@ class PostListViewModel( } } } - currentPage++ - val canFetchMore = postList.size >= PostRepository.DEFAULT_PAGE_SIZE + if (!itemList.isNullOrEmpty()) { + currentPage++ + } mvi.updateState { val newPosts = if (refreshing) { - postList + itemList.orEmpty() } else { - it.posts + postList + it.posts + itemList.orEmpty() }.filter { post -> if (includeNsfw) { true @@ -192,7 +193,7 @@ class PostListViewModel( it.copy( posts = newPosts, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, ) } diff --git a/feature-inbox/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/inbox/mentions/InboxMentionsViewModel.kt b/feature-inbox/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/inbox/mentions/InboxMentionsViewModel.kt index c60812b17..7edc535b6 100644 --- a/feature-inbox/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/inbox/mentions/InboxMentionsViewModel.kt +++ b/feature-inbox/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/inbox/mentions/InboxMentionsViewModel.kt @@ -133,18 +133,19 @@ class InboxMentionsViewModel( unreadOnly = unreadOnly, sort = SortType.New, ) - currentPage++ - val canFetchMore = itemList.size >= CommentRepository.DEFAULT_PAGE_SIZE + if (!itemList.isNullOrEmpty()) { + currentPage++ + } mvi.updateState { val newItems = if (refreshing) { - itemList + itemList.orEmpty() } else { - it.mentions + itemList + it.mentions + itemList.orEmpty() } it.copy( mentions = newItems, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, initial = false, ) @@ -272,9 +273,9 @@ class InboxMentionsViewModel( val auth = identityRepository.authToken.value val unreadCount = if (!auth.isNullOrEmpty()) { val mentionCount = - userRepository.getMentions(auth, page = 1, limit = 50).count() + userRepository.getMentions(auth, page = 1, limit = 50).orEmpty().count() val replyCount = - userRepository.getReplies(auth, page = 1, limit = 50).count() + userRepository.getReplies(auth, page = 1, limit = 50).orEmpty().count() mentionCount + replyCount } else { 0 diff --git a/feature-inbox/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/inbox/messages/InboxMessagesViewModel.kt b/feature-inbox/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/inbox/messages/InboxMessagesViewModel.kt index b204f48f0..68ebfe0f3 100644 --- a/feature-inbox/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/inbox/messages/InboxMessagesViewModel.kt +++ b/feature-inbox/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/inbox/messages/InboxMessagesViewModel.kt @@ -6,7 +6,6 @@ import com.github.diegoberaldin.raccoonforlemmy.core.architecture.MviModel import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationCenter import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationCenterContractKeys import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository -import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.CommentRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.PrivateMessageRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.UserRepository @@ -103,26 +102,27 @@ class InboxMessagesViewModel( auth = auth, page = currentPage, unreadOnly = unreadOnly, - ).groupBy { + )?.groupBy { val creatorId = it.creator?.id ?: 0 val recipientId = it.recipient?.id ?: 0 listOf(creatorId, recipientId).sorted().toString() - }.mapNotNull { + }?.mapNotNull { val messages = it.value.sortedBy { m -> m.publishDate } messages.lastOrNull() } - currentPage++ - val canFetchMore = itemList.size >= CommentRepository.DEFAULT_PAGE_SIZE + if (!itemList.isNullOrEmpty()) { + currentPage++ + } mvi.updateState { val newItems = if (refreshing) { - itemList + itemList.orEmpty() } else { - it.chats + itemList + it.chats + itemList.orEmpty() } it.copy( chats = newItems, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, initial = false, ) @@ -135,9 +135,9 @@ class InboxMessagesViewModel( val auth = identityRepository.authToken.value val unreadCount = if (!auth.isNullOrEmpty()) { val mentionCount = - userRepository.getMentions(auth, page = 1, limit = 50).count() + userRepository.getMentions(auth, page = 1, limit = 50).orEmpty().count() val replyCount = - userRepository.getReplies(auth, page = 1, limit = 50).count() + userRepository.getReplies(auth, page = 1, limit = 50).orEmpty().count() mentionCount + replyCount } else { 0 diff --git a/feature-inbox/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/inbox/replies/InboxRepliesViewModel.kt b/feature-inbox/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/inbox/replies/InboxRepliesViewModel.kt index f6662145f..f037a1212 100644 --- a/feature-inbox/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/inbox/replies/InboxRepliesViewModel.kt +++ b/feature-inbox/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/inbox/replies/InboxRepliesViewModel.kt @@ -142,23 +142,22 @@ class InboxRepliesViewModel( page = currentPage, unreadOnly = unreadOnly, sort = SortType.New, - ).map { + )?.map { val isOwnPost = it.post.creator?.id == currentUserId it.copy(isOwnPost = isOwnPost) } currentPage++ - val canFetchMore = itemList.size >= CommentRepository.DEFAULT_PAGE_SIZE mvi.updateState { val newItems = if (refreshing) { - itemList + itemList.orEmpty() } else { - it.replies + itemList + it.replies + itemList.orEmpty() } it.copy( replies = newItems, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, initial = false, ) @@ -285,9 +284,9 @@ class InboxRepliesViewModel( val auth = identityRepository.authToken.value val unreadCount = if (!auth.isNullOrEmpty()) { val mentionCount = - userRepository.getMentions(auth, page = 1, limit = 50).count() + userRepository.getMentions(auth, page = 1, limit = 50).orEmpty().count() val replyCount = - userRepository.getReplies(auth, page = 1, limit = 50).count() + userRepository.getReplies(auth, page = 1, limit = 50).orEmpty().count() mentionCount + replyCount } else { 0 diff --git a/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/logged/ProfileLoggedViewModel.kt b/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/logged/ProfileLoggedViewModel.kt index 288c7d9bc..3ff33708f 100644 --- a/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/logged/ProfileLoggedViewModel.kt +++ b/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/logged/ProfileLoggedViewModel.kt @@ -174,7 +174,7 @@ class ProfileLoggedViewModel( val userId = currentState.user.id val section = currentState.section if (section == ProfileLoggedSection.Posts) { - val postList = userRepository.getPosts( + val itemList = userRepository.getPosts( auth = auth, id = userId, page = currentPage, @@ -188,43 +188,41 @@ class ProfileLoggedViewModel( id = userId, page = currentPage, sort = SortType.New, - ) + ).orEmpty() } else { currentState.comments } - val canFetchMore = postList.size >= PostRepository.DEFAULT_PAGE_SIZE mvi.updateState { val newPosts = if (refreshing) { - postList + itemList.orEmpty() } else { - it.posts + postList + it.posts + itemList.orEmpty() } it.copy( posts = newPosts, comments = comments, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, ) } } else { - val commentList = userRepository.getComments( + val itemList = userRepository.getComments( auth = auth, id = userId, page = currentPage, sort = SortType.New, ) - val canFetchMore = commentList.size >= PostRepository.DEFAULT_PAGE_SIZE mvi.updateState { val newcomments = if (refreshing) { - commentList + itemList.orEmpty() } else { - it.comments + commentList + it.comments + itemList.orEmpty() } it.copy( comments = newcomments, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, ) } diff --git a/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/login/LoginBottomSheetViewModel.kt b/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/login/LoginBottomSheetViewModel.kt index a8eceef31..55d1c2048 100644 --- a/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/login/LoginBottomSheetViewModel.kt +++ b/feature-profile/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/profile/login/LoginBottomSheetViewModel.kt @@ -105,13 +105,11 @@ class LoginBottomSheetViewModel( mvi.scope?.launch(Dispatchers.IO) { mvi.updateState { it.copy(loading = true) } - val res = runCatching { - communityRepository.getAllInInstance( - instance = instance, - page = 1, - limit = 1 - ) - }.getOrElse { emptyList() } + val res = communityRepository.getAllInInstance( + instance = instance, + page = 1, + limit = 1 + ) ?: emptyList() if (res.isEmpty()) { mvi.updateState { it.copy( diff --git a/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/main/ExploreViewModel.kt b/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/main/ExploreViewModel.kt index 1f93e1a1e..9264db977 100644 --- a/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/main/ExploreViewModel.kt +++ b/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/main/ExploreViewModel.kt @@ -199,7 +199,7 @@ class ExploreViewModel( val sortType = currentState.sortType val resultType = currentState.resultType val settings = settingsRepository.currentSettings.value - val items = communityRepository.getAll( + val itemList = communityRepository.getAll( query = searchText, auth = auth, resultType = resultType, @@ -208,12 +208,11 @@ class ExploreViewModel( sortType = sortType, ) currentPage++ - val canFetchMore = items.size >= PostRepository.DEFAULT_PAGE_SIZE mvi.updateState { val newItems = if (refreshing) { - items + itemList.orEmpty() } else { - it.results + items + it.results + itemList.orEmpty() }.filter { community -> if (settings.includeNsfw) { true @@ -224,7 +223,7 @@ class ExploreViewModel( it.copy( results = newItems, loading = false, - canFetchMore = canFetchMore, + canFetchMore = itemList?.isEmpty() != true, refreshing = false, ) } diff --git a/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/multicommunity/utils/CommunityPaginator.kt b/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/multicommunity/utils/CommunityPaginator.kt index c9c63ad6f..d8de1f54d 100644 --- a/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/multicommunity/utils/CommunityPaginator.kt +++ b/feature-search/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/search/multicommunity/utils/CommunityPaginator.kt @@ -30,7 +30,7 @@ internal class CommunityPaginator( sort = sort, communityId = communityId, ) - canFetchMore = result.size >= PostRepository.DEFAULT_PAGE_SIZE - return result + canFetchMore = result?.isEmpty() != true + return result.orEmpty() } } diff --git a/shared/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/MainViewModel.kt b/shared/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/MainViewModel.kt index 89d7914bd..2ea6341fe 100644 --- a/shared/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/MainViewModel.kt +++ b/shared/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/MainViewModel.kt @@ -28,9 +28,9 @@ class MainViewModel( val unreadCount = if (logged == true) { val auth = identityRepository.authToken.value val mentionCount = - userRepository.getMentions(auth, page = 1, limit = 50).count() + userRepository.getMentions(auth, page = 1, limit = 50).orEmpty().count() val replyCount = - userRepository.getReplies(auth, page = 1, limit = 50).count() + userRepository.getReplies(auth, page = 1, limit = 50).orEmpty().count() mentionCount + replyCount } else { 0