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()
init() {
DDLog.add(DDOSLogger.sharedInstance, with: .all)
// DDLog.add(DDOSLogger.sharedInstance, with: .all)
MLProcessLock.initialize(forProcess: "MainApp")
}

View file

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