Well, I can't show all the code, but I can show a bit more:
swift
class
ChatStream
{
@Published var messages = OrderedSetMessage()
func add(messages inMsgs: [IncomingMessage])
{
for msg in inMsgs
{
let msg = Message(fromIncoming: msg, user: user)
self.messages.append(msg)
}
self.messages.sort()
}
}
...
struct
ChatView: View
{
@ObservedObject public var stream : ChatStream
var body: some View {
ScrollViewReader { scrollView in
ScrollView {
LazyVStack(alignment: .leading, spacing: 0.0) {
ForEach(self.stream.messages) { inMsg in
ChatMessageCell(message: inMsg)
}
.onChange(of: self.stream.messages, perform: { inMessages in --- called multiple times for each update below.
if inMessages.count 1 { return }
withAnimation(.linear(duration: 0.25)) {
scrollView.scrollTo(inMessages.last!.id, anchor: .bottom)
}
})
}
}
}
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: