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 030193b57c..110c3cccaf 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 @@ -31,6 +31,34 @@ data class SectionItems ( val itemPositions: MutableMap ) +data class ChatSectionLoad ( + val position: Int, + val sectionArea: ChatSectionArea +) { + fun prepareItems(items: List): List { + val chatItemsSectionArea = chatModel.chatItemsSectionArea + val itemsToAdd = mutableListOf() + for (cItem in items) { + val itemSectionArea = chatItemsSectionArea[cItem.id] + if (itemSectionArea == null) { + itemsToAdd.add(cItem) + } else if (itemSectionArea != this.sectionArea) { + val targetSection = when (itemSectionArea) { + ChatSectionArea.Bottom -> ChatSectionArea.Bottom + ChatSectionArea.Current -> if (this.sectionArea == ChatSectionArea.Bottom) ChatSectionArea.Bottom else ChatSectionArea.Current + ChatSectionArea.Destination -> if (this.sectionArea == ChatSectionArea.Bottom) ChatSectionArea.Bottom else ChatSectionArea.Destination + } + + chatItemsSectionArea.filter { it.value == itemSectionArea } + .forEach { chatItemsSectionArea[it.key] = targetSection } + } + } + chatItemsSectionArea.putAll(itemsToAdd.associate { it.id to sectionArea }) + + return itemsToAdd + } +} + fun ChatSection.getPreviousShownItem(sectionIndex: Int, itemIndex: Int): ChatItem? { val section = items.getOrNull(sectionIndex) ?: return null @@ -147,31 +175,12 @@ fun List.putIntoSections(revealedItems: Set): List return sections } -data class ChatSectionLoad ( - val position: Int, - val sectionArea: ChatSectionArea -) { - fun prepareItems(items: List): List { - val chatItemsSectionArea = chatModel.chatItemsSectionArea - val itemsToAdd = mutableListOf() - for (cItem in items) { - val itemSectionArea = chatItemsSectionArea[cItem.id] - if (itemSectionArea == null) { - itemsToAdd.add(cItem) - } else if (itemSectionArea != this.sectionArea) { - val targetSection = when (itemSectionArea) { - ChatSectionArea.Bottom -> ChatSectionArea.Bottom - ChatSectionArea.Current -> if (this.sectionArea == ChatSectionArea.Bottom) ChatSectionArea.Bottom else ChatSectionArea.Current - ChatSectionArea.Destination -> if (this.sectionArea == ChatSectionArea.Bottom) ChatSectionArea.Bottom else ChatSectionArea.Destination - } - - chatItemsSectionArea.filter { it.value == itemSectionArea } - .forEach { chatItemsSectionArea[it.key] = targetSection } - } - } - chatItemsSectionArea.putAll(itemsToAdd.associate { it.id to sectionArea }) - - return itemsToAdd +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 }) } } } 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 384d8bdc8e..9c3563473f 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 @@ -1347,13 +1347,7 @@ fun BoxWithConstraintsScope.ChatItemsList( scope.launch { listState.animateScrollToItem(0) preloadItemsEnabled.value = true - - val bottomSection = sections.find { it.area == ChatSectionArea.Bottom } - if (bottomSection != null && sections.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 }) } - } + sections.dropTemporarySections() } }