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))")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,11 +33,11 @@ struct WorkoutDetailView: View {
|
||||
CountdownView()
|
||||
ExerciseListView(workout: workout)
|
||||
ActionsView(completedWorkout: {
|
||||
bridgeModule.workoutEndDate = Date()
|
||||
if let workoutData = createWorkoutData() {
|
||||
presentedSheet = .completedWorkout(workoutData)
|
||||
bridgeModule.resetCurrentWorkout()
|
||||
}
|
||||
|
||||
}, workout: workout)
|
||||
.frame(height: 44)
|
||||
|
||||
@@ -59,12 +59,14 @@ struct WorkoutDetailView: View {
|
||||
|
||||
func createWorkoutData() -> [String:Any]? {
|
||||
guard let workoutid = bridgeModule.currentWorkout?.id,
|
||||
let startTime = bridgeModule.workoutStartDate?.timeFormatForUpload else {
|
||||
let startTime = bridgeModule.workoutStartDate?.timeFormatForUpload,
|
||||
let endTime = bridgeModule.workoutEndDate?.timeFormatForUpload else {
|
||||
return nil
|
||||
}
|
||||
let postBody = [
|
||||
"difficulty": 1,
|
||||
"workout_start_time": startTime,
|
||||
"workout_end_time": endTime,
|
||||
"workout": workoutid,
|
||||
"total_time": bridgeModule.currentWorkoutRunTimeInSeconds
|
||||
] as [String : Any]
|
||||
|
||||
15
Werkout_ios/WatchPackageModel.swift
Normal file
15
Werkout_ios/WatchPackageModel.swift
Normal file
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// WatchPackageModel.swift
|
||||
// Werkout_ios
|
||||
//
|
||||
// Created by Trey Tartt on 6/22/23.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct WatchPackageModel: Codable {
|
||||
var currentExerciseName: String
|
||||
var currentTimeLeft: Int
|
||||
var workoutStartDate: Date
|
||||
var workoutEndDate: Date?
|
||||
}
|
||||
Reference in New Issue
Block a user