Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Loop Status Extension/HKUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,30 @@

import HealthKit

// Code in this extension is duplicated from:
// https://github.com/LoopKit/LoopKit/blob/master/LoopKit/HKUnit.swift
// to avoid pulling in the LoopKit extension since it's not extension-API safe.
public extension HKUnit {
// A formatting helper for determining the preferred decimal style for a given unit
// This is similar to the LoopKit HKUnit extension, but copied here so that we can
// avoid a dependency on LoopKit from the Loop Status Extension.
var preferredMinimumFractionDigits: Int {
if self.unitString == "mg/dL" {
return 0
} else {
return 1
}
}

static func millimolesPerLiterUnit() -> HKUnit {
return HKUnit.moleUnit(with: .milli, molarMass: HKUnitMolarMassBloodGlucose).unitDivided(by: HKUnit.liter())
}

// A glucose-centric presentation helper for the localized unit string
var glucoseUnitDisplayString: String {
if self == HKUnit.millimolesPerLiterUnit() {
return NSLocalizedString("mmol/L", comment: "The unit display string for millimoles of glucose per liter")
} else {
return String(describing: self)
}
}

}
2 changes: 1 addition & 1 deletion Loop Status Extension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>1.1.2</string>
<string>1.2.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>MainAppBundleIdentifier</key>
Expand Down
26 changes: 15 additions & 11 deletions Loop Status Extension/StatusExtensionContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,24 @@ struct GlucoseContext {
let sensor: SensorDisplayable?
}

