blob: 1aee6b2cf369e5744d6b020485bd07b98081a03a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
import SwiftUI
struct ChatView: View {
@Environment(HotlineClient.self) private var hotline
@Environment(\.colorScheme) var colorScheme
@State var input: String = ""
@State private var scrollPos: Int?
@State private var contentHeight: CGFloat = 0
@Namespace var bottomID
var body: some View {
NavigationStack {
VStack(spacing: 0) {
ScrollView {
ScrollViewReader { reader in
LazyVStack(alignment: .leading) {
ForEach(hotline.chatMessages) { msg in
if msg.type == .agreement {
VStack(alignment: .leading) {
Text(msg.text)
.padding()
.opacity(0.75)
.background(colorScheme == .dark ? Color(white: 0.1) : Color(white: 0.96))
.cornerRadius(16)
}
.padding()
}
else {
HStack(alignment: .firstTextBaseline) {
if !msg.username.isEmpty {
Text("\(msg.username):").bold()
}
Text(msg.text)
.textSelection(.enabled)
Spacer()
}
.padding()
}
}
Text("").id(bottomID)
}
.onChange(of: hotline.chatMessages.count) {
withAnimation {
reader.scrollTo(bottomID, anchor: .bottom)
}
print("SCROLLED TO BOTTOM")
}
.onAppear {
withAnimation {
reader.scrollTo("bottom view", anchor: .bottom)
}
}
}
}
Divider()
HStack(alignment: .top) {
Image(systemName: "chevron.right")
TextField("", text: $input, axis: .vertical)
.lineLimit(1...5)
.onSubmit {
hotline.sendChat(message: self.input)
// HotlineClient.shared.sendChat(message: self.input)
self.input = ""
}
}.padding()
}
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
Text(hotline.server?.name ?? "")
.font(.headline)
}
ToolbarItem(placement: .navigationBarLeading) {
Button {
hotline.disconnect()
} label: {
Image(systemName: "xmark.circle.fill")
.symbolRenderingMode(.hierarchical)
.foregroundColor(.secondary)
}
}
}
}
// GeometryReader { geometry in
// ScrollView(.vertical) {
// ScrollViewReader { scrollReader in
// VStack(alignment: .leading) {
// Spacer()
// List(hotline.chatMessages) { msg in
// HStack(alignment: .firstTextBaseline) {
// Text("\(msg.username):").bold().fontDesign(.monospaced)
// Text(msg.message)
// .fontDesign(.monospaced)
// .textSelection(.enabled)
// }
// }
// .padding()
// }
// // .frame(width: geometry.size.width)
// .frame(minHeight: geometry.size.height)
//// .background(Color.red)
// .onAppear() {
//// scrollReader.scrollTo("bottomScroll", anchor: .bottom)
// }
// // .onChange() {
// // scrollReader.scrollTo(10000, anchor: .bottomTrailing)
// // }
// }
// }
// }
}
}
#Preview {
ChatView()
.environment(HotlineClient())
}
|