fix occasional race condition on long navigation

This commit is contained in:
Diogo
2024-10-31 16:55:53 +00:00
parent 638b237b8c
commit 937e66b3b8
2 changed files with 17 additions and 2 deletions
@@ -201,6 +201,21 @@ fun List<ChatSection>.chatItemPosition(chatItemId: Long): Int? {
return null
}
fun List<ChatSection>.revealedItemCount(): Int {
var count = 0
for (section in this) {
for (item in section.items) {
if (item.revealed) {
count += item.items.size
} else {
count++
}
}
}
return count
}
fun List<ChatSection>.dropTemporarySections() {
val bottomSection = this.find { it.area == ChatSectionArea.Bottom }
if (bottomSection != null) {
@@ -1009,7 +1009,7 @@ fun BoxWithConstraintsScope.ChatItemsList(
val sections by remember { derivedStateOf { reversedChatItems.putIntoSections(revealedItems.value) } }
val preloadItemsEnabled = remember { mutableStateOf(true) }
val boundaries = remember { derivedStateOf { sections.map { it.boundary } } }
val scrollPosition: (Int) -> Int = { idx -> min(listState.layoutInfo.totalItemsCount - 1, idx + 1 ) }
val scrollPosition: (Int) -> Int = { idx -> min(sections.revealedItemCount() - 1, idx + 1 ) }
PreloadItems(chatInfo.id, listState, ChatPagination.UNTIL_PRELOAD_COUNT, preloadItemsEnabled, boundaries, loadMessages)
val maxHeightRounded = with(LocalDensity.current) { maxHeight.roundToPx() }
@@ -1067,9 +1067,9 @@ fun BoxWithConstraintsScope.ChatItemsList(
}
val chatSectionLoad = ChatSectionLoad(0, ChatSectionArea.Destination)
apiLoadMessagesAroundItem(rhId = remoteHostId, chatModel = chatModel, chatInfo = chatInfo, aroundItemId = itemId, chatSectionLoad = chatSectionLoad)
val idx = sections.chatItemPosition(itemId)
scope.launch {
val idx = sections.chatItemPosition(itemId)
if (idx != null) {
listState.animateScrollToItem(scrollPosition(idx), -maxHeightRounded)
withContext(Dispatchers.Main) {