From c98e8e774f32ab33b7cb76a1dffb6a6962a36a60 Mon Sep 17 00:00:00 2001 From: Diogo Date: Thu, 31 Oct 2024 22:55:01 +0000 Subject: [PATCH] single api for simple pagination loads --- .../simplex/common/views/chat/ChatSections.kt | 12 ------- .../simplex/common/views/chat/ChatView.kt | 31 ++++++++++++++++--- .../views/chatlist/ChatListNavLinkView.kt | 27 ++++++---------- 3 files changed, 37 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 cc0f960568..d564810095 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 @@ -232,18 +232,6 @@ fun landingSectionToArea(chatLandingSection: ChatLandingSection) = when (chatLan ChatLandingSection.Unread -> ChatSectionArea.Current } -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 = chatSectionLoader.prepareItems(chat.chatItems) - if (itemsToAdd.isNotEmpty()) { - chatModel.chatItems.addAll(chatSectionLoader.position, itemsToAdd) - } - } -} - suspend fun apiLoadBottomSection(chatInfo: ChatInfo, chatModel: ChatModel, rhId: Long?) { val chat = chatController.apiGetChat(rh = rhId, type = chatInfo.chatType, id = chatInfo.apiId) if (chatModel.chatId.value != chatInfo.id || chat == null) return 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 6ad3556f1f..177495a729 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 @@ -300,7 +300,14 @@ fun ChatView(staleChatId: State, onComposed: suspend (chatId: String) - if (c != null && firstId != null) { withBGApi { val chatSectionLoader = ChatSectionLoader(firstSectionItemIdx, section.area) - apiLoadPrevMessages(c, chatModel, firstId, searchText.value, chatSectionLoader) + apiLoadMessages( + rhId = c.remoteHostId, + chatInfo = c.chatInfo, + chatModel = chatModel, + itemId = firstId, + search = "", + chatSectionLoader = chatSectionLoader, + ) } } } @@ -311,7 +318,15 @@ fun ChatView(staleChatId: State, onComposed: suspend (chatId: String) - if (c != null && lastId != null) { withBGApi { val chatSectionLoader = ChatSectionLoader(lastSectionItemIdx + 1, section.area) - apiLoadAfterMessages(c, chatModel, lastId, searchText.value, chatSectionLoader) + apiLoadMessages( + rhId = c.remoteHostId, + chatInfo = c.chatInfo, + chatModel = chatModel, + itemId = lastId, + search = "", + chatSectionLoader = chatSectionLoader, + pagination = ChatPagination.After(lastId, ChatPagination.PRELOAD_COUNT) + ) } } } @@ -1066,9 +1081,17 @@ fun BoxWithConstraintsScope.ChatItemsList( } } val chatSectionLoader = ChatSectionLoader(0, ChatSectionArea.Destination) - apiLoadMessagesAroundItem(rhId = remoteHostId, chatModel = chatModel, chatInfo = chatInfo, aroundItemId = itemId, chatSectionLoader = chatSectionLoader) - val idx = sections.chatItemPosition(itemId) + apiLoadMessages( + rhId = remoteHostId, + chatInfo = chatInfo, + chatModel = chatModel, + itemId = itemId, + search = "", + chatSectionLoader = chatSectionLoader, + pagination = ChatPagination.Around(itemId, ChatPagination.PRELOAD_COUNT * 2) + ) + val idx = sections.chatItemPosition(itemId) scope.launch { if (idx != null) { listState.animateScrollToItem(scrollPosition(idx), -maxHeightRounded) 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 4a05a81260..59265f415d 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,23 +232,16 @@ 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, 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 = chatSectionLoader.prepareItems(chat.chatItems) - if (itemsToAdd.isNotEmpty()) { - chatModel.chatItems.addAll(chatSectionLoader.position, itemsToAdd) - } - } -} - -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 +suspend fun apiLoadMessages( + rhId: Long?, + chatInfo: ChatInfo, + chatModel: ChatModel, + itemId: Long, + search: String, + chatSectionLoader: ChatSectionLoader, + pagination: ChatPagination = ChatPagination.Before(itemId, ChatPagination.PRELOAD_COUNT) +) { + val (chat) = chatModel.controller.apiGetChat(rhId, chatInfo.chatType, chatInfo.apiId, pagination, search) ?: return if (chatModel.chatId.value != chat.id) return withContext(Dispatchers.Main) { val itemsToAdd = chatSectionLoader.prepareItems(chat.chatItems)