This commit is contained in:
Woit 2024-11-25 11:51:00 +01:00
parent ea333acb1a
commit 477e58f35c
3 changed files with 31 additions and 7 deletions

View file

@ -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,

View file

@ -1,4 +1,5 @@
enum AimErrors: Error {
case loginError
case contactRemoveError
case wrongContact
}

View file

@ -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 {