single api for simple pagination loads

This commit is contained in:
Diogo
2024-10-31 22:55:01 +00:00
parent cf32d1d950
commit c98e8e774f
3 changed files with 37 additions and 33 deletions
@@ -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
@@ -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)
@@ -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)