simplify Exercise to only hold on to the unit of the metric, remove ValueKeyboard as input

This commit is contained in:
Felix Förtsch
2024-10-02 12:41:04 +02:00
parent 41a82f081a
commit b7f5caf9dd
15 changed files with 630 additions and 243 deletions
+27 -39
View File
@@ -17,36 +17,34 @@ struct ExerciseEditor: View {
@State private var name: String = ""
@State private var description: String = ""
@State private var metric: ExerciseMetric = .reps
@State private var reps: String = ""
@State private var duration: String = ""
@State private var distance: String = ""
@State private var metric: ExerciseMetric = .none
@State private var isPartOfProgression: Bool = false
@State private var exerciseValue = ExerciseValue()
@State private var isValueKeyboardPresented = false
var body: some View {
NavigationStack {
VStack {
ScrollViewReader { proxy in
VStack {
Form {
Section(footer: Text("The exercise description is optional.")) {
Section {
TextField("Exercise Name", text: $name)
// TODO: Add Autocomplete
TextField("Description", text: $description)
TextEditorWithPlaceholder(text: $description, placeholder: "Description (optional)")
}
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("""
Examples:
• Pull-up → None
• Weighted Pull-up → Weight
• Sprint → Time or Distance
""")) {
Picker("Exercise Metric", selection: $metric) {
ForEach(ExerciseMetric.allCases) { metric in
Text(metric.rawValue.isEmpty ? "None" : metric.rawValue)
.tag(metric)
}
}
.pickerStyle(SegmentedPickerStyle())
}
Section(footer: Text("Feature coming soon.")) {
Toggle(isOn: $isPartOfProgression) {
@@ -79,39 +77,29 @@ struct ExerciseEditor: View {
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
}
}
}
if isValueKeyboardPresented {
ValueKeyboard(isPresented: $isValueKeyboardPresented, value: $exerciseValue)
}
}
}
}
private func save() {
if let exercise {
exercise.name = name
exercise.exerciseDescription = description
exercise.metric = metric
exercise.suggestedReps = reps
exercise.suggestedDuration = duration
exercise.suggestedDistance = distance
exercise.isPartOfProgression = isPartOfProgression
let exerciseToSave: Exercise
if let exercise = exercise {
exerciseToSave = exercise
} else {
let newExercise = Exercise(name, metric)
modelContext.insert(newExercise)
// try? modelContext.save()
exerciseToSave = Exercise(name)
modelContext.insert(exerciseToSave)
}
exerciseToSave.name = name
exerciseToSave.exerciseDescription = description
exerciseToSave.metric = metric
exerciseToSave.isPartOfProgression = isPartOfProgression
}
}