mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-05-25 20:44:38 +00:00
single api for simple pagination loads
This commit is contained in:
-12
@@ -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
|
||||
|
||||
+27
-4
@@ -300,7 +300,14 @@ fun ChatView(staleChatId: State<String?>, 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<String?>, 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)
|
||||
|
||||
+10
-17
@@ -232,23 +232,16 @@ fun openLoadedChat(chat: Chat, chatModel: ChatModel, landingSection: ChatLanding
|
||||
chatModel.chatItemsSectionArea = mutableMapOf<Long, ChatSectionArea>().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)
|
||||
|
||||
Reference in New Issue
Block a user