Files
workoutsplus/WorkoutsPlus/Models/WorkoutSession.swift

112 lines
2.7 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 = ""
var workout: Workout? {
didSet {
self.name = workout?.name ?? "Unknown Workout"
}
}
// State
// var isPaused: Bool
// var isCancelled: Bool
// var isDeleted: Bool
// var isSynced: Bool
// Time
var creationDate = Date.now
// My workout session started at:
var startDate: Date? = nil
// 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
init () { }
func isActive() -> Bool {
return startDate != nil && 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() {
guard let startDate = startDate else { return }
isCompleted = true
stopDate = Date.now
duration = stopDate!.timeIntervalSince(startDate)
}
func prevExercise() {
guard workout != nil else { return }
if currentExercise > 0 {
currentExercise -= 1
}
}
func nextExercise() {
guard let workout = workout else { return }
if currentExercise < workout.getWorkoutItems().count - 1 {
currentExercise += 1
}
}
// MARK: -- Workout Information
func getFormattedDuration() -> String {
guard let startDate = startDate else { return "00:00:00" }
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 {
guard let workout = workout else { return 0 }
return Double(workout.getWorkoutItems().count)
}
func getCurrentExerciseIndex() -> Double {
return Double(currentExercise)
}
func getCurrentTodo() -> String {
return getCurrentExerciseMetric() + " " + getCurrentExerciseName()
}
func getCurrentExerciseName() -> String {
guard let workout = workout else { return "Unknown Workout" }
return workout.getWorkoutItems()[Int(currentExercise)].name
}
func getCurrentExerciseMetric() -> String {
guard let workout = workout else { return "Unknown Workout" }
return String(workout.getWorkoutItems()[Int(currentExercise)].reps)
}
}