From 22fc2e5151d14d07294f9995c7c8ce2cefdacd1e Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Mon, 4 Nov 2024 12:51:29 +0700 Subject: [PATCH] refactor --- .../simplex/common/views/chat/ChatSections.kt | 62 +++++++++---------- 1 file changed, 29 insertions(+), 33 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 682e2f7e5c..629a13c83f 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 @@ -108,34 +108,32 @@ fun ChatSection.getNextShownItem(sectionIndex: Int, itemIndex: Int): ChatItem? { } fun List.putIntoSections(revealedItems: Set): List { + if (isEmpty()) return emptyList() + val chatItemsSectionArea = chatModel.chatItemsSectionArea val sections = mutableListOf() - var recent: SectionItems = if (isNotEmpty()) { - val first = this[0] + val first = this[0] - SectionItems( - mergeCategory = first.mergeCategory, - items = mutableListOf().also { it.add(first) }, - revealed = first.mergeCategory == null || revealedItems.contains(first.id), - showAvatar = mutableSetOf().also { - if (first.chatDir is CIDirection.GroupRcv) { - val second = getOrNull(1) - - if (second != null) { - if (second.chatDir !is CIDirection.GroupRcv || second.chatDir.groupMember.memberId != first.chatDir.groupMember.memberId) { - it.add(first.id) - } - } else { - it.add(first.id) - } - } - }, - originalItemsRange = 0..0 - ) - } else { - return emptyList() + val showAvatar = mutableSetOf() + if (first.chatDir is CIDirection.GroupRcv) { + val second = getOrNull(1) + if (second != null) { + if (second.chatDir !is CIDirection.GroupRcv || second.chatDir.groupMember.memberId != first.chatDir.groupMember.memberId) { + showAvatar.add(first.id) + } + } else { + showAvatar.add(first.id) + } } + var recent = SectionItems( + mergeCategory = first.mergeCategory, + items = mutableListOf(first), + revealed = first.mergeCategory == null || revealedItems.contains(first.id), + showAvatar = showAvatar, + originalItemsRange = 0..0 + ) + val area = chatItemsSectionArea[recent.items[0].id] ?: ChatSectionArea.Bottom sections.add( @@ -162,11 +160,9 @@ fun List.putIntoSections(revealedItems: Set): List positionInList++ val newSection = SectionItems( mergeCategory = item.mergeCategory, - items = mutableListOf().also { it.add(item) }, + items = mutableListOf(item), revealed = item.mergeCategory == null || revealedItems.contains(item.id), - showAvatar = mutableSetOf().also { - it.add(item.id) - }, + showAvatar = mutableSetOf(item.id), originalItemsRange = index..index ) sections.add( @@ -193,12 +189,12 @@ fun List.putIntoSections(revealedItems: Set): List positionInList++ val newSectionItems = SectionItems( mergeCategory = item.mergeCategory, - items = mutableListOf().also { it.add(item) }, + items = mutableListOf(item), revealed = item.mergeCategory == null || revealedItems.contains(item.id), - showAvatar = mutableSetOf().also { - if (item.chatDir is CIDirection.GroupRcv && (prev.chatDir !is CIDirection.GroupRcv || (prev.chatDir as CIDirection.GroupRcv).groupMember.memberId != item.chatDir.groupMember.memberId)) { - it.add(item.id) - } + showAvatar = if (item.chatDir is CIDirection.GroupRcv && (prev.chatDir !is CIDirection.GroupRcv || (prev.chatDir as CIDirection.GroupRcv).groupMember.memberId != item.chatDir.groupMember.memberId)) { + mutableSetOf(item.id) + } else { + mutableSetOf() }, originalItemsRange = index..index ) @@ -246,7 +242,7 @@ fun List.revealedItemCount(): Int { fun List.dropTemporarySections() { val bottomSection = this.find { it.boundary.area == ChatSectionArea.Bottom } if (bottomSection != null) { - val itemsOutsideOfSection = chatModel.chatItems.value.size - 1 - bottomSection.boundary.maxIndex + val itemsOutsideOfSection = chatModel.chatItems.value.lastIndex - bottomSection.boundary.maxIndex chatModel.chatItems.removeRange(fromIndex = 0, toIndex = itemsOutsideOfSection + bottomSection.excessItemCount()) chatModel.chatItemsSectionArea.clear() chatModel.chatItems.value.associateTo(chatModel.chatItemsSectionArea) { it.id to ChatSectionArea.Bottom }