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 e55be0a57a..3fc87b4ced 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 @@ -33,6 +33,7 @@ data class SectionItems ( val items: MutableList, val revealed: Boolean, val showAvatar: MutableSet, + var originalItemsRange: IntRange ) data class ChatSectionLoader ( @@ -105,7 +106,8 @@ fun List.putIntoSections(revealedItems: Set): List it.add(first.id) } } - } + }, + originalItemsRange = 0..0 ) } else { return emptyList() @@ -142,6 +144,7 @@ fun List.putIntoSections(revealedItems: Set): List showAvatar = mutableSetOf().also { it.add(item.id) }, + originalItemsRange = index..index ) sections.add( ChatSection( @@ -160,8 +163,8 @@ fun List.putIntoSections(revealedItems: Set): List if (item.chatDir is CIDirection.GroupRcv && prev.chatDir is CIDirection.GroupRcv && item.chatDir.groupMember.memberId != (prev.chatDir as CIDirection.GroupRcv).groupMember.memberId) { recent.showAvatar.add(item.id) } - recent.items.add(item) + recent.originalItemsRange = recent.originalItemsRange.first..index existingSection.itemPositions[item.id] = positionInList } else { positionInList++ @@ -174,6 +177,7 @@ fun List.putIntoSections(revealedItems: Set): List it.add(item.id) } }, + originalItemsRange = index..index ) existingSection.itemPositions[item.id] = positionInList existingSection.items.add(newSectionItems) 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 ec851f7850..643227b01e 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 @@ -29,6 +29,7 @@ import androidx.compose.ui.unit.* import chat.simplex.common.model.* import chat.simplex.common.model.CIDirection.GroupRcv import chat.simplex.common.model.ChatController.appPrefs +import chat.simplex.common.model.ChatModel.chatItems import chat.simplex.common.model.ChatModel.controller import chat.simplex.common.model.ChatModel.withChats import chat.simplex.common.ui.theme.* @@ -1104,7 +1105,7 @@ fun BoxScope.ChatItemsList( } ) @Composable - fun ChatViewListItem(i: Int, sectionItems: SectionItems, cItem: ChatItem, prevItem: ChatItem?, nextItem: ChatItem?) { + fun ChatViewListItem(i: Int, range: IntRange?, showAvatar: Boolean, cItem: ChatItem, prevItem: ChatItem?, nextItem: ChatItem?) { CompositionLocalProvider( // Makes horizontal and vertical scrolling to coexist nicely. // With default touchSlop when you scroll LazyColumn, you can unintentionally open reply view @@ -1125,7 +1126,7 @@ fun BoxScope.ChatItemsList( val revealed = remember { mutableStateOf(revealedItems.value.contains(cItem.id)) } KeyChangeEffect(revealed.value) { - val revealIds = if (cItem.mergeCategory == null) setOf(cItem.id) else sectionItems.items.map { it.id }.toSet() + val revealIds = if (range == null) setOf(cItem.id) else reversedChatItems.value.subList(range.first, range.last + 1).map { it.id }.toSet() if (revealIds.isNotEmpty()) { if (revealed.value) { @@ -1210,7 +1211,7 @@ fun BoxScope.ChatItemsList( null to 1 } - if (showMemberImage(member, prevItem) || sectionItems.showAvatar.contains(cItem.id)) { + if (showMemberImage(member, prevItem) || showAvatar) { Column( Modifier .padding(top = 8.dp) @@ -1336,14 +1337,20 @@ fun BoxScope.ChatItemsList( LaunchedEffect(cItem.id) { itemScope.launch { delay(600) - val range = if (sectionItems.mergeCategory != null) { - val firstItem = sectionItems.items.first() - val lastItem = sectionItems.items.last() - CC.ItemRange(lastItem.id, firstItem.id) + val itemRange = if (range != null) { + val firstItem = reversedChatItems.value.getOrNull(range.first) + val lastItem = reversedChatItems.value.getOrNull(range.last) + if (lastItem != null && firstItem != null) { + CC.ItemRange(lastItem.id, firstItem.id) + } else { + null + } } else { CC.ItemRange(cItem.id, cItem.id) } - markRead(range, null) + if (itemRange != null) { + markRead(itemRange, null) + } } } } @@ -1355,7 +1362,7 @@ fun BoxScope.ChatItemsList( DateSeparator(itemSeparation.date) } - ChatItemView(cItem, null, prevItem, itemSeparation, previousItemSeparation) + ChatItemView(cItem, range, prevItem, itemSeparation, previousItemSeparation) } } LazyColumnWithScrollBar( @@ -1375,7 +1382,7 @@ fun BoxScope.ChatItemsList( // index here is just temporary, should be removed at all or put in the section items val prevItem = area.getPreviousShownItem(sIdx, i) val nextItem = area.getNextShownItem(sIdx, i) - ChatViewListItem(area.itemPositions[cItem.id] ?: -1, section, cItem, prevItem, nextItem) + ChatViewListItem(area.itemPositions[cItem.id] ?: -1, section.originalItemsRange.takeIf { cItem.mergeCategory != null }, section.showAvatar.contains(cItem.id), cItem, prevItem, nextItem, ) } } else { val item = section.items.first() @@ -1383,7 +1390,7 @@ fun BoxScope.ChatItemsList( // here you make one collapsed item from multiple items (should be already in section items) val prevItem = area.getPreviousShownItem(sIdx, section.items.lastIndex) val nextItem = area.getNextShownItem(sIdx, section.items.lastIndex) - ChatViewListItem(area.itemPositions[item.id] ?: -1, section, item, prevItem, nextItem) + ChatViewListItem(area.itemPositions[item.id] ?: -1, section.originalItemsRange, section.showAvatar.contains(item.id), item, prevItem, nextItem) } } }