131 lines
3.8 KiB
Swift
131 lines
3.8 KiB
Swift
//
|
|
// CurrentWorkoutInfo.swift
|
|
// Werkout_ios
|
|
//
|
|
// Created by Trey Tartt on 7/17/23.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
class CurrentWorkoutInfo {
|
|
var supersetIndex: Int = 0
|
|
var exerciseIndex: Int = 0
|
|
var workout: Workout?
|
|
var complete: (() -> Void)?
|
|
|
|
var currentRound = 1
|
|
var allSupersetExecerciseIndex = 0
|
|
|
|
var superset: [Superset] {
|
|
return workout?.supersets?.sorted(by: { $0.order < $1.order }) ?? [Superset]()
|
|
}
|
|
|
|
var currentExercise: SupersetExercise? {
|
|
guard let supersets = workout?.supersets else { return nil }
|
|
|
|
if supersetIndex >= supersets.count { return nil }
|
|
let superset = supersets[supersetIndex]
|
|
|
|
// will be -1 for a moment while going to previous workout / superset
|
|
if exerciseIndex < 0 { return nil }
|
|
if exerciseIndex > superset.exercises.count { return nil }
|
|
let exercise = superset.exercises[exerciseIndex]
|
|
return exercise
|
|
}
|
|
|
|
var allSupersetExecercise: SupersetExercise? {
|
|
let obj = workout?.allSupersetExecercise?[allSupersetExecerciseIndex]
|
|
return obj
|
|
}
|
|
|
|
var nextExercise: SupersetExercise? {
|
|
guard let workout = workout else { return nil }
|
|
guard let supersets = workout.supersets else { return nil }
|
|
|
|
exerciseIndex += 1
|
|
let currentSuperSet = supersets[supersetIndex]
|
|
|
|
if exerciseIndex >= currentSuperSet.exercises.count {
|
|
currentRound += 1
|
|
|
|
if currentRound > currentSuperSet.rounds {
|
|
supersetIndex += 1
|
|
currentRound = 1
|
|
|
|
if supersetIndex >= supersets.count {
|
|
complete?()
|
|
return nil
|
|
}
|
|
}
|
|
|
|
exerciseIndex = 0
|
|
}
|
|
|
|
let superset = supersets[supersetIndex]
|
|
let exercise = superset.exercises[exerciseIndex]
|
|
allSupersetExecerciseIndex += 1
|
|
return exercise
|
|
}
|
|
|
|
var previousExercise: SupersetExercise? {
|
|
guard let workout = workout else { return nil }
|
|
guard let supersets = workout.supersets else { return nil }
|
|
|
|
exerciseIndex -= 1
|
|
if exerciseIndex < 0 {
|
|
if currentRound > 1 {
|
|
currentRound -= 1
|
|
let superset = supersets[supersetIndex]
|
|
exerciseIndex = superset.exercises.count-1
|
|
} else {
|
|
if supersetIndex > 0 {
|
|
supersetIndex -= 1
|
|
let superset = supersets[supersetIndex]
|
|
currentRound = superset.rounds
|
|
exerciseIndex = superset.exercises.count-1
|
|
} else {
|
|
exerciseIndex = 0
|
|
}
|
|
}
|
|
}
|
|
|
|
let superset = supersets[supersetIndex]
|
|
let exercise = superset.exercises[exerciseIndex]
|
|
|
|
allSupersetExecerciseIndex -= 1
|
|
if allSupersetExecerciseIndex < 0 {
|
|
allSupersetExecerciseIndex = 0
|
|
}
|
|
|
|
return exercise
|
|
}
|
|
|
|
func start(workout: Workout) {
|
|
reset()
|
|
self.workout = workout
|
|
}
|
|
|
|
func reset() {
|
|
supersetIndex = 0
|
|
exerciseIndex = 0
|
|
currentRound = 1
|
|
allSupersetExecerciseIndex = 0
|
|
self.workout = nil
|
|
}
|
|
|
|
func goToWorkoutAt(supersetIndex: Int, exerciseIndex: Int) -> SupersetExercise? {
|
|
self.supersetIndex = supersetIndex
|
|
self.exerciseIndex = exerciseIndex
|
|
self.currentRound = 1
|
|
|
|
let currentExercise = currentExercise
|
|
if let firstIdx = workout?.allSupersetExecercise?.firstIndex(where: {
|
|
$0.exercise.id == currentExercise?.exercise.id
|
|
}) {
|
|
allSupersetExecerciseIndex = firstIdx
|
|
}
|
|
|
|
return currentExercise
|
|
}
|
|
}
|