import Foundation
import SwiftUI
struct TestChatUIView: View {
@State var models: [TestChatModel2] = []
init() {
}
var body: some View {
ZStack(alignment: .top) {
ScrollViewReader { scrollViewProxy in
ScrollView {
LazyVStack(spacing: 0) {
ForEach(models.indices, id: \.self) { index in
VStack {
let message = models[index]
Text("\(message.id)")
.id(message.id)
.flipped()
.frame(minHeight: 40)
.onAppear {
if index < (3) {
for i in 0...20 {
let newModel = TestChatModel2(id: "id \(models.count + 1)", date: Date())
models.insert(newModel, at: 0)
}
}
}
}
}
}
}
.flipped()
}
VStack {
Spacer()
Button {
for int in 0...30 {
models.insert(.init(id: "id \(models.count + 1)", date: Date()), at: 0)
}
} label: {
Text("loadMore")
.background(Color.red)
}
}
}
}
}
struct TestChatModel2: Identifiable {
var id: String = UUID().uuidString
var date: Date
}
extension View {
func flipped() -> some View {
self.rotationEffect(.radians(Double.pi))
.scaleEffect(x: -1, y: 1, anchor: .center)
}
}
I am making chatting system, this code is kinda very sample of it. What I want to is load more messages when it gets close to bottom. But problem is scroll focus keeps locating bottom so it cause recursive loadmore function.
How to fix?
I changed .flipped() modifier's location like on VStack, ForEach, but it never works.