diff --git a/Monal/another.im/XMPP/Wrappers/WrapperChat.swift b/Monal/another.im/XMPP/Wrappers/WrapperChat.swift index 20655e9..98b2133 100644 --- a/Monal/another.im/XMPP/Wrappers/WrapperChat.swift +++ b/Monal/another.im/XMPP/Wrappers/WrapperChat.swift @@ -35,7 +35,7 @@ final class WrapperChat: ObservableObject { isOmemoEnabled = monalContact.isEncrypted subscribe() - NotificationCenter.default.post(name: Notification.Name(kMonalNewMessageNotice), object: nil) + reloadMessages() } init?(with: Chat, account: Account) { @@ -56,7 +56,7 @@ final class WrapperChat: ObservableObject { self.contact = contact subscribe() - NotificationCenter.default.post(name: Notification.Name(kMonalNewMessageNotice), object: nil) + reloadMessages() // guard let account = accounts.first(where: { $0.id == with.accountId }) else { return nil } // @@ -112,9 +112,7 @@ final class WrapperChat: ObservableObject { acc.setMAMQueryMostRecentFor(monalContact, before: lastStanzaId) { [weak self] msgs, _ in DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in self?.mamRequestInProgress = false - if !(msgs ?? []).isEmpty { - self?.refreshMessages() - } + print(msgs) } } } @@ -122,18 +120,25 @@ final class WrapperChat: ObservableObject { private extension WrapperChat { func subscribe() { - let newMsg = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalNewMessageNotice), object: nil, queue: .main) { [weak self] _ in - self?.refreshMessages() + let notificationNames = [ + kMonalNewMessageNotice, + kMonalHistoryMessagesNotice + ] + notificationObservers = notificationNames.map { name in + NotificationCenter.default.addObserver(forName: Notification.Name(name), object: nil, queue: .main) { [weak self] notification in + self?.processEvent(notification: notification) + } } - notificationObservers.append(newMsg) - - let sentMsg = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalSentMessageNotice), object: nil, queue: .main) { [weak self] _ in - self?.refreshMessages() - } - notificationObservers.append(sentMsg) } - func refreshMessages() { + func processEvent(notification: Notification) { + switch notification.name.rawValue { + default: + break + } + } + + func reloadMessages() { let messages = db.messages(forContact: contact.contactJid, forAccount: NSNumber(value: contact.ownerId)) .compactMap { obj -> Message? in guard let message = obj as? MLMessage else { return nil } diff --git a/Monal/another.im/XMPP/Wrappers/WrapperXMPP.swift b/Monal/another.im/XMPP/Wrappers/WrapperXMPP.swift index 2865913..1da84a1 100644 --- a/Monal/another.im/XMPP/Wrappers/WrapperXMPP.swift +++ b/Monal/another.im/XMPP/Wrappers/WrapperXMPP.swift @@ -71,38 +71,29 @@ extension WrapperXMPP { private extension WrapperXMPP { // Subsribe to monalxmpp events func subscribeToUpdates() { - notificationObservers.append( - NotificationCenter.default.addObserver(forName: Notification.Name(kMonalRefresh), object: nil, queue: .main) { [weak self] notification in - self?.processEvent(kMonalRefresh, notification.userInfo?["contact"]) + let notificationNames = [ + kMonalRefresh, + kMonalContactRefresh, + kMonalContactRemoved, + kMLResourceBoundNotice, + kMonalNewMessageNotice, + kMonalHistoryMessagesNotice + ] + notificationObservers = notificationNames.map { name in + NotificationCenter.default.addObserver(forName: Notification.Name(name), object: nil, queue: .main) { [weak self] notification in + self?.processEvent(notification) } - ) - notificationObservers.append( - NotificationCenter.default.addObserver(forName: Notification.Name(kMonalContactRefresh), object: nil, queue: .main) { [weak self] notification in - self?.processEvent(kMonalContactRefresh, notification.userInfo?["contact"]) - } - ) - notificationObservers.append( - NotificationCenter.default.addObserver(forName: Notification.Name(kMonalContactRemoved), object: nil, queue: .main) { [weak self] notification in - self?.processEvent(kMonalContactRemoved, notification.userInfo?["contact"]) - } - ) - notificationObservers.append( - NotificationCenter.default.addObserver(forName: Notification.Name(kMLResourceBoundNotice), object: nil, queue: .main) { [weak self] _ in - self?.processOnStart() - // DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { - // } - } - ) + } } // Process monalxmpp events - func processEvent(_ notificationName: String, _ object: Any?) { - switch notificationName { + func processEvent(_ notification: Notification) { + switch notification.name.rawValue { case kMonalRefresh: print("refresh?") case kMonalContactRefresh: - if let mlContact = object as? MLContact, !mlContact.isSelfChat { + if let mlContact = notification.userInfo?["contact"] as? MLContact, !mlContact.isSelfChat { if let contact = Contact(mlContact) { if let index = contacts.firstIndex(where: { $0.id == mlContact.id }) { contacts[index] = contact @@ -114,10 +105,25 @@ private extension WrapperXMPP { } case kMonalContactRemoved: - if let mlContact = object as? MLContact { + if let mlContact = notification.userInfo?["contact"] as? MLContact { contacts = contacts.filter { $0.id != mlContact.id } } + case kMLResourceBoundNotice: + processOnStart() + + case kMonalNewMessageNotice, kMonalHistoryMessagesNotice: + if let mlContact = notification.userInfo?["contact"] as? MLContact, !mlContact.isSelfChat { + let contactJid = mlContact.contactJid + let accountId = mlContact.account?.accountID.intValue ?? 0 + if activeChats.first(where: { $0.accountId == accountId && $0.participantJid == contactJid }) == nil { + if let chat = Chat(mlContact) { + activeChats.append(chat) + activeChats.sort { $0.participantJid < $1.participantJid } + } + } + } + default: break }