My app allows users to set four dates that get used in various parts of the app. I've created a class that stores these dates in UserDefaults. I'm using the @Environment property wrapper to use this in various views of my app.
In a settings view, I want to give the ability to change these dates. The code to actually update the dates works fine, but I can't get the DatePicker to be "preset" to the current values saved in the Environment variable.
When I try to set the State variable when it's declared, I get this error "Cannot use instance member 'pipeline' within property initializer; property initializers run before 'self' is available". This make sense, as the @Environment var isn't avialable to use yet.
Next I tried using .onAppear(). This sets the @State variables correctly, but the view isnt' refreshing, so the datepickers do not change.
import SwiftUI
struct PipelineView: View {
@Environment(Pipeline.self) private var pipeline
@State private var newQ1: Date = .now
@State private var newQ2: Date = .now
@State private var newQ3: Date = .now
@State private var newQ4: Date = .now
var body: some View {
VStack {
Form {
DatePicker("Q1", selection: $newQ1, displayedComponents: .date)
DatePicker("Q2", selection: $newQ2, displayedComponents: .date)
DatePicker("Q3", selection: $newQ3, displayedComponents: .date)
DatePicker("Q4", selection: $newQ4, displayedComponents: .date)
Button {
pipeline.setQuarterDates(q1: newQ1, q2: newQ2, q3: newQ3, q4: newQ4)
} label: {
Text("Save")
}
}
}
.onAppear() {
newQ1 = pipeline.getQ1()
newQ2 = pipeline.getQ2()
newQ3 = pipeline.getQ3()
newQ4 = pipeline.getQ4()
print(newQ1.formatted()) //testing to make sure the date is actually updated, it is.
}
}
}