From f1c0c8397278eb2767aa3d566110625457c2e2b9 Mon Sep 17 00:00:00 2001 From: TheRealAshik <177647015+TheRealAshik@users.noreply.github.com> Date: Tue, 7 Apr 2026 07:41:37 +0000 Subject: [PATCH] Fix chat message pagination bugs - Moved the `isLoadingMore` block to the bottom of `LazyColumn` in `ChatMessageList` to render it at the top in a reverse layout. - Added a guard `!isLoadingMore` in `ChatMessageList`'s `LaunchedEffect` to avoid duplicate load more API calls. - Added `.onFailure` handler to `loadMoreMessages` in `ChatViewModel` to surface loading failures instead of failing silently. --- .../feature/inbox/inbox/ChatViewModel.kt | 2 ++ .../inbox/inbox/screens/ChatMessageList.kt | 17 +++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/com/synapse/social/studioasinc/feature/inbox/inbox/ChatViewModel.kt b/app/src/main/kotlin/com/synapse/social/studioasinc/feature/inbox/inbox/ChatViewModel.kt index 9209f7e..a2eea14 100644 --- a/app/src/main/kotlin/com/synapse/social/studioasinc/feature/inbox/inbox/ChatViewModel.kt +++ b/app/src/main/kotlin/com/synapse/social/studioasinc/feature/inbox/inbox/ChatViewModel.kt @@ -345,6 +345,8 @@ class ChatViewModel @Inject constructor( (older + current).distinctBy { it.id }.sortedBy { it.createdAt } } } + }.onFailure { e -> + _error.value = "Failed to load older messages: ${e.message}" } _isLoadingMore.value = false } diff --git a/app/src/main/kotlin/com/synapse/social/studioasinc/feature/inbox/inbox/screens/ChatMessageList.kt b/app/src/main/kotlin/com/synapse/social/studioasinc/feature/inbox/inbox/screens/ChatMessageList.kt index adbc143..31fec78 100644 --- a/app/src/main/kotlin/com/synapse/social/studioasinc/feature/inbox/inbox/screens/ChatMessageList.kt +++ b/app/src/main/kotlin/com/synapse/social/studioasinc/feature/inbox/inbox/screens/ChatMessageList.kt @@ -68,7 +68,7 @@ internal fun ChatMessageList( } } LaunchedEffect(shouldLoadMore.value) { - if (shouldLoadMore.value) onLoadMore() + if (shouldLoadMore.value && !isLoadingMore) onLoadMore() } LazyColumn( @@ -84,13 +84,6 @@ internal fun ChatMessageList( ), reverseLayout = true ) { - if (isLoadingMore) { - item(key = "loading_more") { - Box(modifier = Modifier.fillMaxWidth().padding(Spacing.Small), contentAlignment = Alignment.Center) { - CircularProgressIndicator() - } - } - } val reversedItems = chatItems.reversed() itemsIndexed(reversedItems, key = { index, item -> when (item) { @@ -163,5 +156,13 @@ internal fun ChatMessageList( ) } } + + if (isLoadingMore) { + item(key = "loading_more") { + Box(modifier = Modifier.fillMaxWidth().padding(Spacing.Small), contentAlignment = Alignment.Center) { + CircularProgressIndicator() + } + } + } } }