Skip to content

Commit

Permalink
Merge branch 'develop' into fix/self-notification-vibration
Browse files Browse the repository at this point in the history
  • Loading branch information
Garzas authored Apr 2, 2024
2 parents ee2a628 + f24c968 commit c2c8950
Showing 1 changed file with 27 additions and 7 deletions.
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

0 comments on commit c2c8950

Please sign in to comment.