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(generalRefresh) )
notificationObservers.append(
// For contacts NotificationCenter.default.addObserver(forName: Notification.Name(kMonalContactRefresh), object: nil, queue: .main) { [weak self] notification in
let contactRefresh = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalContactRefresh), object: nil, queue: .main) { [weak self] _ in self?.processEvent(kMonalContactRefresh, notification.object)
self?.refreshContacts()
self?.refreshChats()
} }
let contactRemove = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalContactRemoved), 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)
} }
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,16 +133,16 @@ private extension WrapperXMPP {
} else { } else {
accountsAvailability = .someEnabled accountsAvailability = .someEnabled
} }
}
func refreshContacts() { // get all contacts
if !accounts.isEmpty {
contacts = db.contactList() contacts = db.contactList()
.filter { $0.isSubscribedTo || $0.hasOutgoingContactRequest || $0.isSubscribedFrom } .filter { $0.isSubscribedTo || $0.hasOutgoingContactRequest || $0.isSubscribedFrom }
.filter { !$0.isSelfChat } // removed for now .filter { !$0.isSelfChat } // removed for now
.compactMap { Contact($0) } .compactMap { Contact($0) }
}
func refreshChats() { // get active chats
if !contacts.isEmpty {
activeChats = db.activeContacts(withPinned: false) activeChats = db.activeContacts(withPinned: false)
.compactMap { .compactMap {
guard let contact = $0 as? MLContact else { return nil } guard let contact = $0 as? MLContact else { return nil }
@ -145,3 +150,8 @@ private extension WrapperXMPP {
} }
} }
} }
// try reconnect active clients
xmpp.connectIfNecessary()
}
}