fix: pagination issues in case of network or API error

This commit is contained in:
Diego Beraldin 2023-10-07 23:18:04 +02:00
parent e05522e44d
commit 647916fe6c
21 changed files with 139 additions and 143 deletions

View File

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

View File

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

View File

@ -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(

View File

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

View File

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

View File

@ -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++
}
}

View File

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

View File

@ -29,7 +29,7 @@ class CommentRepository(
type: ListingType = ListingType.All,
sort: SortType = SortType.New,
maxDepth: Int = 1,
): List<CommentModel> = runCatching {
): List<CommentModel>? = 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<CommentModel> = runCatching {
): List<CommentModel>? = 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,

View File

@ -29,7 +29,7 @@ class CommunityRepository(
listingType: ListingType = ListingType.All,
sortType: SortType = SortType.Active,
resultType: SearchResultType = SearchResultType.All,
): List<Any> = runCatching {
): List<Any>? = 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<CommunityModel> = runCatching {
): List<CommunityModel>? = 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,

View File

@ -32,7 +32,7 @@ class PostRepository(
type: ListingType = ListingType.Local,
sort: SortType = SortType.Active,
communityId: Int? = null,
): List<PostModel> = runCatching {
): List<PostModel>? = 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<PostModel> = runCatching {
): List<PostModel>? = 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

View File

@ -14,7 +14,7 @@ class PrivateMessageRepository(
page: Int,
limit: Int = PostRepository.DEFAULT_PAGE_SIZE,
unreadOnly: Boolean = true,
): List<PrivateMessageModel> = runCatching {
): List<PrivateMessageModel>? = 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,

View File

@ -69,7 +69,7 @@ class UserRepository(
page: Int,
limit: Int = PostRepository.DEFAULT_PAGE_SIZE,
sort: SortType = SortType.Active,
): List<PostModel> = runCatching {
): List<PostModel>? = 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<PostModel> = runCatching {
): List<PostModel>? = 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<CommentModel> = runCatching {
): List<CommentModel>? = 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<CommentModel> = runCatching {
): List<CommentModel>? = 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<PersonMentionModel> = runCatching {
): List<PersonMentionModel>? = 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<PersonMentionModel> = runCatching {
): List<PersonMentionModel>? = 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,

View File

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

View File

@ -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

View File

@ -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

View File

@ -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

View File

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

View File

@ -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(

View File

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

View File

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

View File

@ -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