From f197c4174cd851bc7765748149cf996c52d2f466 Mon Sep 17 00:00:00 2001 From: Diogo Date: Fri, 1 Nov 2024 18:20:05 +0000 Subject: [PATCH] stability improvements and race conditions --- .../kotlin/chat/simplex/common/model/ChatModel.kt | 4 ++++ .../kotlin/chat/simplex/common/views/chat/ChatSections.kt | 6 ++++++ .../kotlin/chat/simplex/common/views/chat/ChatView.kt | 2 +- .../simplex/common/views/chatlist/ChatListNavLinkView.kt | 2 +- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt index eab3c891fb..bc9d1cf91a 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt @@ -331,6 +331,7 @@ object ChatModel { if (chatId.value == cInfo.id) { // Prevent situation when chat item already in the list received from backend if (chatItems.value.none { it.id == cItem.id }) { + chatItemsSectionArea[cItem.id] = ChatSectionArea.Bottom if (chatItems.value.lastOrNull()?.id == ChatItem.TEMP_LIVE_CHAT_ITEM_ID) { chatItems.add(kotlin.math.max(0, chatItems.value.lastIndex), cItem) } else { @@ -379,6 +380,7 @@ object ChatModel { } else { cItem } + chatItemsSectionArea[ci.id] = ChatSectionArea.Bottom chatItems.add(ci) true } @@ -610,6 +612,7 @@ object ChatModel { val cItem = ChatItem.liveDummy(chatInfo is ChatInfo.Direct) withContext(Dispatchers.Main) { chatItems.add(cItem) + chatItemsSectionArea[cItem.id] = ChatSectionArea.Bottom } return cItem } @@ -617,6 +620,7 @@ object ChatModel { fun removeLiveDummy() { if (chatItems.value.lastOrNull()?.id == ChatItem.TEMP_LIVE_CHAT_ITEM_ID) { chatItems.removeLast() + chatItemsSectionArea.remove(ChatItem.TEMP_LIVE_CHAT_ITEM_ID) } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatSections.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatSections.kt index 5e9bcf5dca..6a22de4556 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatSections.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatSections.kt @@ -261,17 +261,23 @@ suspend fun apiLoadBottomSection(chatInfo: ChatInfo, rhId: Long?) { withContext(Dispatchers.Main) { val updatedItems = chatModel.chatItems.value.toMutableStateList() var insertIndex = updatedItems.size + var needsMerge = false for (cItem in chat.first.chatItems.asReversed()) { if (chatModel.chatItemsSectionArea[cItem.id] == null) { updatedItems.add(insertIndex, cItem) chatModel.chatItemsSectionArea[cItem.id] = ChatSectionArea.Bottom } else { + needsMerge = true chatModel.chatItemsSectionArea[cItem.id] = ChatSectionArea.Bottom insertIndex = max(0, insertIndex - 1) } } + if (needsMerge) { + updatedItems.associateTo(chatModel.chatItemsSectionArea) { it.id to ChatSectionArea.Bottom } + } + chatModel.chatItems.replaceAll(updatedItems) } } \ No newline at end of file diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt index dbe72dfa83..e78711f0e5 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt @@ -1005,7 +1005,7 @@ fun BoxScope.ChatItemsList( LaunchedEffect(Unit) { launch { - snapshotFlow { chatModel.chatId.value } + snapshotFlow { chatInfoUpdated.value.id } .distinctUntilChanged() .collect { if (revealedItems.value.isNotEmpty()) { diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatListNavLinkView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatListNavLinkView.kt index 183e787fb1..0c387d8d66 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatListNavLinkView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatListNavLinkView.kt @@ -229,7 +229,7 @@ fun openLoadedChat(chat: Chat, chatModel: ChatModel, landingSection: ChatLanding chatModel.chatItems.replaceAll(chat.chatItems) chatModel.chatId.value = chat.chatInfo.id chatModel.chatItemsSectionArea.clear() - chat.chatItems.associateTo(chatModel.chatItemsSectionArea) { it.id to landingSectionToArea(landingSection) } + chatModel.chatItems.value.associateTo(chatModel.chatItemsSectionArea) { it.id to landingSectionToArea(landingSection) } } suspend fun apiLoadMessages(