divided apiLoadMessages

This commit is contained in:
Avently
2025-01-21 18:54:11 +07:00
parent 253978952a
commit e17d1a0280
2 changed files with 35 additions and 29 deletions
@@ -1381,34 +1381,39 @@ private fun setChatTTL(
withBGApi {
try {
chatModel.controller.setChatTTL(rhId, chatInfo.chatType, chatInfo.apiId, chatTTL.value)
afterSetChatTTL(chatInfo, progressIndicator)
afterSetChatTTL(rhId, chatInfo, progressIndicator)
} catch (e: Exception) {
chatTTL.value = previousChatTTL
afterSetChatTTL(chatInfo, progressIndicator)
afterSetChatTTL(rhId, chatInfo, progressIndicator)
AlertManager.shared.showAlertMsg(generalGetString(MR.strings.error_changing_message_deletion), e.stackTraceToString())
}
}
}
private fun afterSetChatTTL(chatInfo: ChatInfo, progressIndicator: MutableState<Boolean>) {
withApi {
try {
// this is using current remote host on purpose - if it changes during update, it will load correct chats
// redirectDisabled is set to true to prevent redirecting the current chat/chat items in case the chat changes while messages were updating
apiLoadMessages(
chatModel.remoteHostId(),
chatInfo.chatType,
chatInfo.apiId,
contentTag = null,
pagination = ChatPagination.Initial(ChatPagination.INITIAL_COUNT),
replaceChat = true,
redirectDisabled = true
)
} catch (e: Exception) {
Log.e(TAG, "apiGetChat error: ${e.message}")
} finally {
progressIndicator.value = false
private suspend fun afterSetChatTTL(rhId: Long?, chatInfo: ChatInfo, progressIndicator: MutableState<Boolean>) {
try {
val pagination = ChatPagination.Initial(ChatPagination.INITIAL_COUNT)
val (chat, navInfo) = controller.apiGetChat(rhId, chatInfo.chatType, chatInfo.apiId, null, pagination) ?: return
if (chat.chatItems.isEmpty()) {
// replacing old chat with the same old chat but without items. Less intrusive way of clearing a preview
withChats {
val oldChat = getChat(chat.id)
if (oldChat != null) {
replaceChat(rhId, chatInfo.id, oldChat.copy(chatItems = emptyList()))
}
}
}
if (chat.remoteHostId != chatModel.remoteHostId() || chat.id != chatModel.chatId.value) return
processLoadedChat(
chat,
navInfo,
contentTag = null,
pagination = pagination
)
} catch (e: Exception) {
Log.e(TAG, "apiGetChat error: ${e.message}")
} finally {
progressIndicator.value = false
}
}
@@ -28,15 +28,22 @@ suspend fun apiLoadMessages(
contentTag: MsgContentTag?,
pagination: ChatPagination,
search: String = "",
visibleItemIndexesNonReversed: () -> IntRange = { 0 .. 0 },
replaceChat: Boolean = false,
redirectDisabled: Boolean = false
visibleItemIndexesNonReversed: () -> IntRange = { 0 .. 0 }
) = coroutineScope {
val (chat, navInfo) = chatModel.controller.apiGetChat(rhId, chatType, apiId, contentTag, pagination, search) ?: return@coroutineScope
// For .initial allow the chatItems to be empty as well as chatModel.chatId to not match this chat because these values become set after .initial finishes
if (((chatModel.chatId.value != chat.id || chat.chatItems.isEmpty()) && pagination !is ChatPagination.Initial && pagination !is ChatPagination.Last)
|| !isActive) return@coroutineScope
processLoadedChat(chat, navInfo, contentTag, pagination, visibleItemIndexesNonReversed)
}
suspend fun processLoadedChat(
chat: Chat,
navInfo: NavigationInfo,
contentTag: MsgContentTag?,
pagination: ChatPagination,
visibleItemIndexesNonReversed: () -> IntRange = { 0 .. 0 }
) = coroutineScope {
val chatState = chatModel.chatStateForContent(contentTag)
val (splits, unreadAfterItemId, totalAfter, unreadTotal, unreadAfter, unreadAfterNewestLoaded) = chatState
val oldItems = chatModel.chatItemsForContent(contentTag).value
@@ -49,8 +56,6 @@ suspend fun apiLoadMessages(
withChats {
if (getChat(chat.id) == null) {
addChat(chat)
} else if (replaceChat) {
replaceChat(chat.remoteHostId, chat.id, chat)
} else {
updateChatInfo(chat.remoteHostId, chat.chatInfo)
updateChatStats(chat.remoteHostId, chat.id, chat.chatStats)
@@ -58,10 +63,6 @@ suspend fun apiLoadMessages(
}
}
withChats(contentTag) {
if (redirectDisabled && chatModel.chatId.value != chat.id) {
// redirect is disabled, but the chat is not the current one, so don't update it
return@withChats
}
chatItemStatuses.clear()
chatItems.replaceAll(chat.chatItems)
chatModel.chatId.value = chat.chatInfo.id