Skip to content

Commit c85a665

Browse files
committed
Fix missing glucose value display
1 parent 1c44a2b commit c85a665

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

Common/Models/WatchContext.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ final class WatchContext: NSObject, RawRepresentable {
5656
if let unitString = rawValue["gu"] as? String {
5757
let unit = HKUnit(from: unitString)
5858
preferredGlucoseUnit = unit
59+
}
5960

60-
if let glucoseValue = rawValue["gv"] as? Double {
61-
glucose = HKQuantity(unit: unit, doubleValue: glucoseValue)
62-
}
61+
if let glucoseValue = rawValue["gv"] as? Double {
62+
glucose = HKQuantity(unit: preferredGlucoseUnit ?? .milligramsPerDeciliter(), doubleValue: glucoseValue)
63+
}
6364

64-
if let glucoseValue = rawValue["egv"] as? Double {
65-
eventualGlucose = HKQuantity(unit: unit, doubleValue: glucoseValue)
66-
}
65+
if let glucoseValue = rawValue["egv"] as? Double {
66+
eventualGlucose = HKQuantity(unit: preferredGlucoseUnit ?? .milligramsPerDeciliter(), doubleValue: glucoseValue)
6767
}
6868

6969
glucoseTrendRawValue = rawValue["gt"] as? Int
@@ -102,11 +102,10 @@ final class WatchContext: NSObject, RawRepresentable {
102102
raw["bp"] = batteryPercentage
103103
raw["cob"] = COB
104104

105-
if let unit = preferredGlucoseUnit {
106-
raw["egv"] = eventualGlucose?.doubleValue(for: unit)
107-
raw["gu"] = unit.unitString
108-
raw["gv"] = glucose?.doubleValue(for: unit)
109-
}
105+
let unit = preferredGlucoseUnit ?? .milligramsPerDeciliter()
106+
raw["egv"] = eventualGlucose?.doubleValue(for: unit)
107+
raw["gu"] = preferredGlucoseUnit?.unitString
108+
raw["gv"] = glucose?.doubleValue(for: unit)
110109

111110
raw["gt"] = glucoseTrendRawValue
112111
raw["gd"] = glucoseDate

WatchApp Extension/ExtensionDelegate.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import WatchConnectivity
1010
import WatchKit
11+
import HealthKit
1112
import os
1213
import UserNotifications
1314

@@ -152,10 +153,23 @@ final class ExtensionDelegate: NSObject, WKExtensionDelegate {
152153
}
153154
}
154155

156+
private lazy var healthStore = HKHealthStore()
157+
155158
fileprivate func updateContext(_ data: [String: Any]) {
156159
if let context = WatchContext(rawValue: data as WatchContext.RawValue) {
157-
DispatchQueue.main.async {
158-
self.lastContext = context
160+
if context.preferredGlucoseUnit == nil {
161+
let type = HKQuantityType.quantityType(forIdentifier: .bloodGlucose)!
162+
healthStore.preferredUnits(for: [type]) { (units, error) in
163+
context.preferredGlucoseUnit = units[type]
164+
165+
DispatchQueue.main.async {
166+
self.lastContext = context
167+
}
168+
}
169+
} else {
170+
DispatchQueue.main.async {
171+
self.lastContext = context
172+
}
159173
}
160174
}
161175
}

0 commit comments

Comments
 (0)