95 lines
2.8 KiB
Swift
95 lines
2.8 KiB
Swift
//
|
|
// AddExercise.swift
|
|
// WorkoutsPlus
|
|
//
|
|
// Created by Felix Förtsch on 21.09.24.
|
|
//
|
|
|
|
import SwiftUI
|
|
import SwiftData
|
|
|
|
struct AddExercise: View {
|
|
@Environment(\.modelContext) private var modelContext
|
|
@Environment(\.dismiss) private var dismiss
|
|
var isPresentedAsSheet: Bool = false
|
|
|
|
@State var exercise: Exercise = Exercise("")
|
|
|
|
@State private var name: String = ""
|
|
@State private var description: String = ""
|
|
@State private var type: ExerciseType?
|
|
@State private var unit: ExerciseUnit?
|
|
|
|
@State private var isPartOfProgression: Bool = false
|
|
|
|
@Query(sort: \ExerciseType.name) private var exerciseTypes: [ExerciseType]
|
|
@Query(sort: \ExerciseUnit.name) private var exerciseUnits: [ExerciseUnit]
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
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 Type", selection: $type) {
|
|
Text("None").tag(nil as ExerciseType?)
|
|
ForEach(exerciseTypes, id: \.self) { type in
|
|
Text("\(type.name)").tag(type as ExerciseType?)
|
|
}
|
|
}
|
|
.pickerStyle(NavigationLinkPickerStyle())
|
|
Picker("Exercise Unit", selection: $unit) {
|
|
Text("None").tag(nil as ExerciseUnit?)
|
|
ForEach(exerciseUnits, id: \.self) { unit in
|
|
Text("\(unit.name) (\(unit.symbol))").tag(unit as ExerciseUnit?)
|
|
}
|
|
}
|
|
.pickerStyle(NavigationLinkPickerStyle())
|
|
}
|
|
}
|
|
.navigationTitle("Add New Exercise")
|
|
.toolbar {
|
|
if isPresentedAsSheet {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
Button("Cancel", role: .cancel) {
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
Button("Save") {
|
|
withAnimation {
|
|
saveNew()
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func saveNew() {
|
|
let exerciseToSave = Exercise(name)
|
|
modelContext.insert(exerciseToSave)
|
|
|
|
exerciseToSave.name = name
|
|
exerciseToSave.exerciseDescription = description
|
|
exerciseToSave.type = type
|
|
exerciseToSave.isPartOfProgression = isPartOfProgression
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
AddExercise()
|
|
}
|