Browse Source

Scroll to message when loading

master
Peter Hajas 3 years ago
parent
commit
fc8742e896
  1. 26
      Kordophone/Shared/ConversationView.swift

26
Kordophone/Shared/ConversationView.swift

@ -7,18 +7,25 @@ struct ConversationView : View {
var messages: [Message]
var body: some View {
ScrollView {
LazyVStack {
ForEach(messages) { message in
if message.sentByMe {
Spacer()
}
MessageView(message: message)
if !message.sentByMe {
Spacer()
ScrollViewReader { proxy in
ScrollView {
LazyVStack {
ForEach(messages) { message in
if message.sentByMe {
Spacer()
}
MessageView(message: message)
.id(message)
if !message.sentByMe {
Spacer()
}
}
}
}
.navigationTitle(conversation.effectiveDisplayName)
.onChange(of: messages) { newValue in
proxy.scrollTo(newValue.last, anchor: .bottom)
}
}
}
}
@ -29,6 +36,7 @@ struct ConversationViewWrapper : View {
@EnvironmentObject private var connection: Connection
private func refresh() {
messages.removeAll()
Task {
self.messages = await connection.messages(in: conversation) ?? .init()
}

Loading…
Cancel
Save