AddHabitView
is the source of truth for each habit's properties. New habits are added via this method:
func saveAndExit() {
habits.list.append(Habit(name: name, goal: goal, metric: metric, frequency: frequency, progress: progress))
self.presentationMode.wrappedValue.dismiss()
}
In DetailView
, I have a stepper that should update the progress
property on ListView
and any other views that display information about each habit.
The problem is that it isn't.
Here is the code for DetailView
. Note that ProgressBar
contains a stepper with a @Binding
property.
struct DetailView: View {
@EnvironmentObject var habits: HabitList
@State private var progress = 0
var body: some View {
VStack {
Text("\(progress)% Complete")
.font(.largeTitle)
ProgressBar(value: $progress)
}
.navigationBarTitle("Habit Progress", displayMode: .inline)
}
}
I have also tried this, but run into the same problem:
struct DetailView: View {
@EnvironmentObject var habits: HabitList
@State var habit: Habit
var body: some View {
VStack {
Text("\(habit.progress)% Complete")
.font(.largeTitle)
ProgressBar(value: $habit.progress)
}
.navigationBarTitle("Habit Progress", displayMode: .inline)
}
}
My guess is the problem in both cases is that I'm using an @State
property wrapper, which can only update values locally, and writing Habit
as a struct, which can only be read by other views, not updated.
So, to reiterate: How should I structure the data for this app?
As I mentioned, I tried writing Habit
as a class, which would allow me to use @StateObject
to share updates across views. However, my code wouldn't compile.
I've been stuck on this for days. It's really frustrating!