Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update last read message on conversation opening [WPB-7208] #2822

Merged
merged 7 commits into from
Apr 2, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ private fun SnackBarMessage(
}
}

@Suppress("ComplexMethod")
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun MessageList(
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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(),
Expand Down
Loading