From 937e66b3b867232bdbb3c48be78a00c90b00f19f Mon Sep 17 00:00:00 2001 From: Diogo Date: Thu, 31 Oct 2024 16:55:53 +0000 Subject: [PATCH] fix occasional race condition on long navigation --- .../simplex/common/views/chat/ChatSections.kt | 15 +++++++++++++++ .../chat/simplex/common/views/chat/ChatView.kt | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatSections.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatSections.kt index 6dd7dfdb4c..19ee09fbcc 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatSections.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatSections.kt @@ -201,6 +201,21 @@ fun List.chatItemPosition(chatItemId: Long): Int? { return null } +fun List.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.dropTemporarySections() { val bottomSection = this.find { it.area == ChatSectionArea.Bottom } if (bottomSection != null) { diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt index bb3f4a24ed..7822f1b2e0 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt @@ -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) {