Files
2024-11-26 17:22:29 +01:00

64 lines
1.7 KiB
Swift

//
// WorkoutSession.swift
// WorkoutsPlus
//
// Created by Felix Förtsch on 07.09.24.
//
import Foundation
import SwiftData
@Model
final class WorkoutSession: Nameable {
static var systemImage = "figure.squash.circle"
var id = UUID()
var name: String
var workoutSessionStatus: WorkoutSessionStatus
enum WorkoutSessionStatus: Codable {
case planned
case inProgress
case paused
case completed
}
var startDate: Date = Date.now
var stopDate: Date? = nil
var workoutDuration: TimeInterval = 0.0
var pauseDuration: TimeInterval = 0.0
// TODO: Think about if a refrence to the workout makes sense; a Workout could change.
// var workout: Workout
var workoutSessionItems: [WorkoutSessionItem] = []
// Exercise progress, modeled as index into workoutSessionItems: [WorkoutSessionItem]
var currentExercise = 0
init(start workout: Workout) {
self.name = workout.name
self.workoutSessionStatus = .planned
for workoutItem in workout.getWorkoutItems() {
workoutSessionItems.append(WorkoutSessionItem(workoutSession: self, planned: workoutItem))
}
}
// MARK: -- Workout Information
func getFormattedDuration() -> String {
let elapsedTime = Date.now.timeIntervalSince(startDate)
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute, .second]
formatter.unitsStyle = .positional
return formatter.string(from: elapsedTime) ?? "00:00:00"
}
func getTotalExerciseCount() -> Double {
return Double(workoutSessionItems.count)
}
func getCurrentExerciseIndex() -> Double {
return Double(currentExercise)
}
func getCurrentExerciseName() -> String {
return workoutSessionItems[currentExercise].name
}
}