WIP
This commit is contained in:
@@ -63,6 +63,7 @@ extension BridgeModule {
|
|||||||
completedWorkout()
|
completedWorkout()
|
||||||
self.completedWorkout = nil
|
self.completedWorkout = nil
|
||||||
}
|
}
|
||||||
|
resetCurrentWorkout()
|
||||||
}
|
}
|
||||||
|
|
||||||
func start(workout: Workout) {
|
func start(workout: Workout) {
|
||||||
|
|||||||
@@ -177,7 +177,6 @@ struct WorkoutDetailView: View {
|
|||||||
if let workoutData = createWorkoutData() {
|
if let workoutData = createWorkoutData() {
|
||||||
workoutComplete = .completedWorkout(workoutData)
|
workoutComplete = .completedWorkout(workoutData)
|
||||||
}
|
}
|
||||||
bridgeModule.resetCurrentWorkout()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onReceive(NotificationCenter.default.publisher(
|
.onReceive(NotificationCenter.default.publisher(
|
||||||
|
|||||||
@@ -10,17 +10,21 @@ import HealthKit
|
|||||||
|
|
||||||
class WatchWorkout: NSObject, ObservableObject, HKWorkoutSessionDelegate, HKLiveWorkoutBuilderDelegate {
|
class WatchWorkout: NSObject, ObservableObject, HKWorkoutSessionDelegate, HKLiveWorkoutBuilderDelegate {
|
||||||
static let shared = WatchWorkout()
|
static let shared = WatchWorkout()
|
||||||
|
|
||||||
@Published var heartValue: Int?
|
|
||||||
|
|
||||||
let healthStore = HKHealthStore()
|
let healthStore = HKHealthStore()
|
||||||
var hkWorkoutSession: HKWorkoutSession
|
var hkWorkoutSession: HKWorkoutSession!
|
||||||
var hkBuilder: HKLiveWorkoutBuilder
|
var hkBuilder: HKLiveWorkoutBuilder!
|
||||||
var heartRates = [Int]()
|
var heartRates = [Int]()
|
||||||
|
|
||||||
|
@Published var heartValue: Int?
|
||||||
@Published var isInWorkout = false
|
@Published var isInWorkout = false
|
||||||
@Published var isPaused = false
|
@Published var isPaused = false
|
||||||
|
|
||||||
private override init() {
|
private override init() {
|
||||||
|
super.init()
|
||||||
|
setupCore()
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupCore() {
|
||||||
do {
|
do {
|
||||||
let configuration = HKWorkoutConfiguration()
|
let configuration = HKWorkoutConfiguration()
|
||||||
configuration.activityType = .functionalStrengthTraining
|
configuration.activityType = .functionalStrengthTraining
|
||||||
@@ -28,7 +32,6 @@ class WatchWorkout: NSObject, ObservableObject, HKWorkoutSessionDelegate, HKLive
|
|||||||
|
|
||||||
hkWorkoutSession = try HKWorkoutSession(healthStore: healthStore, configuration: configuration)
|
hkWorkoutSession = try HKWorkoutSession(healthStore: healthStore, configuration: configuration)
|
||||||
hkBuilder = hkWorkoutSession.associatedWorkoutBuilder()
|
hkBuilder = hkWorkoutSession.associatedWorkoutBuilder()
|
||||||
super.init()
|
|
||||||
hkWorkoutSession.delegate = self
|
hkWorkoutSession.delegate = self
|
||||||
hkBuilder.delegate = self
|
hkBuilder.delegate = self
|
||||||
|
|
||||||
@@ -42,28 +45,47 @@ class WatchWorkout: NSObject, ObservableObject, HKWorkoutSessionDelegate, HKLive
|
|||||||
|
|
||||||
func startWorkout() {
|
func startWorkout() {
|
||||||
if isInWorkout { return }
|
if isInWorkout { return }
|
||||||
// Start the workout session and begin data collection
|
|
||||||
hkWorkoutSession.startActivity(with: Date())
|
hkWorkoutSession.startActivity(with: Date())
|
||||||
hkBuilder.beginCollection(withStart: Date()) { (succ, error) in
|
|
||||||
if !succ {
|
|
||||||
fatalError("Error beginning collection from builder: \(String(describing: error)))")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
isInWorkout = true
|
|
||||||
//WKInterfaceDevice.current().play(.start)
|
//WKInterfaceDevice.current().play(.start)
|
||||||
}
|
}
|
||||||
|
|
||||||
func stopWorkout(sendDetails: Bool) {
|
func stopWorkout(sendDetails: Bool) {
|
||||||
hkWorkoutSession.end()
|
hkWorkoutSession.end()
|
||||||
self.heartRates.removeAll()
|
}
|
||||||
self.isInWorkout = false
|
|
||||||
|
func togglePaused() {
|
||||||
|
self.isPaused.toggle()
|
||||||
|
}
|
||||||
|
|
||||||
|
func beginDataCollection() {
|
||||||
|
hkBuilder.beginCollection(withStart: Date()) { (succ, error) in
|
||||||
|
if !succ {
|
||||||
|
fatalError("Error beginning collection from builder: \(String(describing: error)))")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.isInWorkout = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getWorkoutBuilderDetails(completion: @escaping (() -> Void)) {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.heartRates.removeAll()
|
||||||
|
self.isInWorkout = false
|
||||||
|
}
|
||||||
|
|
||||||
hkBuilder.endCollection(withEnd: Date()) { (success, error) in
|
hkBuilder.endCollection(withEnd: Date()) { (success, error) in
|
||||||
if !success || error != nil { return }
|
if !success || error != nil {
|
||||||
|
completion()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
self.hkBuilder.finishWorkout { (workout, error) in
|
self.hkBuilder.finishWorkout { (workout, error) in
|
||||||
guard let workout = workout else { return }
|
guard let workout = workout else {
|
||||||
if !sendDetails { return }
|
completion()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// if !sendDetails { return }
|
||||||
|
|
||||||
DispatchQueue.main.async() {
|
DispatchQueue.main.async() {
|
||||||
let watchFinishWorkoutModel = WatchFinishWorkoutModel(healthKitUUID: workout.uuid)
|
let watchFinishWorkoutModel = WatchFinishWorkoutModel(healthKitUUID: workout.uuid)
|
||||||
@@ -71,21 +93,42 @@ class WatchWorkout: NSObject, ObservableObject, HKWorkoutSessionDelegate, HKLive
|
|||||||
let watchAction = WatchActions.workoutComplete(data)
|
let watchAction = WatchActions.workoutComplete(data)
|
||||||
let watchActionData = try! JSONEncoder().encode(watchAction)
|
let watchActionData = try! JSONEncoder().encode(watchAction)
|
||||||
DataSender.send(watchActionData)
|
DataSender.send(watchActionData)
|
||||||
|
completion()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func togglePaused() {
|
|
||||||
self.isPaused.toggle()
|
|
||||||
}
|
|
||||||
|
|
||||||
func workoutSession(_ workoutSession: HKWorkoutSession, didChangeTo toState: HKWorkoutSessionState, from fromState: HKWorkoutSessionState, date: Date) {
|
func workoutSession(_ workoutSession: HKWorkoutSession, didChangeTo toState: HKWorkoutSessionState, from fromState: HKWorkoutSessionState, date: Date) {
|
||||||
|
switch toState {
|
||||||
|
case .notStarted:
|
||||||
|
print("not started")
|
||||||
|
case .running:
|
||||||
|
print("running")
|
||||||
|
startWorkout()
|
||||||
|
beginDataCollection()
|
||||||
|
case .ended:
|
||||||
|
print("ended")
|
||||||
|
getWorkoutBuilderDetails(completion: {
|
||||||
|
self.setupCore()
|
||||||
|
})
|
||||||
|
case .paused:
|
||||||
|
print("paused")
|
||||||
|
case .prepared:
|
||||||
|
print("prepared")
|
||||||
|
case .stopped:
|
||||||
|
print("stopped")
|
||||||
|
@unknown default:
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
print("[workoutSession] Changed State: \(toState.rawValue)")
|
print("[workoutSession] Changed State: \(toState.rawValue)")
|
||||||
}
|
}
|
||||||
|
|
||||||
func workoutSession(_ workoutSession: HKWorkoutSession, didFailWithError error: Error) {
|
func workoutSession(_ workoutSession: HKWorkoutSession, didFailWithError error: Error) {
|
||||||
print("[workoutSession] Encountered an error: \(error)")
|
workoutSession.end()
|
||||||
|
setupCore()
|
||||||
|
startWorkout()
|
||||||
|
beginBuilderWorkout()
|
||||||
}
|
}
|
||||||
|
|
||||||
func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) {
|
func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) {
|
||||||
|
|||||||
Reference in New Issue
Block a user