109 lines
3.1 KiB
Swift
109 lines
3.1 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 = .none
|
|
|
|
@State private var isPartOfProgression: Bool = false
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
VStack {
|
|
ScrollViewReader { proxy in
|
|
VStack {
|
|
Form {
|
|
Section {
|
|
TextField("Exercise Name", text: $name)
|
|
// TODO: Add Autocomplete
|
|
TextEditorWithPlaceholder(text: $description, placeholder: "Description (optional)")
|
|
}
|
|
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) {
|
|
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.isPartOfProgression = exercise.isPartOfProgression
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func save() {
|
|
let exerciseToSave: Exercise
|
|
|
|
if let exercise = exercise {
|
|
exerciseToSave = exercise
|
|
} else {
|
|
exerciseToSave = Exercise(name)
|
|
modelContext.insert(exerciseToSave)
|
|
}
|
|
|
|
exerciseToSave.name = name
|
|
exerciseToSave.exerciseDescription = description
|
|
exerciseToSave.metric = metric
|
|
exerciseToSave.isPartOfProgression = isPartOfProgression
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
ExerciseEditor()
|
|
}
|