From f84ac713d7538588e9da3f89e67bd2efabfc3ab8 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Wed, 10 Jul 2024 18:50:23 +0400 Subject: [PATCH] android: lookup group members via map (#4432) --- .../chat/simplex/common/model/ChatModel.kt | 24 ++++++++++++++++--- .../simplex/common/views/chat/ChatView.kt | 1 + .../views/chatlist/ChatListNavLinkView.kt | 2 ++ 3 files changed, 24 insertions(+), 3 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 795baece0c..5e2cf9481c 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 @@ -65,6 +65,7 @@ object ChatModel { val deletedChats = mutableStateOf>>(emptyList()) val chatItemStatuses = mutableMapOf() val groupMembers = mutableStateListOf() + val groupMembersIndexes = mutableStateMapOf() val terminalItems = mutableStateOf>(listOf()) val userAddress = mutableStateOf(null) @@ -170,7 +171,23 @@ object ChatModel { fun getChat(id: String): Chat? = chats.toList().firstOrNull { it.id == id } fun getContactChat(contactId: Long): Chat? = chats.toList().firstOrNull { it.chatInfo is ChatInfo.Direct && it.chatInfo.apiId == contactId } fun getGroupChat(groupId: Long): Chat? = chats.toList().firstOrNull { it.chatInfo is ChatInfo.Group && it.chatInfo.apiId == groupId } - fun getGroupMember(groupMemberId: Long): GroupMember? = groupMembers.firstOrNull { it.groupMemberId == groupMemberId } + + fun populateGroupMembersIndexes() { + groupMembersIndexes.clear() + groupMembers.forEachIndexed { i, member -> + groupMembersIndexes[member.groupMemberId] = i + } + } + + fun getGroupMember(groupMemberId: Long): GroupMember? { + val memberIndex = groupMembersIndexes[groupMemberId] + return if (memberIndex != null) { + groupMembers[memberIndex] + } else { + null + } + } + private fun getChatIndex(rhId: Long?, id: String): Int = chats.toList().indexOfFirst { it.id == id && it.remoteHostId == rhId } fun addChat(chat: Chat) = chats.add(index = 0, chat) @@ -620,12 +637,13 @@ object ChatModel { } // update current chat return if (chatId.value == groupInfo.id) { - val memberIndex = groupMembers.indexOfFirst { it.groupMemberId == member.groupMemberId } - if (memberIndex >= 0) { + val memberIndex = groupMembersIndexes[member.groupMemberId] + if (memberIndex != null) { groupMembers[memberIndex] = member false } else { groupMembers.add(member) + groupMembersIndexes[member.groupMemberId] = groupMembers.size - 1 true } } else { 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 ec34a73d31..0cd33d659b 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 @@ -159,6 +159,7 @@ fun ChatView(chatId: String, chatModel: ChatModel, onComposed: suspend (chatId: AudioPlayer.stop() chatModel.chatId.value = null chatModel.groupMembers.clear() + chatModel.groupMembersIndexes.clear() }, info = { if (ModalManager.end.hasModalsOpen()) { 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 2324d62ea3..d06e2ae88e 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 @@ -253,7 +253,9 @@ suspend fun setGroupMembers(rhId: Long?, groupInfo: GroupInfo, chatModel: ChatMo } } chatModel.groupMembers.clear() + chatModel.groupMembersIndexes.clear() chatModel.groupMembers.addAll(newMembers) + chatModel.populateGroupMembersIndexes() } @Composable