Skip to content

Commit e7e5b05

Browse files
committed
fix(fc): handle read pointers while sitting at bottom of chat
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent affa4bd commit e7e5b05

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

ui/components/src/main/kotlin/com/getcode/ui/components/chat/MessageList.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,20 @@ import androidx.paging.compose.itemKey
2828
import com.getcode.model.ID
2929
import com.getcode.model.chat.MessageContent
3030
import com.getcode.model.chat.MessageStatus
31+
import com.getcode.model.uuid
3132
import com.getcode.theme.CodeTheme
3233
import com.getcode.ui.components.chat.messagecontents.MessageControlAction
3334
import com.getcode.ui.components.chat.utils.ChatItem
3435
import com.getcode.ui.components.chat.utils.MessageTip
3536
import com.getcode.ui.components.text.markup.Markup
3637
import com.getcode.util.formatDateRelatively
38+
import com.getcode.utils.timestamp
3739
import kotlinx.coroutines.flow.combine
3840
import kotlinx.coroutines.flow.distinctUntilChanged
3941
import kotlinx.coroutines.flow.filter
4042
import kotlinx.coroutines.flow.filterNot
43+
import kotlinx.coroutines.flow.launchIn
44+
import kotlinx.coroutines.flow.onEach
4145

4246
sealed interface MessageListEvent {
4347
data class AdvancePointer(val messageId: ID) : MessageListEvent
@@ -275,12 +279,18 @@ private fun HandleMessageReads(
275279
}.distinctUntilChanged(),
276280
snapshotFlow { listState.isScrollInProgress },
277281
snapshotFlow { listState.firstVisibleItemIndex },
278-
) { loadState, isScrolling, firstVisibleIndex ->
279-
Triple(loadState, isScrolling, firstVisibleIndex)
280-
}.filter { (loadStateIsNotLoading, isScrolling, _) ->
281-
// Wait until scrolling stops, messages are not loading, and we are at the bottom
282-
loadStateIsNotLoading && !isScrolling && messages.itemCount > 0 && hasSetAtUnread
283-
}.collect { (_, _, firstVisibleIndex) ->
282+
snapshotFlow { messages.itemCount },
283+
) { loadState, isScrolling, firstVisibleIndex, itemCount ->
284+
Triple(loadState, isScrolling, firstVisibleIndex) to itemCount
285+
}.filter { (state, itemCount) ->
286+
val (loadStateIsNotLoading, isScrolling, firstVisibleIndex) = state
287+
// Ensure we react to new messages while at the bottom
288+
loadStateIsNotLoading && !isScrolling && hasSetAtUnread &&
289+
(firstVisibleIndex == 0 || firstVisibleIndex == itemCount - 1) && itemCount > 0
290+
}.onEach { (state, _) ->
291+
292+
val (_, _, firstVisibleIndex) = state
293+
284294
val closestChatMessage =
285295
messages[firstVisibleIndex]?.let { it as? ChatItem.Message }
286296

@@ -298,7 +308,7 @@ private fun HandleMessageReads(
298308
}
299309
}
300310
}
301-
}
311+
}.launchIn(this)
302312
}
303313
}
304314

ui/components/src/main/kotlin/com/getcode/ui/components/chat/utils/HandleMessageChanges.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fun HandleMessageChanges(
3838
.filter { it.refresh is LoadState.NotLoading }
3939
.mapNotNull { items.itemSnapshotList.firstOrNull() }
4040
.filterIsInstance<ChatItem.Message>()
41-
.distinctUntilChangedBy { it.date }
41+
.distinctUntilChangedBy { it.chatMessageId }
4242
.collect { newMessage ->
4343
if (newMessage.status.isOutgoing()) {
4444
if (newMessage.date.toEpochMilliseconds() > lastMessageSent) {

0 commit comments

Comments
 (0)