From cb323a6867ad70490262717ee0fea044f4f67628 Mon Sep 17 00:00:00 2001 From: Theo Spears Date: Sun, 28 Jan 2024 16:17:42 -0800 Subject: [PATCH 1/4] Round steps to an integer --- BeeKit/HeathKit/HealthKitConfig.swift | 2 +- BeeKit/HeathKit/QuantityHealthKitMetric.swift | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/BeeKit/HeathKit/HealthKitConfig.swift b/BeeKit/HeathKit/HealthKitConfig.swift index 2ab4b469..d6f10722 100644 --- a/BeeKit/HeathKit/HealthKitConfig.swift +++ b/BeeKit/HeathKit/HealthKitConfig.swift @@ -22,7 +22,7 @@ public class HealthKitConfig : NSObject { QuantityHealthKitMetric.init(humanText: "Nike Fuel", databaseString: "nikeFuel", category: .Activity, hkQuantityTypeIdentifier: .nikeFuel), QuantityHealthKitMetric.init(humanText: "Resting energy", databaseString: "basalEnergy", category: .Activity, hkQuantityTypeIdentifier: .basalEnergyBurned), StandHoursHealthKitMetric.init(humanText: "Stand hours", databaseString: "standHour", category: .Activity), - QuantityHealthKitMetric.init(humanText: "Steps", databaseString: "steps", category: .Activity, hkQuantityTypeIdentifier: .stepCount), + QuantityHealthKitMetric.init(humanText: "Steps", databaseString: "steps", category: .Activity, hkQuantityTypeIdentifier: .stepCount, precision: [HKUnit.count(): 0]), QuantityHealthKitMetric.init(humanText: "Swimming distance", databaseString: "swimDistance", category: .Activity, hkQuantityTypeIdentifier: .distanceSwimming), QuantityHealthKitMetric.init(humanText: "Swimming strokes", databaseString: "swimStrokes", category: .Activity, hkQuantityTypeIdentifier: .swimmingStrokeCount), QuantityHealthKitMetric.init(humanText: "Walking/running distance", databaseString: "walkRunDistance", category: .Activity, hkQuantityTypeIdentifier: .distanceWalkingRunning), diff --git a/BeeKit/HeathKit/QuantityHealthKitMetric.swift b/BeeKit/HeathKit/QuantityHealthKitMetric.swift index 738dcaba..2a414f7c 100644 --- a/BeeKit/HeathKit/QuantityHealthKitMetric.swift +++ b/BeeKit/HeathKit/QuantityHealthKitMetric.swift @@ -15,12 +15,14 @@ class QuantityHealthKitMetric : HealthKitMetric { let databaseString : String let category : HealthKitCategory let hkQuantityTypeIdentifier : HKQuantityTypeIdentifier + let precision: [HKUnit: Int] - internal init(humanText: String, databaseString: String, category : HealthKitCategory, hkQuantityTypeIdentifier: HKQuantityTypeIdentifier) { + internal init(humanText: String, databaseString: String, category : HealthKitCategory, hkQuantityTypeIdentifier: HKQuantityTypeIdentifier, precision: [HKUnit: Int] = [:]) { self.humanText = humanText self.databaseString = databaseString self.category = category self.hkQuantityTypeIdentifier = hkQuantityTypeIdentifier + self.precision = precision } func sampleType() -> HKSampleType { @@ -109,11 +111,16 @@ class QuantityHealthKitMetric : HealthKitMetric { } }() - guard let datapointValue = value else { + guard var datapointValue = value else { logger.error("updateBeeminderWithStatsCollection(\(self.databaseString, privacy: .public)): No datapoint value") continue } + if let unitPrecision = precision[unit] { + let roundingFactor = pow(10.0, Double(unitPrecision)) + datapointValue = round(datapointValue * roundingFactor) / roundingFactor + } + let id = "apple-health-" + daystamp.description results.append(NewDataPoint(requestid: id, daystamp: daystamp, value: NSNumber(value: datapointValue), comment: "Auto-entered via Apple Health")) } From 6b35d3ef2bf08ae72c13a360672cd5cb9a03d5c0 Mon Sep 17 00:00:00 2001 From: Theo Spears Date: Sun, 28 Jan 2024 16:19:43 -0800 Subject: [PATCH 2/4] Weights to 1 decimal place --- BeeKit/HeathKit/HealthKitConfig.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BeeKit/HeathKit/HealthKitConfig.swift b/BeeKit/HeathKit/HealthKitConfig.swift index d6f10722..c1b5b505 100644 --- a/BeeKit/HeathKit/HealthKitConfig.swift +++ b/BeeKit/HeathKit/HealthKitConfig.swift @@ -29,7 +29,7 @@ public class HealthKitConfig : NSObject { WorkoutMinutesHealthKitMetric.init(humanText: "Workout minutes", databaseString: "workoutMinutes", category: .Activity), // Body Measurements - QuantityHealthKitMetric.init(humanText: "Weight", databaseString: "weight", category: .BodyMeasurements, hkQuantityTypeIdentifier: .bodyMass), + QuantityHealthKitMetric.init(humanText: "Weight", databaseString: "weight", category: .BodyMeasurements, hkQuantityTypeIdentifier: .bodyMass, precision: [HKUnit.pound(): 1, HKUnit.gramUnit(with: .kilo): 1]), // Heart From a15a68ab14c2f0cce407f47c3e5202a92210f090 Mon Sep 17 00:00:00 2001 From: Theo Spears Date: Sun, 28 Jan 2024 16:28:27 -0800 Subject: [PATCH 3/4] Round energy metrics --- BeeKit/HeathKit/HealthKitConfig.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BeeKit/HeathKit/HealthKitConfig.swift b/BeeKit/HeathKit/HealthKitConfig.swift index c1b5b505..2fb3ad43 100644 --- a/BeeKit/HeathKit/HealthKitConfig.swift +++ b/BeeKit/HeathKit/HealthKitConfig.swift @@ -16,11 +16,11 @@ public class HealthKitConfig : NSObject { public let metrics : [HealthKitMetric] = { var allMetrics : [HealthKitMetric] = [ // Activity - QuantityHealthKitMetric.init(humanText: "Active energy", databaseString: "activeEnergy", category: .Activity, hkQuantityTypeIdentifier: .activeEnergyBurned), + QuantityHealthKitMetric.init(humanText: "Active energy", databaseString: "activeEnergy", category: .Activity, hkQuantityTypeIdentifier: .activeEnergyBurned, precision: [HKUnit.largeCalorie(): 0]), QuantityHealthKitMetric.init(humanText: "Cycling distance", databaseString: "cyclingDistance", category: .Activity, hkQuantityTypeIdentifier: .distanceCycling), QuantityHealthKitMetric.init(humanText: "Exercise time", databaseString: "exerciseTime", category: .Activity, hkQuantityTypeIdentifier: .appleExerciseTime), QuantityHealthKitMetric.init(humanText: "Nike Fuel", databaseString: "nikeFuel", category: .Activity, hkQuantityTypeIdentifier: .nikeFuel), - QuantityHealthKitMetric.init(humanText: "Resting energy", databaseString: "basalEnergy", category: .Activity, hkQuantityTypeIdentifier: .basalEnergyBurned), + QuantityHealthKitMetric.init(humanText: "Resting energy", databaseString: "basalEnergy", category: .Activity, hkQuantityTypeIdentifier: .basalEnergyBurned, precision: [HKUnit.largeCalorie(): 0]), StandHoursHealthKitMetric.init(humanText: "Stand hours", databaseString: "standHour", category: .Activity), QuantityHealthKitMetric.init(humanText: "Steps", databaseString: "steps", category: .Activity, hkQuantityTypeIdentifier: .stepCount, precision: [HKUnit.count(): 0]), QuantityHealthKitMetric.init(humanText: "Swimming distance", databaseString: "swimDistance", category: .Activity, hkQuantityTypeIdentifier: .distanceSwimming), From 7eb0f97506a14ed9acf353f37b87d16e2509a483 Mon Sep 17 00:00:00 2001 From: Theo Spears Date: Sun, 28 Jan 2024 21:51:58 -0800 Subject: [PATCH 4/4] 2dp for kg --- BeeKit/HeathKit/HealthKitConfig.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BeeKit/HeathKit/HealthKitConfig.swift b/BeeKit/HeathKit/HealthKitConfig.swift index 2fb3ad43..a1ef8140 100644 --- a/BeeKit/HeathKit/HealthKitConfig.swift +++ b/BeeKit/HeathKit/HealthKitConfig.swift @@ -29,7 +29,7 @@ public class HealthKitConfig : NSObject { WorkoutMinutesHealthKitMetric.init(humanText: "Workout minutes", databaseString: "workoutMinutes", category: .Activity), // Body Measurements - QuantityHealthKitMetric.init(humanText: "Weight", databaseString: "weight", category: .BodyMeasurements, hkQuantityTypeIdentifier: .bodyMass, precision: [HKUnit.pound(): 1, HKUnit.gramUnit(with: .kilo): 1]), + QuantityHealthKitMetric.init(humanText: "Weight", databaseString: "weight", category: .BodyMeasurements, hkQuantityTypeIdentifier: .bodyMass, precision: [HKUnit.pound(): 1, HKUnit.gramUnit(with: .kilo): 2]), // Heart