From 6034d41a10229696fab21c8bdb4d9c48aa57399e Mon Sep 17 00:00:00 2001 From: QuentinArguillere Date: Fri, 27 Sep 2024 10:37:45 +0200 Subject: [PATCH] Replace chatRoomSuscriptions with delegates in ConversationViewModel --- .../ViewModel/ConversationViewModel.swift | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/Linphone/UI/Main/Conversations/ViewModel/ConversationViewModel.swift b/Linphone/UI/Main/Conversations/ViewModel/ConversationViewModel.swift index 34309dc91..30f0dec23 100644 --- a/Linphone/UI/Main/Conversations/ViewModel/ConversationViewModel.swift +++ b/Linphone/UI/Main/Conversations/ViewModel/ConversationViewModel.swift @@ -36,7 +36,7 @@ class ConversationViewModel: ObservableObject { @Published var messageText: String = "" - private var chatRoomSuscriptions = Set() + private var chatRoomDelegate: ChatRoomDelegate? private var chatMessageSuscriptions = Set() @Published var conversationMessagesSection: [MessagesSection] = [] @@ -77,13 +77,12 @@ class ConversationViewModel: ObservableObject { func addConversationDelegate() { coreContext.doOnCoreQueue { _ in if self.displayedConversation != nil { - self.chatRoomSuscriptions.insert(self.displayedConversation?.chatRoom.publisher?.onChatMessageSending?.postOnCoreQueue { (cbValue: (chatRoom: ChatRoom, eventLog: EventLog)) in - self.getNewMessages(eventLogs: [cbValue.eventLog]) - }) - - self.chatRoomSuscriptions.insert(self.displayedConversation?.chatRoom.publisher?.onChatMessagesReceived?.postOnCoreQueue { (cbValue: (chatRoom: ChatRoom, eventLogs: [EventLog])) in - self.getNewMessages(eventLogs: cbValue.eventLogs) + self.chatRoomDelegate = ChatRoomDelegateStub(onChatMessagesReceived: { (_: ChatRoom, eventLogs: [EventLog]) in + self.getNewMessages(eventLogs: eventLogs) + }, onChatMessageSending: { (_: ChatRoom, eventLog: EventLog) in + self.getNewMessages(eventLogs: [eventLog]) }) + self.displayedConversation?.chatRoom.addDelegate(delegate: self.chatRoomDelegate!) } } } @@ -181,7 +180,10 @@ class ConversationViewModel: ObservableObject { } func removeConversationDelegate() { - self.chatRoomSuscriptions.removeAll() + if let crDelegate = self.chatRoomDelegate { + self.displayedConversation?.chatRoom.removeDelegate(delegate: crDelegate) + } + self.chatRoomDelegate = nil self.chatMessageSuscriptions.removeAll() } @@ -1341,19 +1343,16 @@ class ConversationViewModel: ObservableObject { func resetDisplayedChatRoom(conversationsList: [ConversationModel]) { removeConversationDelegate() - if self.displayedConversation != nil { conversationsList.forEach { conversation in if conversation.id == self.displayedConversation!.id { self.displayedConversation = conversation - - self.chatRoomSuscriptions.insert(conversation.chatRoom.publisher?.onChatMessageSending?.postOnCoreQueue { (cbValue: (chatRoom: ChatRoom, eventLog: EventLog)) in - self.getNewMessages(eventLogs: [cbValue.eventLog]) - }) - - self.chatRoomSuscriptions.insert(conversation.chatRoom.publisher?.onChatMessagesReceived?.postOnCoreQueue { (cbValue: (chatRoom: ChatRoom, eventLogs: [EventLog])) in - self.getNewMessages(eventLogs: cbValue.eventLogs) + self.chatRoomDelegate = ChatRoomDelegateStub(onChatMessagesReceived: { (_: ChatRoom, eventLogs: [EventLog]) in + self.getNewMessages(eventLogs: eventLogs) + }, onChatMessageSending: { (_: ChatRoom, eventLog: EventLog) in + self.getNewMessages(eventLogs: [eventLog]) }) + self.displayedConversation?.chatRoom.addDelegate(delegate: self.chatRoomDelegate!) } } }