WIP
This commit is contained in:
@@ -6,8 +6,15 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import WatchConnectivity
|
||||
|
||||
enum WatchActions: String, Codable {
|
||||
case nextExercise
|
||||
}
|
||||
|
||||
class BridgeModule: NSObject, ObservableObject {
|
||||
private let kMessageKey = "message"
|
||||
|
||||
class BridgeModule: ObservableObject {
|
||||
static let shared = BridgeModule()
|
||||
@Published var isShowingOnExternalDisplay = false
|
||||
|
||||
@@ -19,6 +26,7 @@ class BridgeModule: ObservableObject {
|
||||
var currentExerciseIdx: Int = -1
|
||||
|
||||
var workoutStartDate: Date?
|
||||
var workoutEndDate: Date?
|
||||
@Published var currentWorkoutRunTimeInSeconds: Int = -1
|
||||
private var currentWorkoutRunTimer: Timer?
|
||||
|
||||
@@ -36,6 +44,11 @@ class BridgeModule: ObservableObject {
|
||||
startWorkoutTimer()
|
||||
workoutStartDate = Date()
|
||||
isInWorkout = true
|
||||
|
||||
if WCSession.isSupported() {
|
||||
WCSession.default.delegate = self
|
||||
WCSession.default.activate()
|
||||
}
|
||||
}
|
||||
|
||||
func resetCurrentWorkout() {
|
||||
@@ -52,8 +65,14 @@ class BridgeModule: ObservableObject {
|
||||
currentExercise = nil
|
||||
currentWorkout = nil
|
||||
|
||||
if isInWorkout {
|
||||
let watchModel = WatchPackageModel(currentExerciseName: currentExercise?.exercise.name ?? "-", currentTimeLeft: timeLeft, workoutStartDate: workoutStartDate ?? Date(), workoutEndDate: Date())
|
||||
let data = try! JSONEncoder().encode(watchModel)
|
||||
send(data)
|
||||
}
|
||||
isInWorkout = false
|
||||
workoutStartDate = nil
|
||||
workoutEndDate = nil
|
||||
}
|
||||
|
||||
private func startWorkoutTimer() {
|
||||
@@ -82,6 +101,11 @@ class BridgeModule: ObservableObject {
|
||||
@objc func updateCounter() {
|
||||
if timeLeft > 0 {
|
||||
timeLeft -= 1
|
||||
|
||||
let watchModel = WatchPackageModel(currentExerciseName: currentExercise?.exercise.name ?? "-", currentTimeLeft: timeLeft, workoutStartDate: workoutStartDate ?? Date())
|
||||
let data = try! JSONEncoder().encode(watchModel)
|
||||
send(data)
|
||||
|
||||
} else {
|
||||
timer?.invalidate()
|
||||
timer = nil
|
||||
@@ -114,3 +138,46 @@ class BridgeModule: ObservableObject {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BridgeModule: WCSessionDelegate {
|
||||
func session(_ session: WCSession, didReceiveMessageData messageData: Data) {
|
||||
if let model = try? JSONDecoder().decode(WatchActions.self, from: messageData) {
|
||||
switch model {
|
||||
case .nextExercise:
|
||||
nextExercise()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func session(_ session: WCSession,
|
||||
activationDidCompleteWith activationState: WCSessionActivationState,
|
||||
error: Error?) {}
|
||||
|
||||
#if os(iOS)
|
||||
func sessionDidBecomeInactive(_ session: WCSession) {}
|
||||
func sessionDidDeactivate(_ session: WCSession) {
|
||||
session.activate()
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
func send(_ data: Data) {
|
||||
guard WCSession.default.activationState == .activated else {
|
||||
return
|
||||
}
|
||||
#if os(iOS)
|
||||
guard WCSession.default.isWatchAppInstalled else {
|
||||
return
|
||||
}
|
||||
#else
|
||||
guard WCSession.default.isCompanionAppInstalled else {
|
||||
return
|
||||
}
|
||||
#endif
|
||||
WCSession.default.sendMessageData(data, replyHandler: nil)
|
||||
{ error in
|
||||
print("Cannot send message: \(String(describing: error))")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user