Files
workoutsplus/WorkoutsPlus/Features/Workout/WorkoutItem.swift
2024-10-21 15:03:47 +02:00

109 lines
2.9 KiB
Swift

//
// WorkoutItem.swift
// WorkoutsPlus
//
// Created by Felix Förtsch on 10.08.24.
//
// 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: 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)
// }
// struct ExerciseValue: ValueType {
// var value: String = ""
// var unit: ExerciseUnit
// }
// TODO: Deload items -> Think about how to model deload/sick/rest/holiday week
//enum PerformanceMetric: String, CaseIterable, CustomStringConvertible {
// case speed = "m/s"
// case pace = "s/m"
//
// var description: String { rawValue }
//}
import Foundation
import SwiftData
@Model
final class WorkoutItem: Nameable, Positionable {
var id = UUID()
var name: String
var workout: Workout?
// TODO: Make WorkoutItem a protocol so the type can be distinguished by the class type
var workoutItemType: WorkoutItemType // Differentiates between exercise/rest/set
var position: Int = 0
var set: [WorkoutItem] = []
var exercise: Exercise // Do Push-up | Run Marathon
var plannedReps: Int // 8 times | 1 time
var plannedValue: Double // With 10 | 42,187
var metric: ExerciseMetric? // kg (weight) | km (distance)
enum WorkoutItemType: Codable {
case exercise
case rest
case set
}
init(_ exercise: Exercise) {
self.exercise = exercise
self.workoutItemType = .exercise
// Push-up
self.name = exercise.name
// 8x
self.plannedReps = 1
// 0
self.plannedValue = 0
// kg
self.metric = exercise.metric
}
// init(set: [WorkoutItem] = []) {
// self.workoutItemType = .set
// self.name = "Set"
// self.plannedReps = 3
// self.plannedValue = 0
// set.forEach(addChild)
// }
// init(rest: Double) {
// self.workoutItemType = .rest
// self.name = "Rest"
// self.plannedReps = 1
// self.plannedValue = rest
// self.metric = .time
// }
func addChild(_ child: WorkoutItem) {
if self.workoutItemType == .set {
self.set.append(child)
}
}
}
extension WorkoutItem {
static let sampleDataRecommendedRoutine: [WorkoutItem] = {
var exercises = [WorkoutItem]()
for exercise in Exercise.sampleDataRecommendedRoutine {
exercises.append(WorkoutItem(exercise))
}
return exercises
}()
static let sampleDataRings: [WorkoutItem] = {
var exercises = [WorkoutItem]()
for exercise in Exercise.sampleDataRings {
exercises.append(WorkoutItem(exercise))
}
return exercises
}()
}