Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ extension MainController: WorkoutTrackingDelegate {
WorkoutTracking.cadence = WatchKitConnection.cadence
WorkoutTracking.steps = WatchKitConnection.steps

if Locale.current.measurementSystem != "Metric" {
self.distanceLabel.setText("Distance \(String(format:"%.2f", WorkoutTracking.distance))")
} else {
self.distanceLabel.setText("Distance \(String(format:"%.2f", WorkoutTracking.distance * 1.60934))")
}
if Locale.current.measurementSystem != "Metric" {
self.distanceLabel.setText("Distance \(String(format:"%.2f", WorkoutTracking.distance * 0.621371)) mi")
} else {
self.distanceLabel.setText("Distance \(String(format:"%.2f", WorkoutTracking.distance)) km")
}
self.caloriesLabel.setText("KCal \(Int(WorkoutTracking.kcal))")
//WorkoutTracking.cadenceSteps = pedometer.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,29 +162,29 @@ extension WorkoutTracking: WorkoutTrackingProtocol {

var infoToShare: Set<HKSampleType> = []

if #available(watchOSApplicationExtension 11.0, *) {
infoToShare = Set([
HKSampleType.quantityType(forIdentifier: .stepCount)!,
HKSampleType.quantityType(forIdentifier: .heartRate)!,
HKSampleType.quantityType(forIdentifier: .distanceCycling)!,
HKSampleType.quantityType(forIdentifier: .distanceWalkingRunning)!,
HKSampleType.quantityType(forIdentifier: .activeEnergyBurned)!,
HKSampleType.quantityType(forIdentifier: .basalEnergyBurned)!,
HKSampleType.quantityType(forIdentifier: .cyclingPower)!,
HKSampleType.quantityType(forIdentifier: .cyclingSpeed)!,
HKSampleType.quantityType(forIdentifier: .cyclingCadence)!,
HKSampleType.quantityType(forIdentifier: .runningPower)!,
HKSampleType.quantityType(forIdentifier: .runningSpeed)!,
HKSampleType.quantityType(forIdentifier: .runningStrideLength)!,
HKSampleType.quantityType(forIdentifier: .runningVerticalOscillation)!,
HKSampleType.quantityType(forIdentifier: .walkingSpeed)!,
HKSampleType.quantityType(forIdentifier: .walkingStepLength)!,
HKSampleType.quantityType(forIdentifier: .distanceRowing)!,
HKSampleType.quantityType(forIdentifier: .rowingSpeed)!,
HKSampleType.quantityType(forIdentifier: .flightsClimbed)!,
HKSampleType.workoutType()
])
} else if #available(watchOSApplicationExtension 10.0, *) {
if #available(watchOSApplicationExtension 11.0, *) {
infoToShare = Set([
HKSampleType.quantityType(forIdentifier: .stepCount)!,
HKSampleType.quantityType(forIdentifier: .heartRate)!,
HKSampleType.quantityType(forIdentifier: .distanceCycling)!,
HKSampleType.quantityType(forIdentifier: .distanceWalkingRunning)!,
HKSampleType.quantityType(forIdentifier: .activeEnergyBurned)!,
HKSampleType.quantityType(forIdentifier: .basalEnergyBurned)!,
HKSampleType.quantityType(forIdentifier: .cyclingPower)!,
HKSampleType.quantityType(forIdentifier: .cyclingSpeed)!,
HKSampleType.quantityType(forIdentifier: .cyclingCadence)!,
HKSampleType.quantityType(forIdentifier: .runningPower)!,
HKSampleType.quantityType(forIdentifier: .runningSpeed)!,
HKSampleType.quantityType(forIdentifier: .runningStrideLength)!,
HKSampleType.quantityType(forIdentifier: .runningVerticalOscillation)!,
HKSampleType.quantityType(forIdentifier: .walkingSpeed)!,
HKSampleType.quantityType(forIdentifier: .walkingStepLength)!,
HKSampleType.quantityType(forIdentifier: .distanceRowing)!,
HKSampleType.quantityType(forIdentifier: .rowingSpeed)!,
HKSampleType.quantityType(forIdentifier: .flightsClimbed)!,
HKSampleType.workoutType()
])
} else if #available(watchOSApplicationExtension 10.0, *) {
infoToShare = Set([
HKSampleType.quantityType(forIdentifier: .stepCount)!,
HKSampleType.quantityType(forIdentifier: .heartRate)!,
Expand Down Expand Up @@ -235,8 +235,8 @@ extension WorkoutTracking: WorkoutTrackingProtocol {
// Reset flights climbed and elevation gain for new workout
WorkoutTracking.flightsClimbed = 0
WorkoutTracking.elevationGain = 0
WorkoutTracking.totalKcal = 0
WatchKitConnection.totalKcal = 0
WorkoutTracking.totalKcal = 0
WatchKitConnection.totalKcal = 0
print("Start workout")
configWorkout()
workoutSession.startActivity(with: Date())
Expand All @@ -253,56 +253,56 @@ extension WorkoutTracking: WorkoutTrackingProtocol {
}

func stopWorkOut() {
print("Stop workout")
WorkoutTracking.kcal = WatchKitConnection.kcal
WorkoutTracking.totalKcal = WatchKitConnection.totalKcal
WatchKitConnection.shared.sendDebug("stop pressed local watchKcal=\(WatchKitConnection.kcal) watchTotalKcal=\(WatchKitConnection.totalKcal) trackingKcal=\(WorkoutTracking.kcal) trackingTotalKcal=\(WorkoutTracking.totalKcal)")
workoutSession.stopActivity(with: Date())
workoutSession.end()
print("Stop workout")
WorkoutTracking.kcal = WatchKitConnection.kcal
WorkoutTracking.totalKcal = WatchKitConnection.totalKcal
WatchKitConnection.shared.sendDebug("stop pressed local watchKcal=\(WatchKitConnection.kcal) watchTotalKcal=\(WatchKitConnection.totalKcal) trackingKcal=\(WorkoutTracking.kcal) trackingTotalKcal=\(WorkoutTracking.totalKcal)")
workoutSession.stopActivity(with: Date())
workoutSession.end()

// Write active and basal calories
guard let activeQuantityType = HKQuantityType.quantityType(
forIdentifier: .activeEnergyBurned) else {
return
}

let unit = HKUnit.kilocalorie()
let activeEnergyBurned = WorkoutTracking.kcal
let activeQuantity = HKQuantity(unit: unit,
doubleValue: activeEnergyBurned)
let basalEnergyBurned = max(WorkoutTracking.totalKcal - activeEnergyBurned, 0)
WatchKitConnection.shared.sendDebug("stop energy samples active=\(activeEnergyBurned) total=\(WorkoutTracking.totalKcal) basal=\(basalEnergyBurned)")

let startDate = workoutSession.startDate ?? WorkoutTracking.lastDateMetric

let activeSample = HKCumulativeQuantitySeriesSample(type: activeQuantityType,
quantity: activeQuantity,
start: startDate,
end: Date())
var energySamples = [activeSample]

if basalEnergyBurned > 0,
let basalQuantityType = HKQuantityType.quantityType(forIdentifier: .basalEnergyBurned) {
let basalQuantity = HKQuantity(unit: unit,
doubleValue: basalEnergyBurned)
let basalSample = HKCumulativeQuantitySeriesSample(type: basalQuantityType,
quantity: basalQuantity,
start: startDate,
end: Date())
energySamples.append(basalSample)
}

workoutBuilder.add(energySamples) {(success, error) in
if let error = error {
print("WatchWorkoutTracking energy calories: \(error.localizedDescription)")
WatchKitConnection.shared.sendDebug("stop energy add error=\(error.localizedDescription) active=\(activeEnergyBurned) total=\(WorkoutTracking.totalKcal) basal=\(basalEnergyBurned)")
} else {
WatchKitConnection.shared.sendDebug("stop energy add success=\(success) active=\(activeEnergyBurned) total=\(WorkoutTracking.totalKcal) basal=\(basalEnergyBurned)")
}
}
// Write active and basal calories
guard let activeQuantityType = HKQuantityType.quantityType(
forIdentifier: .activeEnergyBurned) else {
return
}
let unit = HKUnit.kilocalorie()
let activeEnergyBurned = WorkoutTracking.kcal
let activeQuantity = HKQuantity(unit: unit,
doubleValue: activeEnergyBurned)
let basalEnergyBurned = max(WorkoutTracking.totalKcal - activeEnergyBurned, 0)
WatchKitConnection.shared.sendDebug("stop energy samples active=\(activeEnergyBurned) total=\(WorkoutTracking.totalKcal) basal=\(basalEnergyBurned)")
let startDate = workoutSession.startDate ?? WorkoutTracking.lastDateMetric
let activeSample = HKCumulativeQuantitySeriesSample(type: activeQuantityType,
quantity: activeQuantity,
start: startDate,
end: Date())
var energySamples = [activeSample]
if basalEnergyBurned > 0,
let basalQuantityType = HKQuantityType.quantityType(forIdentifier: .basalEnergyBurned) {
let basalQuantity = HKQuantity(unit: unit,
doubleValue: basalEnergyBurned)
let basalSample = HKCumulativeQuantitySeriesSample(type: basalQuantityType,
quantity: basalQuantity,
start: startDate,
end: Date())
energySamples.append(basalSample)
}
workoutBuilder.add(energySamples) {(success, error) in
if let error = error {
print("WatchWorkoutTracking energy calories: \(error.localizedDescription)")
WatchKitConnection.shared.sendDebug("stop energy add error=\(error.localizedDescription) active=\(activeEnergyBurned) total=\(WorkoutTracking.totalKcal) basal=\(basalEnergyBurned)")
} else {
WatchKitConnection.shared.sendDebug("stop energy add success=\(success) active=\(activeEnergyBurned) total=\(WorkoutTracking.totalKcal) basal=\(basalEnergyBurned)")
}
}

let unitDistance = HKUnit.mile()
let miles = WorkoutTracking.distance
let miles = WorkoutTracking.distance * 0.621371
let quantityMiles = HKQuantity(unit: unitDistance,
doubleValue: miles)

Expand Down Expand Up @@ -369,12 +369,12 @@ extension WorkoutTracking: WorkoutTrackingProtocol {
}
}

let quantityTypeDistance: HKQuantityType?
if #available(watchOSApplicationExtension 11.0, *) {
quantityTypeDistance = HKQuantityType.quantityType(forIdentifier: .distanceRowing)
} else {
quantityTypeDistance = HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning)
}
let quantityTypeDistance: HKQuantityType?
if #available(watchOSApplicationExtension 11.0, *) {
quantityTypeDistance = HKQuantityType.quantityType(forIdentifier: .distanceRowing)
} else {
quantityTypeDistance = HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning)
}

guard let typeDistance = quantityTypeDistance else {
return
Expand Down
2 changes: 1 addition & 1 deletion src/ios/lockscreen.mm
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@

void lockscreen::setDistance(double distance)
{
[h setDistanceWithDistance:distance * 0.621371];
[h setDistanceWithDistance:distance];
}

void lockscreen::setSteps(double steps)
Expand Down
Loading