So I've been trying to create a foreach containing an asyncimage which will later navigate to the detailview when pressed, but when I press the images that don't match the order of the images I have, my images show them in random order. I don't know how to go to detailview with asyncimage according to the order of data in foreach in HistoryView()
import SwiftUI
struct HistoryView: View {
@EnvironmentObject var historyManager : HistoryManager
@State var isActive : Bool = false
var body: some View {
NavigationView{
let jsonDataList = historyManager.jsondata
ScrollView{
VStack{
if let response = jsonDataList{
//MARK: diberi reversed agar ketika update maka akan munculnya dari bawah
ForEach(response.dataJson.result.check_in.reversed(), id:\.self){ items in
ZStack(alignment: .leading){
//color changing when late is true
if (historyManager.warna == true){
Image("RoundedRectangle-red")
.resizable()
.frame(width: 370, height: 200)
}else{
Image("RoundedRectangle-green")
.resizable()
.frame(width: 370, height: 200)
}
//Image
NavigationLink(isActive : $isActive){
DetailImageHistory(isActive: $isActive, name: items.links)
} label: {
let url = URL(string: items.links)
AsyncImage(url: url){ image in
image
.resizable()
.scaledToFill()
.frame(width: 45, height: 45)
.clipShape(Circle())
} placeholder: {
ProgressView()
.progressViewStyle(.circular)
}
.frame(width: 45, height: 45)
.clipShape(Circle())
}.offset(x: 300, y: -70)
//Content
VStack(alignment:.leading, spacing: 2){
Text("Presensi Datang").bold().font(.system(size: 15))
Text("\(items.createdAts)").font(.system(size: 15))
if(historyManager.keteranganKehadiran == true){
Text("Tepat Waktu").fontWeight(.bold).foregroundColor(.white)
}else{
Text("Terlambat").fontWeight(.bold).foregroundColor(.white).font(.system(size: 15))
}
Text("\(items.places)").font(.system(size: 15))
Text("\(items.locations)").multilineTextAlignment(.leading).font(.system(size: 15))
Text("Keterangan: ").bold().font(.system(size: 15))
Text("\(items.tujuans)").bold().font(.system(size: 15))
}.padding(.horizontal)
}
}
}
}
}
}
}
}
and this is my DetailView
import SwiftUI
struct DetailImageHistory: View {
@EnvironmentObject var historyManager: HistoryManager
@Binding var isActive : Bool
var name : String
var body: some View {
let url = URL(string: name)
AsyncImage(url: url){ image in
image
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 300, height: 300)
} placeholder: {
Color.gray
}
.frame(width: 300, height: 300)
}
}
UPDATE: my problem is solved i dont know why I'm not changing anything but somehow it is actually worked now lol