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
renaming milligramsPerDeciliterUnit() to milligramsPerDeciliter()
  • Loading branch information
ps2 committed May 13, 2017
commit d0f0fa0c67a00a64d6a1443020c49749d8847f5f
6 changes: 3 additions & 3 deletions Common/Extensions/HKUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ extension HKUnit {
}
}

static func milligramsPerDeciliterUnit() -> HKUnit {
static func milligramsPerDeciliter() -> HKUnit {
return HKUnit.gramUnit(with: .milli).unitDivided(by: HKUnit.literUnit(with: .deci))
}

static func millimolesPerLiterUnit() -> HKUnit {
static func millimolesPerLiter() -> 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() {
if self == HKUnit.millimolesPerLiter() {
return NSLocalizedString("mmol/L", comment: "The unit display string for millimoles of glucose per liter")
} else {
return String(describing: self)
Expand Down
24 changes: 11 additions & 13 deletions DoseMathTests/DoseMathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class RecommendTempBasalTests: XCTestCase {
return fixture.map {
return GlucoseFixtureValue(
startDate: dateFormatter.date(from: $0["date"] as! String)!,
quantity: HKQuantity(unit: HKUnit.milligramsPerDeciliterUnit(), doubleValue: $0["amount"] as! Double)
quantity: HKQuantity(unit: HKUnit.milligramsPerDeciliter(), doubleValue: $0["amount"] as! Double)
)
}
}
Expand All @@ -81,15 +81,15 @@ class RecommendTempBasalTests: XCTestCase {
}

var glucoseTargetRange: GlucoseRangeSchedule {
return GlucoseRangeSchedule(unit: HKUnit.milligramsPerDeciliterUnit(), dailyItems: [RepeatingScheduleValue(startTime: TimeInterval(0), value: DoubleRange(minValue: 90, maxValue: 120))], workoutRange: nil)!
return GlucoseRangeSchedule(unit: HKUnit.milligramsPerDeciliter(), dailyItems: [RepeatingScheduleValue(startTime: TimeInterval(0), value: DoubleRange(minValue: 90, maxValue: 120))], workoutRange: nil)!
}

var insulinSensitivitySchedule: InsulinSensitivitySchedule {
return InsulinSensitivitySchedule(unit: HKUnit.milligramsPerDeciliterUnit(), dailyItems: [RepeatingScheduleValue(startTime: 0.0, value: 60.0)])!
return InsulinSensitivitySchedule(unit: HKUnit.milligramsPerDeciliter(), dailyItems: [RepeatingScheduleValue(startTime: 0.0, value: 60.0)])!
}

var minimumBGGuard: GlucoseThreshold {
return GlucoseThreshold(unit: HKUnit.milligramsPerDeciliterUnit(), value: 55)
return GlucoseThreshold(unit: HKUnit.milligramsPerDeciliter(), value: 55)
}

var insulinActionDuration: TimeInterval {
Expand Down Expand Up @@ -385,7 +385,7 @@ class RecommendTempBasalTests: XCTestCase {
XCTAssertEqual(TimeInterval(minutes: 30), dose!.duration)

// Use mmol sensitivity value
let insulinSensitivitySchedule = InsulinSensitivitySchedule(unit: HKUnit.millimolesPerLiterUnit(), dailyItems: [RepeatingScheduleValue(startTime: 0.0, value: 3.33)])!
let insulinSensitivitySchedule = InsulinSensitivitySchedule(unit: HKUnit.millimolesPerLiter(), dailyItems: [RepeatingScheduleValue(startTime: 0.0, value: 3.33)])!

dose = DoseMath.recommendTempBasalFromPredictedGlucose(glucose,
atDate: glucose.first!.startDate,
Expand Down Expand Up @@ -465,7 +465,7 @@ class RecommendBolusTests: XCTestCase {
return fixture.map {
return GlucoseFixtureValue(
startDate: dateFormatter.date(from: $0["date"] as! String)!,
quantity: HKQuantity(unit: HKUnit.milligramsPerDeciliterUnit(), doubleValue: $0["amount"] as! Double)
quantity: HKQuantity(unit: HKUnit.milligramsPerDeciliter(), doubleValue: $0["amount"] as! Double)
)
}
}
Expand All @@ -485,15 +485,15 @@ class RecommendBolusTests: XCTestCase {
}

var glucoseTargetRange: GlucoseRangeSchedule {
return GlucoseRangeSchedule(unit: HKUnit.milligramsPerDeciliterUnit(), dailyItems: [RepeatingScheduleValue(startTime: TimeInterval(0), value: DoubleRange(minValue: 90, maxValue: 120))], workoutRange: nil)!
return GlucoseRangeSchedule(unit: HKUnit.milligramsPerDeciliter(), dailyItems: [RepeatingScheduleValue(startTime: TimeInterval(0), value: DoubleRange(minValue: 90, maxValue: 120))], workoutRange: nil)!
}

var insulinSensitivitySchedule: InsulinSensitivitySchedule {
return InsulinSensitivitySchedule(unit: HKUnit.milligramsPerDeciliterUnit(), dailyItems: [RepeatingScheduleValue(startTime: 0.0, value: 60.0)])!
return InsulinSensitivitySchedule(unit: HKUnit.milligramsPerDeciliter(), dailyItems: [RepeatingScheduleValue(startTime: 0.0, value: 60.0)])!
}

var minimumBGGuard: GlucoseThreshold {
return GlucoseThreshold(unit: HKUnit.milligramsPerDeciliterUnit(), value: 55)
return GlucoseThreshold(unit: HKUnit.milligramsPerDeciliter(), value: 55)
}

var insulinActionDuration: TimeInterval {
Expand Down Expand Up @@ -589,13 +589,11 @@ class RecommendBolusTests: XCTestCase {
XCTAssertEqual(1.325, dose.amount)

if case BolusRecommendationNotice.currentGlucoseBelowTarget(let glucose, let units) = dose.notice! {
XCTAssertEqual(units, HKUnit.milligramsPerDeciliterUnit())
XCTAssertEqual(units, HKUnit.milligramsPerDeciliter())
XCTAssertEqual(glucose.quantity.doubleValue(for: units), 60)
} else {
XCTFail("Expected currentGlucoseBelowTarget, but got \(dose.notice!)")
}

//XCTAssertEqual(BolusRecommendationNotice.currentGlucoseBelowTarget(glucose: GlucoseValue(56), unit: HKUnit.milligramsPerDeciliterUnit()), dose.notice!)
}

func testDroppingBelowRangeThenRising() {
Expand Down Expand Up @@ -747,7 +745,7 @@ class RecommendBolusTests: XCTestCase {
XCTAssertEqual(1.0, dose.amount)

// Use mmol sensitivity value
let insulinSensitivitySchedule = InsulinSensitivitySchedule(unit: HKUnit.millimolesPerLiterUnit(), dailyItems: [RepeatingScheduleValue(startTime: 0.0, value: 10.0 / 3)])!
let insulinSensitivitySchedule = InsulinSensitivitySchedule(unit: HKUnit.millimolesPerLiter(), dailyItems: [RepeatingScheduleValue(startTime: 0.0, value: 10.0 / 3)])!

dose = DoseMath.recommendBolusFromPredictedGlucose(glucose,
atDate: glucose.first!.startDate,
Expand Down
4 changes: 2 additions & 2 deletions Loop Status Extension/StatusViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class StatusViewController: UIViewController, NCWidgetProviding {
)

charts.glucoseDisplayRange = (
min: HKQuantity(unit: HKUnit.milligramsPerDeciliterUnit(), doubleValue: 100),
max: HKQuantity(unit: HKUnit.milligramsPerDeciliterUnit(), doubleValue: 175)
min: HKQuantity(unit: HKUnit.milligramsPerDeciliter(), doubleValue: 100),
max: HKQuantity(unit: HKUnit.milligramsPerDeciliter(), doubleValue: 175)
)

return charts
Expand Down
2 changes: 1 addition & 1 deletion Loop/Managers/CGM/EnliteCGMManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class EnliteCGMManager: CGMManager {
self.sensorState = EnliteSensorDisplayable(latestSensorEvent)
}

let unit = HKUnit.milligramsPerDeciliterUnit()
let unit = HKUnit.milligramsPerDeciliter()
let glucoseValues = events
// TODO: Is the { $0.date > latestGlucoseDate } filter duplicative?
.filter({ $0.glucoseEvent is SensorValueGlucoseEvent && $0.date > latestGlucoseDate })
Expand Down
2 changes: 1 addition & 1 deletion Loop/Managers/DeviceDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ final class DeviceDataManager {
// Enlite data is included
if let date = glucoseDateComponents?.date {
loopManager.addGlucose([(
quantity: HKQuantity(unit: HKUnit.milligramsPerDeciliterUnit(), doubleValue: Double(glucose)),
quantity: HKQuantity(unit: HKUnit.milligramsPerDeciliter(), doubleValue: Double(glucose)),
date: date,
isDisplayOnly: false
)], from: nil)
Expand Down
2 changes: 1 addition & 1 deletion Loop/Managers/DoseMath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct DoseMath {
- returns: The determined basal rate, in Units/hour
*/
private static func calculateTempBasalRateForGlucose(_ currentGlucose: HKQuantity, toTargetGlucose targetGlucose: HKQuantity, insulinSensitivity: HKQuantity, currentBasalRate: Double, maxBasalRate: Double, duration: TimeInterval) -> Double {
let unit = HKUnit.milligramsPerDeciliterUnit()
let unit = HKUnit.milligramsPerDeciliter()
let doseUnits = (currentGlucose.doubleValue(for: unit) - targetGlucose.doubleValue(for: unit)) / insulinSensitivity.doubleValue(for: unit)

let rate = min(maxBasalRate, max(0, doseUnits / (duration / TimeInterval(hours: 1)) + currentBasalRate))
Expand Down
2 changes: 1 addition & 1 deletion Loop/Managers/LoopDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ final class LoopDataManager {
self.retrospectivePredictedGlucose = retrospectivePrediction

guard let lastGlucose = retrospectivePrediction.last else { return }
let glucoseUnit = HKUnit.milligramsPerDeciliterUnit()
let glucoseUnit = HKUnit.milligramsPerDeciliter()
let velocityUnit = glucoseUnit.unitDivided(by: HKUnit.second())

let discrepancy = change.end.quantity.doubleValue(for: glucoseUnit) - lastGlucose.quantity.doubleValue(for: glucoseUnit) // mg/dL
Expand Down
2 changes: 1 addition & 1 deletion Loop/Managers/StatusExtensionDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ final class StatusExtensionDataManager {
)
context.predictedGlucose = PredictedGlucoseContext(
values: (1...36).map { 89.123 + Double($0 * 5) }, // 3 hours of linear data
unit: HKUnit.milligramsPerDeciliterUnit(),
unit: HKUnit.milligramsPerDeciliter(),
startDate: Date(),
interval: TimeInterval(minutes: 5))

Expand Down
2 changes: 1 addition & 1 deletion Loop/Managers/WatchDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ final class WatchDataManager: NSObject, WCSessionDelegate {
private var lastComplicationContext: WatchContext?

private let minTrendDrift: Double = 20
private lazy var minTrendUnit = HKUnit.milligramsPerDeciliterUnit()
private lazy var minTrendUnit = HKUnit.milligramsPerDeciliter()

private func sendWatchContext(_ context: WatchContext) {
if let session = watchSession, session.isPaired && session.isWatchAppInstalled {
Expand Down
2 changes: 2 additions & 0 deletions Loop/Models/BolusRecommendation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation
import LoopKit
import HealthKit


enum BolusRecommendationNotice: CustomStringConvertible, Equatable {
case glucoseBelowMinimumGuard(minGlucose: GlucoseValue, unit: HKUnit)
case currentGlucoseBelowTarget(glucose: GlucoseValue, unit: HKUnit)
Expand Down Expand Up @@ -74,6 +75,7 @@ struct BolusRecommendation {
}
}


extension BolusRecommendation: Comparable {
static func ==(lhs: BolusRecommendation, rhs: BolusRecommendation) -> Bool {
return lhs.amount == rhs.amount
Expand Down
2 changes: 1 addition & 1 deletion Loop/Models/GlucoseG4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import LoopUI

extension GlucoseG4: GlucoseValue {
public var quantity: HKQuantity {
return HKQuantity(unit: HKUnit.milligramsPerDeciliterUnit(), doubleValue: Double(glucose))
return HKQuantity(unit: HKUnit.milligramsPerDeciliter(), doubleValue: Double(glucose))
}

public var startDate: Date {
Expand Down
2 changes: 1 addition & 1 deletion Loop/Models/ShareGlucose+GlucoseKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension ShareGlucose: GlucoseValue {
}

public var quantity: HKQuantity {
return HKQuantity(unit: HKUnit.milligramsPerDeciliterUnit(), doubleValue: Double(glucose))
return HKQuantity(unit: HKUnit.milligramsPerDeciliter(), doubleValue: Double(glucose))
}
}

Expand Down
2 changes: 1 addition & 1 deletion Loop/View Controllers/BolusViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ final class BolusViewController: UITableViewController, IdentifiableClass, UITex

// MARK: - State

var glucoseUnit: HKUnit = HKUnit.milligramsPerDeciliterUnit()
var glucoseUnit: HKUnit = HKUnit.milligramsPerDeciliter()

var bolusRecommendation: BolusRecommendation? = nil {
didSet {
Expand Down
4 changes: 2 additions & 2 deletions Loop/View Controllers/PredictionTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class PredictionTableViewController: UITableViewController, IdentifiableClass, U
let charts = StatusChartsManager(colors: .default, settings: .default)

charts.glucoseDisplayRange = (
min: HKQuantity(unit: HKUnit.milligramsPerDeciliterUnit(), doubleValue: 60),
max: HKQuantity(unit: HKUnit.milligramsPerDeciliterUnit(), doubleValue: 200)
min: HKQuantity(unit: HKUnit.milligramsPerDeciliter(), doubleValue: 60),
max: HKQuantity(unit: HKUnit.milligramsPerDeciliter(), doubleValue: 200)
)

return charts
Expand Down
4 changes: 2 additions & 2 deletions Loop/View Controllers/StatusTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,8 @@ final class StatusTableViewController: UITableViewController, UIGestureRecognize
let charts = StatusChartsManager(colors: .default, settings: .default)

charts.glucoseDisplayRange = (
min: HKQuantity(unit: HKUnit.milligramsPerDeciliterUnit(), doubleValue: 100),
max: HKQuantity(unit: HKUnit.milligramsPerDeciliterUnit(), doubleValue: 175)
min: HKQuantity(unit: HKUnit.milligramsPerDeciliter(), doubleValue: 100),
max: HKQuantity(unit: HKUnit.milligramsPerDeciliter(), doubleValue: 175)
)

return charts
Expand Down
4 changes: 2 additions & 2 deletions LoopUI/Managers/StatusChartsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public final class StatusChartsManager {
}
}

public var glucoseUnit: HKUnit = HKUnit.milligramsPerDeciliterUnit() {
public var glucoseUnit: HKUnit = HKUnit.milligramsPerDeciliter() {
didSet {
if glucoseUnit != oldValue {
// Regenerate the glucose display points
Expand Down Expand Up @@ -695,7 +695,7 @@ public final class StatusChartsManager {

private extension HKUnit {
var glucoseUnitYAxisSegmentSize: Double {
if self == HKUnit.milligramsPerDeciliterUnit() {
if self == HKUnit.milligramsPerDeciliter() {
return 25
} else {
return 1
Expand Down