118 lines
2.9 KiB
Swift
118 lines
2.9 KiB
Swift
//
|
|
// WorkoutItem.swift
|
|
// WorkoutsPlus
|
|
//
|
|
// Created by Felix Förtsch on 10.08.24.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftData
|
|
|
|
@Model
|
|
final class WorkoutItem: Nameable, Positionable {
|
|
var id = UUID()
|
|
var name: String
|
|
|
|
var workout: Workout?
|
|
var workoutItemType: WorkoutItemType
|
|
enum WorkoutItemType: Codable {
|
|
// TODO: Add workout as WorkoutItemType (needs recursive dealing)
|
|
// case workout
|
|
case rest
|
|
case set
|
|
case exerciseWithReps
|
|
case exerciseWithDuration
|
|
}
|
|
|
|
var position: Int = 0
|
|
// TODO: Wondering if a SortDescriptor in the Model is useful?
|
|
// https://old.reddit.com/r/SwiftUI/comments/1fnvkud/extracting_the_creation_of_swiftdata_query_into/
|
|
// static var sorted: SortDescriptor<WorkoutItem> {
|
|
// SortDescriptor(\.position, order: .forward)
|
|
// }
|
|
|
|
var reps: Int = 0
|
|
var duration: Int = 0
|
|
|
|
// EXERCISE
|
|
var exercise: Exercise?
|
|
// TODO: Think about what's happening when an exercise (template) is deleted or changed
|
|
// If it is delete -> we just keep the WorkoutItem with the name :)
|
|
// The only relevant delete is delete Workout -> Delete Workoutitems
|
|
// { didSet { self.name = exercise?.name ?? "self.name" } }
|
|
|
|
// TODO: Deload items -> Think about how to model deload/sick/rest/holiday week
|
|
|
|
// Exercise
|
|
init(reps: Int, _ exercise: String) {
|
|
self.workoutItemType = .exerciseWithReps
|
|
self.name = exercise
|
|
self.reps = reps
|
|
self.exercise = Exercise(exercise, .reps)
|
|
}
|
|
|
|
init(duration: Int, _ exercise: String) {
|
|
self.workoutItemType = .exerciseWithDuration
|
|
self.name = exercise
|
|
self.duration = duration
|
|
self.exercise = Exercise(exercise, .duration)
|
|
}
|
|
|
|
init(exercise: Exercise) {
|
|
self.workoutItemType = .exerciseWithReps
|
|
self.name = exercise.name
|
|
self.exercise = exercise
|
|
}
|
|
|
|
// SET
|
|
var set: [WorkoutItem] = []
|
|
|
|
init(set: [WorkoutItem] = []) {
|
|
self.workoutItemType = .set
|
|
self.name = "Set"
|
|
self.reps = 3
|
|
set.forEach(addChild)
|
|
}
|
|
|
|
func addChild(_ child: WorkoutItem) {
|
|
if self.workoutItemType == .set {
|
|
self.set.append(child)
|
|
}
|
|
}
|
|
|
|
// PAUSE
|
|
init (rest: Int) {
|
|
self.workoutItemType = .rest
|
|
self.name = "Rest"
|
|
self.duration = rest
|
|
}
|
|
}
|
|
|
|
extension WorkoutItem {
|
|
static let sampleDataRecommendedRoutine: [WorkoutItem] = {
|
|
var exercises = [WorkoutItem]()
|
|
|
|
for exercise in Exercise.sampleDataRecommendedRoutine {
|
|
exercises.append(WorkoutItem(exercise: exercise))
|
|
}
|
|
|
|
// var set = WorkoutItem(workoutItems: [
|
|
// WorkoutItem(from: Exercise("Set item 1")),
|
|
// WorkoutItem(from: Exercise("Set item 2"))
|
|
// ])
|
|
// exercises.append(set)
|
|
|
|
return exercises
|
|
}()
|
|
|
|
static let sampleDataRings: [WorkoutItem] = {
|
|
var exercises = [WorkoutItem]()
|
|
|
|
for exercise in Exercise.sampleDataRings {
|
|
exercises.append(WorkoutItem(exercise: exercise))
|
|
}
|
|
|
|
return exercises
|
|
}()
|
|
}
|