Files
workoutsplus/WorkoutsPlus/Features/WorkoutSession/WorkoutSession.swift
2024-11-12 15:02:52 +01:00

95 lines
2.3 KiB
Swift

//
// WorkoutSession.swift
// WorkoutsPlus
//
// Created by Felix Förtsch on 07.09.24.
//
import Foundation
import SwiftData
@Model
final class WorkoutSession: Nameable {
var id = UUID()
var name: String
// TODO: Think about if a refrence to the workout makes sense; a Workout could change.
// var workout: Workout
var workoutSessionItems: [WorkoutSessionItem] = []
init(start workout: Workout) {
self.name = workout.name
for workoutItem in workout.getWorkoutItems() {
workoutSessionItems.append(WorkoutSessionItem(workoutSession: self, planned: workoutItem))
}
}
var isPaused = false
// My workout session started at:
var startDate: Date = Date.now
// My workout session was completed at:
var stopDate: Date? = nil
// My workout session took me x seconds.
var workoutDuration: TimeInterval = 0.0
var pauseDuration: TimeInterval = 0.0
// Exercise Progress
var currentExercise = 0
func isCompleted() -> Bool {
stopDate != nil
}
// MARK: -- Workout Controls
// func pause() {
// isPaused = true
// }
//
// func resume() {
// isPaused = false
// }
// Call stop() to terminate the workout.
func stop() -> WorkoutLogItem {
stopDate = Date.now
// duration = stopDate!.timeIntervalSince(startDate)
return WorkoutLogItem(log: self)
}
func prevExercise() {
workoutSessionItems[currentExercise].startDate = Date.now
if currentExercise > 0 {
currentExercise -= 1
}
}
func nextExercise() {
workoutSessionItems[currentExercise].stopDate = Date.now
if currentExercise < workoutSessionItems.count - 1 {
currentExercise += 1
}
}
// 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
}
}