Update publishers to manage the subscriptions manually

This commit is contained in:
QuentinArguillere 2024-01-02 17:27:16 +01:00
parent 81448d8006
commit d0ae11c880
4 changed files with 38 additions and 56 deletions

View file

@ -18,6 +18,8 @@
*/
// swiftlint:disable large_tuple
// swiftlint:disable line_length
import linphonesw
import Combine
import UniformTypeIdentifiers
@ -34,8 +36,9 @@ final class CoreContext: ObservableObject {
@Published var coreIsStarted: Bool = false
private var mCore: Core!
private var mIteratePublisher: AnyCancellable?
private var mIterateSuscription: AnyCancellable?
private var mCoreSuscriptions = Set<AnyCancellable?>()
private init() {
do {
try initialiseCore()
@ -89,7 +92,7 @@ final class CoreContext: ObservableObject {
self.mCore.callkitEnabled = true
self.mCore.pushNotificationEnabled = true
self.mCore.publisher?.onGlobalStateChanged?.postOnMainQueue { (cbVal: (core: Core, state: GlobalState, message: String)) in
self.mCoreSuscriptions.insert(self.mCore.publisher?.onGlobalStateChanged?.postOnMainQueue { (cbVal: (core: Core, state: GlobalState, message: String)) in
if cbVal.state == GlobalState.On {
self.defaultAccount = self.mCore.defaultAccount
self.coreIsStarted = true
@ -97,13 +100,13 @@ final class CoreContext: ObservableObject {
self.defaultAccount = nil
self.coreIsStarted = true
}
}
})
try? self.mCore.start()
// Create a Core listener to listen for the callback we need
// In this case, we want to know about the account registration status
self.mCore.publisher?.onConfiguringStatus?.postOnMainQueue { (cbVal: (core: Core, status: Config.ConfiguringState, message: String)) in
self.mCoreSuscriptions.insert(self.mCore.publisher?.onConfiguringStatus?.postOnMainQueue { (cbVal: (core: Core, status: Config.ConfiguringState, message: String)) in
NSLog("New configuration state is \(cbVal.status) = \(cbVal.message)\n")
if cbVal.status == Config.ConfiguringState.Successful {
ToastViewModel.shared.toastMessage = "Successful"
@ -112,11 +115,9 @@ final class CoreContext: ObservableObject {
ToastViewModel.shared.toastMessage = "Failed"
ToastViewModel.shared.displayToast.toggle()
}
}
})
self.mCore.publisher?.onAccountRegistrationStateChanged?.postOnMainQueue {(cbVal:
(core: Core, account: Account, state: RegistrationState, message: String)
) in
self.mCoreSuscriptions.insert(self.mCore.publisher?.onAccountRegistrationStateChanged?.postOnMainQueue { (cbVal: (core: Core, account: Account, state: RegistrationState, message: String)) in
// If account has been configured correctly, we will go through Progress and Ok states
// Otherwise, we will be Failed.
NSLog("New registration state is \(cbVal.state) for user id " +
@ -135,7 +136,9 @@ final class CoreContext: ObservableObject {
self.loggingInProgress = false
self.loggedIn = false
}
}.postOnCoreQueue { (cbVal: (core: Core, account: Account, state: RegistrationState, message: String)) in
})
self.mCoreSuscriptions.insert(self.mCore.publisher?.onAccountRegistrationStateChanged?.postOnCoreQueue { (cbVal: (core: Core, account: Account, state: RegistrationState, message: String)) in
// If registration failed, remove account from core
if cbVal.state != .Ok && cbVal.state != .Progress {
let params = cbVal.account.params
@ -148,13 +151,13 @@ final class CoreContext: ObservableObject {
cbVal.core.clearAllAuthInfo()
}
TelecomManager.shared.onAccountRegistrationStateChanged(core: cbVal.core, account: cbVal.account, state: cbVal.state, message: cbVal.message)
}
})
self.mCore.publisher?.onCallStateChanged?.postOnCoreQueue { (cbVal: (core: Core, call: Call, state: Call.State, message: String)) in
self.mCoreSuscriptions.insert(self.mCore.publisher?.onCallStateChanged?.postOnCoreQueue { (cbVal: (core: Core, call: Call, state: Call.State, message: String)) in
TelecomManager.shared.onCallStateChanged(core: cbVal.core, call: cbVal.call, state: cbVal.state, message: cbVal.message)
}
})
self.mCore.publisher?.onLogCollectionUploadStateChanged?.postOnMainQueue { (cbValue: (_: Core, _: Core.LogCollectionUploadState, info: String)) in
self.mCoreSuscriptions.insert(self.mCore.publisher?.onLogCollectionUploadStateChanged?.postOnMainQueue { (cbValue: (_: Core, _: Core.LogCollectionUploadState, info: String)) in
if cbValue.info.starts(with: "https") {
UIPasteboard.general.setValue(
cbValue.info,
@ -164,9 +167,9 @@ final class CoreContext: ObservableObject {
ToastViewModel.shared.toastMessage = "Success_copied_into_clipboard"
ToastViewModel.shared.displayToast.toggle()
}
}
})
self.mIteratePublisher = Timer.publish(every: 0.02, on: .main, in: .common)
self.mIterateSuscription = Timer.publish(every: 0.02, on: .main, in: .common)
.autoconnect()
.receive(on: coreQueue)
.sink { _ in
@ -202,3 +205,4 @@ final class CoreContext: ObservableObject {
}
// swiftlint:enable large_tuple
// swiftlint:enable line_length

View file

@ -19,6 +19,7 @@
import Foundation
import linphonesw
import Combine
class ContactAvatarModel: ObservableObject {
@ -30,7 +31,7 @@ class ContactAvatarModel: ObservableObject {
@Published var presenceStatus: ConsolidatedPresence
private var friendDelegate: FriendDelegate?
private var friendSuscription: AnyCancellable?
init(friend: Friend?, withPresence: Bool?) {
self.friend = friend
@ -51,22 +52,20 @@ class ContactAvatarModel: ObservableObject {
self.lastPresenceInfo = ""
}
if self.friendDelegate != nil {
self.friend!.removeDelegate(delegate: self.friendDelegate!)
self.friendDelegate = nil
if self.friendSuscription != nil {
self.friendSuscription = nil
}
addDelegate()
addSubscription()
} else {
self.lastPresenceInfo = ""
self.presenceStatus = .Offline
}
}
func addDelegate() {
func addSubscription() {
/*
self.friend?.publisher?.onPresenceReceived?.postOnMainQueue { (cbValue: (Friend)) in
friendSuscription = self.friend?.publisher?.onPresenceReceived?.postOnMainQueue { (cbValue: (Friend)) in
print("publisherpublisher onLogCollectionUploadStateChanged \(cbValue.address?.asStringUriOnly())")
self.presenceStatus = cbValue.consolidatedPresence
@ -80,37 +79,12 @@ class ContactAvatarModel: ObservableObject {
self.lastPresenceInfo = ""
}
}
*/
let newFriendDelegate = FriendDelegateStub(
onPresenceReceived: { (linphoneFriend: Friend) -> Void in
DispatchQueue.main.sync {
self.presenceStatus = linphoneFriend.consolidatedPresence
if linphoneFriend.consolidatedPresence == .Online || linphoneFriend.consolidatedPresence == .Busy {
if linphoneFriend.consolidatedPresence == .Online || linphoneFriend.presenceModel!.latestActivityTimestamp != -1 {
self.lastPresenceInfo = linphoneFriend.consolidatedPresence == .Online ? "Online" : self.getCallTime(startDate: linphoneFriend.presenceModel!.latestActivityTimestamp)
} else {
self.lastPresenceInfo = "Away"
}
} else {
self.lastPresenceInfo = ""
}
}
}
)
friendDelegate = newFriendDelegate
if friendDelegate != nil {
friend!.addDelegate(delegate: friendDelegate!)
}
}
func removeAllDelegate() {
if friendDelegate != nil {
func removeAllSuscription() {
if friendSuscription != nil {
presenceStatus = .Offline
friend!.removeDelegate(delegate: friendDelegate!)
friendDelegate = nil
friendSuscription = nil
}
}

View file

@ -18,6 +18,7 @@
*/
import linphonesw
import Combine
class HistoryListViewModel: ObservableObject {
@ -27,7 +28,7 @@ class HistoryListViewModel: ObservableObject {
var callLogsTmp: [CallLog] = []
var callLogsAddressToDelete = ""
var callLogSubscription : AnyCancellable?
init() {
computeCallLogsList()
}
@ -47,7 +48,7 @@ class HistoryListViewModel: ObservableObject {
}
}
core.publisher?.onCallLogUpdated?.postOnCoreQueue { (_: (_: Core, _: CallLog)) in
self.callLogSubscription = core.publisher?.onCallLogUpdated?.postOnCoreQueue { (_: (_: Core, _: CallLog)) in
print("publisherpublisher onCallLogUpdated")
let account = core.defaultAccount
let logs = account != nil ? account!.callLogs : core.callLogs

View file

@ -18,6 +18,7 @@
*/
import linphonesw
import Combine
final class MagicSearchSingleton: ObservableObject {
@ -40,6 +41,8 @@ final class MagicSearchSingleton: ObservableObject {
@Published var allContact = false
private var domainDefaultAccount = ""
var searchSubscription : AnyCancellable?
private init() {
coreContext.doOnCoreQueue { core in
self.domainDefaultAccount = core.defaultAccount?.params?.domain ?? ""
@ -47,7 +50,7 @@ final class MagicSearchSingleton: ObservableObject {
self.magicSearch = try? core.createMagicSearch()
self.magicSearch.limitedSearch = false
self.magicSearch.publisher?.onSearchResultsReceived?.postOnMainQueue { (magicSearch: MagicSearch) in
self.searchSubscription = self.magicSearch.publisher?.onSearchResultsReceived?.postOnMainQueue { (magicSearch: MagicSearch) in
self.needUpdateLastSearchContacts = true
var lastSearchFriend: [SearchResult] = []
@ -72,7 +75,7 @@ final class MagicSearchSingleton: ObservableObject {
})
self.contactsManager.avatarListModel.forEach { contactAvatarModel in
contactAvatarModel.removeAllDelegate()
contactAvatarModel.removeAllSuscription()
}
self.contactsManager.avatarListModel.removeAll()