From cefe2f9d948e34f1fe3b6410aabad6a410591ab7 Mon Sep 17 00:00:00 2001 From: Anders Ha Date: Tue, 14 Nov 2023 10:27:13 +0000 Subject: [PATCH] CS1-28: Fix percent value scale --- .../HealthKit/Models/CoreModels+Extensions.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sources/VitalHealthKit/HealthKit/Models/CoreModels+Extensions.swift b/Sources/VitalHealthKit/HealthKit/Models/CoreModels+Extensions.swift index f4e7652d..589d23bf 100644 --- a/Sources/VitalHealthKit/HealthKit/Models/CoreModels+Extensions.swift +++ b/Sources/VitalHealthKit/HealthKit/Models/CoreModels+Extensions.swift @@ -141,7 +141,15 @@ extension QuantitySample { guard let value = sample as? HKQuantitySample else { return nil } - + + let unit = sample.sampleType.toHealthKitUnits + var doubleValue = value.quantity.doubleValue(for: unit) + + if unit == HKUnit.percent() { + // Vital uses [0, 100[ instead of [0, 1[, so we need to scale up the percentage. + doubleValue = doubleValue * 100 + } + self.init( id: value.uuid.uuidString, value: value.quantity.doubleValue(for: sample.sampleType.toHealthKitUnits),