clean up ActiveWorkoutSession
This commit is contained in:
92
WorkoutsPlus/Features/Exercise/AddExercise.swift
Normal file
92
WorkoutsPlus/Features/Exercise/AddExercise.swift
Normal file
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// 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("", symbol: "")
|
||||
|
||||
@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) {
|
||||
ForEach(exerciseTypes, id: \.self) { type in
|
||||
Text("\(type.name)").tag(type as ExerciseType?)
|
||||
}
|
||||
}
|
||||
.pickerStyle(NavigationLinkPickerStyle())
|
||||
Picker("Exercise Unit", selection: $unit) {
|
||||
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 {
|
||||
save()
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func save() {
|
||||
let exerciseToSave = Exercise(name)
|
||||
modelContext.insert(exerciseToSave)
|
||||
|
||||
exerciseToSave.name = name
|
||||
exerciseToSave.exerciseDescription = description
|
||||
exerciseToSave.type = type
|
||||
exerciseToSave.isPartOfProgression = isPartOfProgression
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
AddExercise()
|
||||
}
|
||||
Reference in New Issue
Block a user