This commit is contained in:
Woit 2024-12-09 20:28:46 +01:00
parent 868f326bbc
commit 55b3469284
5 changed files with 14 additions and 9 deletions

View file

@ -2,7 +2,7 @@ import Foundation
import SwiftUI import SwiftUI
struct ConversationMessageRow: View { struct ConversationMessageRow: View {
@EnvironmentObject var chatWrapper: WrapperXMPP @ObservedObject var chatWrapper: WrapperChat
let message: Message let message: Message
@State private var offset: CGSize = .zero @State private var offset: CGSize = .zero
@ -51,7 +51,7 @@ struct ConversationMessageRow: View {
} }
if value.translation.width <= targetWidth { if value.translation.width <= targetWidth {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.02) { DispatchQueue.main.asyncAfter(deadline: .now() + 0.02) {
// messages.replyText = message.body ?? "" chatWrapper.replyText = message.body
} }
} }
} }

View file

@ -22,6 +22,7 @@ struct ConversationScreen: View {
leftButton: .init( leftButton: .init(
image: Image(systemName: "chevron.left"), image: Image(systemName: "chevron.left"),
action: { action: {
chatWrapper.deactivate()
router.dismissScreen() router.dismissScreen()
} }
), ),
@ -30,8 +31,7 @@ struct ConversationScreen: View {
image: Image(systemName: "gear"), image: Image(systemName: "gear"),
action: { action: {
router.showScreen(.push) { _ in router.showScreen(.push) { _ in
ConversationSettingsScreen() ConversationSettingsScreen(chatWrapper: chatWrapper)
.environmentObject(chatWrapper)
.navigationBarHidden(true) .navigationBarHidden(true)
} }
} }
@ -51,7 +51,7 @@ struct ConversationScreen: View {
ScrollView { ScrollView {
LazyVStack(spacing: 0) { LazyVStack(spacing: 0) {
ForEach(chatWrapper.messages) { message in ForEach(chatWrapper.messages) { message in
ConversationMessageRow(message: message) ConversationMessageRow(chatWrapper: chatWrapper, message: message)
.id(message.id) .id(message.id)
.flip() .flip()
.onAppear { .onAppear {
@ -116,8 +116,7 @@ struct ConversationScreen: View {
} }
} }
.safeAreaInset(edge: .bottom, spacing: 0) { .safeAreaInset(edge: .bottom, spacing: 0) {
ConversationTextInput(autoScroll: $autoScroll) ConversationTextInput(chatWrapper: chatWrapper, autoScroll: $autoScroll)
.environmentObject(chatWrapper)
} }
.onLoad { .onLoad {
if chatWrapper.messages.isEmpty { if chatWrapper.messages.isEmpty {

View file

@ -3,7 +3,7 @@ import Foundation
import SwiftUI import SwiftUI
struct ConversationSettingsScreen: View { struct ConversationSettingsScreen: View {
@EnvironmentObject var chatWrapper: WrapperChat @ObservedObject var chatWrapper: WrapperChat
@Environment(\.router) var router @Environment(\.router) var router
var body: some View { var body: some View {

View file

@ -3,7 +3,7 @@ import UIKit
struct ConversationTextInput: View { struct ConversationTextInput: View {
@Environment(\.router) var router @Environment(\.router) var router
@EnvironmentObject var chatWrapper: WrapperChat @ObservedObject var chatWrapper: WrapperChat
@State private var messageStr = "" @State private var messageStr = ""
@FocusState private var isFocused: Bool @FocusState private var isFocused: Bool

View file

@ -107,6 +107,12 @@ final class WrapperChat: ObservableObject {
} }
} }
} }
func deactivate() {
messages = []
notificationObservers.forEach { NotificationCenter.default.removeObserver($0) }
print("Chat wrapper unsubscribed")
}
} }
private extension WrapperChat { private extension WrapperChat {