Skip to content

Commit

Permalink
New try: the pebble values are now forced to always read in mgdl. Int…
Browse files Browse the repository at this point in the history
…ernally I can therefore use only mgdl. Only for the UI, these are calculated to mmol. This enables us again to let the user switch to other units. Nevertheless what is defined on the backend. Because alls values come now in mgdl from the backend
  • Loading branch information
dhermanns committed Oct 3, 2020
1 parent 527230c commit f296b1c
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 49 deletions.
6 changes: 3 additions & 3 deletions nightguard WatchKit Extension/ChartPainter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,18 @@ class ChartPainter {
// paint the maximum BGValue Label only if it has enought space and doesn't intersect with
// the upper bound BGValue
if maxBgValueAsFloat > upperBoundNiceValue + 25 {
let maxBgValueString = UnitsConverter.toDisplayUnits(maxBgValueAsFloat.cleanValue)
let maxBgValueString = UnitsConverter.mgdlToDisplayUnits(maxBgValueAsFloat.cleanValue)
maxBgValueString.draw(
with: CGRect(x: CGFloat(x), y: CGFloat.init(calcYValue(maxBgValueAsFloat)) + 3, width: 40, height: 14),
options: .usesLineFragmentOrigin, attributes: attrs, context: nil)
}

let upperBoundString = UnitsConverter.toDisplayUnits(upperBoundNiceValue.cleanValue)
let upperBoundString = UnitsConverter.mgdlToDisplayUnits(upperBoundNiceValue.cleanValue)
upperBoundString.draw(
with: CGRect(x: CGFloat(x), y: CGFloat.init(calcYValue(upperBoundNiceValue)), width: 40, height: 14),
options: .usesLineFragmentOrigin, attributes: attrs, context: nil)

let lowerBoundString = UnitsConverter.toDisplayUnits(lowerBoundNiceValue.cleanValue)
let lowerBoundString = UnitsConverter.mgdlToDisplayUnits(lowerBoundNiceValue.cleanValue)
lowerBoundString.draw(
with: CGRect(x: CGFloat(x), y: CGFloat.init(calcYValue(lowerBoundNiceValue))-15, width: 40, height: 14),
options: .usesLineFragmentOrigin, attributes: attrs, context: nil)
Expand Down
35 changes: 19 additions & 16 deletions nightguard WatchKit Extension/ComplicationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class ComplicationController: NSObject, CLKComplicationDataSource {
if useRelativeTimeWhenPossible {
modTemplate.line1TextProvider = CLKSimpleTextProvider(text: getSgvAndArrow(currentNightscoutData, " "))
modTemplate.line1TextProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.toDisplayUnits(currentNightscoutData.sgv))
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
modTemplate.line2TextProvider = getRelativeDateTextProvider(for: currentNightscoutData.time)
} else {
modTemplate.line1TextProvider = CLKSimpleTextProvider(text: "\(currentNightscoutData.hourAndMinutes)")
modTemplate.line1TextProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.toDisplayUnits(currentNightscoutData.sgv))
modTemplate.line2TextProvider = CLKSimpleTextProvider(text: "\(currentNightscoutData.sgv)\(UnitsConverter.toDisplayDeltaUnits(currentNightscoutData.bgdeltaString))\(currentNightscoutData.bgdeltaArrow)")
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
modTemplate.line2TextProvider = CLKSimpleTextProvider(text: "\(UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))\(UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.bgdeltaString))\(currentNightscoutData.bgdeltaArrow)")
}
template = modTemplate
case .modularLarge:
Expand All @@ -59,7 +59,7 @@ class ComplicationController: NSObject, CLKComplicationDataSource {

modTemplate.row1Column1TextProvider = CLKSimpleTextProvider(text: getOneLine(currentNightscoutData))
modTemplate.row1Column1TextProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.toDisplayUnits(currentNightscoutData.sgv))
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
modTemplate.row1Column2TextProvider = getRelativeDateTextProvider(for: currentNightscoutData.time)
modTemplate.row2Column1TextProvider = CLKSimpleTextProvider(text: "")
modTemplate.row2Column2TextProvider = CLKSimpleTextProvider(text: "")
Expand Down Expand Up @@ -95,20 +95,21 @@ class ComplicationController: NSObject, CLKComplicationDataSource {
let modTemplate = CLKComplicationTemplateUtilitarianSmallFlat()
modTemplate.textProvider = CLKSimpleTextProvider(text: self.getOneBigLine(currentNightscoutData))
modTemplate.textProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.toDisplayUnits(currentNightscoutData.sgv))
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
template = modTemplate
case .utilitarianLarge:
let modTemplate = CLKComplicationTemplateUtilitarianLargeFlat()
modTemplate.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "Complication/Circular")!)
modTemplate.textProvider = CLKSimpleTextProvider(text: self.getOneBigLine(currentNightscoutData))
modTemplate.textProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.toDisplayUnits(currentNightscoutData.sgv))
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
template = modTemplate
case .circularSmall:
let modTemplate = CLKComplicationTemplateCircularSmallRingText()
modTemplate.textProvider = CLKSimpleTextProvider(text: UnitsConverter.toDisplayUnits(currentNightscoutData.sgv))
modTemplate.textProvider = CLKSimpleTextProvider(text:
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
modTemplate.textProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.toDisplayUnits(currentNightscoutData.sgv))
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))

