From 413d4f1c72d2b55312c4d9ce00e7002bd89b41a1 Mon Sep 17 00:00:00 2001 From: Woit Date: Mon, 9 Dec 2024 20:49:39 +0100 Subject: [PATCH] wip --- Monal/Monal.xcodeproj/project.pbxproj | 12 ++--- .../XMPP/Wrappers/WrapperChat.swift | 49 +++++++++++-------- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/Monal/Monal.xcodeproj/project.pbxproj b/Monal/Monal.xcodeproj/project.pbxproj index 8f0d9b1..48ce45c 100644 --- a/Monal/Monal.xcodeproj/project.pbxproj +++ b/Monal/Monal.xcodeproj/project.pbxproj @@ -3749,7 +3749,7 @@ CODE_SIGN_ENTITLEMENTS = another.im/Resources/another.im.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 5; + CURRENT_PROJECT_VERSION = 6; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_ASSET_PATHS = ""; DEVELOPMENT_TEAM = K78H7BT98L; @@ -3840,7 +3840,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 5; + CURRENT_PROJECT_VERSION = 6; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_ASSET_PATHS = ""; DEVELOPMENT_TEAM = K78H7BT98L; @@ -3937,7 +3937,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 5; + CURRENT_PROJECT_VERSION = 6; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_ASSET_PATHS = ""; DEVELOPMENT_TEAM = K78H7BT98L; @@ -4034,7 +4034,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 5; + CURRENT_PROJECT_VERSION = 6; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_ASSET_PATHS = ""; DEVELOPMENT_TEAM = K78H7BT98L; @@ -4131,7 +4131,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 5; + CURRENT_PROJECT_VERSION = 6; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_ASSET_PATHS = ""; DEVELOPMENT_TEAM = K78H7BT98L; @@ -4228,7 +4228,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 5; + CURRENT_PROJECT_VERSION = 6; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_ASSET_PATHS = ""; DEVELOPMENT_TEAM = K78H7BT98L; diff --git a/Monal/another.im/XMPP/Wrappers/WrapperChat.swift b/Monal/another.im/XMPP/Wrappers/WrapperChat.swift index dd23c39..980455c 100644 --- a/Monal/another.im/XMPP/Wrappers/WrapperChat.swift +++ b/Monal/another.im/XMPP/Wrappers/WrapperChat.swift @@ -14,9 +14,9 @@ final class WrapperChat: ObservableObject { let contact: Contact let account: Account - private let xmpp: MLXMPPManager - private let db: DataLayer - private let monalContact: MLContact + private var xmpp: MLXMPPManager? + private var db: DataLayer? + private var monalContact: MLContact? private var notificationObservers: [AnyObject] = [] init?(with: Contact, account: Account) { @@ -26,7 +26,7 @@ final class WrapperChat: ObservableObject { self.account = account guard - let monalContact = db.contactList() + let monalContact = db?.contactList() .first(where: { $0.accountID.intValue == with.ownerId && $0.contactJid == with.contactJid @@ -45,7 +45,7 @@ final class WrapperChat: ObservableObject { self.account = account guard - let monalContact = db.contactList() + let monalContact = db?.contactList() .first(where: { $0.accountID.intValue == with.accountId && $0.contactJid == with.participantJid @@ -63,7 +63,16 @@ final class WrapperChat: ObservableObject { deinit { notificationObservers.forEach { NotificationCenter.default.removeObserver($0) } - print("Chat wrapper deinit") + // print("Chat wrapper deinit") + } + + func deactivate() { + db = nil + xmpp = nil + monalContact = nil + messages = [] + notificationObservers.forEach { NotificationCenter.default.removeObserver($0) } + // print("Chat wrapper deactivated") } var chatTitle: String { @@ -71,8 +80,9 @@ final class WrapperChat: ObservableObject { } func sendText(_ text: String) { + guard let monalContact else { return } let newMessageId = UUID().uuidString - let histId = db.addMessageHistory( + let histId = db?.addMessageHistory( to: contact.contactJid, forAccount: monalContact.accountID, withMessage: text, @@ -83,19 +93,20 @@ final class WrapperChat: ObservableObject { mimeType: nil, size: nil ) - if let newMlMessage = db.message(forHistoryID: histId), let message = Message(newMlMessage) { + if let newMlMessage = db?.message(forHistoryID: histId), let message = Message(newMlMessage) { messages.insert(message, at: 0) } - xmpp.sendMessage(text, to: monalContact, isEncrypted: monalContact.isEncrypted, isUpload: false, messageId: newMessageId) + xmpp?.sendMessage(text, to: monalContact, isEncrypted: monalContact.isEncrypted, isUpload: false, messageId: newMessageId) } func requestMAM() { + guard let monalContact else { return } guard let acc = monalContact.account else { return } if mamRequestInProgress { return } mamRequestInProgress = true - let lastStanzaId = messages.last?.messageId // last here because list is reversed - ?? db.lastStanzaId(forAccount: NSNumber(value: contact.ownerId)) + let lastStanzaId = messages.last?.stanzaId // last here because list is reversed + ?? db?.lastStanzaId(forAccount: NSNumber(value: contact.ownerId)) acc.setMAMQueryMostRecentFor(monalContact, before: lastStanzaId) { [weak self] msgs, _ in DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in @@ -107,12 +118,6 @@ final class WrapperChat: ObservableObject { } } } - - func deactivate() { - messages = [] - notificationObservers.forEach { NotificationCenter.default.removeObserver($0) } - print("Chat wrapper unsubscribed") - } } private extension WrapperChat { @@ -157,18 +162,20 @@ private extension WrapperChat { } func reloadMessages() { - let messages = db.messages(forContact: contact.contactJid, forAccount: NSNumber(value: contact.ownerId)) + 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 } return Message(message) } .sorted { $0.timestamp > $1.timestamp } - self.messages = messages + self.messages = messages ?? [] } func toggleOmemo(_ new: Bool) { - if monalContact.isEncrypted != new { - monalContact.toggleEncryption(new) + if let monalContact { + if monalContact.isEncrypted != new { + monalContact.toggleEncryption(new) + } } } }