117 lines
4.5 KiB
Swift
117 lines
4.5 KiB
Swift
//
|
|
// WatchWorkout.swift
|
|
// Werkout_watch Watch App
|
|
//
|
|
// Created by Trey Tartt on 7/1/24.
|
|
//
|
|
|
|
import Foundation
|
|
import HealthKit
|
|
|
|
class WatchWorkout: NSObject, ObservableObject, HKWorkoutSessionDelegate, HKLiveWorkoutBuilderDelegate {
|
|
static let shared = WatchWorkout()
|
|
|
|
@Published var heartValue: Int?
|
|
|
|
let healthStore = HKHealthStore()
|
|
var hkWorkoutSession: HKWorkoutSession
|
|
var hkBuilder: HKLiveWorkoutBuilder
|
|
var heartRates = [Int]()
|
|
@Published var isInWorkout = false
|
|
@Published var isPaused = false
|
|
|
|
private override init() {
|
|
do {
|
|
let configuration = HKWorkoutConfiguration()
|
|
configuration.activityType = .functionalStrengthTraining
|
|
configuration.locationType = .indoor
|
|
|
|
hkWorkoutSession = try HKWorkoutSession(healthStore: healthStore, configuration: configuration)
|
|
hkBuilder = hkWorkoutSession.associatedWorkoutBuilder()
|
|
super.init()
|
|
hkWorkoutSession.delegate = self
|
|
hkBuilder.delegate = self
|
|
|
|
/// Set the workout builder's data source.
|
|
hkBuilder.dataSource = HKLiveWorkoutDataSource(healthStore: healthStore,
|
|
workoutConfiguration: configuration)
|
|
} catch {
|
|
fatalError()
|
|
}
|
|
}
|
|
|
|
func startWorkout() {
|
|
if isInWorkout { return }
|
|
// Start the workout session and begin data collection
|
|
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)
|
|
}
|
|
|
|
func stopWorkout(sendDetails: Bool) {
|
|
hkWorkoutSession.end()
|
|
self.heartRates.removeAll()
|
|
self.isInWorkout = false
|
|
|
|
hkBuilder.endCollection(withEnd: Date()) { (success, error) in
|
|
if !success || error != nil { return }
|
|
|
|
self.hkBuilder.finishWorkout { (workout, error) in
|
|
guard let workout = workout else { return }
|
|
if !sendDetails { return }
|
|
|
|
DispatchQueue.main.async() {
|
|
let watchFinishWorkoutModel = WatchFinishWorkoutModel(healthKitUUID: workout.uuid)
|
|
let data = try! JSONEncoder().encode(watchFinishWorkoutModel)
|
|
let watchAction = WatchActions.workoutComplete(data)
|
|
let watchActionData = try! JSONEncoder().encode(watchAction)
|
|
DataSender.send(watchActionData)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func togglePaused() {
|
|
self.isPaused.toggle()
|
|
}
|
|
|
|
func workoutSession(_ workoutSession: HKWorkoutSession, didChangeTo toState: HKWorkoutSessionState, from fromState: HKWorkoutSessionState, date: Date) {
|
|
print("[workoutSession] Changed State: \(toState.rawValue)")
|
|
}
|
|
|
|
func workoutSession(_ workoutSession: HKWorkoutSession, didFailWithError error: Error) {
|
|
print("[workoutSession] Encountered an error: \(error)")
|
|
}
|
|
|
|
func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) {
|
|
for type in collectedTypes {
|
|
guard let quantityType = type as? HKQuantityType else {
|
|
return
|
|
}
|
|
switch quantityType {
|
|
case HKQuantityType.quantityType(forIdentifier: .heartRate):
|
|
DispatchQueue.main.async() {
|
|
let statistics = workoutBuilder.statistics(for: quantityType)
|
|
let heartRateUnit = HKUnit.count().unitDivided(by: HKUnit.minute())
|
|
let value = statistics!.mostRecentQuantity()?.doubleValue(for: heartRateUnit)
|
|
self.heartValue = Int(Double(round(1 * value!) / 1))
|
|
self.heartRates.append(Int(Double(round(1 * value!) / 1)))
|
|
print("[workoutBuilder] Heart Rate: \(String(describing: self.heartValue))")
|
|
}
|
|
default:
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func workoutBuilderDidCollectEvent(_ workoutBuilder: HKLiveWorkoutBuilder) {
|
|
guard let workoutEventType = workoutBuilder.workoutEvents.last?.type else { return }
|
|
print("[workoutBuilderDidCollectEvent] Workout Builder changed event: \(workoutEventType.rawValue)")
|
|
}
|
|
}
|