WIP
This commit is contained in:
@@ -14,10 +14,50 @@ class BridgeModule: ObservableObject {
|
||||
private var timer: Timer?
|
||||
@Published var timeLeft: Int = 0
|
||||
|
||||
var timerCompleted: (() -> Void)?
|
||||
@Published var currentExercise: ExerciseElement?
|
||||
var currentWorkout: Workout?
|
||||
@Published var currentExerciseIdx: Int = -1
|
||||
var currentExerciseIdx: Int = -1
|
||||
|
||||
@Published var currentWorkoutRunTimeInSeconds: Int = -1
|
||||
private var currentWorkoutRunTimer: Timer?
|
||||
|
||||
func start(workout: Workout, atExerciseIndex: Int = 0) {
|
||||
self.currentWorkout = workout
|
||||
currentWorkoutRunTimeInSeconds = 0
|
||||
currentWorkoutRunTimer?.invalidate()
|
||||
currentWorkoutRunTimer = nil
|
||||
|
||||
currentExerciseIdx = 0
|
||||
let exercise = workout.exercises[currentExerciseIdx]
|
||||
updateCurrent(exercise: exercise)
|
||||
startWorkoutTimer()
|
||||
}
|
||||
|
||||
func completeWorkout() {
|
||||
currentWorkoutRunTimeInSeconds = 0
|
||||
currentWorkoutRunTimer?.invalidate()
|
||||
currentWorkoutRunTimer = nil
|
||||
|
||||
currentWorkoutRunTimer?.invalidate()
|
||||
currentWorkoutRunTimer = nil
|
||||
|
||||
currentWorkoutRunTimeInSeconds = -1
|
||||
currentExerciseIdx = -1
|
||||
|
||||
currentExercise = nil
|
||||
currentWorkout = nil
|
||||
}
|
||||
|
||||
private func startWorkoutTimer() {
|
||||
currentWorkoutRunTimer?.invalidate()
|
||||
currentWorkoutRunTimer = nil
|
||||
currentWorkoutRunTimer = Timer.scheduledTimer(timeInterval: 1,
|
||||
target: self,
|
||||
selector: #selector(addOneToWorkoutRunTime),
|
||||
userInfo: nil,
|
||||
repeats: true)
|
||||
currentWorkoutRunTimer?.fire()
|
||||
}
|
||||
|
||||
private func startTimerWith(duration: Int) {
|
||||
timer?.invalidate()
|
||||
@@ -37,17 +77,24 @@ class BridgeModule: ObservableObject {
|
||||
} else {
|
||||
timer?.invalidate()
|
||||
timer = nil
|
||||
timerCompleted?()
|
||||
|
||||
currentExerciseIdx += 1
|
||||
if let currentWorkout = currentWorkout {
|
||||
if currentExerciseIdx < currentWorkout.exercises.count {
|
||||
let nextExercise = currentWorkout.exercises[currentExerciseIdx]
|
||||
updateCurrent(exercise: nextExercise)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func updateCurrent(workout: Workout) {
|
||||
self.currentWorkout = workout
|
||||
@objc func addOneToWorkoutRunTime() {
|
||||
currentWorkoutRunTimeInSeconds += 1
|
||||
}
|
||||
|
||||
func updateCurrent(exercise: ExerciseElement) {
|
||||
self.currentExercise = exercise
|
||||
|
||||
|
||||
if let duration = exercise.duration {
|
||||
startTimerWith(duration: duration)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user