Skip to content

Commit 7edb724

Browse files
cagnuleinclaude
andcommitted
fix(apple-watch): correggi unità distanza km→miglia nel workout HealthKit
Il canale iPhone→Watch via WatchConnectivity trasportava un valore con unità inconsistente: miglia quando l'iPhone era il device primario (lockscreen::setDistance convertiva km→mi), km quando un altro device QZ (iPad, "SENDER=PAD") inviava la distanza grezza. Il Watch scriveva sempre il valore ricevuto come HKUnit.mile(), causando distanze gonfiate in Apple Health (4.4 mi invece di 2.78 mi per 4.47 km). Fix: standardizzare il contratto a "sempre km" su quel canale. - lockscreen.mm: rimuove la conversione * 0.621371, invia km grezzi - WatchWorkoutTracking stopWorkOut(): moltiplica per 0.621371 prima di scrivere su HealthKit, così il Watch fa sempre la conversione finale Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent be5a34b commit 7edb724

2 files changed

Lines changed: 79 additions & 79 deletions

File tree

build-qdomyos-zwift-Qt_5_15_2_for_iOS-Debug/watchkit Extension/WatchWorkoutTracking.swift

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -162,29 +162,29 @@ extension WorkoutTracking: WorkoutTrackingProtocol {
162162

163163
var infoToShare: Set<HKSampleType> = []
164164

165-
if #available(watchOSApplicationExtension 11.0, *) {
166-
infoToShare = Set([
167-
HKSampleType.quantityType(forIdentifier: .stepCount)!,
168-
HKSampleType.quantityType(forIdentifier: .heartRate)!,
169-
HKSampleType.quantityType(forIdentifier: .distanceCycling)!,
170-
HKSampleType.quantityType(forIdentifier: .distanceWalkingRunning)!,
171-
HKSampleType.quantityType(forIdentifier: .activeEnergyBurned)!,
172-
HKSampleType.quantityType(forIdentifier: .basalEnergyBurned)!,
173-
HKSampleType.quantityType(forIdentifier: .cyclingPower)!,
174-
HKSampleType.quantityType(forIdentifier: .cyclingSpeed)!,
175-
HKSampleType.quantityType(forIdentifier: .cyclingCadence)!,
176-
HKSampleType.quantityType(forIdentifier: .runningPower)!,
177-
HKSampleType.quantityType(forIdentifier: .runningSpeed)!,
178-
HKSampleType.quantityType(forIdentifier: .runningStrideLength)!,
179-
HKSampleType.quantityType(forIdentifier: .runningVerticalOscillation)!,
180-
HKSampleType.quantityType(forIdentifier: .walkingSpeed)!,
181-
HKSampleType.quantityType(forIdentifier: .walkingStepLength)!,
182-
HKSampleType.quantityType(forIdentifier: .distanceRowing)!,
183-
HKSampleType.quantityType(forIdentifier: .rowingSpeed)!,
184-
HKSampleType.quantityType(forIdentifier: .flightsClimbed)!,
185-
HKSampleType.workoutType()
186-
])
187-
} else if #available(watchOSApplicationExtension 10.0, *) {
165+
if #available(watchOSApplicationExtension 11.0, *) {
166+
infoToShare = Set([
167+
HKSampleType.quantityType(forIdentifier: .stepCount)!,
168+
HKSampleType.quantityType(forIdentifier: .heartRate)!,
169+
HKSampleType.quantityType(forIdentifier: .distanceCycling)!,
170+
HKSampleType.quantityType(forIdentifier: .distanceWalkingRunning)!,
171+
HKSampleType.quantityType(forIdentifier: .activeEnergyBurned)!,
172+
HKSampleType.quantityType(forIdentifier: .basalEnergyBurned)!,
173+
HKSampleType.quantityType(forIdentifier: .cyclingPower)!,
174+
HKSampleType.quantityType(forIdentifier: .cyclingSpeed)!,
175+
HKSampleType.quantityType(forIdentifier: .cyclingCadence)!,
176+
HKSampleType.quantityType(forIdentifier: .runningPower)!,
177+
HKSampleType.quantityType(forIdentifier: .runningSpeed)!,
178+
HKSampleType.quantityType(forIdentifier: .runningStrideLength)!,
179+
HKSampleType.quantityType(forIdentifier: .runningVerticalOscillation)!,
180+
HKSampleType.quantityType(forIdentifier: .walkingSpeed)!,
181+
HKSampleType.quantityType(forIdentifier: .walkingStepLength)!,
182+
HKSampleType.quantityType(forIdentifier: .distanceRowing)!,
183+
HKSampleType.quantityType(forIdentifier: .rowingSpeed)!,
184+
HKSampleType.quantityType(forIdentifier: .flightsClimbed)!,
185+
HKSampleType.workoutType()
186+
])
187+
} else if #available(watchOSApplicationExtension 10.0, *) {
188188
infoToShare = Set([
189189
HKSampleType.quantityType(forIdentifier: .stepCount)!,
190190
HKSampleType.quantityType(forIdentifier: .heartRate)!,
@@ -235,8 +235,8 @@ extension WorkoutTracking: WorkoutTrackingProtocol {
235235
// Reset flights climbed and elevation gain for new workout
236236
WorkoutTracking.flightsClimbed = 0
237237
WorkoutTracking.elevationGain = 0
238-
WorkoutTracking.totalKcal = 0
239-
WatchKitConnection.totalKcal = 0
238+
WorkoutTracking.totalKcal = 0
239+
WatchKitConnection.totalKcal = 0
240240
print("Start workout")
241241
configWorkout()
242242
workoutSession.startActivity(with: Date())
@@ -253,56 +253,56 @@ extension WorkoutTracking: WorkoutTrackingProtocol {
253253
}
254254

255255
func stopWorkOut() {
256-
print("Stop workout")
257-
WorkoutTracking.kcal = WatchKitConnection.kcal
258-
WorkoutTracking.totalKcal = WatchKitConnection.totalKcal
259-
WatchKitConnection.shared.sendDebug("stop pressed local watchKcal=\(WatchKitConnection.kcal) watchTotalKcal=\(WatchKitConnection.totalKcal) trackingKcal=\(WorkoutTracking.kcal) trackingTotalKcal=\(WorkoutTracking.totalKcal)")
260-
workoutSession.stopActivity(with: Date())
261-
workoutSession.end()
256+
print("Stop workout")
257+
WorkoutTracking.kcal = WatchKitConnection.kcal
258+
WorkoutTracking.totalKcal = WatchKitConnection.totalKcal
259+
WatchKitConnection.shared.sendDebug("stop pressed local watchKcal=\(WatchKitConnection.kcal) watchTotalKcal=\(WatchKitConnection.totalKcal) trackingKcal=\(WorkoutTracking.kcal) trackingTotalKcal=\(WorkoutTracking.totalKcal)")
260+
workoutSession.stopActivity(with: Date())
261+
workoutSession.end()
262262

263-
// Write active and basal calories
264-
guard let activeQuantityType = HKQuantityType.quantityType(
265-
forIdentifier: .activeEnergyBurned) else {
266-
return
267-
}
268-
269-
let unit = HKUnit.kilocalorie()
270-
let activeEnergyBurned = WorkoutTracking.kcal
271-
let activeQuantity = HKQuantity(unit: unit,
272-
doubleValue: activeEnergyBurned)
273-
let basalEnergyBurned = max(WorkoutTracking.totalKcal - activeEnergyBurned, 0)
274-
WatchKitConnection.shared.sendDebug("stop energy samples active=\(activeEnergyBurned) total=\(WorkoutTracking.totalKcal) basal=\(basalEnergyBurned)")
275-
276-
let startDate = workoutSession.startDate ?? WorkoutTracking.lastDateMetric
277-
278-
let activeSample = HKCumulativeQuantitySeriesSample(type: activeQuantityType,
279-
quantity: activeQuantity,
280-
start: startDate,
281-
end: Date())
282-
var energySamples = [activeSample]
283-
284-
if basalEnergyBurned > 0,
285-
let basalQuantityType = HKQuantityType.quantityType(forIdentifier: .basalEnergyBurned) {
286-
let basalQuantity = HKQuantity(unit: unit,
287-
doubleValue: basalEnergyBurned)
288-
let basalSample = HKCumulativeQuantitySeriesSample(type: basalQuantityType,
289-
quantity: basalQuantity,
290-
start: startDate,
291-
end: Date())
292-
energySamples.append(basalSample)
293-
}
294-
295-
workoutBuilder.add(energySamples) {(success, error) in
296-
if let error = error {
297-
print("WatchWorkoutTracking energy calories: \(error.localizedDescription)")
298-
WatchKitConnection.shared.sendDebug("stop energy add error=\(error.localizedDescription) active=\(activeEnergyBurned) total=\(WorkoutTracking.totalKcal) basal=\(basalEnergyBurned)")
299-
} else {
300-
WatchKitConnection.shared.sendDebug("stop energy add success=\(success) active=\(activeEnergyBurned) total=\(WorkoutTracking.totalKcal) basal=\(basalEnergyBurned)")
301-
}
302-
}
263+
// Write active and basal calories
264+
guard let activeQuantityType = HKQuantityType.quantityType(
265+
forIdentifier: .activeEnergyBurned) else {
266+
return
267+
}
268+
269+
let unit = HKUnit.kilocalorie()
270+
let activeEnergyBurned = WorkoutTracking.kcal
271+
let activeQuantity = HKQuantity(unit: unit,
272+
doubleValue: activeEnergyBurned)
273+
let basalEnergyBurned = max(WorkoutTracking.totalKcal - activeEnergyBurned, 0)
274+
WatchKitConnection.shared.sendDebug("stop energy samples active=\(activeEnergyBurned) total=\(WorkoutTracking.totalKcal) basal=\(basalEnergyBurned)")
275+
276+
let startDate = workoutSession.startDate ?? WorkoutTracking.lastDateMetric
277+
278+
let activeSample = HKCumulativeQuantitySeriesSample(type: activeQuantityType,
279+
quantity: activeQuantity,
280+
start: startDate,
281+
end: Date())
282+
var energySamples = [activeSample]
283+
284+
if basalEnergyBurned > 0,
285+
let basalQuantityType = HKQuantityType.quantityType(forIdentifier: .basalEnergyBurned) {
286+
let basalQuantity = HKQuantity(unit: unit,
287+
doubleValue: basalEnergyBurned)
288+
let basalSample = HKCumulativeQuantitySeriesSample(type: basalQuantityType,
289+
quantity: basalQuantity,
290+
start: startDate,
291+
end: Date())
292+
energySamples.append(basalSample)
293+
}
294+
295+
workoutBuilder.add(energySamples) {(success, error) in
296+
if let error = error {
297+
print("WatchWorkoutTracking energy calories: \(error.localizedDescription)")
298+
WatchKitConnection.shared.sendDebug("stop energy add error=\(error.localizedDescription) active=\(activeEnergyBurned) total=\(WorkoutTracking.totalKcal) basal=\(basalEnergyBurned)")
299+
} else {
300+
WatchKitConnection.shared.sendDebug("stop energy add success=\(success) active=\(activeEnergyBurned) total=\(WorkoutTracking.totalKcal) basal=\(basalEnergyBurned)")
301+
}
302+
}
303303

304304
let unitDistance = HKUnit.mile()
305-
let miles = WorkoutTracking.distance
305+
let miles = WorkoutTracking.distance * 0.621371
306306
let quantityMiles = HKQuantity(unit: unitDistance,
307307
doubleValue: miles)
308308

@@ -369,12 +369,12 @@ extension WorkoutTracking: WorkoutTrackingProtocol {
369369
}
370370
}
371371

372-
let quantityTypeDistance: HKQuantityType?
373-
if #available(watchOSApplicationExtension 11.0, *) {
374-
quantityTypeDistance = HKQuantityType.quantityType(forIdentifier: .distanceRowing)
375-
} else {
376-
quantityTypeDistance = HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning)
377-
}
372+
let quantityTypeDistance: HKQuantityType?
373+
if #available(watchOSApplicationExtension 11.0, *) {
374+
quantityTypeDistance = HKQuantityType.quantityType(forIdentifier: .distanceRowing)
375+
} else {
376+
quantityTypeDistance = HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning)
377+
}
378378

379379
guard let typeDistance = quantityTypeDistance else {
380380
return

src/ios/lockscreen.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153

154154
void lockscreen::setDistance(double distance)
155155
{
156-
[h setDistanceWithDistance:distance * 0.621371];
156+
[h setDistanceWithDistance:distance];
157157
}
158158

159159
void lockscreen::setSteps(double steps)

0 commit comments

Comments
 (0)