56 lines
1.4 KiB
Swift
56 lines
1.4 KiB
Swift
//
|
|
// TimerModule.swift
|
|
// Werkout_ios
|
|
//
|
|
// Created by Trey Tartt on 6/14/23.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
class BridgeModule: ObservableObject {
|
|
static let shared = BridgeModule()
|
|
@Published var isShowingOnExternalDisplay = false
|
|
|
|
private var timer: Timer?
|
|
@Published var timeLeft: Int = 0
|
|
|
|
var timerCompleted: (() -> Void)?
|
|
@Published var currentExercise: ExerciseElement?
|
|
@Published var currentWorkout: Workout?
|
|
@Published var currentExerciseIdx: Int = -1
|
|
|
|
private func startTimerWith(duration: Int) {
|
|
timer?.invalidate()
|
|
timer = nil
|
|
timeLeft = duration
|
|
timer = Timer.scheduledTimer(timeInterval: 1,
|
|
target: self,
|
|
selector: #selector(updateCounter),
|
|
userInfo: nil,
|
|
repeats: true)
|
|
timer?.fire()
|
|
}
|
|
|
|
@objc func updateCounter() {
|
|
if timeLeft > 0 {
|
|
timeLeft -= 1
|
|
} else {
|
|
timer?.invalidate()
|
|
timer = nil
|
|
timerCompleted?()
|
|
}
|
|
}
|
|
|
|
func updateCurrent(workout: Workout) {
|
|
self.currentWorkout = workout
|
|
}
|
|
|
|
func updateCurrent(exercise: ExerciseElement) {
|
|
self.currentExercise = exercise
|
|
|
|
if let duration = exercise.duration {
|
|
startTimerWith(duration: duration)
|
|
}
|
|
}
|
|
}
|