fix scroll down

This commit is contained in:
cheykrym 2025-10-22 06:20:41 +03:00
parent 5f03feba66
commit 91a3117595

View File

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