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 messages: [Message]
var body: some View { var body: some View {
ScrollView { ScrollViewReader { proxy in
LazyVStack { ScrollView {
ForEach(messages) { message in LazyVStack {
if message.sentByMe { ForEach(messages) { message in
Spacer() if message.sentByMe {
} Spacer()
MessageView(message: message) }
if !message.sentByMe { MessageView(message: message)
Spacer() .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 @EnvironmentObject private var connection: Connection
private func refresh() { private func refresh() {
messages.removeAll()
Task { Task {
self.messages = await connection.messages(in: conversation) ?? .init() self.messages = await connection.messages(in: conversation) ?? .init()
} }

Loading…
Cancel
Save