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() {