From 55b3469284d61101e5a963d9f73cee6f70b5517c Mon Sep 17 00:00:00 2001 From: Woit Date: Mon, 9 Dec 2024 20:28:46 +0100 Subject: [PATCH] wip --- .../Views/Conversation/ConversationMessageRow.swift | 4 ++-- .../Views/Conversation/ConversationScreen.swift | 9 ++++----- .../Views/Conversation/ConversationSettingsScreen.swift | 2 +- .../Views/Conversation/ConversationTextInput.swift | 2 +- Monal/another.im/XMPP/Wrappers/WrapperChat.swift | 6 ++++++ 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Monal/another.im/Views/Conversation/ConversationMessageRow.swift b/Monal/another.im/Views/Conversation/ConversationMessageRow.swift index d1c7716..21030ed 100644 --- a/Monal/another.im/Views/Conversation/ConversationMessageRow.swift +++ b/Monal/another.im/Views/Conversation/ConversationMessageRow.swift @@ -2,7 +2,7 @@ import Foundation import SwiftUI struct ConversationMessageRow: View { - @EnvironmentObject var chatWrapper: WrapperXMPP + @ObservedObject var chatWrapper: WrapperChat let message: Message @State private var offset: CGSize = .zero @@ -51,7 +51,7 @@ struct ConversationMessageRow: View { } if value.translation.width <= targetWidth { DispatchQueue.main.asyncAfter(deadline: .now() + 0.02) { - // messages.replyText = message.body ?? "" + chatWrapper.replyText = message.body } } } diff --git a/Monal/another.im/Views/Conversation/ConversationScreen.swift b/Monal/another.im/Views/Conversation/ConversationScreen.swift index f741b5e..0388755 100644 --- a/Monal/another.im/Views/Conversation/ConversationScreen.swift +++ b/Monal/another.im/Views/Conversation/ConversationScreen.swift @@ -22,6 +22,7 @@ struct ConversationScreen: View { leftButton: .init( image: Image(systemName: "chevron.left"), action: { + chatWrapper.deactivate() router.dismissScreen() } ), @@ -30,8 +31,7 @@ struct ConversationScreen: View { image: Image(systemName: "gear"), action: { router.showScreen(.push) { _ in - ConversationSettingsScreen() - .environmentObject(chatWrapper) + ConversationSettingsScreen(chatWrapper: chatWrapper) .navigationBarHidden(true) } } @@ -51,7 +51,7 @@ struct ConversationScreen: View { ScrollView { LazyVStack(spacing: 0) { ForEach(chatWrapper.messages) { message in - ConversationMessageRow(message: message) + ConversationMessageRow(chatWrapper: chatWrapper, message: message) .id(message.id) .flip() .onAppear { @@ -116,8 +116,7 @@ struct ConversationScreen: View { } } .safeAreaInset(edge: .bottom, spacing: 0) { - ConversationTextInput(autoScroll: $autoScroll) - .environmentObject(chatWrapper) + ConversationTextInput(chatWrapper: chatWrapper, autoScroll: $autoScroll) } .onLoad { if chatWrapper.messages.isEmpty { diff --git a/Monal/another.im/Views/Conversation/ConversationSettingsScreen.swift b/Monal/another.im/Views/Conversation/ConversationSettingsScreen.swift index 678c92c..3455994 100644 --- a/Monal/another.im/Views/Conversation/ConversationSettingsScreen.swift +++ b/Monal/another.im/Views/Conversation/ConversationSettingsScreen.swift @@ -3,7 +3,7 @@ import Foundation import SwiftUI struct ConversationSettingsScreen: View { - @EnvironmentObject var chatWrapper: WrapperChat + @ObservedObject var chatWrapper: WrapperChat @Environment(\.router) var router var body: some View { diff --git a/Monal/another.im/Views/Conversation/ConversationTextInput.swift b/Monal/another.im/Views/Conversation/ConversationTextInput.swift index 86be111..ef0a6d5 100644 --- a/Monal/another.im/Views/Conversation/ConversationTextInput.swift +++ b/Monal/another.im/Views/Conversation/ConversationTextInput.swift @@ -3,7 +3,7 @@ import UIKit struct ConversationTextInput: View { @Environment(\.router) var router - @EnvironmentObject var chatWrapper: WrapperChat + @ObservedObject var chatWrapper: WrapperChat @State private var messageStr = "" @FocusState private var isFocused: Bool diff --git a/Monal/another.im/XMPP/Wrappers/WrapperChat.swift b/Monal/another.im/XMPP/Wrappers/WrapperChat.swift index 307b785..dd23c39 100644 --- a/Monal/another.im/XMPP/Wrappers/WrapperChat.swift +++ b/Monal/another.im/XMPP/Wrappers/WrapperChat.swift @@ -107,6 +107,12 @@ final class WrapperChat: ObservableObject { } } } + + func deactivate() { + messages = [] + notificationObservers.forEach { NotificationCenter.default.removeObserver($0) } + print("Chat wrapper unsubscribed") + } } private extension WrapperChat {