This commit is contained in:
Woit 2024-12-06 08:55:59 +01:00
parent 006ae2a612
commit c8cf403d15
2 changed files with 51 additions and 35 deletions

View file

@ -13,28 +13,62 @@ final class WrapperChat: ObservableObject {
let contact: Contact let contact: Contact
private let monalContact: MLContact
private let account: Account
private let xmpp: MLXMPPManager private let xmpp: MLXMPPManager
private let db: DataLayer private let db: DataLayer
private let monalContact: MLContact
// private let account: Account
private var notificationObservers: [AnyObject] = [] private var notificationObservers: [AnyObject] = []
init(account: Account, contact: Contact, db: DataLayer, xmpp: MLXMPPManager) { init?(with: Contact) {
self.contact = contact contact = with
self.account = account xmpp = MLXMPPManager.sharedInstance()
self.db = db db = DataLayer.sharedInstance()
self.xmpp = xmpp
// swiftlint:disable:next force_unwrapping guard
monalContact = db.contactList().first { $0.accountID.intValue == contact.ownerId && $0.contactJid == contact.contactJid }! let monalContact = db.contactList()
.first(where: {
$0.accountID.intValue == with.ownerId
})
else { return nil }
self.monalContact = monalContact
isOmemoEnabled = monalContact.isEncrypted 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 { deinit {
notificationObservers.forEach { NotificationCenter.default.removeObserver($0) } notificationObservers.forEach { NotificationCenter.default.removeObserver($0) }
} }

View file

@ -56,30 +56,12 @@ extension WrapperXMPP {
} }
} }
func chat(with: Contact) -> WrapperChat { func chat(with: Contact) -> WrapperChat? {
// swiftlint:disable:next force_unwrapping WrapperChat(with: with)
let account = accounts.first { $0.id == with.ownerId }!
let chatModel = WrapperChat(account: account, contact: with, db: db, xmpp: xmpp)
return chatModel
} }
func chat(with: Chat) -> WrapperChat? { func chat(with: Chat) -> WrapperChat? {
guard let account = accounts.first(where: { $0.id == with.accountId }) else { return nil } WrapperChat(with: with)
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
} }
} }