69 lines
1.5 KiB
Swift
69 lines
1.5 KiB
Swift
//
|
|
// Workout.swift
|
|
// WorkoutsPlus
|
|
//
|
|
// Created by Felix Förtsch on 10.08.24.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftData
|
|
|
|
@Model
|
|
final class Workout: Nameable {
|
|
static var systemImage = "figure.run.square.stack"
|
|
|
|
var id = UUID()
|
|
@Attribute(.unique) var name: String
|
|
|
|
// Other properties and methods
|
|
var timestamp: Date = Date.now
|
|
|
|
@Relationship(deleteRule: .cascade) var workoutItems: [WorkoutItem] = []
|
|
|
|
init(name: String) {
|
|
self.name = name
|
|
}
|
|
|
|
func add(workoutItem: WorkoutItem) {
|
|
self.workoutItems.append(workoutItem)
|
|
updateWorkoutItemsPositions()
|
|
}
|
|
|
|
func add(workoutItems: [WorkoutItem]) {
|
|
for workoutItem in workoutItems {
|
|
self.workoutItems.append(workoutItem)
|
|
}
|
|
updateWorkoutItemsPositions()
|
|
}
|
|
|
|
func moveWorkoutItem(from source: IndexSet, to destination: Int) {
|
|
workoutItems.move(fromOffsets: source, toOffset: destination)
|
|
updateWorkoutItemsPositions()
|
|
}
|
|
|
|
private func updateWorkoutItemsPositions() {
|
|
for (index, exercise) in workoutItems.enumerated() {
|
|
exercise.position = index
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension Workout {
|
|
private convenience init(name: String, exercises: [WorkoutItem]) {
|
|
self.init(name: name)
|
|
self.workoutItems = exercises
|
|
}
|
|
|
|
static let sampleData: Workout = {
|
|
var workout = Workout(name: "Recommended Routine")
|
|
|
|
for workoutItem in WorkoutItem.sampleData {
|
|
workout.add(workoutItem: workoutItem)
|
|
}
|
|
|
|
return workout
|
|
}()
|
|
|
|
}
|