add ModelContainerPreview conecept, move SampleData, fix Exercise, ExerciseUnit, ExerciseType
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
//
|
||||
// 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())
|
||||
}
|
||||
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!)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user