clean up ActiveWorkoutSession
This commit is contained in:
@@ -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()
|
||||
}
|
||||
@@ -26,53 +26,42 @@ final class Exercise: Nameable {
|
||||
@Attribute(.unique) var name: String
|
||||
var exerciseDescription = ""
|
||||
|
||||
var metric = ExerciseMetric.none
|
||||
var unit: ExerciseUnit
|
||||
var type: ExerciseType?
|
||||
var unit: ExerciseUnit?
|
||||
|
||||
var isPartOfProgression = false
|
||||
|
||||
var equipment: [Equipment] = []
|
||||
|
||||
init(_ name: String, _ metric: ExerciseMetric = .none) {
|
||||
init(_ name: String) {
|
||||
self.name = name
|
||||
self.metric = metric
|
||||
self.unit = ExerciseUnit(name: "Meters", symbol: "m")
|
||||
}
|
||||
}
|
||||
|
||||
enum ExerciseMetric: String, Codable, CaseIterable, Identifiable {
|
||||
case none = ""
|
||||
case distance = "Distance"
|
||||
case time = "Time"
|
||||
case weight = "Weight"
|
||||
|
||||
var id: Self { self }
|
||||
}
|
||||
|
||||
extension Exercise {
|
||||
static let sampleDataRecommendedRoutine: [Exercise] = [
|
||||
Exercise("Shoulder Band Warm-up"),
|
||||
Exercise("Squat Sky Reaches"),
|
||||
Exercise("GMB Wrist Prep", .time),
|
||||
Exercise("GMB Wrist Prep"),
|
||||
Exercise("Dead Bugs"),
|
||||
Exercise("Pull-up Progression", .weight),
|
||||
Exercise("Dip Progression", .weight),
|
||||
Exercise("Squat Progression", .weight),
|
||||
Exercise("Hinge Progression", .weight),
|
||||
Exercise("Row Progression", .weight),
|
||||
Exercise("Push-up Progression", .weight),
|
||||
Exercise("Handstand Practice", .time),
|
||||
Exercise("Support Practice", .time)
|
||||
Exercise("Pull-up Progression"),
|
||||
Exercise("Dip Progression"),
|
||||
Exercise("Squat Progression"),
|
||||
Exercise("Hinge Progression"),
|
||||
Exercise("Row Progression"),
|
||||
Exercise("Push-up Progression"),
|
||||
Exercise("Handstand Practice"),
|
||||
Exercise("Support Practice")
|
||||
]
|
||||
|
||||
static let sampleDataRings: [Exercise] = [
|
||||
Exercise("Dips", .weight),
|
||||
Exercise("Chin-ups", .weight),
|
||||
Exercise("Push-ups", .weight),
|
||||
Exercise("Inverted Rows", .weight),
|
||||
Exercise("Hanging Knee Raises", .weight),
|
||||
Exercise("Pistol Squats", .weight),
|
||||
Exercise("Hanging Leg Curls", .weight),
|
||||
Exercise("Dips"),
|
||||
Exercise("Chin-ups"),
|
||||
Exercise("Push-ups"),
|
||||
Exercise("Inverted Rows"),
|
||||
Exercise("Hanging Knee Raises"),
|
||||
Exercise("Pistol Squats"),
|
||||
Exercise("Hanging Leg Curls"),
|
||||
Exercise("Sissy Squats")
|
||||
]
|
||||
|
||||
|
||||
@@ -6,26 +6,64 @@
|
||||
//
|
||||
|
||||
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 {
|
||||
TextField("Exercise Name", text: $exercise.name)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .topBarTrailing) {
|
||||
Button("Save") {
|
||||
saveItem()
|
||||
dismiss()
|
||||
}
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle("Exercise Details")
|
||||
}
|
||||
|
||||
private func saveItem() {
|
||||
@@ -41,6 +79,6 @@ struct ExerciseDetail: View {
|
||||
|
||||
#Preview {
|
||||
NavigationStack {
|
||||
ExerciseDetail(exercise: Exercise.sampleDataRecommendedRoutine.first!)
|
||||
ExerciseDetail(exercise: Exercise("Squat Sky Reaches"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,117 +0,0 @@
|
||||
//
|
||||
// AddExercise.swift
|
||||
// WorkoutsPlus
|
||||
//
|
||||
// Created by Felix Förtsch on 21.09.24.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
struct ExerciseEditor: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
var isPresentedAsSheet: Bool = false
|
||||
|
||||
@State var exercise: Exercise?
|
||||
|
||||
@State private var name: String = ""
|
||||
@State private var description: String = ""
|
||||
@State private var metric = ExerciseMetric.none
|
||||
@State private var unit = ExerciseUnit(name: "", symbol: "")
|
||||
|
||||
@State private var isPartOfProgression: Bool = false
|
||||
|
||||
let exerciseUnits = ExerciseUnit.getUnits
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
VStack {
|
||||
ScrollViewReader { proxy in
|
||||
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 Metric", selection: $metric) {
|
||||
ForEach(ExerciseMetric.allCases) { metric in
|
||||
Text(metric.rawValue.isEmpty ? "None" : metric.rawValue)
|
||||
.tag(metric)
|
||||
}
|
||||
}
|
||||
.pickerStyle(SegmentedPickerStyle())
|
||||
Picker("Select 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")
|
||||
.toolbar {
|
||||
if isPresentedAsSheet {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button("Cancel", role: .cancel) {
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
Button("Save") {
|
||||
withAnimation {
|
||||
save()
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
if let exercise {
|
||||
self.name = exercise.name
|
||||
self.description = exercise.exerciseDescription
|
||||
self.metric = exercise.metric
|
||||
self.isPartOfProgression = exercise.isPartOfProgression
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func save() {
|
||||
let exerciseToSave: Exercise
|
||||
|
||||
if let exercise = exercise {
|
||||
exerciseToSave = exercise
|
||||
} else {
|
||||
exerciseToSave = Exercise(name)
|
||||
modelContext.insert(exerciseToSave)
|
||||
}
|
||||
|
||||
exerciseToSave.name = name
|
||||
exerciseToSave.exerciseDescription = description
|
||||
exerciseToSave.metric = metric
|
||||
exerciseToSave.isPartOfProgression = isPartOfProgression
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ExerciseEditor()
|
||||
}
|
||||
@@ -31,17 +31,7 @@ struct ExerciseLibrary: View {
|
||||
List {
|
||||
Section {
|
||||
ForEach(filteredItems) { exercise in
|
||||
NavigationLink {
|
||||
ExerciseEditor(exercise: exercise)
|
||||
} label: {
|
||||
HStack {
|
||||
Text(exercise.name)
|
||||
Spacer()
|
||||
// Text(exercise.metric.rawValue)
|
||||
// .font(.footnote)
|
||||
// .foregroundStyle(.gray)
|
||||
}
|
||||
}
|
||||
NavigationLink(destination: ExerciseDetail(exercise: exercise), label: {Text(exercise.name)})
|
||||
}
|
||||
.onDelete(perform: deleteExercise)
|
||||
if filteredItems.isEmpty {
|
||||
@@ -66,7 +56,7 @@ struct ExerciseLibrary: View {
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $isAddingExercise) {
|
||||
ExerciseEditor(isPresentedAsSheet: true)
|
||||
AddExercise(isPresentedAsSheet: true)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// ExerciseType.swift
|
||||
// WorkoutsPlus
|
||||
//
|
||||
// Created by Felix Förtsch on 09.11.24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
@Model
|
||||
final class ExerciseType: Identifiable {
|
||||
@Attribute(.unique) var name: String
|
||||
|
||||
@Relationship(deleteRule: .nullify, inverse: \Exercise.type)
|
||||
var exercises = [Exercise]()
|
||||
|
||||
init(_ name: String) {
|
||||
self.name = name
|
||||
}
|
||||
}
|
||||
|
||||
extension ExerciseType {
|
||||
static let getTypes = [
|
||||
ExerciseType("Kilograms"),
|
||||
ExerciseType("Kilometers"),
|
||||
ExerciseType("Meters"),
|
||||
]
|
||||
}
|
||||
@@ -9,20 +9,23 @@ import Foundation
|
||||
import SwiftData
|
||||
|
||||
@Model
|
||||
final class ExerciseUnit: Unit {
|
||||
var name: String
|
||||
var symbol: String
|
||||
final class ExerciseUnit: Unit, Identifiable {
|
||||
@Attribute(.unique) var name: String
|
||||
@Attribute(.unique) var symbol: String
|
||||
|
||||
init(name: String, symbol: String) {
|
||||
@Relationship(deleteRule: .cascade, inverse: \Exercise.unit)
|
||||
var exercises = [Exercise]()
|
||||
|
||||
init(_ name: String, symbol: String) {
|
||||
self.name = name
|
||||
self.symbol = symbol
|
||||
}
|
||||
}
|
||||
|
||||
extension ExerciseUnit: Identifiable {
|
||||
extension ExerciseUnit {
|
||||
static let getUnits = [
|
||||
ExerciseUnit(name: "Kilograms", symbol: "kg"),
|
||||
ExerciseUnit(name: "Kilometers", symbol: "km"),
|
||||
ExerciseUnit(name: "Meters", symbol: "m"),
|
||||
ExerciseUnit("Kilograms", symbol: "kg"),
|
||||
ExerciseUnit("Kilometers", symbol: "km"),
|
||||
ExerciseUnit("Meters", symbol: "m"),
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user