118 lines
3.4 KiB
Swift
118 lines
3.4 KiB
Swift
//
|
|
// ExerciseDetail.swift
|
|
// WorkoutsPlus
|
|
//
|
|
// Created by Felix Förtsch on 10.08.24.
|
|
//
|
|
|
|
import SwiftUI
|
|
import SwiftData
|
|
|
|
struct ExerciseEditor: View {
|
|
@Environment(\.dismiss) private var dismiss
|
|
@Environment(\.modelContext) private var modelContext
|
|
|
|
@State var exercise: Exercise
|
|
|
|
@State private var name: String = ""
|
|
@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: $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) {
|
|
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())
|
|
}
|
|
|
|
|
|
AssignmentButton<Exercise, Equipment>(
|
|
title: "Equipment",
|
|
owner: exercise,
|
|
sortBy: SortDescriptor(\Equipment.name),
|
|
getAssignedItems: { exercise in
|
|
exercise.equipment
|
|
},
|
|
assign: { exercise, equipment in
|
|
exercise.equipment.append(equipment)
|
|
},
|
|
unassign: { exercise, equipment in
|
|
exercise.equipment.removeAll(where: { $0.id == equipment.id })
|
|
},
|
|
createNew: { Equipment(name: "") }
|
|
)
|
|
|
|
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") {
|
|
saveExisting()
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
.onAppear() {
|
|
name = exercise.name
|
|
description = exercise.exerciseDescription
|
|
isPartOfProgression = exercise.isPartOfProgression
|
|
if let type = exercise.type {
|
|
self.type = type
|
|
}
|
|
if let unit = exercise.unit {
|
|
self.unit = unit
|
|
}
|
|
}
|
|
}
|
|
|
|
private func saveExisting() {
|
|
exercise.name = name
|
|
exercise.exerciseDescription = description
|
|
exercise.isPartOfProgression = isPartOfProgression
|
|
exercise.type = type
|
|
exercise.unit = unit
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
ModelContainerPreview(ModelContainer.sample) {
|
|
NavigationStack {
|
|
ExerciseEditor(exercise: Exercise.sampleDataRecommendedRoutine.first!)
|
|
}
|
|
}
|
|
}
|