diff --git a/yobble/Views/Chat/PrivateChatView.swift b/yobble/Views/Chat/PrivateChatView.swift index 2349397..b5c3a16 100644 --- a/yobble/Views/Chat/PrivateChatView.swift +++ b/yobble/Views/Chat/PrivateChatView.swift @@ -22,33 +22,22 @@ struct PrivateChatView: View { } var body: some View { - ZStack(alignment: .bottomTrailing) { - ScrollViewReader { proxy in + ScrollViewReader { proxy in + ZStack(alignment: .bottomTrailing) { content .onChange(of: viewModel.messages.count) { _ in guard !viewModel.isLoadingMore else { return } - let targetId = viewModel.messages.last?.id ?? bottomAnchorId - DispatchQueue.main.async { - withAnimation(.easeInOut(duration: 0.2)) { - proxy.scrollTo(targetId, anchor: .bottom) - } - hasPositionedToBottom = true - } + scrollToBottom(proxy: proxy) } .onChange(of: scrollToBottomTrigger) { _ in - let targetId = viewModel.messages.last?.id ?? bottomAnchorId - DispatchQueue.main.async { - withAnimation(.easeInOut(duration: 0.2)) { - proxy.scrollTo(targetId, anchor: .bottom) - } - } + scrollToBottom(proxy: proxy) } - } - if !isBottomAnchorVisible { - scrollToBottomButton - .padding(.trailing, 20) - .padding(.bottom, 30) + if !isBottomAnchorVisible { + scrollToBottomButton(proxy: proxy) + .padding(.trailing, 20) + .padding(.bottom, 56) + } } } .navigationTitle(title) @@ -294,8 +283,10 @@ struct PrivateChatView: View { .background(.ultraThinMaterial) } - private var scrollToBottomButton: some View { - Button(action: scrollToBottom) { + private func scrollToBottomButton(proxy: ScrollViewProxy) -> some View { + Button { + scrollToBottom(proxy: proxy) + } label: { Image(systemName: "arrow.down") .font(.system(size: 18, weight: .semibold)) .foregroundColor(.white) @@ -369,10 +360,16 @@ struct PrivateChatView: View { } } - private func scrollToBottom() { - scrollToBottomTrigger = .init() + private func scrollToBottom(proxy: ScrollViewProxy) { hasPositionedToBottom = true - isBottomAnchorVisible = true + + let targetId = viewModel.messages.last?.id ?? bottomAnchorId + + DispatchQueue.main.async { + withAnimation(.easeInOut(duration: 0.2)) { + proxy.scrollTo(targetId, anchor: .bottom) + } + } } private struct ComposerIconButtonStyle: ButtonStyle {