121 lines
3.5 KiB
Swift
121 lines
3.5 KiB
Swift
//
|
|
// AddExercise.swift
|
|
// WorkoutsPlus
|
|
//
|
|
// Created by Felix Förtsch on 21.09.24.
|
|
//
|
|
|
|
import SwiftUI
|
|
import SwiftData
|
|
|
|
struct ExerciseEditor: View {
|
|
@Environment(\.modelContext) private var modelContext
|
|
@Environment(\.dismiss) private var dismiss
|
|
var isPresentedAsSheet: Bool = false
|
|
|
|
@State var exercise: Exercise?
|
|
|
|
@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 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.")) {
|
|
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()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.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
|
|
}
|
|
}
|
|
}
|
|
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
|
|
} else {
|
|
let newExercise = Exercise(name, metric)
|
|
modelContext.insert(newExercise)
|
|
// try? modelContext.save()
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
ExerciseEditor()
|
|
}
|