wip
This commit is contained in:
parent
8e3bc01632
commit
02801e46b4
|
@ -35,7 +35,7 @@ final class WrapperChat: ObservableObject {
|
||||||
isOmemoEnabled = monalContact.isEncrypted
|
isOmemoEnabled = monalContact.isEncrypted
|
||||||
|
|
||||||
subscribe()
|
subscribe()
|
||||||
NotificationCenter.default.post(name: Notification.Name(kMonalNewMessageNotice), object: nil)
|
reloadMessages()
|
||||||
}
|
}
|
||||||
|
|
||||||
init?(with: Chat, account: Account) {
|
init?(with: Chat, account: Account) {
|
||||||
|
@ -56,7 +56,7 @@ final class WrapperChat: ObservableObject {
|
||||||
self.contact = contact
|
self.contact = contact
|
||||||
|
|
||||||
subscribe()
|
subscribe()
|
||||||
NotificationCenter.default.post(name: Notification.Name(kMonalNewMessageNotice), object: nil)
|
reloadMessages()
|
||||||
|
|
||||||
// guard let account = accounts.first(where: { $0.id == with.accountId }) else { return nil }
|
// 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
|
acc.setMAMQueryMostRecentFor(monalContact, before: lastStanzaId) { [weak self] msgs, _ in
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
|
||||||
self?.mamRequestInProgress = false
|
self?.mamRequestInProgress = false
|
||||||
if !(msgs ?? []).isEmpty {
|
print(msgs)
|
||||||
self?.refreshMessages()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,18 +120,25 @@ final class WrapperChat: ObservableObject {
|
||||||
|
|
||||||
private extension WrapperChat {
|
private extension WrapperChat {
|
||||||
func subscribe() {
|
func subscribe() {
|
||||||
let newMsg = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalNewMessageNotice), object: nil, queue: .main) { [weak self] _ in
|
let notificationNames = [
|
||||||
self?.refreshMessages()
|
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))
|
let messages = db.messages(forContact: contact.contactJid, forAccount: NSNumber(value: contact.ownerId))
|
||||||
.compactMap { obj -> Message? in
|
.compactMap { obj -> Message? in
|
||||||
guard let message = obj as? MLMessage else { return nil }
|
guard let message = obj as? MLMessage else { return nil }
|
||||||
|
|
|
@ -71,38 +71,29 @@ extension WrapperXMPP {
|
||||||
private extension WrapperXMPP {
|
private extension WrapperXMPP {
|
||||||
// Subsribe to monalxmpp events
|
// Subsribe to monalxmpp events
|
||||||
func subscribeToUpdates() {
|
func subscribeToUpdates() {
|
||||||
notificationObservers.append(
|
let notificationNames = [
|
||||||
NotificationCenter.default.addObserver(forName: Notification.Name(kMonalRefresh), object: nil, queue: .main) { [weak self] notification in
|
kMonalRefresh,
|
||||||
self?.processEvent(kMonalRefresh, notification.userInfo?["contact"])
|
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
|
// Process monalxmpp events
|
||||||
func processEvent(_ notificationName: String, _ object: Any?) {
|
func processEvent(_ notification: Notification) {
|
||||||
switch notificationName {
|
switch notification.name.rawValue {
|
||||||
case kMonalRefresh:
|
case kMonalRefresh:
|
||||||
print("refresh?")
|
print("refresh?")
|
||||||
|
|
||||||
case kMonalContactRefresh:
|
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 contact = Contact(mlContact) {
|
||||||
if let index = contacts.firstIndex(where: { $0.id == mlContact.id }) {
|
if let index = contacts.firstIndex(where: { $0.id == mlContact.id }) {
|
||||||
contacts[index] = contact
|
contacts[index] = contact
|
||||||
|
@ -114,10 +105,25 @@ private extension WrapperXMPP {
|
||||||
}
|
}
|
||||||
|
|
||||||
case kMonalContactRemoved:
|
case kMonalContactRemoved:
|
||||||
if let mlContact = object as? MLContact {
|
if let mlContact = notification.userInfo?["contact"] as? MLContact {
|
||||||
contacts = contacts.filter { $0.id != mlContact.id }
|
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:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue