Skip to content

Commit 50214d4

Browse files
authored
Merge pull request #316 from bharat/issue_311
Properly support mmol/L in the status extension.
2 parents 042ed70 + bde902e commit 50214d4

File tree

6 files changed

+41
-22
lines changed

6 files changed

+41
-22
lines changed

Loop Status Extension/HKUnit.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,30 @@
88

99
import HealthKit
1010

11+
// Code in this extension is duplicated from:
12+
// https://github.com/LoopKit/LoopKit/blob/master/LoopKit/HKUnit.swift
13+
// to avoid pulling in the LoopKit extension since it's not extension-API safe.
1114
public extension HKUnit {
1215
// A formatting helper for determining the preferred decimal style for a given unit
13-
// This is similar to the LoopKit HKUnit extension, but copied here so that we can
14-
// avoid a dependency on LoopKit from the Loop Status Extension.
1516
var preferredMinimumFractionDigits: Int {
1617
if self.unitString == "mg/dL" {
1718
return 0
1819
} else {
1920
return 1
2021
}
2122
}
23+
24+
static func millimolesPerLiterUnit() -> HKUnit {
25+
return HKUnit.moleUnit(with: .milli, molarMass: HKUnitMolarMassBloodGlucose).unitDivided(by: HKUnit.liter())
26+
}
27+
28+
// A glucose-centric presentation helper for the localized unit string
29+
var glucoseUnitDisplayString: String {
30+
if self == HKUnit.millimolesPerLiterUnit() {
31+
return NSLocalizedString("mmol/L", comment: "The unit display string for millimoles of glucose per liter")
32+
} else {
33+
return String(describing: self)
34+
}
35+
}
36+
2237
}

