78 lines
1.5 KiB
Swift
78 lines
1.5 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
|
|
var position: Int = 0
|
|
|
|
var reps: Int = 8
|
|
|
|
// EXERCISE
|
|
var exercise: Exercise?
|
|
// TODO: Think about what's happening when an exercise (template) is deleted or changed
|
|
// { didSet { self.name = exercise?.name ?? "self.name" } }
|
|
|
|
init(_ reps: Int, _ exercise: String) {
|
|
self.workoutItemType = .exercise
|
|
|
|
self.name = exercise
|
|
self.reps = reps
|
|
self.exercise = Exercise(exercise)
|
|
}
|
|
|
|
init(from exercise: Exercise) {
|
|
self.workoutItemType = .exercise
|
|
|
|
self.name = exercise.name
|
|
self.exercise = exercise
|
|
}
|
|
|
|
// SET
|
|
var workoutItems: [WorkoutItem] = []
|
|
|
|
init(workoutItems: [WorkoutItem] = []) {
|
|
self.name = "Set"
|
|
self.workoutItemType = .set
|
|
self.reps = 3
|
|
workoutItems.forEach(addChild)
|
|
}
|
|
|
|
func addChild(_ child: WorkoutItem) {
|
|
if self.workoutItemType == .set {
|
|
self.workoutItems.append(child)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension WorkoutItem {
|
|
enum WorkoutItemType: Codable {
|
|
case set
|
|
case workout
|
|
case exercise
|
|
}
|
|
}
|
|
|
|
extension WorkoutItem {
|
|
static let sampleData: [WorkoutItem] = {
|
|
var exercises = [WorkoutItem]()
|
|
|
|
for exercise in Exercise.sampleData {
|
|
exercises.append(WorkoutItem(from: exercise))
|
|
}
|
|
|
|
return exercises
|
|
}()
|
|
}
|