From 39e3a8cb7053534ae94a4cbb0308aec8ae493799 Mon Sep 17 00:00:00 2001 From: Diogo Date: Tue, 29 Oct 2024 21:56:43 +0000 Subject: [PATCH] trim section on jump to bottom --- .../simplex/common/views/chat/ChatSections.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 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 110c3cccaf..b17d4722fe 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 @@ -4,6 +4,9 @@ import chat.simplex.common.model.* import chat.simplex.common.platform.chatModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import kotlin.math.max + +const val MAX_SECTION_SIZE = 500 enum class ChatSectionArea { Bottom, @@ -177,13 +180,18 @@ fun List.putIntoSections(revealedItems: Set): List fun List.dropTemporarySections() { val bottomSection = this.find { it.area == ChatSectionArea.Bottom } - if (bottomSection != null && this.size > 1) { - val removeTo = chatModel.chatItems.value.size - 1 - bottomSection.boundary.maxIndex - chatModel.chatItems.value.removeRange(fromIndex = 0, toIndex = removeTo) - chatModel.chatItemsSectionArea = mutableMapOf().also { it.putAll(chatModel.chatItems.value.associate { it.id to ChatSectionArea.Bottom }) } + if (bottomSection != null) { + val items = chatModel.chatItems.value + val itemsOutsideOfSection = items.size - 1 - bottomSection.boundary.maxIndex + chatModel.chatItems.value.removeRange(fromIndex = 0, toIndex = itemsOutsideOfSection + bottomSection.excessItemCount()) + chatModel.chatItemsSectionArea = mutableMapOf().also { it.putAll(items.associate { it.id to ChatSectionArea.Bottom }) } } } +fun ChatSection.excessItemCount(): Int { + return max(boundary.maxIndex.minus(boundary.minIndex) + 1 - MAX_SECTION_SIZE, 0) +} + suspend fun apiLoadMessagesAroundItem(chatInfo: ChatInfo, chatModel: ChatModel, aroundItemId: Long, rhId: Long?, chatSectionLoad: ChatSectionLoad) { val pagination = ChatPagination.Around(aroundItemId, ChatPagination.PRELOAD_COUNT * 2) val chat = chatModel.controller.apiGetChat(rhId, chatInfo.chatType, chatInfo.apiId, pagination) ?: return