diff --git a/Monal/another.im/Views/Main/ChatList/ChatsListScreen.swift b/Monal/another.im/Views/Main/ChatList/ChatsListScreen.swift index e0d8b39..228e046 100644 --- a/Monal/another.im/Views/Main/ChatList/ChatsListScreen.swift +++ b/Monal/another.im/Views/Main/ChatList/ChatsListScreen.swift @@ -49,7 +49,6 @@ private struct ChatsRow: View { var chat: Chat var body: some View { - Text("dump") SharedListRow(iconType: .charCircle(chat.name), text: chat.name, controlType: .none) .onTapGesture { Task { @@ -61,12 +60,15 @@ private struct ChatsRow: View { } do { - // try? await clientsStore.addRosterForNewChatIfNeeded(chat) - // let (messages, attachments, settings) = try await clientsStore.conversationStores(for: chat) - // router.showScreen(.push) { _ in - // ConversationScreen(messagesStore: messages, attachments: attachments, settings: settings) - // .navigationBarHidden(true) - // } + guard let model = wrapper.chat(with: chat) else { + throw AimErrors.wrongContact + } + + router.showScreen(.push) { _ in + ConversationScreen() + .navigationBarHidden(true) + .environmentObject(model) + } } catch { router.showAlert( .alert, diff --git a/Monal/another.im/XMPP/AimErrors.swift b/Monal/another.im/XMPP/AimErrors.swift index 2ac6658..42c3f7f 100644 --- a/Monal/another.im/XMPP/AimErrors.swift +++ b/Monal/another.im/XMPP/AimErrors.swift @@ -1,4 +1,5 @@ enum AimErrors: Error { case loginError case contactRemoveError + case wrongContact } diff --git a/Monal/another.im/XMPP/MonalXmppWrapper.swift b/Monal/another.im/XMPP/MonalXmppWrapper.swift index d74d5c1..7f9c998 100644 --- a/Monal/another.im/XMPP/MonalXmppWrapper.swift +++ b/Monal/another.im/XMPP/MonalXmppWrapper.swift @@ -23,6 +23,9 @@ final class MonalXmppWrapper: ObservableObject { // subscribe to monalxmpp notifications and fire notification for update subscribeToUpdates() NotificationCenter.default.post(name: Notification.Name(kMonalRefresh), object: nil) + + // reconnect + xmpp.connectIfNecessary() } deinit { @@ -59,6 +62,12 @@ extension MonalXmppWrapper { let chatModel = MonalChatWrapper(contact: with, db: db, xmpp: xmpp) return chatModel } + + func chat(with: Chat) -> MonalChatWrapper? { + guard let contact = contacts.first(where: { $0.ownerId == with.accountId && $0.contactJid == with.participantJid }) else { return nil } + let chatModel = MonalChatWrapper(contact: contact, db: db, xmpp: xmpp) + return chatModel + } } // MARK: - Try login from Login screen @@ -173,6 +182,7 @@ final class MonalChatWrapper: ObservableObject { let contact: Contact private let xmpp: MLXMPPManager private let db: DataLayer + private var notificationObservers: [AnyObject] = [] init(contact: Contact, db: DataLayer, xmpp: MLXMPPManager) { self.contact = contact @@ -180,6 +190,17 @@ final class MonalChatWrapper: ObservableObject { self.xmpp = xmpp } + deinit { + notificationObservers.forEach { NotificationCenter.default.removeObserver($0) } + } + + private func subscribe() { + let newMsg = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalNewMessageNotice), object: nil, queue: .main) { [weak self] _ in + self?.refreshMessages() + } + notificationObservers.append(newMsg) + } + private func refreshMessages() { messages = db.messages(forContact: contact.contactJid, forAccount: NSNumber(value: contact.ownerId)) .compactMap {