From e17d1a02805e946c9c325c3a356c60b760177d72 Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Tue, 21 Jan 2025 18:54:11 +0700 Subject: [PATCH] divided apiLoadMessages --- .../simplex/common/views/chat/ChatInfoView.kt | 45 ++++++++++--------- .../common/views/chat/ChatItemsLoader.kt | 19 ++++---- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatInfoView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatInfoView.kt index a07ba75abe..9131d332f3 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatInfoView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatInfoView.kt @@ -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) { - 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) { + 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 } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatItemsLoader.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatItemsLoader.kt index 849e2016f7..ebc6ec5207 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatItemsLoader.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatItemsLoader.kt @@ -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