add ValueKeyboard as input for exercise values
This commit is contained in:
@@ -13,7 +13,6 @@ struct ExerciseEditor: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
var isPresentedAsSheet: Bool = false
|
||||
|
||||
// @Query(sort: \Exercise.name) var exercises: [Exercise]
|
||||
@State var exercise: Exercise?
|
||||
|
||||
@State private var name: String = ""
|
||||
@@ -26,72 +25,75 @@ struct ExerciseEditor: View {
|
||||
|
||||
@State private var isPartOfProgression: Bool = false
|
||||
|
||||
@State private var exerciseValue = ExerciseValue()
|
||||
@State private var isValueKeyboardPresented = false
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
Form {
|
||||
Section(footer: Text("The exercise description is optional.")) {
|
||||
TextField("Exercise Name", text: $name)
|
||||
// TODO: Add Autocomplete
|
||||
TextField("Description", text: $description)
|
||||
}
|
||||
|
||||
Section(footer: Text(Exercise.getAdvice(for: name, with: metric))) {
|
||||
Picker("Metric", selection: $metric) {
|
||||
Text("Reps").tag(ExerciseMetric.reps)
|
||||
Text("Duration").tag(ExerciseMetric.duration)
|
||||
Text("Distance").tag(ExerciseMetric.distance)
|
||||
VStack {
|
||||
ScrollViewReader { proxy in
|
||||
VStack {
|
||||
Form {
|
||||
Section(footer: Text("The exercise description is optional.")) {
|
||||
TextField("Exercise Name", text: $name)
|
||||
// TODO: Add Autocomplete
|
||||
TextField("Description", text: $description)
|
||||
}
|
||||
Section {
|
||||
Button(action: {
|
||||
isValueKeyboardPresented.toggle()
|
||||
proxy.scrollTo("valueButton", anchor: .center)
|
||||
}, label: {
|
||||
Text("\(exerciseValue.value) \(exerciseValue.unit.rawValue)")
|
||||
})
|
||||
.id("valueButton") // Assign a unique ID to the button
|
||||
}
|
||||
Section(footer: Text("Feature coming soon.")) {
|
||||
Toggle(isOn: $isPartOfProgression) {
|
||||
Text("Exercise is Part of a Progression")
|
||||
.foregroundStyle(.gray)
|
||||
}
|
||||
.disabled(true)
|
||||
}
|
||||
}
|
||||
.navigationTitle("Edit")
|
||||
.toolbar {
|
||||
if isPresentedAsSheet {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button("Cancel", role: .cancel) {
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
Button("Save") {
|
||||
withAnimation {
|
||||
save()
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.pickerStyle(SegmentedPickerStyle())
|
||||
switch metric {
|
||||
case .reps:
|
||||
RepsPicker(reps: $reps)
|
||||
case .duration:
|
||||
DurationPicker(duration: $duration)
|
||||
case .distance:
|
||||
DistancePicker(distance: $distance)
|
||||
}
|
||||
}
|
||||
|
||||
Section(footer: Text("Feature coming soon.")) {
|
||||
Toggle(isOn: $isPartOfProgression) {
|
||||
Text("Exercise is Part of a Progression")
|
||||
.foregroundStyle(.gray)
|
||||
}
|
||||
.disabled(true)
|
||||
}
|
||||
}
|
||||
.navigationTitle("Edit")
|
||||
.toolbar() {
|
||||
if isPresentedAsSheet {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button("Cancel", role: .cancel) {
|
||||
dismiss()
|
||||
.onAppear {
|
||||
if let exercise {
|
||||
self.name = exercise.name
|
||||
self.description = exercise.exerciseDescription
|
||||
|
||||
self.metric = exercise.metric
|
||||
self.reps = exercise.suggestedReps
|
||||
self.duration = exercise.suggestedDuration
|
||||
self.distance = exercise.suggestedDistance
|
||||
|
||||
self.isPartOfProgression = exercise.isPartOfProgression
|
||||
}
|
||||
}
|
||||
}
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
Button("Save") {
|
||||
withAnimation {
|
||||
save()
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
if isValueKeyboardPresented {
|
||||
ValueKeyboard(isPresented: $isValueKeyboardPresented, value: $exerciseValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
.onAppear() {
|
||||
if let exercise {
|
||||
self.name = exercise.name
|
||||
self.description = exercise.exerciseDescription
|
||||
|
||||
self.metric = exercise.metric
|
||||
self.reps = exercise.suggestedReps
|
||||
self.duration = exercise.suggestedDuration
|
||||
self.distance = exercise.suggestedDistance
|
||||
|
||||
self.isPartOfProgression = exercise.isPartOfProgression
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func save() {
|
||||
@@ -108,7 +110,7 @@ struct ExerciseEditor: View {
|
||||
} else {
|
||||
let newExercise = Exercise(name, metric)
|
||||
modelContext.insert(newExercise)
|
||||
// try? modelContext.save()
|
||||
// try? modelContext.save()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user