From 34ff5bcf161677fb3bdd7c78899677ff05ecbba6 Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Wed, 29 Jan 2025 09:55:54 +0100 Subject: [PATCH] Refactor resetDisplayedChatRoom function for better performance and to avoid crashes --- .../ViewModel/ConversationViewModel.swift | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Linphone/UI/Main/Conversations/ViewModel/ConversationViewModel.swift b/Linphone/UI/Main/Conversations/ViewModel/ConversationViewModel.swift index 848dda5aa..52fef0bbd 100644 --- a/Linphone/UI/Main/Conversations/ViewModel/ConversationViewModel.swift +++ b/Linphone/UI/Main/Conversations/ViewModel/ConversationViewModel.swift @@ -1740,18 +1740,23 @@ class ConversationViewModel: ObservableObject { } func resetDisplayedChatRoom(conversationsList: [ConversationModel]) { + guard !conversationsList.isEmpty else { + Log.info("\(ConversationViewModel.TAG) The conversation list is empty.") + return + } + if !self.conversationMessagesSection.isEmpty && !self.conversationMessagesSection[0].rows.isEmpty { - if self.displayedConversation != nil { + if let displayedConversation = self.displayedConversation { conversationsList.forEach { conversation in - if conversation.id == self.displayedConversation!.id { + if conversation.id == displayedConversation.id { self.displayedConversation = conversation self.computeComposingLabel() - if self.displayedConversation != nil { + if let updatedDisplayedConversation = self.displayedConversation { CoreContext.shared.doOnCoreQueue { _ in - let historyEventsSizeTmp = self.displayedConversation!.chatRoom.historyEventsSize + let historyEventsSizeTmp = updatedDisplayedConversation.chatRoom.historyEventsSize if self.displayedConversationHistorySize < historyEventsSizeTmp { - let eventLogList = self.displayedConversation!.chatRoom.getHistoryRangeEvents(begin: 0, end: historyEventsSizeTmp - self.displayedConversationHistorySize) + let eventLogList = updatedDisplayedConversation.chatRoom.getHistoryRangeEvents(begin: 0, end: historyEventsSizeTmp - self.displayedConversationHistorySize) if !eventLogList.isEmpty { self.getNewMessages(eventLogs: eventLogList)