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 19ee09fbcc..cc0f960568 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 @@ -24,7 +24,6 @@ data class ChatSectionAreaBoundary ( data class ChatSection ( val items: MutableList, - val area: ChatSectionArea, val boundary: ChatSectionAreaBoundary, val itemPositions: MutableMap ) @@ -36,7 +35,7 @@ data class SectionItems ( val showAvatar: MutableSet, ) -data class ChatSectionLoad ( +data class ChatSectionLoader ( val position: Int, val sectionArea: ChatSectionArea ) { @@ -117,7 +116,6 @@ fun List.putIntoSections(revealedItems: Set): List sections.add( ChatSection( items = mutableListOf(recent), - area = area, boundary = ChatSectionAreaBoundary(minIndex = 0, maxIndex = 0, area = area), itemPositions = mutableMapOf(recent.items[0].id to 0) ) @@ -133,7 +131,7 @@ fun List.putIntoSections(revealedItems: Set): List } val item = this[index] val itemArea = chatItemsSectionArea[item.id] ?: ChatSectionArea.Bottom - val existingSection = sections.find { it.area == itemArea } + val existingSection = sections.find { it.boundary.area == itemArea } if (existingSection == null) { positionInList++ @@ -148,7 +146,6 @@ fun List.putIntoSections(revealedItems: Set): List sections.add( ChatSection( items = mutableListOf(newSection), - area = itemArea, boundary = ChatSectionAreaBoundary(minIndex = index, maxIndex = index, area = itemArea), itemPositions = mutableMapOf(item.id to positionInList) ) @@ -217,7 +214,7 @@ fun List.revealedItemCount(): Int { } fun List.dropTemporarySections() { - val bottomSection = this.find { it.area == ChatSectionArea.Bottom } + val bottomSection = this.find { it.boundary.area == ChatSectionArea.Bottom } if (bottomSection != null) { val items = chatModel.chatItems.value val itemsOutsideOfSection = items.size - 1 - bottomSection.boundary.maxIndex @@ -235,14 +232,14 @@ fun landingSectionToArea(chatLandingSection: ChatLandingSection) = when (chatLan ChatLandingSection.Unread -> ChatSectionArea.Current } -suspend fun apiLoadMessagesAroundItem(chatInfo: ChatInfo, chatModel: ChatModel, aroundItemId: Long, rhId: Long?, chatSectionLoad: ChatSectionLoad) { +suspend fun apiLoadMessagesAroundItem(chatInfo: ChatInfo, chatModel: ChatModel, aroundItemId: Long, rhId: Long?, chatSectionLoader: ChatSectionLoader) { val pagination = ChatPagination.Around(aroundItemId, ChatPagination.PRELOAD_COUNT * 2) val (chat) = chatModel.controller.apiGetChat(rhId, chatInfo.chatType, chatInfo.apiId, pagination) ?: return if (chatModel.chatId.value != chat.id) return withContext(Dispatchers.Main) { - val itemsToAdd = chatSectionLoad.prepareItems(chat.chatItems) + val itemsToAdd = chatSectionLoader.prepareItems(chat.chatItems) if (itemsToAdd.isNotEmpty()) { - chatModel.chatItems.addAll(chatSectionLoad.position, itemsToAdd) + chatModel.chatItems.addAll(chatSectionLoader.position, itemsToAdd) } } } 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 7822f1b2e0..6ad3556f1f 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 @@ -299,8 +299,8 @@ fun ChatView(staleChatId: State, onComposed: suspend (chatId: String) - if (c != null && firstId != null) { withBGApi { - val chatSectionLoad = ChatSectionLoad(firstSectionItemIdx, section.area) - apiLoadPrevMessages(c, chatModel, firstId, searchText.value, chatSectionLoad) + val chatSectionLoader = ChatSectionLoader(firstSectionItemIdx, section.area) + apiLoadPrevMessages(c, chatModel, firstId, searchText.value, chatSectionLoader) } } } @@ -310,8 +310,8 @@ fun ChatView(staleChatId: State, onComposed: suspend (chatId: String) - if (c != null && lastId != null) { withBGApi { - val chatSectionLoad = ChatSectionLoad(lastSectionItemIdx + 1, section.area) - apiLoadAfterMessages(c, chatModel, lastId, searchText.value, chatSectionLoad) + val chatSectionLoader = ChatSectionLoader(lastSectionItemIdx + 1, section.area) + apiLoadAfterMessages(c, chatModel, lastId, searchText.value, chatSectionLoader) } } } @@ -1058,15 +1058,15 @@ fun BoxWithConstraintsScope.ChatItemsList( } else { withBGApi { try { - val destinationSection = sections.find { it.area == ChatSectionArea.Destination } + val destinationSection = sections.find { it.boundary.area == ChatSectionArea.Destination } val itemsToDrop = destinationSection?.items?.flatMap { it.items }?.toList() withContext(Dispatchers.Main) { itemsToDrop?.forEach { chatModel.chatItemsSectionArea[it.id] = ChatSectionArea.Current } } - val chatSectionLoad = ChatSectionLoad(0, ChatSectionArea.Destination) - apiLoadMessagesAroundItem(rhId = remoteHostId, chatModel = chatModel, chatInfo = chatInfo, aroundItemId = itemId, chatSectionLoad = chatSectionLoad) + val chatSectionLoader = ChatSectionLoader(0, ChatSectionArea.Destination) + apiLoadMessagesAroundItem(rhId = remoteHostId, chatModel = chatModel, chatInfo = chatInfo, aroundItemId = itemId, chatSectionLoader = chatSectionLoader) val idx = sections.chatItemPosition(itemId) scope.launch { diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatListNavLinkView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatListNavLinkView.kt index 02e32c3d54..4a05a81260 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatListNavLinkView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatListNavLinkView.kt @@ -232,28 +232,28 @@ fun openLoadedChat(chat: Chat, chatModel: ChatModel, landingSection: ChatLanding chatModel.chatItemsSectionArea = mutableMapOf().also { map -> map.putAll(chatModel.chatItems.value.associate { it.id to landingSectionToArea(landingSection) }) } } -suspend fun apiLoadPrevMessages(ch: Chat, chatModel: ChatModel, beforeChatItemId: Long, search: String, chatSectionLoad: ChatSectionLoad) { +suspend fun apiLoadPrevMessages(ch: Chat, chatModel: ChatModel, beforeChatItemId: Long, search: String, chatSectionLoader: ChatSectionLoader) { val chatInfo = ch.chatInfo val pagination = ChatPagination.Before(beforeChatItemId, ChatPagination.PRELOAD_COUNT) val (chat) = chatModel.controller.apiGetChat(ch.remoteHostId, chatInfo.chatType, chatInfo.apiId, pagination, search) ?: return if (chatModel.chatId.value != chat.id) return withContext(Dispatchers.Main) { - val itemsToAdd = chatSectionLoad.prepareItems(chat.chatItems) + val itemsToAdd = chatSectionLoader.prepareItems(chat.chatItems) if (itemsToAdd.isNotEmpty()) { - chatModel.chatItems.addAll(chatSectionLoad.position, itemsToAdd) + chatModel.chatItems.addAll(chatSectionLoader.position, itemsToAdd) } } } -suspend fun apiLoadAfterMessages(ch: Chat, chatModel: ChatModel, afterChatItemId: Long, search: String, chatSectionLoad: ChatSectionLoad) { +suspend fun apiLoadAfterMessages(ch: Chat, chatModel: ChatModel, afterChatItemId: Long, search: String, chatSectionLoader: ChatSectionLoader) { val chatInfo = ch.chatInfo val pagination = ChatPagination.After(afterChatItemId, ChatPagination.PRELOAD_COUNT) val (chat) = chatModel.controller.apiGetChat(ch.remoteHostId, chatInfo.chatType, chatInfo.apiId, pagination, search) ?: return if (chatModel.chatId.value != chat.id) return withContext(Dispatchers.Main) { - val itemsToAdd = chatSectionLoad.prepareItems(chat.chatItems) + val itemsToAdd = chatSectionLoader.prepareItems(chat.chatItems) if (itemsToAdd.isNotEmpty()) { - chatModel.chatItems.addAll(chatSectionLoad.position, itemsToAdd) + chatModel.chatItems.addAll(chatSectionLoader.position, itemsToAdd) } } }