scroll down
This commit is contained in:
parent
055c57c208
commit
b9ea0807e5
@ -3,9 +3,11 @@ import SwiftUI
|
|||||||
struct PrivateChatView: View {
|
struct PrivateChatView: View {
|
||||||
let chat: PrivateChatListItem
|
let chat: PrivateChatListItem
|
||||||
let currentUserId: String?
|
let currentUserId: String?
|
||||||
|
private let bottomAnchorId = "PrivateChatBottomAnchor"
|
||||||
|
|
||||||
@StateObject private var viewModel: PrivateChatViewModel
|
@StateObject private var viewModel: PrivateChatViewModel
|
||||||
@State private var hasPositionedToBottom: Bool = false
|
@State private var hasPositionedToBottom: Bool = false
|
||||||
|
@State private var scrollToBottomTrigger: UUID = .init()
|
||||||
@State private var draftText: String = ""
|
@State private var draftText: String = ""
|
||||||
@State private var inputTab: ComposerTab = .chat
|
@State private var inputTab: ComposerTab = .chat
|
||||||
@State private var isVideoPreferred: Bool = false
|
@State private var isVideoPreferred: Bool = false
|
||||||
@ -22,15 +24,23 @@ struct PrivateChatView: View {
|
|||||||
ScrollViewReader { proxy in
|
ScrollViewReader { proxy in
|
||||||
content
|
content
|
||||||
.onChange(of: viewModel.messages.count) { _ in
|
.onChange(of: viewModel.messages.count) { _ in
|
||||||
guard !viewModel.isLoadingMore,
|
guard !viewModel.isLoadingMore else { return }
|
||||||
let lastId = viewModel.messages.last?.id else { return }
|
let targetId = viewModel.messages.last?.id ?? bottomAnchorId
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
withAnimation(.easeInOut(duration: 0.2)) {
|
withAnimation(.easeInOut(duration: 0.2)) {
|
||||||
proxy.scrollTo(lastId, anchor: .bottom)
|
proxy.scrollTo(targetId, anchor: .bottom)
|
||||||
}
|
}
|
||||||
hasPositionedToBottom = true
|
hasPositionedToBottom = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.onChange(of: scrollToBottomTrigger) { _ in
|
||||||
|
let targetId = viewModel.messages.last?.id ?? bottomAnchorId
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
withAnimation(.easeInOut(duration: 0.2)) {
|
||||||
|
proxy.scrollTo(targetId, anchor: .bottom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.navigationTitle(title)
|
.navigationTitle(title)
|
||||||
.navigationBarTitleDisplayMode(.inline)
|
.navigationBarTitleDisplayMode(.inline)
|
||||||
@ -85,6 +95,10 @@ struct PrivateChatView: View {
|
|||||||
!viewModel.messages.isEmpty {
|
!viewModel.messages.isEmpty {
|
||||||
errorBanner(message: message)
|
errorBanner(message: message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Color.clear
|
||||||
|
.frame(height: 1)
|
||||||
|
.id(bottomAnchorId)
|
||||||
}
|
}
|
||||||
.padding(.vertical, 12)
|
.padding(.vertical, 12)
|
||||||
}
|
}
|
||||||
@ -324,6 +338,7 @@ struct PrivateChatView: View {
|
|||||||
guard !text.isEmpty else { return }
|
guard !text.isEmpty else { return }
|
||||||
|
|
||||||
draftText = ""
|
draftText = ""
|
||||||
|
scrollToBottomTrigger = .init()
|
||||||
viewModel.sendMessage(text: text) { success in
|
viewModel.sendMessage(text: text) { success in
|
||||||
if success {
|
if success {
|
||||||
hasPositionedToBottom = true
|
hasPositionedToBottom = true
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user