I'm working on a project that calls an api for data. When I created a view for displaying the data, it shows up correctly. The data is placed on 6 different cards. If I place that view in side another view it shows correctly, but once I use .overlay() the data on each card is the same. I can't figure out why .overlay() is causing this issue. I've tried changing the ViewModel var in the views to @StateObject, @EnvironmentObject, & @ObservedObject with no luck. I'm open to other approaches, but everything else looks perfect.
This view shows correctly:
import SwiftUI struct TopActivityCardView: View {
// @EnvironmentObject var vm: ViewModel
@StateObject var vm = ViewModel()
var timePeriod = "" var body: some View {
ZStack {
ScrollView{
ForEach(vm.fetch(), id: .title) { item in
RoundedRectangle(cornerRadius: 20)
.foregroundColor(Color.darkBlue)
.frame(height: 120, alignment: .center)
.padding(.top, 80)
.overlay(
ActivityTitleView(activityTitle: item.title)
).overlay(
ActivityHoursView(workHours: item.timeframes.weekly.current)
).overlay(
ActivityEllipisView()
).overlay(
TimePeriodView(timePeriod: "Last Week", weeklyHrs:
item.timeframes.weekly.previous)
)
}
}
}
}
}
struct TopActivityCardView_Previews: PreviewProvider {
static var previews: some View {
TopActivityCardView()
.environmentObject(ViewModel())
}
}
This view causes data on each card to be the same:
struct RectCardView: View {
var colorArray: [(colorName: Color, imageName: String)] = [(Color.work, "icon-work"), (Color.play, "icon-play"), (Color.study, "icon-study"), (Color.exercise, "icon-exercise"), (Color.social, "icon-social"), (Color.selfCare, "icon-self-care")]
// @StateObject var vm = ViewModel()
@ObservedObject var vm = ViewModel()
var body: some View {
ZStack{
ScrollView{
ForEach(colorArray, id: .imageName) { colorName, imageName in
RoundedRectangle(cornerRadius: 20)
.foregroundColor(colorName)
.frame(height: 110, alignment: .center)
.overlay(
Image(imageName)
.brightness(-0.2)
.frame(width: 60, height: 20, alignment: .topLeading)
.offset(x: 103, y: -60)
).clipped()
.overlay{
TopActivityCardView()
.padding(.top, -40)
}
.padding(.top, 20)
}
}
}
}
}
struct RectCardView_Previews: PreviewProvider {
static var previews: some View {
RectCardView()
.environmentObject(ViewModel())
}
}
Any help is greatly appreciated.