WIP
This commit is contained in:
@@ -16,12 +16,9 @@ class WatchMainViewModel: NSObject, ObservableObject {
|
||||
@Published var heartValue: Int?
|
||||
|
||||
let healthStore = HKHealthStore()
|
||||
var hkWorkoutSession: HKWorkoutSession? {
|
||||
didSet {
|
||||
print("here")
|
||||
}
|
||||
}
|
||||
var hkWorkoutSession: HKWorkoutSession?
|
||||
var hkBuilder: HKLiveWorkoutBuilder?
|
||||
var heartRates = [Int]()
|
||||
|
||||
override init() {
|
||||
session = WCSession.default
|
||||
@@ -71,6 +68,10 @@ extension WatchMainViewModel: WCSessionDelegate {
|
||||
func session(_ session: WCSession, didReceiveMessageData messageData: Data) {
|
||||
if let model = try? JSONDecoder().decode(WatchPackageModel.self, from: messageData) {
|
||||
DispatchQueue.main.async {
|
||||
if model.currentTimeLeft == -100 {
|
||||
self.watchPackageModel = nil
|
||||
return
|
||||
}
|
||||
if self.watchPackageModel?.workoutEndDate != nil {
|
||||
self.watchPackageModel = nil
|
||||
self.stopWorkout()
|
||||
@@ -157,6 +158,14 @@ extension WatchMainViewModel: HKWorkoutSessionDelegate, HKLiveWorkoutBuilderDele
|
||||
DispatchQueue.main.async() {
|
||||
self.hkWorkoutSession = nil
|
||||
self.hkBuilder = nil
|
||||
let totalEnergy = workout?.totalEnergyBurned?.doubleValue(for: .kilocalorie()) ?? -1
|
||||
let watchFinishWorkoutModel = WatchFinishWorkoutModel(totalBurnedEnergery: totalEnergy, allHeartRates: self.heartRates)
|
||||
let data = try! JSONEncoder().encode(watchFinishWorkoutModel)
|
||||
let watchAction = WatchActions.workoutComplete(data)
|
||||
let watchActionData = try! JSONEncoder().encode(watchAction)
|
||||
self.send(watchActionData)
|
||||
|
||||
self.heartRates.removeAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -182,6 +191,7 @@ extension WatchMainViewModel: HKWorkoutSessionDelegate, HKLiveWorkoutBuilderDele
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user