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.NotificationCenter
import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationCenterContractKeys import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationCenterContractKeys
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository 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.PrivateMessageRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.UserRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.UserRepository
@ -93,28 +92,29 @@ class InboxChatViewModel(
auth = auth, auth = auth,
page = currentPage, page = currentPage,
unreadOnly = false, unreadOnly = false,
).filter { )?.filter {
it.creator?.id == otherUserId || it.recipient?.id == otherUserId it.creator?.id == otherUserId || it.recipient?.id == otherUserId
}.onEach { }?.onEach {
if (!it.read) { if (!it.read) {
launch { launch {
markAsRead(true, it.id) markAsRead(true, it.id)
} }
} }
} }
currentPage++ if (!itemList.isNullOrEmpty()) {
val canFetchMore = itemList.size >= CommentRepository.DEFAULT_PAGE_SIZE currentPage++
}
mvi.updateState { mvi.updateState {
val newItems = if (refreshing) { val newItems = if (refreshing) {
itemList itemList.orEmpty()
} else { } else {
it.messages + itemList it.messages + itemList.orEmpty()
} }
it.copy( it.copy(
messages = newItems, messages = newItems,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, 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.SortType
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.shareUrl 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.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.CommunityRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.PostRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.PostRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository
@ -168,18 +167,19 @@ class CommunityDetailViewModel(
sort = sort, sort = sort,
) )
} }
currentPage++ if (!itemList.isNullOrEmpty()) {
val canFetchMore = itemList.size >= CommentRepository.DEFAULT_PAGE_SIZE currentPage++
}
mvi.updateState { mvi.updateState {
val newItems = if (refreshing) { val newItems = if (refreshing) {
itemList itemList.orEmpty()
} else { } else {
it.posts + itemList it.posts + itemList.orEmpty()
} }
it.copy( it.copy(
posts = newItems, posts = newItems,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, refreshing = false,
) )
} }

View File

@ -98,13 +98,11 @@ class ModalDrawerViewModel(
mvi.scope?.launch(Dispatchers.IO) { mvi.scope?.launch(Dispatchers.IO) {
mvi.updateState { it.copy(changeInstanceloading = true) } mvi.updateState { it.copy(changeInstanceloading = true) }
val res = runCatching { val res = communityRepository.getAllInInstance(
communityRepository.getAllInInstance( instance = instanceName,
instance = instanceName, page = 1,
page = 1, limit = 1
limit = 1 ) ?: emptyList()
)
}.getOrElse { emptyList() }
if (res.isEmpty()) { if (res.isEmpty()) {
mvi.updateState { mvi.updateState {
it.copy( 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.DefaultMviModel
import com.github.diegoberaldin.raccoonforlemmy.core.architecture.MviModel import com.github.diegoberaldin.raccoonforlemmy.core.architecture.MviModel
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository 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.CommunityRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -72,18 +71,19 @@ class InstanceInfoViewModel(
page = currentPage, page = currentPage,
limit = 50, limit = 50,
) )
currentPage++ if (!itemList.isNullOrEmpty()) {
val canFetchMore = itemList.size >= CommentRepository.DEFAULT_PAGE_SIZE currentPage++
}
mvi.updateState { mvi.updateState {
val newItems = if (refreshing) { val newItems = if (refreshing) {
itemList.filter { e -> e.instanceUrl == url } itemList?.filter { e -> e.instanceUrl == url }.orEmpty()
} else { } else {
it.communities + itemList.filter { e -> e.instanceUrl == url } it.communities + itemList?.filter { e -> e.instanceUrl == url }.orEmpty()
} }
it.copy( it.copy(
communities = newItems, communities = newItems,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, refreshing = false,
) )
} }

View File

@ -225,13 +225,13 @@ class PostDetailViewModel(
val auth = identityRepository.authToken.value val auth = identityRepository.authToken.value
val refreshing = currentState.refreshing val refreshing = currentState.refreshing
val sort = currentState.sortType val sort = currentState.sortType
val commentList = commentRepository.getAll( val itemList = commentRepository.getAll(
auth = auth, auth = auth,
postId = post.id, postId = post.id,
page = currentPage, page = currentPage,
sort = sort, sort = sort,
maxDepth = CommentRepository.MAX_COMMENT_DEPTH, maxDepth = CommentRepository.MAX_COMMENT_DEPTH,
).let { )?.let {
processCommentsToGetNestedOrder( processCommentsToGetNestedOrder(
items = it, items = it,
) )
@ -239,24 +239,25 @@ class PostDetailViewModel(
if (refreshing) { if (refreshing) {
it it
} else { } else {
it.filter { c1 -> it?.filter { c1 ->
// prevents accidental duplication // prevents accidental duplication
currentState.comments.none { c2 -> c1.id == c2.id } currentState.comments.none { c2 -> c1.id == c2.id }
} }
} }
} }
currentPage++ if (!itemList.isNullOrEmpty()) {
val canFetchMore = commentList.size >= CommentRepository.DEFAULT_PAGE_SIZE currentPage++
}
mvi.updateState { mvi.updateState {
val newcomments = if (refreshing) { val newcomments = if (refreshing) {
commentList itemList.orEmpty()
} else { } else {
it.comments + commentList it.comments + itemList.orEmpty()
} }
it.copy( it.copy(
comments = newcomments, comments = newcomments,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, refreshing = false,
initial = false, initial = false,
) )
@ -288,7 +289,7 @@ class PostDetailViewModel(
parentId = parentId, parentId = parentId,
sort = sort, sort = sort,
maxDepth = CommentRepository.MAX_COMMENT_DEPTH, maxDepth = CommentRepository.MAX_COMMENT_DEPTH,
).let { )?.let {
processCommentsToGetNestedOrder( processCommentsToGetNestedOrder(
items = it, items = it,
ancestorId = parentId.toString(), ancestorId = parentId.toString(),
@ -297,7 +298,7 @@ class PostDetailViewModel(
val newList = uiState.value.comments.let { list -> val newList = uiState.value.comments.let { list ->
val index = list.indexOfFirst { c -> c.id == parentId } val index = list.indexOfFirst { c -> c.id == parentId }
list.toMutableList().apply { list.toMutableList().apply {
addAll(index + 1, fetchResult) addAll(index + 1, fetchResult.orEmpty())
}.toList() }.toList()
} }
mvi.updateState { it.copy(comments = newList) } mvi.updateState { it.copy(comments = newList) }

View File

@ -134,49 +134,52 @@ class SavedItemsViewModel(
val section = currentState.section val section = currentState.section
val sortType = currentState.sortType val sortType = currentState.sortType
if (section == SavedItemsSection.Posts) { if (section == SavedItemsSection.Posts) {
val postList = userRepository.getSavedPosts( val itemList = userRepository.getSavedPosts(
auth = auth, auth = auth,
id = user.id, id = user.id,
page = currentPage, page = currentPage,
sort = sortType, sort = sortType,
) )
val canFetchMore = postList.size >= PostRepository.DEFAULT_PAGE_SIZE if (!itemList.isNullOrEmpty()) {
currentPage++
}
mvi.updateState { mvi.updateState {
val newPosts = if (refreshing) { val newPosts = if (refreshing) {
postList itemList.orEmpty()
} else { } else {
it.posts + postList it.posts + itemList.orEmpty()
} }
it.copy( it.copy(
posts = newPosts, posts = newPosts,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, refreshing = false,
) )
} }
} else { } else {
val commentList = userRepository.getSavedComments( val itemList = userRepository.getSavedComments(
auth = auth, auth = auth,
id = user.id, id = user.id,
page = currentPage, page = currentPage,
sort = sortType, sort = sortType,
) )
val canFetchMore = commentList.size >= PostRepository.DEFAULT_PAGE_SIZE if (!itemList.isNullOrEmpty()) {
currentPage++
}
mvi.updateState { mvi.updateState {
val newComments = if (refreshing) { val newComments = if (refreshing) {
commentList itemList.orEmpty()
} else { } else {
it.comments + commentList it.comments + itemList.orEmpty()
} }
it.copy( it.copy(
comments = newComments, comments = newComments,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, refreshing = false,
) )
} }
} }
currentPage++
} }
} }

View File

@ -186,7 +186,7 @@ class UserDetailViewModel(
val section = currentState.section val section = currentState.section
val userId = currentState.user.id val userId = currentState.user.id
if (section == UserDetailSection.Posts) { if (section == UserDetailSection.Posts) {
val postList = userRepository.getPosts( val itemList = userRepository.getPosts(
auth = auth, auth = auth,
id = userId, id = userId,
page = currentPage, page = currentPage,
@ -200,44 +200,42 @@ class UserDetailViewModel(
id = userId, id = userId,
page = currentPage, page = currentPage,
sort = currentState.sortType, sort = currentState.sortType,
) ).orEmpty()
} else { } else {
currentState.comments currentState.comments
} }
val canFetchMore = postList.size >= PostRepository.DEFAULT_PAGE_SIZE
mvi.updateState { mvi.updateState {
val newPosts = if (refreshing) { val newPosts = if (refreshing) {
postList itemList.orEmpty()
} else { } else {
it.posts + postList it.posts + itemList.orEmpty()
} }
it.copy( it.copy(
posts = newPosts, posts = newPosts,
comments = comments, comments = comments,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, refreshing = false,
) )
} }
} else { } else {
val commentList = userRepository.getComments( val itemList = userRepository.getComments(
auth = auth, auth = auth,
id = userId, id = userId,
page = currentPage, page = currentPage,
sort = currentState.sortType, sort = currentState.sortType,
) )
val canFetchMore = commentList.size >= PostRepository.DEFAULT_PAGE_SIZE
mvi.updateState { mvi.updateState {
val newcomments = if (refreshing) { val newcomments = if (refreshing) {
commentList itemList.orEmpty()
} else { } else {
it.comments + commentList it.comments + itemList.orEmpty()
} }
it.copy( it.copy(
comments = newcomments, comments = newcomments,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, refreshing = false,
) )
} }

View File

@ -29,7 +29,7 @@ class CommentRepository(
type: ListingType = ListingType.All, type: ListingType = ListingType.All,
sort: SortType = SortType.New, sort: SortType = SortType.New,
maxDepth: Int = 1, maxDepth: Int = 1,
): List<CommentModel> = runCatching { ): List<CommentModel>? = runCatching {
val response = services.comment.getAll( val response = services.comment.getAll(
auth = auth, auth = auth,
postId = postId, postId = postId,
@ -41,7 +41,7 @@ class CommentRepository(
) )
val dto = response.body()?.comments ?: emptyList() val dto = response.body()?.comments ?: emptyList()
dto.map { it.toModel() } dto.map { it.toModel() }
}.getOrElse { emptyList() } }.getOrNull()
suspend fun getBy(id: Int, auth: String?): CommentModel? = runCatching { suspend fun getBy(id: Int, auth: String?): CommentModel? = runCatching {
services.comment.getBy(id, auth).body()?.commentView?.toModel() services.comment.getBy(id, auth).body()?.commentView?.toModel()
@ -54,7 +54,7 @@ class CommentRepository(
type: ListingType = ListingType.All, type: ListingType = ListingType.All,
sort: SortType = SortType.New, sort: SortType = SortType.New,
maxDepth: Int = 1, maxDepth: Int = 1,
): List<CommentModel> = runCatching { ): List<CommentModel>? = runCatching {
val response = services.comment.getAll( val response = services.comment.getAll(
auth = auth, auth = auth,
parentId = parentId, parentId = parentId,
@ -65,7 +65,7 @@ class CommentRepository(
) )
val dto = response.body()?.comments ?: emptyList() val dto = response.body()?.comments ?: emptyList()
dto.map { it.toModel() } dto.map { it.toModel() }
}.getOrElse { emptyList() } }.getOrNull()
fun asUpVoted(comment: CommentModel, voted: Boolean) = comment.copy( fun asUpVoted(comment: CommentModel, voted: Boolean) = comment.copy(
myVote = if (voted) 1 else 0, myVote = if (voted) 1 else 0,

View File

@ -29,7 +29,7 @@ class CommunityRepository(
listingType: ListingType = ListingType.All, listingType: ListingType = ListingType.All,
sortType: SortType = SortType.Active, sortType: SortType = SortType.Active,
resultType: SearchResultType = SearchResultType.All, resultType: SearchResultType = SearchResultType.All,
): List<Any> = runCatching { ): List<Any>? = runCatching {
val response = services.search.search( val response = services.search.search(
q = query, q = query,
auth = auth, auth = auth,
@ -47,7 +47,7 @@ class CommunityRepository(
// returns everything // returns everything
posts + comments + communities + users posts + comments + communities + users
}.getOrElse { emptyList() } }.getOrNull()
suspend fun getAllInInstance( suspend fun getAllInInstance(
instance: String = "", instance: String = "",
@ -55,7 +55,7 @@ class CommunityRepository(
page: Int, page: Int,
limit: Int = DEFAULT_PAGE_SIZE, limit: Int = DEFAULT_PAGE_SIZE,
sort: SortType = SortType.Active, sort: SortType = SortType.Active,
): List<CommunityModel> = runCatching { ): List<CommunityModel>? = runCatching {
customServices.changeInstance(instance) customServices.changeInstance(instance)
val response = customServices.community.getAll( val response = customServices.community.getAll(
auth = auth, auth = auth,
@ -66,7 +66,7 @@ class CommunityRepository(
response?.communities?.map { response?.communities?.map {
it.toModel() it.toModel()
}.orEmpty() }.orEmpty()
}.getOrElse { emptyList() } }.getOrNull()
suspend fun getSubscribed( suspend fun getSubscribed(
auth: String? = null, auth: String? = null,

View File

@ -32,7 +32,7 @@ class PostRepository(
type: ListingType = ListingType.Local, type: ListingType = ListingType.Local,
sort: SortType = SortType.Active, sort: SortType = SortType.Active,
communityId: Int? = null, communityId: Int? = null,
): List<PostModel> = runCatching { ): List<PostModel>? = runCatching {
val response = services.post.getAll( val response = services.post.getAll(
auth = auth, auth = auth,
communityId = communityId, communityId = communityId,
@ -43,7 +43,7 @@ class PostRepository(
) )
val dto = response.body()?.posts ?: emptyList() val dto = response.body()?.posts ?: emptyList()
dto.map { it.toModel() } dto.map { it.toModel() }
}.getOrElse { emptyList() } }.getOrNull()
suspend fun getAllInInstance( suspend fun getAllInInstance(
instance: String, instance: String,
@ -52,7 +52,7 @@ class PostRepository(
type: ListingType = ListingType.Local, type: ListingType = ListingType.Local,
sort: SortType = SortType.Active, sort: SortType = SortType.Active,
communityId: Int? = null, communityId: Int? = null,
): List<PostModel> = runCatching { ): List<PostModel>? = runCatching {
customServices.changeInstance(instance) customServices.changeInstance(instance)
val response = customServices.post.getAll( val response = customServices.post.getAll(
communityId = communityId, communityId = communityId,
@ -63,7 +63,7 @@ class PostRepository(
) )
val dto = response.body()?.posts ?: emptyList() val dto = response.body()?.posts ?: emptyList()
dto.map { it.toModel() } dto.map { it.toModel() }
}.getOrElse { emptyList() } }.getOrNull()
suspend fun get(id: Int, auth: String? = null): PostModel? = runCatching { suspend fun get(id: Int, auth: String? = null): PostModel? = runCatching {
val dto = services.post.get(auth, id).body()?.postView val dto = services.post.get(auth, id).body()?.postView

View File

@ -14,7 +14,7 @@ class PrivateMessageRepository(
page: Int, page: Int,
limit: Int = PostRepository.DEFAULT_PAGE_SIZE, limit: Int = PostRepository.DEFAULT_PAGE_SIZE,
unreadOnly: Boolean = true, unreadOnly: Boolean = true,
): List<PrivateMessageModel> = runCatching { ): List<PrivateMessageModel>? = runCatching {
val response = serviceProvider.privateMessages.getPrivateMessages( val response = serviceProvider.privateMessages.getPrivateMessages(
auth = auth, auth = auth,
limit = limit, limit = limit,
@ -23,7 +23,7 @@ class PrivateMessageRepository(
) )
val dto = response.body() ?: return@runCatching emptyList() val dto = response.body() ?: return@runCatching emptyList()
dto.privateMessages.map { it.toModel() } dto.privateMessages.map { it.toModel() }
}.getOrElse { emptyList() } }.getOrNull()
suspend fun create( suspend fun create(
message: String, message: String,

View File

@ -69,7 +69,7 @@ class UserRepository(
page: Int, page: Int,
limit: Int = PostRepository.DEFAULT_PAGE_SIZE, limit: Int = PostRepository.DEFAULT_PAGE_SIZE,
sort: SortType = SortType.Active, sort: SortType = SortType.Active,
): List<PostModel> = runCatching { ): List<PostModel>? = runCatching {
val response = serviceProvider.user.getDetails( val response = serviceProvider.user.getDetails(
auth = auth, auth = auth,
personId = id, personId = id,
@ -79,7 +79,7 @@ class UserRepository(
) )
val dto = response.body() ?: return@runCatching emptyList() val dto = response.body() ?: return@runCatching emptyList()
dto.posts.map { it.toModel() } dto.posts.map { it.toModel() }
}.getOrElse { emptyList() } }.getOrNull()
suspend fun getSavedPosts( suspend fun getSavedPosts(
id: Int, id: Int,
@ -87,7 +87,7 @@ class UserRepository(
page: Int, page: Int,
limit: Int = PostRepository.DEFAULT_PAGE_SIZE, limit: Int = PostRepository.DEFAULT_PAGE_SIZE,
sort: SortType = SortType.Active, sort: SortType = SortType.Active,
): List<PostModel> = runCatching { ): List<PostModel>? = runCatching {
val response = serviceProvider.user.getDetails( val response = serviceProvider.user.getDetails(
auth = auth, auth = auth,
personId = id, personId = id,
@ -98,7 +98,7 @@ class UserRepository(
) )
val dto = response.body() ?: return@runCatching emptyList() val dto = response.body() ?: return@runCatching emptyList()
dto.posts.map { it.toModel() } dto.posts.map { it.toModel() }
}.getOrElse { emptyList() } }.getOrNull()
suspend fun getComments( suspend fun getComments(
id: Int, id: Int,
@ -106,7 +106,7 @@ class UserRepository(
page: Int, page: Int,
limit: Int = PostRepository.DEFAULT_PAGE_SIZE, limit: Int = PostRepository.DEFAULT_PAGE_SIZE,
sort: SortType = SortType.Active, sort: SortType = SortType.Active,
): List<CommentModel> = runCatching { ): List<CommentModel>? = runCatching {
val response = serviceProvider.user.getDetails( val response = serviceProvider.user.getDetails(
auth = auth, auth = auth,
personId = id, personId = id,
@ -116,7 +116,7 @@ class UserRepository(
) )
val dto = response.body() ?: return@runCatching emptyList() val dto = response.body() ?: return@runCatching emptyList()
dto.comments.map { it.toModel() } dto.comments.map { it.toModel() }
}.getOrElse { emptyList() } }.getOrNull()
suspend fun getSavedComments( suspend fun getSavedComments(
id: Int, id: Int,
@ -124,7 +124,7 @@ class UserRepository(
page: Int, page: Int,
limit: Int = PostRepository.DEFAULT_PAGE_SIZE, limit: Int = PostRepository.DEFAULT_PAGE_SIZE,
sort: SortType = SortType.Active, sort: SortType = SortType.Active,
): List<CommentModel> = runCatching { ): List<CommentModel>? = runCatching {
val response = serviceProvider.user.getDetails( val response = serviceProvider.user.getDetails(
auth = auth, auth = auth,
personId = id, personId = id,
@ -135,7 +135,7 @@ class UserRepository(
) )
val dto = response.body() ?: return@runCatching emptyList() val dto = response.body() ?: return@runCatching emptyList()
dto.comments.map { it.toModel() } dto.comments.map { it.toModel() }
}.getOrElse { emptyList() } }.getOrNull()
suspend fun getMentions( suspend fun getMentions(
auth: String? = null, auth: String? = null,
@ -143,7 +143,7 @@ class UserRepository(
limit: Int = PostRepository.DEFAULT_PAGE_SIZE, limit: Int = PostRepository.DEFAULT_PAGE_SIZE,
sort: SortType = SortType.New, sort: SortType = SortType.New,
unreadOnly: Boolean = true, unreadOnly: Boolean = true,
): List<PersonMentionModel> = runCatching { ): List<PersonMentionModel>? = runCatching {
val response = serviceProvider.user.getMentions( val response = serviceProvider.user.getMentions(
auth = auth, auth = auth,
limit = limit, limit = limit,
@ -153,7 +153,7 @@ class UserRepository(
) )
val dto = response.body() ?: return@runCatching emptyList() val dto = response.body() ?: return@runCatching emptyList()
dto.mentions.map { it.toModel() } dto.mentions.map { it.toModel() }
}.getOrElse { emptyList() } }.getOrNull()
suspend fun getReplies( suspend fun getReplies(
auth: String? = null, auth: String? = null,
@ -161,7 +161,7 @@ class UserRepository(
limit: Int = PostRepository.DEFAULT_PAGE_SIZE, limit: Int = PostRepository.DEFAULT_PAGE_SIZE,
sort: SortType = SortType.New, sort: SortType = SortType.New,
unreadOnly: Boolean = true, unreadOnly: Boolean = true,
): List<PersonMentionModel> = runCatching { ): List<PersonMentionModel>? = runCatching {
val response = serviceProvider.user.getReplies( val response = serviceProvider.user.getReplies(
auth = auth, auth = auth,
limit = limit, limit = limit,
@ -171,7 +171,7 @@ class UserRepository(
) )
val dto = response.body() ?: return@runCatching emptyList() val dto = response.body() ?: return@runCatching emptyList()
dto.replies.map { it.toModel() } dto.replies.map { it.toModel() }
}.getOrElse { emptyList() } }.getOrNull()
suspend fun readAll( suspend fun readAll(
auth: String? = null, auth: String? = null,

View File

@ -160,12 +160,12 @@ class PostListViewModel(
val sort = currentState.sortType ?: SortType.Active val sort = currentState.sortType ?: SortType.Active
val refreshing = currentState.refreshing val refreshing = currentState.refreshing
val includeNsfw = settingsRepository.currentSettings.value.includeNsfw val includeNsfw = settingsRepository.currentSettings.value.includeNsfw
val postList = postRepository.getAll( val itemList = postRepository.getAll(
auth = auth, auth = auth,
page = currentPage, page = currentPage,
type = type, type = type,
sort = sort, sort = sort,
).let { )?.let {
if (refreshing) { if (refreshing) {
it it
} else { } else {
@ -175,13 +175,14 @@ class PostListViewModel(
} }
} }
} }
currentPage++ if (!itemList.isNullOrEmpty()) {
val canFetchMore = postList.size >= PostRepository.DEFAULT_PAGE_SIZE currentPage++
}
mvi.updateState { mvi.updateState {
val newPosts = if (refreshing) { val newPosts = if (refreshing) {
postList itemList.orEmpty()
} else { } else {
it.posts + postList it.posts + itemList.orEmpty()
}.filter { post -> }.filter { post ->
if (includeNsfw) { if (includeNsfw) {
true true
@ -192,7 +193,7 @@ class PostListViewModel(
it.copy( it.copy(
posts = newPosts, posts = newPosts,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, refreshing = false,
) )
} }

View File

@ -133,18 +133,19 @@ class InboxMentionsViewModel(
unreadOnly = unreadOnly, unreadOnly = unreadOnly,
sort = SortType.New, sort = SortType.New,
) )
currentPage++ if (!itemList.isNullOrEmpty()) {
val canFetchMore = itemList.size >= CommentRepository.DEFAULT_PAGE_SIZE currentPage++
}
mvi.updateState { mvi.updateState {
val newItems = if (refreshing) { val newItems = if (refreshing) {
itemList itemList.orEmpty()
} else { } else {
it.mentions + itemList it.mentions + itemList.orEmpty()
} }
it.copy( it.copy(
mentions = newItems, mentions = newItems,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, refreshing = false,
initial = false, initial = false,
) )
@ -272,9 +273,9 @@ class InboxMentionsViewModel(
val auth = identityRepository.authToken.value val auth = identityRepository.authToken.value
val unreadCount = if (!auth.isNullOrEmpty()) { val unreadCount = if (!auth.isNullOrEmpty()) {
val mentionCount = val mentionCount =
userRepository.getMentions(auth, page = 1, limit = 50).count() userRepository.getMentions(auth, page = 1, limit = 50).orEmpty().count()
val replyCount = val replyCount =
userRepository.getReplies(auth, page = 1, limit = 50).count() userRepository.getReplies(auth, page = 1, limit = 50).orEmpty().count()
mentionCount + replyCount mentionCount + replyCount
} else { } else {
0 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.NotificationCenter
import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationCenterContractKeys import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationCenterContractKeys
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository 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.PrivateMessageRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.UserRepository import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.UserRepository
@ -103,26 +102,27 @@ class InboxMessagesViewModel(
auth = auth, auth = auth,
page = currentPage, page = currentPage,
unreadOnly = unreadOnly, unreadOnly = unreadOnly,
).groupBy { )?.groupBy {
val creatorId = it.creator?.id ?: 0 val creatorId = it.creator?.id ?: 0
val recipientId = it.recipient?.id ?: 0 val recipientId = it.recipient?.id ?: 0
listOf(creatorId, recipientId).sorted().toString() listOf(creatorId, recipientId).sorted().toString()
}.mapNotNull { }?.mapNotNull {
val messages = it.value.sortedBy { m -> m.publishDate } val messages = it.value.sortedBy { m -> m.publishDate }
messages.lastOrNull() messages.lastOrNull()
} }
currentPage++ if (!itemList.isNullOrEmpty()) {
val canFetchMore = itemList.size >= CommentRepository.DEFAULT_PAGE_SIZE currentPage++
}
mvi.updateState { mvi.updateState {
val newItems = if (refreshing) { val newItems = if (refreshing) {
itemList itemList.orEmpty()
} else { } else {
it.chats + itemList it.chats + itemList.orEmpty()
} }
it.copy( it.copy(
chats = newItems, chats = newItems,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, refreshing = false,
initial = false, initial = false,
) )
@ -135,9 +135,9 @@ class InboxMessagesViewModel(
val auth = identityRepository.authToken.value val auth = identityRepository.authToken.value
val unreadCount = if (!auth.isNullOrEmpty()) { val unreadCount = if (!auth.isNullOrEmpty()) {
val mentionCount = val mentionCount =
userRepository.getMentions(auth, page = 1, limit = 50).count() userRepository.getMentions(auth, page = 1, limit = 50).orEmpty().count()
val replyCount = val replyCount =
userRepository.getReplies(auth, page = 1, limit = 50).count() userRepository.getReplies(auth, page = 1, limit = 50).orEmpty().count()
mentionCount + replyCount mentionCount + replyCount
} else { } else {
0 0

View File

@ -142,23 +142,22 @@ class InboxRepliesViewModel(
page = currentPage, page = currentPage,
unreadOnly = unreadOnly, unreadOnly = unreadOnly,
sort = SortType.New, sort = SortType.New,
).map { )?.map {
val isOwnPost = it.post.creator?.id == currentUserId val isOwnPost = it.post.creator?.id == currentUserId
it.copy(isOwnPost = isOwnPost) it.copy(isOwnPost = isOwnPost)
} }
currentPage++ currentPage++
val canFetchMore = itemList.size >= CommentRepository.DEFAULT_PAGE_SIZE
mvi.updateState { mvi.updateState {
val newItems = if (refreshing) { val newItems = if (refreshing) {
itemList itemList.orEmpty()
} else { } else {
it.replies + itemList it.replies + itemList.orEmpty()
} }
it.copy( it.copy(
replies = newItems, replies = newItems,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, refreshing = false,
initial = false, initial = false,
) )
@ -285,9 +284,9 @@ class InboxRepliesViewModel(
val auth = identityRepository.authToken.value val auth = identityRepository.authToken.value
val unreadCount = if (!auth.isNullOrEmpty()) { val unreadCount = if (!auth.isNullOrEmpty()) {
val mentionCount = val mentionCount =
userRepository.getMentions(auth, page = 1, limit = 50).count() userRepository.getMentions(auth, page = 1, limit = 50).orEmpty().count()
val replyCount = val replyCount =
userRepository.getReplies(auth, page = 1, limit = 50).count() userRepository.getReplies(auth, page = 1, limit = 50).orEmpty().count()
mentionCount + replyCount mentionCount + replyCount
} else { } else {
0 0

View File

@ -174,7 +174,7 @@ class ProfileLoggedViewModel(
val userId = currentState.user.id val userId = currentState.user.id
val section = currentState.section val section = currentState.section
if (section == ProfileLoggedSection.Posts) { if (section == ProfileLoggedSection.Posts) {
val postList = userRepository.getPosts( val itemList = userRepository.getPosts(
auth = auth, auth = auth,
id = userId, id = userId,
page = currentPage, page = currentPage,
@ -188,43 +188,41 @@ class ProfileLoggedViewModel(
id = userId, id = userId,
page = currentPage, page = currentPage,
sort = SortType.New, sort = SortType.New,
) ).orEmpty()
} else { } else {
currentState.comments currentState.comments
} }
val canFetchMore = postList.size >= PostRepository.DEFAULT_PAGE_SIZE
mvi.updateState { mvi.updateState {
val newPosts = if (refreshing) { val newPosts = if (refreshing) {
postList itemList.orEmpty()
} else { } else {
it.posts + postList it.posts + itemList.orEmpty()
} }
it.copy( it.copy(
posts = newPosts, posts = newPosts,
comments = comments, comments = comments,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, refreshing = false,
) )
} }
} else { } else {
val commentList = userRepository.getComments( val itemList = userRepository.getComments(
auth = auth, auth = auth,
id = userId, id = userId,
page = currentPage, page = currentPage,
sort = SortType.New, sort = SortType.New,
) )
val canFetchMore = commentList.size >= PostRepository.DEFAULT_PAGE_SIZE
mvi.updateState { mvi.updateState {
val newcomments = if (refreshing) { val newcomments = if (refreshing) {
commentList itemList.orEmpty()
} else { } else {
it.comments + commentList it.comments + itemList.orEmpty()
} }
it.copy( it.copy(
comments = newcomments, comments = newcomments,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, refreshing = false,
) )
} }

View File

@ -105,13 +105,11 @@ class LoginBottomSheetViewModel(
mvi.scope?.launch(Dispatchers.IO) { mvi.scope?.launch(Dispatchers.IO) {
mvi.updateState { it.copy(loading = true) } mvi.updateState { it.copy(loading = true) }
val res = runCatching { val res = communityRepository.getAllInInstance(
communityRepository.getAllInInstance( instance = instance,
instance = instance, page = 1,
page = 1, limit = 1
limit = 1 ) ?: emptyList()
)
}.getOrElse { emptyList() }
if (res.isEmpty()) { if (res.isEmpty()) {
mvi.updateState { mvi.updateState {
it.copy( it.copy(

View File

@ -199,7 +199,7 @@ class ExploreViewModel(
val sortType = currentState.sortType val sortType = currentState.sortType
val resultType = currentState.resultType val resultType = currentState.resultType
val settings = settingsRepository.currentSettings.value val settings = settingsRepository.currentSettings.value
val items = communityRepository.getAll( val itemList = communityRepository.getAll(
query = searchText, query = searchText,
auth = auth, auth = auth,
resultType = resultType, resultType = resultType,
@ -208,12 +208,11 @@ class ExploreViewModel(
sortType = sortType, sortType = sortType,
) )
currentPage++ currentPage++
val canFetchMore = items.size >= PostRepository.DEFAULT_PAGE_SIZE
mvi.updateState { mvi.updateState {
val newItems = if (refreshing) { val newItems = if (refreshing) {
items itemList.orEmpty()
} else { } else {
it.results + items it.results + itemList.orEmpty()
}.filter { community -> }.filter { community ->
if (settings.includeNsfw) { if (settings.includeNsfw) {
true true
@ -224,7 +223,7 @@ class ExploreViewModel(
it.copy( it.copy(
results = newItems, results = newItems,
loading = false, loading = false,
canFetchMore = canFetchMore, canFetchMore = itemList?.isEmpty() != true,
refreshing = false, refreshing = false,
) )
} }

View File

@ -30,7 +30,7 @@ internal class CommunityPaginator(
sort = sort, sort = sort,
communityId = communityId, communityId = communityId,
) )
canFetchMore = result.size >= PostRepository.DEFAULT_PAGE_SIZE canFetchMore = result?.isEmpty() != true
return result return result.orEmpty()
} }
} }

View File

@ -28,9 +28,9 @@ class MainViewModel(
val unreadCount = if (logged == true) { val unreadCount = if (logged == true) {
val auth = identityRepository.authToken.value val auth = identityRepository.authToken.value
val mentionCount = val mentionCount =
userRepository.getMentions(auth, page = 1, limit = 50).count() userRepository.getMentions(auth, page = 1, limit = 50).orEmpty().count()
val replyCount = val replyCount =
userRepository.getReplies(auth, page = 1, limit = 50).count() userRepository.getReplies(auth, page = 1, limit = 50).orEmpty().count()
mentionCount + replyCount mentionCount + replyCount
} else { } else {
0 0