Files
workoutsplus/WorkoutsPlus/Models/WorkoutSession.swift

106 lines
2.4 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 = ""
// The Workout is what *should* happen
var workout: Workout {
didSet {
self.name = workout?.name ?? "Unknown Workout"
}
}
init(start with: Workout) {
self.workout = with
}
// State
// var isPaused: Bool
// var isCancelled: Bool
// var isDeleted: Bool
// var isSynced: Bool
// 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 duration: TimeInterval? = nil
// My workout session is completed and moved to the workout log.
var isCompleted: Bool = false
// Exercise Progress
var currentExercise = 0
func isActive() -> Bool {
return stopDate == nil
}
// MARK: -- Workout Controls
// func start(with workout: Workout) {
// self.workout = workout
// startDate = Date.now
// }
func pause() {
// TODO: Implement proper Pause
}
// Call stop() to terminate the workout.
func stop() {
isCompleted = true
stopDate = Date.now
duration = stopDate!.timeIntervalSince(startDate)
}
func prevExercise() {
if currentExercise > 0 {
currentExercise -= 1
}
}
func nextExercise() {
if currentExercise < workout.getWorkoutItems().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(workout.getWorkoutItems().count)
}
func getCurrentExerciseIndex() -> Double {
return Double(currentExercise)
}
func getCurrentTodo() -> String {
return getCurrentExerciseMetric() + " " + getCurrentExerciseName()
}
func getCurrentExerciseName() -> String {
return workout.getWorkoutItems()[Int(currentExercise)].name
}
func getCurrentExerciseMetric() -> String {
return String(workout.getWorkoutItems()[Int(currentExercise)].plannedReps)
}
}