From cbe67fe3f1ca070c851db0999bd8ff42855f9b80 Mon Sep 17 00:00:00 2001 From: Woit Date: Fri, 6 Dec 2024 22:57:59 +0100 Subject: [PATCH] wip --- .../XMPP/Wrappers/WrapperChat.swift | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Monal/another.im/XMPP/Wrappers/WrapperChat.swift b/Monal/another.im/XMPP/Wrappers/WrapperChat.swift index 3cac784..b4550ad 100644 --- a/Monal/another.im/XMPP/Wrappers/WrapperChat.swift +++ b/Monal/another.im/XMPP/Wrappers/WrapperChat.swift @@ -86,7 +86,7 @@ final class WrapperChat: ObservableObject { func sendText(_ text: String) { let newMessageId = UUID().uuidString - _ = db.addMessageHistory( + let histId = db.addMessageHistory( to: contact.contactJid, forAccount: monalContact.accountID, withMessage: text, @@ -97,7 +97,9 @@ final class WrapperChat: ObservableObject { mimeType: nil, size: nil ) - print(newMessageId) + 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) } @@ -133,13 +135,14 @@ private extension WrapperChat { } func processEvent(notification: Notification) { - if let mlMessage = notification.userInfo?["message"] { - print("AAAAAAAAAA", mlMessage) + guard let mlMessage = notification.userInfo?["message"] as? MLMessage else { return } + guard let message = Message(mlMessage) else { return } + if let index = messages.firstIndex(where: { $0.id == message.id }) { + messages[index] = message + } else { + messages.append(message) + messages.sort { $0.timestamp > $1.timestamp } } - // switch notification.name.rawValue { - // default: - // break - // } } func reloadMessages() {