My ContentView show a list of activities, each activity is shown in an ActivityView. The list has a + button in the right of the navigation bar that show a AddActivityView.
A NavigationLink is associated with each item of the list. The link go to an ActivityCompletionView.
The following variables are declared in the ActivityCompletionView struct
@State var activity: Activity
@ObservedObject var activities: Activities
In the view I have the following section.
Section(header: Text("Completion")) {
HStack {
Text("\(self.activity.completionCount, specifier: "%d") times")
Spacer()
Button(action: {
self.augmentCompletionCount()
}) {
Image(systemName: "plus")
}
}
}
And the ActivityCompletionView struct has the augmentCompletionCount().
func augmentCompletionCount() {
let index = activities.activityIndex(of: activity.id)!
activities.items[index].completionCount += 1
activity.completionCount += 1
}
I should have avoided keeping two things synchonized here. And also avoided a UUID to index conversion "activityIndex(of: activity.id)" but I was in a hurry to go to the next session :-)