mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-04-25 07:42:15 +00:00
simplify
This commit is contained in:
@@ -24,7 +24,6 @@ data class ChatSectionAreaBoundary (
|
||||
|
||||
data class ChatSection (
|
||||
val items: MutableList<SectionItems>,
|
||||
val area: ChatSectionArea,
|
||||
val boundary: ChatSectionAreaBoundary,
|
||||
val itemPositions: MutableMap<Long, Int>
|
||||
)
|
||||
@@ -36,7 +35,7 @@ data class SectionItems (
|
||||
val showAvatar: MutableSet<Long>,
|
||||
)
|
||||
|
||||
data class ChatSectionLoad (
|
||||
data class ChatSectionLoader (
|
||||
val position: Int,
|
||||
val sectionArea: ChatSectionArea
|
||||
) {
|
||||
@@ -117,7 +116,6 @@ fun List<ChatItem>.putIntoSections(revealedItems: Set<Long>): List<ChatSection>
|
||||
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<ChatItem>.putIntoSections(revealedItems: Set<Long>): List<ChatSection>
|
||||
}
|
||||
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<ChatItem>.putIntoSections(revealedItems: Set<Long>): List<ChatSection>
|
||||
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<ChatSection>.revealedItemCount(): Int {
|
||||
}
|
||||
|
||||
fun List<ChatSection>.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,8 +299,8 @@ fun ChatView(staleChatId: State<String?>, 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<String?>, 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 {
|
||||
|
||||
@@ -232,28 +232,28 @@ 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, 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user