From 3261cebd5f6bc5f2ace329a7fc8fe2afb2d04fd8 Mon Sep 17 00:00:00 2001 From: QuentinArguillere Date: Tue, 10 Sep 2024 17:38:27 +0200 Subject: [PATCH] Replace publisher with delegate in AccountModel --- Linphone/Core/CoreContext.swift | 8 ++-- Linphone/UI/Main/Viewmodel/AccountModel.swift | 40 ++++++++++++------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/Linphone/Core/CoreContext.swift b/Linphone/Core/CoreContext.swift index 1191cc080..090b8c4be 100644 --- a/Linphone/Core/CoreContext.swift +++ b/Linphone/Core/CoreContext.swift @@ -166,7 +166,7 @@ final class CoreContext: ObservableObject { var accountModels: [AccountModel] = [] for account in self.mCore.accountList { - accountModels.append(AccountModel(account: account, corePublisher: self.mCore.publisher)) + accountModels.append(AccountModel(account: account, core: self.mCore)) } DispatchQueue.main.async { self.coreIsStarted = true @@ -203,7 +203,7 @@ final class CoreContext: ObservableObject { Log.info("New configuration state is \(status) = \(message)\n") var accountModels: [AccountModel] = [] for account in self.mCore.accountList { - accountModels.append(AccountModel(account: account, corePublisher: self.mCore.publisher)) + accountModels.append(AccountModel(account: account, core: self.mCore)) } DispatchQueue.main.async { if status == ConfiguringState.Successful { @@ -281,7 +281,7 @@ final class CoreContext: ObservableObject { }, onAccountAdded: { (_: Core, _: Account) in var accountModels: [AccountModel] = [] for account in self.mCore.accountList { - accountModels.append(AccountModel(account: account, corePublisher: self.mCore.publisher)) + accountModels.append(AccountModel(account: account, core: self.mCore)) } DispatchQueue.main.async { self.accounts = accountModels @@ -289,7 +289,7 @@ final class CoreContext: ObservableObject { }, onAccountRemoved: { (_: Core, _: Account) in var accountModels: [AccountModel] = [] for account in self.mCore.accountList { - accountModels.append(AccountModel(account: account, corePublisher: self.mCore.publisher)) + accountModels.append(AccountModel(account: account, core: self.mCore)) } DispatchQueue.main.async { self.accounts = accountModels diff --git a/Linphone/UI/Main/Viewmodel/AccountModel.swift b/Linphone/UI/Main/Viewmodel/AccountModel.swift index b4c4a7dbd..b11c8933f 100644 --- a/Linphone/UI/Main/Viewmodel/AccountModel.swift +++ b/Linphone/UI/Main/Viewmodel/AccountModel.swift @@ -31,32 +31,42 @@ class AccountModel: ObservableObject { @Published var displayName: String = "" @Published var address: String = "" - private var mSuscriptions = Set() + private var accountDelegate: AccountDelegate? + private var coreDelegate: CoreDelegate? - init(account: Account, corePublisher: CoreDelegatePublisher?) { + init(account: Account, core: Core) { self.account = account - mSuscriptions.insert(account.publisher?.onRegistrationStateChanged?.postOnCoreQueue { _ in + accountDelegate = AccountDelegateStub(onRegistrationStateChanged: { (_: Account, _: RegistrationState, _: String) in self.update() }) - mSuscriptions.insert(corePublisher?.onChatRoomRead?.postOnCoreQueue( - receiveValue: { _ in - self.computeNotificationsCount() - })) - mSuscriptions.insert(corePublisher?.onMessagesReceived?.postOnCoreQueue( - receiveValue: { _ in - self.computeNotificationsCount() - })) - mSuscriptions.insert(corePublisher?.onCallStateChanged?.postOnCoreQueue( - receiveValue: { _ in - self.computeNotificationsCount() - })) + account.addDelegate(delegate: accountDelegate!) + + coreDelegate = CoreDelegateStub(onCallStateChanged: { (_: Core, _: Call, _: Call.State, _: String) in + self.computeNotificationsCount() + }, onMessagesReceived: { (_: Core, _: ChatRoom, _:[ChatMessage]) in + self.computeNotificationsCount() + }, onChatRoomRead: { (_: Core, _: ChatRoom) in + self.computeNotificationsCount() + }) + core.addDelegate(delegate: coreDelegate!) CoreContext.shared.doOnCoreQueue { _ in self.update() } } + deinit { + if let delegate = accountDelegate { + account.removeDelegate(delegate: delegate) + } + if let delegate = coreDelegate { + CoreContext.shared.doOnCoreQueue { core in + core.removeDelegate(delegate: delegate) + } + } + } + private func update() { let state = account.state var isDefault: Bool = false