diff --git a/app/src/main/kotlin/com/wire/android/ui/home/conversations/ConversationScreen.kt b/app/src/main/kotlin/com/wire/android/ui/home/conversations/ConversationScreen.kt index 387a3275c34..f31389eb4e3 100644 --- a/app/src/main/kotlin/com/wire/android/ui/home/conversations/ConversationScreen.kt +++ b/app/src/main/kotlin/com/wire/android/ui/home/conversations/ConversationScreen.kt @@ -887,6 +887,7 @@ private fun SnackBarMessage( } } +@Suppress("ComplexMethod") @SuppressLint("UnusedMaterial3ScaffoldPaddingParameter") @Composable fun MessageList( @@ -914,6 +915,8 @@ fun MessageList( interactionAvailability: InteractionAvailability, ) { val prevItemCount = remember { mutableStateOf(lazyPagingMessages.itemCount) } + val readLastMessageAtStartTriggered = remember { mutableStateOf(false) } + LaunchedEffect(lazyPagingMessages.itemCount) { if (lazyPagingMessages.itemCount > prevItemCount.value && selectedMessageId == null) { val canScrollToLastMessage = prevItemCount.value > 0 @@ -926,17 +929,20 @@ fun MessageList( } } + // update last read message when scroll ends LaunchedEffect(lazyListState.isScrollInProgress) { if (!lazyListState.isScrollInProgress && lazyPagingMessages.itemCount > 0) { val lastVisibleMessage = lazyPagingMessages[lazyListState.firstVisibleItemIndex] ?: return@LaunchedEffect + updateLastReadMessage(lastVisibleMessage, lastUnreadMessageInstant, onUpdateConversationReadDate) + } + } - val lastVisibleMessageInstant = Instant.parse(lastVisibleMessage.header.messageTime.utcISO) - - // TODO: This IF condition should be in the UseCase - // If there are no unread messages, then use distant future and don't update read date - if (lastVisibleMessageInstant > (lastUnreadMessageInstant ?: Instant.DISTANT_FUTURE)) { - onUpdateConversationReadDate(lastVisibleMessage.header.messageTime.utcISO) - } + // update last read message on start + LaunchedEffect(lazyPagingMessages.itemCount) { + if (!readLastMessageAtStartTriggered.value && lazyPagingMessages.itemSnapshotList.items.isNotEmpty()) { + val lastVisibleMessage = lazyPagingMessages[lazyListState.firstVisibleItemIndex] ?: return@LaunchedEffect + readLastMessageAtStartTriggered.value = true + updateLastReadMessage(lastVisibleMessage, lastUnreadMessageInstant, onUpdateConversationReadDate) } } @@ -1023,6 +1029,20 @@ fun MessageList( }) } +private fun updateLastReadMessage( + lastVisibleMessage: UIMessage, + lastUnreadMessageInstant: Instant?, + onUpdateConversationReadDate: (String) -> Unit +) { + val lastVisibleMessageInstant = Instant.parse(lastVisibleMessage.header.messageTime.utcISO) + + // TODO: This IF condition should be in the UseCase + // If there are no unread messages, then use distant future and don't update read date + if (lastVisibleMessageInstant > (lastUnreadMessageInstant ?: Instant.DISTANT_FUTURE)) { + onUpdateConversationReadDate(lastVisibleMessage.header.messageTime.utcISO) + } +} + @Composable fun JumpToLastMessageButton( coroutineScope: CoroutineScope = rememberCoroutineScope(),