85 lines
2.3 KiB
Swift
85 lines
2.3 KiB
Swift
//
|
|
// ExerciseDetail.swift
|
|
// WorkoutsPlus
|
|
//
|
|
// Created by Felix Förtsch on 10.08.24.
|
|
//
|
|
|
|
import SwiftUI
|
|
import SwiftData
|
|
|
|
struct ExerciseDetail: View {
|
|
@Environment(\.dismiss) private var dismiss
|
|
@Environment(\.modelContext) private var modelContext
|
|
|
|
@State var exercise: Exercise
|
|
@State private var description: String = ""
|
|
@State private var isPartOfProgression: Bool = false
|
|
|
|
@State private var type: ExerciseType?
|
|
@State private var unit: ExerciseUnit?
|
|
|
|
@Query(sort: \ExerciseType.name) private var exerciseTypes: [ExerciseType]
|
|
@Query(sort: \ExerciseUnit.name) private var exerciseUnits: [ExerciseUnit]
|
|
|
|
var body: some View {
|
|
Form {
|
|
Section {
|
|
TextField("Exercise Name", text: $exercise.name)
|
|
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())
|
|
}
|
|
Section(footer: Text("Feature coming soon.")) {
|
|
Toggle(isOn: $isPartOfProgression) {
|
|
Text("Exercise is Part of a Progression")
|
|
.foregroundStyle(.gray)
|
|
}
|
|
.disabled(true)
|
|
}
|
|
}
|
|
.navigationTitle("Edit Exercise")
|
|
.toolbar {
|
|
ToolbarItem(placement: .topBarTrailing) {
|
|
Button("Save") {
|
|
saveItem()
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func saveItem() {
|
|
if modelContext.hasChanges {
|
|
do {
|
|
try modelContext.save()
|
|
} catch {
|
|
print("Failed to save exercise: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
NavigationStack {
|
|
ExerciseDetail(exercise: Exercise("Squat Sky Reaches"))
|
|
}
|
|
}
|