Loop Status Extension/StatusExtensionContext.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final class StatusExtensionContext: NSObject, RawRepresentable {
4444
typealias RawValue = [String: Any]
4545
private let version = 1
4646

47-
var preferredUnitDisplayString: String?
47+
var preferredUnitString: String?
4848
var latestGlucose: GlucoseContext?
4949
var reservoir: ReservoirContext?
5050
var loop: LoopContext?
@@ -60,7 +60,7 @@ final class StatusExtensionContext: NSObject, RawRepresentable {
6060
super.init()
6161
let raw = rawValue
6262

63-
if let preferredString = raw["preferredUnitDisplayString"] as? String,
63+
if let preferredString = raw["preferredUnitString"] as? String,
6464
let latestValue = raw["latestGlucose_value"] as? Double,
6565
let startDate = raw["latestGlucose_startDate"] as? Date {
6666

@@ -81,7 +81,7 @@ final class StatusExtensionContext: NSObject, RawRepresentable {
8181
isLocal: local)
8282
}
8383

84-
preferredUnitDisplayString = preferredString
84+
preferredUnitString = preferredString
8585
latestGlucose = GlucoseContext(
8686
quantity: latestValue,
8787
startDate: startDate,
@@ -115,10 +115,10 @@ final class StatusExtensionContext: NSObject, RawRepresentable {
115115
"version": version
116116
]
117117

118-
raw["preferredUnitDisplayString"] = preferredUnitDisplayString
118+
raw["preferredUnitString"] = preferredUnitString
119119

120-
if let glucose = latestGlucose,
121-
preferredUnitDisplayString != nil {
120+
if preferredUnitString != nil,
121+
let glucose = latestGlucose {
122122
raw["latestGlucose_value"] = glucose.quantity
123123
raw["latestGlucose_startDate"] = glucose.startDate
124124
}

Loop Status Extension/StatusViewController.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class StatusViewController: UIViewController, NCWidgetProviding {
4343
// the wrong units and that could be very harmful. So unless there's a preferred
4444
// unit, assume that none of the rest of the data is reliable.
4545
guard
46-
let preferredUnitDisplayString = context.preferredUnitDisplayString
46+
let preferredUnitString = context.preferredUnitString
4747
else {
4848
completionHandler(NCUpdateResult.failed)
4949
return
@@ -52,7 +52,7 @@ class StatusViewController: UIViewController, NCWidgetProviding {
5252
if let glucose = context.latestGlucose {
5353
glucoseHUD.set(glucoseQuantity: glucose.quantity,
5454
at: glucose.startDate,
55-
unitDisplayString: preferredUnitDisplayString,
55+
unitString: preferredUnitString,
5656
from: glucose.sensor)
5757
}
5858

@@ -74,14 +74,16 @@ class StatusViewController: UIViewController, NCWidgetProviding {
7474
loopCompletionHUD.lastLoopCompleted = loop.lastCompleted
7575
}
7676

77-
if let eventualGlucose = context.eventualGlucose {
78-
let quantity = HKQuantity(unit: HKUnit(from: preferredUnitDisplayString),
79-
doubleValue: eventualGlucose.rounded())
77+
let preferredUnit = HKUnit(from: preferredUnitString)
78+
let formatter = NumberFormatter.glucoseFormatter(for: preferredUnit)
79+
if let eventualGlucose = context.eventualGlucose,
80+
let eventualGlucoseNumberString = formatter.string(from: NSNumber(value: eventualGlucose)) {
8081
subtitleLabel.text = String(
8182
format: NSLocalizedString(
82-
"Eventually %@",
83-
comment: "The subtitle format describing eventual glucose. (1: localized glucose value description)"),
84-
String(describing: quantity))
83+
"Eventually %1$@ %2$@",
84+
comment: "The subtitle format describing eventual glucose. (1: localized glucose value description) (2: localized glucose units description)"),
85+
eventualGlucoseNumberString,
86+
preferredUnit.glucoseUnitDisplayString)
8587
subtitleLabel.alpha = 1
8688
} else {
8789
subtitleLabel.alpha = 0

Loop/Managers/StatusExtensionDataManager.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ final class StatusExtensionDataManager {
5757
context.batteryPercentage = 0.25
5858
context.reservoir = ReservoirContext(startDate: Date(), unitVolume: 42.5, capacity: 200)
5959
context.netBasal = NetBasalContext(rate: 2.1, percentage: 0.6, startDate: Date() - TimeInterval(250))
60-
context.eventualGlucose = 119.123
60+
context.eventualGlucose = HKQuantity(unit: HKUnit.milligramsPerDeciliterUnit(), doubleValue: 119.123)
61+
.doubleValue(for: preferredUnit)
6162
#endif
6263

63-
context.preferredUnitDisplayString = preferredUnit.glucoseUnitDisplayString
64+
context.preferredUnitString = preferredUnit.unitString
6465
context.loop = LoopContext(
6566
dosingEnabled: dataManager.loopManager.dosingEnabled,
6667
lastCompleted: lastLoopCompleted)

Loop/View Controllers/StatusTableViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ final class StatusTableViewController: UITableViewController, UIGestureRecognize
269269
if let glucose = self.dataManager.glucoseStore?.latestGlucose {
270270
self.glucoseHUD.set(glucoseQuantity: glucose.quantity.doubleValue(for: self.charts.glucoseUnit),
271271
at: glucose.startDate,
272-
unitDisplayString: self.charts.glucoseUnit.glucoseUnitDisplayString,
272+
unitString: self.charts.glucoseUnit.unitString,
273273
from: self.dataManager.sensorInfo)
274274
}
275275

Loop/Views/GlucoseHUDView.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,20 @@ final class GlucoseHUDView: HUDView {
6464
}
6565
}
6666

67-
func set(glucoseQuantity: Double, at glucoseStartDate: Date, unitDisplayString: String, from sensor: SensorDisplayable?) {
67+
func set(glucoseQuantity: Double, at glucoseStartDate: Date, unitString: String, from sensor: SensorDisplayable?) {
6868
var accessibilityStrings = [String]()
6969

7070
let time = timeFormatter.string(from: glucoseStartDate)
7171
caption?.text = time
72+
let unit = HKUnit(from: unitString)
7273

73-
let numberFormatter = NumberFormatter.glucoseFormatter(for: HKUnit(from: unitDisplayString))
74+
let numberFormatter = NumberFormatter.glucoseFormatter(for: unit)
7475
if let valueString = numberFormatter.string(from: NSNumber(value: glucoseQuantity)) {
7576
glucoseLabel.text = valueString
7677
accessibilityStrings.append(String(format: NSLocalizedString("%1$@ at %2$@", comment: "Accessbility format value describing glucose: (1: glucose number)(2: glucose time)"), valueString, time))
7778
}
7879

79-
var unitStrings = [unitDisplayString]
80+
var unitStrings = [unit.glucoseUnitDisplayString]
8081

8182
if let trend = sensor?.trendType {
8283
unitStrings.append(trend.symbol)

0 commit comments

Comments
 (0)