73 lines
2.0 KiB
Swift
73 lines
2.0 KiB
Swift
import Foundation
|
|
import SwiftData
|
|
import SwiftUI
|
|
|
|
@Model
|
|
final class Exercise {
|
|
var name: String = ""
|
|
var descr: String = ""
|
|
|
|
@Relationship(deleteRule: .nullify, inverse: \ExerciseType.exercises)
|
|
var type: ExerciseType?
|
|
|
|
@Relationship(deleteRule: .nullify, inverse: \Muscle.exercises)
|
|
var muscles: [Muscle]? = []
|
|
|
|
@Relationship(deleteRule: .nullify, inverse: \SplitExerciseAssignment.exercise)
|
|
var splits: [SplitExerciseAssignment]? = []
|
|
|
|
@Relationship(deleteRule: .nullify, inverse: \WorkoutLog.exercise)
|
|
var logs: [WorkoutLog]? = []
|
|
|
|
init(name: String, descr: String) {
|
|
self.name = name
|
|
self.descr = descr
|
|
}
|
|
|
|
static let unnamed = "Unnamed Exercise"
|
|
}
|
|
|
|
extension Exercise: EditableEntity {
|
|
static func createNew() -> Exercise {
|
|
return Exercise(name: "", descr: "")
|
|
}
|
|
|
|
static var navigationTitle: String {
|
|
return "Exercises"
|
|
}
|
|
|
|
@ViewBuilder
|
|
static func formView(for model: Exercise) -> some View {
|
|
EntityAddEditView(model: model) { $model in
|
|
// This internal view is necessary to use @Query within the form.
|
|
ExerciseFormView(model: $model)
|
|
}
|
|
}
|
|
}
|
|
|
|
fileprivate struct ExerciseFormView: View {
|
|
@Binding var model: Exercise
|
|
@Query(sort: [SortDescriptor(\ExerciseType.name)]) var exerciseTypes: [ExerciseType]
|
|
|
|
var body: some View {
|
|
Section(header: Text("Name")) {
|
|
TextField("Name", text: $model.name)
|
|
.bold()
|
|
}
|
|
|
|
Section(header: Text("Exercise Type")) {
|
|
Picker("Type", selection: $model.type) {
|
|
Text("Select a type").tag(nil as ExerciseType?)
|
|
ForEach(exerciseTypes) { type in
|
|
Text(type.name).tag(type as ExerciseType?)
|
|
}
|
|
}
|
|
}
|
|
|
|
Section(header: Text("Description")) {
|
|
TextEditor(text: $model.descr)
|
|
.frame(minHeight: 100)
|
|
}
|
|
}
|
|
}
|