From a3906cf88fb5054436e52359df4eccf56a8dffa1 Mon Sep 17 00:00:00 2001 From: Diogo Date: Sat, 2 Nov 2024 22:27:20 +0000 Subject: [PATCH] reduce looping --- .../simplex/common/views/chat/ChatSections.kt | 15 +++++++++++---- 1 file changed, 11 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 6a22de4556..682e2f7e5c 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 @@ -44,10 +44,17 @@ data class ChatSectionLoader ( val chatItemsSectionArea = chatModel.chatItemsSectionArea val itemsToAdd = mutableListOf() val sectionsToMerge = mutableMapOf() + val itemsThatCouldRequireMerge = mutableListOf() for (cItem in items) { val itemSectionArea = chatItemsSectionArea[cItem.id] if (itemSectionArea == null) { itemsToAdd.add(cItem) + val targetSection = sectionsToMerge[sectionArea] ?: this.sectionArea + chatItemsSectionArea[cItem.id] = targetSection + + if (targetSection == this.sectionArea) { + itemsThatCouldRequireMerge.add(cItem) + } } else if (itemSectionArea != this.sectionArea) { val (targetSection, sectionToDrop) = when (itemSectionArea) { ChatSectionArea.Bottom -> ChatSectionArea.Bottom to this.sectionArea @@ -69,11 +76,11 @@ data class ChatSectionLoader ( chatItemsSectionArea[it.id] = newSection } } - } - itemsToAdd.forEach { - val targetSection = sectionsToMerge[sectionArea] ?: sectionArea - chatItemsSectionArea[it.id] = targetSection + itemsThatCouldRequireMerge.forEach { + val targetSection = sectionsToMerge[sectionArea] ?: sectionArea + chatItemsSectionArea[it.id] = targetSection + } } return itemsToAdd