diff --git a/Monal/another.im/XMPP/Wrappers/WrapperChat.swift b/Monal/another.im/XMPP/Wrappers/WrapperChat.swift index 9b4c6dc..b638c57 100644 --- a/Monal/another.im/XMPP/Wrappers/WrapperChat.swift +++ b/Monal/another.im/XMPP/Wrappers/WrapperChat.swift @@ -13,28 +13,62 @@ final class WrapperChat: ObservableObject { let contact: Contact - private let monalContact: MLContact - private let account: Account private let xmpp: MLXMPPManager private let db: DataLayer + private let monalContact: MLContact + // private let account: Account private var notificationObservers: [AnyObject] = [] - init(account: Account, contact: Contact, db: DataLayer, xmpp: MLXMPPManager) { - self.contact = contact - self.account = account - self.db = db - self.xmpp = xmpp + init?(with: Contact) { + contact = with + xmpp = MLXMPPManager.sharedInstance() + db = DataLayer.sharedInstance() - // swiftlint:disable:next force_unwrapping - monalContact = db.contactList().first { $0.accountID.intValue == contact.ownerId && $0.contactJid == contact.contactJid }! + guard + let monalContact = db.contactList() + .first(where: { + $0.accountID.intValue == with.ownerId + }) + else { return nil } + self.monalContact = monalContact isOmemoEnabled = monalContact.isEncrypted - - subscribe() - NotificationCenter.default.post(name: Notification.Name(kMonalNewMessageNotice), object: nil) - - // } + init?(with _: Chat) { + // guard let account = accounts.first(where: { $0.id == with.accountId }) else { return nil } + // + // var contact = contacts.first(where: { $0.ownerId == with.accountId && $0.contactJid == with.participantJid }) + // if contact == nil { + // let semaphore = DispatchSemaphore(value: 0) + // Task { + // await addContact(contactJid: with.participantJid, forAccountID: with.accountId) + // semaphore.signal() + // } + // semaphore.wait() + // contact = contacts.first(where: { $0.ownerId == with.accountId && $0.contactJid == with.participantJid }) + // } + // + // guard let contact else { return nil } + // let chatModel = WrapperChat(account: account, contact: contact, db: db, xmpp: xmpp) + // return chatModel + nil + } + + // init(account: Account, contact: Contact, db: DataLayer, xmpp: MLXMPPManager) { + // self.contact = contact + // self.account = account + // self.db = db + // self.xmpp = xmpp + // + // monalContact = db.contactList().first { $0.accountID.intValue == contact.ownerId && $0.contactJid == contact.contactJid }! + // isOmemoEnabled = monalContact.isEncrypted + // + // subscribe() + // NotificationCenter.default.post(name: Notification.Name(kMonalNewMessageNotice), object: nil) + // + // // + // } + deinit { notificationObservers.forEach { NotificationCenter.default.removeObserver($0) } } diff --git a/Monal/another.im/XMPP/Wrappers/WrapperXMPP.swift b/Monal/another.im/XMPP/Wrappers/WrapperXMPP.swift index 9d07dc8..436eb98 100644 --- a/Monal/another.im/XMPP/Wrappers/WrapperXMPP.swift +++ b/Monal/another.im/XMPP/Wrappers/WrapperXMPP.swift @@ -56,30 +56,12 @@ extension WrapperXMPP { } } - func chat(with: Contact) -> WrapperChat { - // swiftlint:disable:next force_unwrapping - let account = accounts.first { $0.id == with.ownerId }! - let chatModel = WrapperChat(account: account, contact: with, db: db, xmpp: xmpp) - return chatModel + func chat(with: Contact) -> WrapperChat? { + WrapperChat(with: with) } func chat(with: Chat) -> WrapperChat? { - guard let account = accounts.first(where: { $0.id == with.accountId }) else { return nil } - - var contact = contacts.first(where: { $0.ownerId == with.accountId && $0.contactJid == with.participantJid }) - if contact == nil { - let semaphore = DispatchSemaphore(value: 0) - Task { - await addContact(contactJid: with.participantJid, forAccountID: with.accountId) - semaphore.signal() - } - semaphore.wait() - contact = contacts.first(where: { $0.ownerId == with.accountId && $0.contactJid == with.participantJid }) - } - - guard let contact else { return nil } - let chatModel = WrapperChat(account: account, contact: contact, db: db, xmpp: xmpp) - return chatModel + WrapperChat(with: with) } }