I have an EnvironmentObject
that I'm using to generate a list
class ActivityViewModel: ObservableObject {
@Published var Activities = [Activity]()
init() {
self.Activities = ActivityService.fetchActivities()
}
}
struct Activity: Codable {
var Id: UUID
var Name: String
var Records: [Record]
init(Name:String) {
self.Id = UUID.init()
self.Name = Name
self.Records = [Record]()
}
}
When I'm using this in the view, I can use a ForEach
to access each value in the Activities
array.
My issue is when I use a NavigationLink to pass this Activity
to another view, it's not then using the EnvironmentObject
My 'parent view' code:
ForEach(0..<activities.Activities.count, id: \.self) { activity in
HStack {
ActivityListItemView(activity: self.activities.Activities[activity])
}
}
My child view code:
struct ActivityListItemView: View {
let activity: Activity
var body: some View {
NavigationLink(destination: ActivityDetail(Activity: activity)) {
HStack {
VStack {
HStack {
Text(activity.Name)
Text("\(activity.Records.count) records")
}
}
Text(">")
}
}
.buttonStyle(PlainButtonStyle())
}
}
If I then update the viewModel, I need this to replicate into the child view. But I can't figure out how to make it use the EnvironmentObject
, rather than just the variable I pass to the view.
Any help would be appreciated.
Edit: Added activity type