From fc8742e8961169e7fe01bab4a76cd36a2ad24ce1 Mon Sep 17 00:00:00 2001 From: Peter Hajas Date: Tue, 10 May 2022 21:52:29 -0600 Subject: [PATCH] Scroll to message when loading --- Kordophone/Shared/ConversationView.swift | 26 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/Kordophone/Shared/ConversationView.swift b/Kordophone/Shared/ConversationView.swift index c5e3923..f3361ef 100644 --- a/Kordophone/Shared/ConversationView.swift +++ b/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() }