From 70267b6d3bf716c5fb9879cfc82c4d4ef8b43db3 Mon Sep 17 00:00:00 2001 From: QuentinArguillere Date: Tue, 10 Sep 2024 17:24:21 +0200 Subject: [PATCH] Replace publisher with delegate in ContactViewModel --- .../Contacts/ViewModel/ContactViewModel.swift | 46 +++++++++++-------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/Linphone/UI/Main/Contacts/ViewModel/ContactViewModel.swift b/Linphone/UI/Main/Contacts/ViewModel/ContactViewModel.swift index 283f3df41..38390ac93 100644 --- a/Linphone/UI/Main/Contacts/ViewModel/ContactViewModel.swift +++ b/Linphone/UI/Main/Contacts/ViewModel/ContactViewModel.swift @@ -34,7 +34,7 @@ class ContactViewModel: ObservableObject { @Published var operationInProgress: Bool = false @Published var displayedConversation: ConversationModel? - private var chatRoomSuscriptions = Set() + private var contactChatRoomDelegate: ChatRoomDelegate? init() {} @@ -178,14 +178,31 @@ class ContactViewModel: ObservableObject { } func chatRoomAddDelegate(core: Core, chatRoom: ChatRoom) { - self.chatRoomSuscriptions.insert(chatRoom.publisher?.onConferenceJoined?.postOnCoreQueue { (chatRoom: ChatRoom, _: EventLog) in + contactChatRoomDelegate = ChatRoomDelegateStub(onStateChanged: { (chatRoom: ChatRoom, state: ChatRoom.State) in + let state = chatRoom.state + let id = LinphoneUtils.getChatRoomId(room: chatRoom) + if state == ChatRoom.State.CreationFailed { + Log.error("\(StartConversationViewModel.TAG) Conversation \(id) creation has failed!") + if let delegate = self.contactChatRoomDelegate { + chatRoom.removeDelegate(delegate: delegate) + self.contactChatRoomDelegate = nil + } + DispatchQueue.main.async { + self.operationInProgress = false + ToastViewModel.shared.toastMessage = "Failed_to_create_conversation_error" + ToastViewModel.shared.displayToast = true + } + } + }, onConferenceJoined: { (chatRoom: ChatRoom, _: EventLog) in let state = chatRoom.state let id = LinphoneUtils.getChatRoomId(room: chatRoom) Log.info("\(StartConversationViewModel.TAG) Conversation \(id) \(chatRoom.subject ?? "") state changed: \(state)") if state == ChatRoom.State.Created { Log.info("\(StartConversationViewModel.TAG) Conversation \(id) successfully created") - self.chatRoomSuscriptions.removeAll() - + if let delegate = self.contactChatRoomDelegate { + chatRoom.removeDelegate(delegate: delegate) + self.contactChatRoomDelegate = nil + } let model = ConversationModel(chatRoom: chatRoom) if self.operationInProgress == false { DispatchQueue.main.async { @@ -204,21 +221,11 @@ class ContactViewModel: ObservableObject { } } else if state == ChatRoom.State.CreationFailed { Log.error("\(StartConversationViewModel.TAG) Conversation \(id) creation has failed!") - self.chatRoomSuscriptions.removeAll() - DispatchQueue.main.async { - self.operationInProgress = false - ToastViewModel.shared.toastMessage = "Failed_to_create_conversation_error" - ToastViewModel.shared.displayToast = true - } - } - }) - - self.chatRoomSuscriptions.insert(chatRoom.publisher?.onStateChanged?.postOnCoreQueue { (chatRoom: ChatRoom, state: ChatRoom.State) in - let state = chatRoom.state - let id = LinphoneUtils.getChatRoomId(room: chatRoom) - if state == ChatRoom.State.CreationFailed { - Log.error("\(StartConversationViewModel.TAG) Conversation \(id) creation has failed!") - self.chatRoomSuscriptions.removeAll() + + if let delegate = self.contactChatRoomDelegate { + chatRoom.removeDelegate(delegate: delegate) + self.contactChatRoomDelegate = nil + } DispatchQueue.main.async { self.operationInProgress = false ToastViewModel.shared.toastMessage = "Failed_to_create_conversation_error" @@ -226,6 +233,7 @@ class ContactViewModel: ObservableObject { } } }) + chatRoom.addDelegate(delegate: contactChatRoomDelegate!) } } // swiftlint:enable line_length