modTemplate.fillFraction = self.getAgeOfDataInMinutes(currentNightscoutData.time) / 60
modTemplate.ringStyle = CLKComplicationRingStyle.closed
Expand All @@ -118,7 +119,7 @@ class ComplicationController: NSObject, CLKComplicationDataSource {
let modTemplate = CLKComplicationTemplateGraphicCornerStackText()
modTemplate.outerTextProvider = CLKSimpleTextProvider(text: self.getOneShortLine(currentNightscoutData))
modTemplate.outerTextProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.toDisplayUnits(currentNightscoutData.sgv))
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
modTemplate.innerTextProvider = CLKSimpleTextProvider(text: self.getLastReadingTime(currentNightscoutData))
template = modTemplate
} else {
Expand All @@ -127,8 +128,9 @@ class ComplicationController: NSObject, CLKComplicationDataSource {
case .graphicCircular:
if #available(watchOSApplicationExtension 5.0, *) {
let modTemplate = CLKComplicationTemplateGraphicCircularClosedGaugeText()
modTemplate.centerTextProvider = CLKSimpleTextProvider(text: "\(UnitsConverter.toDisplayUnits(currentNightscoutData.sgv))")
modTemplate.centerTextProvider.tintColor = UIColorChanger.getBgColor(UnitsConverter.toDisplayUnits(currentNightscoutData.sgv))
modTemplate.centerTextProvider = CLKSimpleTextProvider(text: "\(UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))")
modTemplate.centerTextProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
modTemplate.gaugeProvider = CLKSimpleGaugeProvider(style: .fill, gaugeColor: UIColor.black, fillFraction: 0.0)
template = modTemplate
} else {
Expand All @@ -138,7 +140,8 @@ class ComplicationController: NSObject, CLKComplicationDataSource {
if #available(watchOSApplicationExtension 5.0, *) {
let modTemplate = CLKComplicationTemplateGraphicBezelCircularText()
modTemplate.textProvider = CLKSimpleTextProvider(text: self.getOneBigLine(currentNightscoutData))
modTemplate.textProvider?.tintColor = UIColorChanger.getBgColor(UnitsConverter.toDisplayUnits(currentNightscoutData.sgv))
modTemplate.textProvider?.tintColor = UIColorChanger.getBgColor(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
let modImageTemplate = CLKComplicationTemplateGraphicCircularImage()
modImageTemplate.imageProvider = CLKFullColorImageProvider(fullColorImage: UIImage(named: "Complication/Graphic Circular")!)
modTemplate.circularTemplate = modImageTemplate
Expand Down Expand Up @@ -173,7 +176,7 @@ class ComplicationController: NSObject, CLKComplicationDataSource {

// Display 11:24 113+2
func getOneBigLine(_ data : NightscoutData) -> String {
return "\(data.hourAndMinutes) \(UnitsConverter.toDisplayUnits(data.sgv))\(UnitsConverter.toDisplayDeltaUnits(data.bgdeltaString))\(data.bgdeltaArrow)"
return "\(data.hourAndMinutes) \(UnitsConverter.mgdlToDisplayUnits(data.sgv))\(UnitsConverter.mgdlToDisplayUnits(data.bgdeltaString))\(data.bgdeltaArrow)"
}

// Display 11:24
Expand All @@ -183,12 +186,12 @@ class ComplicationController: NSObject, CLKComplicationDataSource {

// Displays 113 ↗ +2
func getOneLine(_ data : NightscoutData) -> String {
return "\(getSgvAndArrow(data, " "))\t\(UnitsConverter.toDisplayDeltaUnits(data.bgdeltaString))"
return "\(getSgvAndArrow(data, " "))\t\(UnitsConverter.mgdlToDisplayUnits(data.bgdeltaString))"
}

// Displays 113↗+2
func getOneShortLine(_ data : NightscoutData) -> String {
return "\(getSgvAndArrow(data, ""))\(UnitsConverter.toDisplayDeltaUnits(data.bgdeltaString))"
return "\(getSgvAndArrow(data, ""))\(UnitsConverter.mgdlToDisplayUnits(data.bgdeltaString))"
}

func getSgvAndArrow(_ data: NightscoutData, _ separator: String) -> String {
Expand All @@ -201,7 +204,7 @@ class ComplicationController: NSObject, CLKComplicationDataSource {
bgdeltaArrow = ""
}

return [UnitsConverter.toDisplayUnits(data.sgv), bgdeltaArrow].joined(separator: separator)
return [UnitsConverter.mgdlToDisplayUnits(data.sgv), bgdeltaArrow].joined(separator: separator)
}

// If the age is older than 59 minutes => return 60 in that case
Expand Down
11 changes: 7 additions & 4 deletions nightguard WatchKit Extension/InterfaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,16 @@ class InterfaceController: WKInterfaceController, WKCrownDelegate {

fileprivate func paintCurrentBgData(currentNightscoutData : NightscoutData) {

self.bgLabel.setText(UnitsConverter.toDisplayUnits(currentNightscoutData.sgv))
self.bgLabel.setTextColor(UIColorChanger.getBgColor(UnitsConverter.toDisplayUnits(currentNightscoutData.sgv)))
self.bgLabel.setText(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
self.bgLabel.setTextColor(UIColorChanger.getBgColor(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv)))

self.deltaLabel.setText(UnitsConverter.toDisplayDeltaUnits(currentNightscoutData.bgdeltaString))
self.deltaLabel.setText(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.bgdeltaString))
self.deltaArrowLabel.setText(currentNightscoutData.bgdeltaArrow)
self.deltaLabel.setTextColor(UIColorChanger.getDeltaLabelColor(
UnitsConverter.toDisplayUnits(currentNightscoutData.bgdelta)))
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.bgdelta)))

self.timeLabel.setText(currentNightscoutData.timeString)
self.timeLabel.setTextColor(UIColorChanger.getTimeLabelColor(currentNightscoutData.time))
Expand Down
13 changes: 8 additions & 5 deletions nightguard/AlarmViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ class AlarmViewController: CustomFormViewController {

override func constructForm() {

aboveSliderRow = SliderRow.glucoseLevelSlider(initialValue: AlarmRule.alertIfAboveValue.value, minimumValue: MIN_ALERT_ABOVE_VALUE, maximumValue: MAX_ALERT_ABOVE_VALUE)
aboveSliderRow = SliderRow.glucoseLevelSlider(
initialValue: UnitsConverter.mgdlToDisplayUnits(AlarmRule.alertIfAboveValue.value),
minimumValue: UnitsConverter.mgdlToDisplayUnits(MIN_ALERT_ABOVE_VALUE),
maximumValue: UnitsConverter.mgdlToDisplayUnits(MAX_ALERT_ABOVE_VALUE))
aboveSliderRow.cell.slider.addTarget(self, action: #selector(onSliderValueChanged(slider:event:)), for: .valueChanged)

belowSliderRow = SliderRow.glucoseLevelSlider(initialValue: AlarmRule.alertIfBelowValue.value, minimumValue: MIN_ALERT_BELOW_VALUE, maximumValue: MAX_ALERT_BELOW_VALUE)
Expand Down Expand Up @@ -111,7 +114,7 @@ class AlarmViewController: CustomFormViewController {
if AlarmRule.isEdgeDetectionAlarmEnabled.value {

let deltaInMgdl = AlarmRule.deltaAmount.value
let delta = UnitsConverter.toDisplayUnits("\(deltaInMgdl)")
let delta = UnitsConverter.mgdlToDisplayUnits("\(deltaInMgdl)")
let units = UserDefaultsRepository.units.value.description
let consecutiveValue = AlarmRule.numberOfConsecutiveValues.value

Expand All @@ -135,7 +138,7 @@ class AlarmViewController: CustomFormViewController {
row.detailTextProvider = {

let urgentHighInMgdl = AlarmRule.persistentHighUpperBound.value
let urgentHigh = UnitsConverter.toDisplayUnits("\(urgentHighInMgdl)")
let urgentHigh = UnitsConverter.mgdlToDisplayUnits("\(urgentHighInMgdl)")
let units = UserDefaultsRepository.units.value.description
let urgentHighWithUnits = "\(urgentHigh) \(units)"

Expand Down Expand Up @@ -259,9 +262,9 @@ class AlarmViewController: CustomFormViewController {
}

private func updateSliderRowsFromUserDefaultsValues() {
aboveSliderRow.value = Float(UnitsConverter.toDisplayUnits("\(AlarmRule.alertIfAboveValue.value)"))!
aboveSliderRow.value = Float(UnitsConverter.mgdlToDisplayUnits("\(AlarmRule.alertIfAboveValue.value)"))
aboveSliderRow.updateCell()
belowSliderRow.value = Float(UnitsConverter.toDisplayUnits("\(AlarmRule.alertIfBelowValue.value)"))!
belowSliderRow.value = Float(UnitsConverter.mgdlToDisplayUnits("\(AlarmRule.alertIfBelowValue.value)"))
belowSliderRow.updateCell()
}
}
6 changes: 3 additions & 3 deletions nightguard/CustomFormViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,11 @@ extension SliderRow {
class func glucoseLevelSlider(initialValue: Float, minimumValue: Float, maximumValue: Float, snapIncrementForMgDl: Float = 10.0) -> SliderRow {

return SliderRow() { row in
row.value = Float(UnitsConverter.toDisplayUnits("\(initialValue)"))!
row.value = initialValue
}.cellSetup { cell, row in

let minimumValue = Float(UnitsConverter.toDisplayUnits("\(minimumValue)"))!
let maximumValue = Float(UnitsConverter.toDisplayUnits("\(maximumValue)"))!
let minimumValue = Float(UnitsConverter.mgdlToDisplayUnits("\(minimumValue)"))!
let maximumValue = Float(UnitsConverter.mgdlToDisplayUnits("\(maximumValue)"))!
let snapIncrement = (UserDefaultsRepository.units.value == .mgdl) ? snapIncrementForMgDl : 0.1

let steps = (maximumValue - minimumValue) / snapIncrement
Expand Down
15 changes: 9 additions & 6 deletions nightguard/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,17 @@ class MainViewController: UIViewController, SlideToSnoozeDelegate {
if currentNightscoutData.sgv == "---" {
self.bgLabel.text = "---"
} else {
self.bgLabel.text = UnitsConverter.toDisplayUnits(currentNightscoutData.sgv)
self.bgLabel.text = UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv)
}
self.bgLabel.textColor = UIColorChanger.getBgColor(UnitsConverter.toDisplayUnits(currentNightscoutData.sgv))
self.bgLabel.textColor = UIColorChanger.getBgColor(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))

self.deltaLabel.text = UnitsConverter.toDisplayDeltaUnits(currentNightscoutData.bgdeltaString)
self.deltaLabel.text = UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.bgdeltaString)
self.deltaArrowsLabel.text = currentNightscoutData.bgdeltaArrow
self.deltaLabel.textColor = UIColorChanger.getDeltaLabelColor(currentNightscoutData.bgdelta)
self.deltaArrowsLabel.textColor = UIColorChanger.getDeltaLabelColor(currentNightscoutData.bgdelta)
self.deltaLabel.textColor = UIColorChanger.getDeltaLabelColor(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.bgdelta))
self.deltaArrowsLabel.textColor = UIColorChanger.getDeltaLabelColor(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.bgdelta))

self.lastUpdateLabel.text = currentNightscoutData.timeString
self.lastUpdateLabel.textColor = UIColorChanger.getTimeLabelColor(currentNightscoutData.time)
Expand Down Expand Up @@ -542,7 +545,7 @@ class MainViewController: UIViewController, SlideToSnoozeDelegate {

let temporaryTargetData = NightscoutCacheService.singleton.getTemporaryTargetData()
if temporaryTargetData.activeUntilDate.remainingMinutes() > 0 {
self.temporaryTargetLabel.text = "TT \(UnitsConverter.toDisplayUnits(temporaryTargetData.targetTop)) \(temporaryTargetData.activeUntilDate.remainingMinutes())m"
self.temporaryTargetLabel.text = "TT \(UnitsConverter.mgdlToDisplayUnits("\(temporaryTargetData.targetTop)")) \(temporaryTargetData.activeUntilDate.remainingMinutes())m"
} else {
self.temporaryTargetLabel.text = "TT --"
}
Expand Down
6 changes: 5 additions & 1 deletion nightguard/NightscoutService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,11 @@ class NightscoutService {
}

// Get the current data from REST-Call
let url = UserDefaultsRepository.getUrlWithPathAndQueryParameters(path: "pebble", queryParams: ["": ""])
// Force mgdl here, since all values are kept internall in mgdl.
// Only for the UI, they are transformed to mmol. This enables us to let the user manually
// switch to the wished units.
// All other endpoints deliver in mgdl, too. So this makes sence to enforce it here, too:
let url = UserDefaultsRepository.getUrlWithPathAndQueryParameters(path: "pebble", queryParams: ["units": "mgdl"])
guard url != nil else {
resultHandler(.error(createEmptyOrInvalidUriError()))
return nil
Expand Down
Loading

0 comments on commit f296b1c

Please sign in to comment.