This commit is contained in:
Woit 2024-12-04 16:00:37 +01:00
parent 6a4acfb6f3
commit f489425b99
2 changed files with 47 additions and 37 deletions

View file

@ -7,7 +7,7 @@ struct AnotherIMApp: App {
@StateObject var wrapper = WrapperXMPP() @StateObject var wrapper = WrapperXMPP()
init() { init() {
DDLog.add(DDOSLogger.sharedInstance, with: .all) // DDLog.add(DDOSLogger.sharedInstance, with: .all)
MLProcessLock.initialize(forProcess: "MainApp") MLProcessLock.initialize(forProcess: "MainApp")
} }

View file

@ -25,8 +25,9 @@ final class WrapperXMPP: ObservableObject {
// subscribe to monalxmpp notifications and fire notification for update // subscribe to monalxmpp notifications and fire notification for update
subscribeToUpdates() subscribeToUpdates()
xmpp.reconnectAll() processOnStart()
NotificationCenter.default.post(name: Notification.Name(kMonalRefresh), object: nil) // xmpp.reconnectAll()
// NotificationCenter.default.post(name: Notification.Name(kMonalRefresh), object: nil)
} }
deinit { deinit {
@ -42,7 +43,7 @@ extension WrapperXMPP {
if !result { if !result {
throw AimErrors.loginError throw AimErrors.loginError
} else { } else {
NotificationCenter.default.post(name: Notification.Name(kMonalRefresh), object: nil) processOnStart()
} }
} }
@ -90,37 +91,41 @@ extension WrapperXMPP {
// MARK: - Handle notifications // MARK: - Handle notifications
private extension WrapperXMPP { private extension WrapperXMPP {
// Subsribe to monalxmpp events
func subscribeToUpdates() { func subscribeToUpdates() {
// General notificationObservers.append(
let generalRefresh = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalRefresh), object: nil, queue: .main) { [weak self] _ in NotificationCenter.default.addObserver(forName: Notification.Name(kMonalRefresh), object: nil, queue: .main) { [weak self] notification in
self?.refreshAccounts() self?.processEvent(kMonalRefresh, notification.object)
self?.refreshContacts() }
self?.refreshChats() )
} notificationObservers.append(
notificationObservers.append(generalRefresh) NotificationCenter.default.addObserver(forName: Notification.Name(kMonalContactRefresh), object: nil, queue: .main) { [weak self] notification in
self?.processEvent(kMonalContactRefresh, notification.object)
// For contacts }
let contactRefresh = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalContactRefresh), object: nil, queue: .main) { [weak self] _ in )
self?.refreshContacts() notificationObservers.append(
self?.refreshChats() NotificationCenter.default.addObserver(forName: Notification.Name(kMonalContactRemoved), object: nil, queue: .main) { [weak self] notification in
} self?.processEvent(kMonalContactRemoved, notification.object)
let contactRemove = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalContactRemoved), object: nil, queue: .main) { [weak self] _ in }
self?.refreshContacts() )
self?.refreshChats()
}
notificationObservers.append(contentsOf: [contactRefresh, contactRemove])
} }
func refreshAccounts() { // Process monalxmpp events
func processEvent(_ notificationName: String, _ object: Any?) {
print(notificationName, object)
}
// Initital monalxmpp db fetch
func processOnStart() {
// get all accounts and contacts once
let accounts = db.accountList() let accounts = db.accountList()
.compactMap { dict -> Account? in .compactMap { dict -> Account? in
guard let dict = dict as? NSDictionary else { return nil } guard let dict = dict as? NSDictionary else { return nil }
return Account(dict) return Account(dict)
} }
self.accounts = accounts self.accounts = accounts
xmpp.connectIfNecessary()
// mark accounts availability // check if active accounts existed
if accounts.isEmpty { if accounts.isEmpty {
accountsAvailability = .noAccounts accountsAvailability = .noAccounts
} else if accounts.filter({ $0.isEnabled }).isEmpty { } else if accounts.filter({ $0.isEnabled }).isEmpty {
@ -128,20 +133,25 @@ private extension WrapperXMPP {
} else { } else {
accountsAvailability = .someEnabled accountsAvailability = .someEnabled
} }
}
func refreshContacts() { // get all contacts
contacts = db.contactList() if !accounts.isEmpty {
.filter { $0.isSubscribedTo || $0.hasOutgoingContactRequest || $0.isSubscribedFrom } contacts = db.contactList()
.filter { !$0.isSelfChat } // removed for now .filter { $0.isSubscribedTo || $0.hasOutgoingContactRequest || $0.isSubscribedFrom }
.compactMap { Contact($0) } .filter { !$0.isSelfChat } // removed for now
} .compactMap { Contact($0) }
func refreshChats() { // get active chats
activeChats = db.activeContacts(withPinned: false) if !contacts.isEmpty {
.compactMap { activeChats = db.activeContacts(withPinned: false)
guard let contact = $0 as? MLContact else { return nil } .compactMap {
return Chat(contact) guard let contact = $0 as? MLContact else { return nil }
return Chat(contact)
}
} }
}
// try reconnect active clients
xmpp.connectIfNecessary()
} }
} }