119 lines
3.3 KiB
Swift
119 lines
3.3 KiB
Swift
//
|
|
// BridgeModule+WorkoutActions.swift
|
|
// Werkout_ios
|
|
//
|
|
// Created by Trey Tartt on 6/21/24.
|
|
//
|
|
|
|
import Foundation
|
|
import WatchConnectivity
|
|
|
|
extension BridgeModule {
|
|
func pauseWorkout() {
|
|
if let _ = currentExerciseTimer {
|
|
currentExerciseTimer?.invalidate()
|
|
currentExerciseTimer = nil
|
|
isPaused = true
|
|
} else {
|
|
isPaused = false
|
|
startExerciseTimerWith(duration: currentExerciseTimeLeft)
|
|
}
|
|
}
|
|
|
|
func nextExercise() {
|
|
if let nextSupersetExercise = currentWorkoutInfo.goToNextExercise {
|
|
updateCurrent(exercise: nextSupersetExercise)
|
|
} else {
|
|
completeWorkout()
|
|
}
|
|
}
|
|
|
|
func previousExercise() {
|
|
if let nextSupersetExercise = currentWorkoutInfo.previousExercise {
|
|
updateCurrent(exercise: nextSupersetExercise)
|
|
} else {
|
|
completeWorkout()
|
|
}
|
|
}
|
|
|
|
func restartExercise() {
|
|
if let currentExercise = currentWorkoutInfo.currentExercise {
|
|
updateCurrent(exercise: currentExercise)
|
|
}
|
|
}
|
|
|
|
func updateCurrent(exercise: SupersetExercise) {
|
|
DispatchQueue.main.async {
|
|
self.currentExerciseTimer?.invalidate()
|
|
self.currentExerciseTimer = nil
|
|
|
|
if let duration = exercise.duration, duration > 0 {
|
|
self.startExerciseTimerWith(duration: duration)
|
|
}
|
|
}
|
|
}
|
|
|
|
func completeWorkout() {
|
|
self.currentExerciseTimer?.invalidate()
|
|
self.currentExerciseTimer = nil
|
|
self.isInWorkout = false
|
|
|
|
workoutEndDate = Date()
|
|
if let completedWorkout = completedWorkout {
|
|
completedWorkout()
|
|
self.completedWorkout = nil
|
|
}
|
|
resetCurrentWorkout()
|
|
}
|
|
|
|
func start(workout: Workout) {
|
|
currentWorkoutInfo.complete = {
|
|
self.completeWorkout()
|
|
}
|
|
|
|
currentWorkoutInfo.start(workout: workout)
|
|
currentWorkoutRunTimeInSeconds = 0
|
|
currentWorkoutRunTimer?.invalidate()
|
|
currentWorkoutRunTimer = nil
|
|
isPaused = false
|
|
|
|
if let superetExercise = currentWorkoutInfo.currentExercise {
|
|
updateCurrent(exercise: superetExercise)
|
|
startWorkoutTimer()
|
|
workoutStartDate = Date()
|
|
isInWorkout = true
|
|
|
|
if WCSession.isSupported() {
|
|
session.delegate = self
|
|
session.activate()
|
|
sendStartWorkoutToWatch()
|
|
}
|
|
}
|
|
}
|
|
|
|
var nextExerciseObject: SupersetExercise? {
|
|
currentWorkoutInfo.goToNextExercise
|
|
}
|
|
|
|
func resetCurrentWorkout() {
|
|
DispatchQueue.main.async {
|
|
if self.isInWorkout {
|
|
self.sendWorkoutCompleteToWatch()
|
|
}
|
|
self.currentWorkoutRunTimeInSeconds = 0
|
|
self.currentWorkoutRunTimer?.invalidate()
|
|
self.currentWorkoutRunTimer = nil
|
|
|
|
self.currentExerciseTimer?.invalidate()
|
|
self.currentExerciseTimer = nil
|
|
|
|
self.currentWorkoutRunTimeInSeconds = -1
|
|
self.currentWorkoutInfo.reset()
|
|
|
|
self.isInWorkout = false
|
|
self.workoutStartDate = nil
|
|
self.workoutEndDate = nil
|
|
}
|
|
}
|
|
}
|