final class StatusExtensionContext: NSObject, RawRepresentable {
final class StatusExtensionContext: RawRepresentable {
typealias RawValue = [String: Any]
private let version = 1

var preferredUnitDisplayString: String?
var preferredUnitString: String?
var latestGlucose: GlucoseContext?
var reservoir: ReservoirContext?
var loop: LoopContext?
var netBasal: NetBasalContext?
var batteryPercentage: Double?
var eventualGlucose: Double?

override init() {
super.init()
}
init() { }

required init?(rawValue: RawValue) {
super.init()
let raw = rawValue

if let preferredString = raw["preferredUnitDisplayString"] as? String,
if let preferredString = raw["preferredUnitString"] as? String,
let latestValue = raw["latestGlucose_value"] as? Double,
let startDate = raw["latestGlucose_startDate"] as? Date {

Expand All @@ -81,7 +78,7 @@ final class StatusExtensionContext: NSObject, RawRepresentable {
isLocal: local)
}

preferredUnitDisplayString = preferredString
preferredUnitString = preferredString
latestGlucose = GlucoseContext(
quantity: latestValue,
startDate: startDate,
Expand Down Expand Up @@ -115,10 +112,10 @@ final class StatusExtensionContext: NSObject, RawRepresentable {
"version": version
]

raw["preferredUnitDisplayString"] = preferredUnitDisplayString
raw["preferredUnitString"] = preferredUnitString

if let glucose = latestGlucose,
preferredUnitDisplayString != nil {
if preferredUnitString != nil,
let glucose = latestGlucose {
raw["latestGlucose_value"] = glucose.quantity
raw["latestGlucose_startDate"] = glucose.startDate
}
Expand Down Expand Up @@ -161,3 +158,10 @@ final class StatusExtensionContext: NSObject, RawRepresentable {
return raw
}
}


extension StatusExtensionContext: CustomDebugStringConvertible {
var debugDescription: String {
return String(reflecting: rawValue)
}
}
18 changes: 10 additions & 8 deletions Loop Status Extension/StatusViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class StatusViewController: UIViewController, NCWidgetProviding {
// the wrong units and that could be very harmful. So unless there's a preferred
// unit, assume that none of the rest of the data is reliable.
guard
let preferredUnitDisplayString = context.preferredUnitDisplayString
let preferredUnitString = context.preferredUnitString
else {
completionHandler(NCUpdateResult.failed)
return
Expand All @@ -52,7 +52,7 @@ class StatusViewController: UIViewController, NCWidgetProviding {
if let glucose = context.latestGlucose {
glucoseHUD.set(glucoseQuantity: glucose.quantity,
at: glucose.startDate,
unitDisplayString: preferredUnitDisplayString,
unitString: preferredUnitString,
from: glucose.sensor)
}

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

if let eventualGlucose = context.eventualGlucose {
let quantity = HKQuantity(unit: HKUnit(from: preferredUnitDisplayString),
doubleValue: eventualGlucose.rounded())
let preferredUnit = HKUnit(from: preferredUnitString)
let formatter = NumberFormatter.glucoseFormatter(for: preferredUnit)
if let eventualGlucose = context.eventualGlucose,
let eventualGlucoseNumberString = formatter.string(from: NSNumber(value: eventualGlucose)) {
subtitleLabel.text = String(
format: NSLocalizedString(
"Eventually %@",
comment: "The subtitle format describing eventual glucose. (1: localized glucose value description)"),
String(describing: quantity))
"Eventually %1$@ %2$@",
comment: "The subtitle format describing eventual glucose. (1: localized glucose value description) (2: localized glucose units description)"),
eventualGlucoseNumberString,
preferredUnit.glucoseUnitDisplayString)
subtitleLabel.alpha = 1
} else {
subtitleLabel.alpha = 0
Expand Down
2 changes: 1 addition & 1 deletion Loop.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

// Change this on first setup to your own unique organization identifier in
// reverse-domain name syntax.
MAIN_APP_BUNDLE_IDENTIFIER = com.loudnate
MAIN_APP_BUNDLE_IDENTIFIER = com.loopkit
3 changes: 1 addition & 2 deletions Loop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1387,8 +1387,7 @@
4F70C1E31DE8DCA7006380B7 /* Base */,
);
name = MainInterface.storyboard;
path = "Loop Status Extension";
sourceTree = "<unknown>";
sourceTree = "<group>";
};
C1C73EF91DE3D0230022FC89 /* InfoPlist.strings */ = {
isa = PBXVariantGroup;
Expand Down
10 changes: 7 additions & 3 deletions Loop/Managers/DeviceDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -975,11 +975,11 @@ final class DeviceDataManager: CarbStoreDelegate, CarbStoreSyncDelegate, DoseSto

// MARK: - WatchKit

private(set) var watchManager: WatchDataManager!
fileprivate var watchManager: WatchDataManager!

// MARK: - Status Extension

private(set) var statusExtensionManager: StatusExtensionDataManager!
fileprivate var statusExtensionManager: StatusExtensionDataManager!

// MARK: - Initialization

Expand Down Expand Up @@ -1075,7 +1075,11 @@ extension DeviceDataManager: CustomDebugStringConvertible {
"workoutModeEnabled: \(workoutModeEnabled)",
"maximumBasalRatePerHour: \(maximumBasalRatePerHour)",
"maximumBolus: \(maximumBolus)",
String(reflecting: rileyLinkManager)
String(reflecting: rileyLinkManager),
String(reflecting: statusExtensionManager!),
"",
"## NSUserDefaults",
String(reflecting: UserDefaults.standard.dictionaryRepresentation())
].joined(separator: "\n")
}
}
Expand Down
21 changes: 18 additions & 3 deletions Loop/Managers/StatusExtensionDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ final class StatusExtensionDataManager {
NotificationCenter.default.addObserver(self, selector: #selector(update(_:)), name: .LoopDataUpdated, object: deviceDataManager.loopManager)
}

fileprivate var defaults: UserDefaults? {
return UserDefaults(suiteName: Bundle.main.appGroupSuiteName)
}

@objc private func update(_ notification: Notification) {

self.dataManager.glucoseStore?.preferredUnit() { (unit, error) in
if error == nil, let unit = unit {
self.createContext(unit) { (context) in
if let context = context {
UserDefaults(suiteName: Bundle.main.appGroupSuiteName)?.statusExtensionContext = context
self.defaults?.statusExtensionContext = context
}
}
}
Expand Down Expand Up @@ -57,10 +61,11 @@ final class StatusExtensionDataManager {
context.batteryPercentage = 0.25
context.reservoir = ReservoirContext(startDate: Date(), unitVolume: 42.5, capacity: 200)
context.netBasal = NetBasalContext(rate: 2.1, percentage: 0.6, startDate: Date() - TimeInterval(250))
context.eventualGlucose = 119.123
context.eventualGlucose = HKQuantity(unit: HKUnit.milligramsPerDeciliterUnit(), doubleValue: 119.123)
.doubleValue(for: preferredUnit)
#endif

context.preferredUnitDisplayString = preferredUnit.glucoseUnitDisplayString
context.preferredUnitString = preferredUnit.unitString
context.loop = LoopContext(
dosingEnabled: dataManager.loopManager.dosingEnabled,
lastCompleted: lastLoopCompleted)
Expand Down Expand Up @@ -98,3 +103,13 @@ final class StatusExtensionDataManager {
}
}
}


extension StatusExtensionDataManager: CustomDebugStringConvertible {
var debugDescription: String {
return [
"## StatusExtensionDataManager",
"statusExtensionContext: \(String(reflecting: defaults?.statusExtensionContext))"
].joined(separator: "\n")
}
}
2 changes: 1 addition & 1 deletion Loop/View Controllers/StatusTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ final class StatusTableViewController: UITableViewController, UIGestureRecognize
if let glucose = self.dataManager.glucoseStore?.latestGlucose {
self.glucoseHUD.set(glucoseQuantity: glucose.quantity.doubleValue(for: self.charts.glucoseUnit),
at: glucose.startDate,
unitDisplayString: self.charts.glucoseUnit.glucoseUnitDisplayString,
unitString: self.charts.glucoseUnit.unitString,
from: self.dataManager.sensorInfo)
}

Expand Down
7 changes: 4 additions & 3 deletions Loop/Views/GlucoseHUDView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,20 @@ final class GlucoseHUDView: HUDView {
}
}

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

let time = timeFormatter.string(from: glucoseStartDate)
caption?.text = time
let unit = HKUnit(from: unitString)

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

var unitStrings = [unitDisplayString]
var unitStrings = [unit.glucoseUnitDisplayString]

if let trend = sensor?.trendType {
unitStrings.append(trend.symbol)
Expand Down