Skip to content

Instantly share code, notes, and snippets.

@someoneAnyone
Last active March 16, 2021 10:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save someoneAnyone/deff3c796a00a7379fb419542f84e0e3 to your computer and use it in GitHub Desktop.
Save someoneAnyone/deff3c796a00a7379fb419542f84e0e3 to your computer and use it in GitHub Desktop.
Glucose Value Conversion on iOS 10
//: # Glucose Value Conversion on iOS 10.
import Foundation // iOS 10.0, wathOS 3.0, macOS 10.12
/*:
****
## Current Nightscouter Implmentation
- Note: The current app treats glucose values as an aliased Double.
In the example below, we have two values taken from a Nightscout site. The first is a mg/dL value and the second is the value converted to mmol/L by nightscout.
*/
public typealias MgdlValue = Double
public typealias MmolValue = Double
let mgdlValue: MgdlValue = 245.0
let mmolValue: MmolValue = 13.6
/*:
****
### Current methods for converting glucose units.
- Note: This method was derived from the Nightscout javascript code.
*/
// Take the current mg/dL value and divide it by 18.0 to get a mmol/L value.
let oldWayToMmol = mgdlValue / 18.0
// Take the current mg/dL value and multiple it by 18.0 to get a mg/dL value.
let oldWayToMgdl = mmolValue * 18.0
// The line of code above doesn't always convert cleanly. Here is the difference.
let oldWayDifference = mgdlValue - oldWayToMgdl
// Take the current mg/dL value and multiple it by 18.0 to get a mg/dL value but this time ceil the value so that it is a whole number.
let oldWayToMgdlCleaned = ceil(mmolValue * 18.0) // eturns a rounded up mg/dL value.
/*:
****
## Foundation Improvements in iOS 10, watchOS 3, macOS 10.12
- Note: As this is only available in the upcoming release of foundation from Apple, adoption of this might be delayed.
In the example below, we use the new "Measurement" class in foundation to create two new objects. The 'sgInMgdL' is a mg/dL unit of measuement and 'sgInMmol' is in mmol/L units.
*/
let gvInMgdL = Measurement(value: mgdlValue, unit: UnitConcentrationMass.milligramsPerDeciliter)
let gvInMmol = Measurement(value: mmolValue, unit: UnitConcentrationMass.millimolesPerLiter(withGramsPerMole: 0.01))
// Below uses foundation's `converted(to: )` method to transform a mmol/L into mg/dL.
print("Converted \(gvInMmol) to \(gvInMmol.converted(to: UnitConcentrationMass.milligramsPerDeciliter))")
// Below uses foundation's `converted(to: )` method to transform a mg/dL into mmol/L.
print("Converted \(gvInMgdL) to \(gvInMmol.converted(to: UnitConcentrationMass.millimolesPerLiter(withGramsPerMole: 0.01)))")
/*:
****
### Math using differnt glucose values.
- Note: As this is only available in the upcoming release of foundation from Apple, adoption of this might be delayed.
In the following examples you can see how it is possible to add two different types of glucose values.
- Note: This isn't something that is needed for Nightscouter but it is cool.
*/
var subResult = gvInMmol - gvInMgdL
print(subResult.converted(to: UnitConcentrationMass.milligramsPerDeciliter))
var addResult = gvInMmol + gvInMgdL
let addResultConvertedToMgdl = addResult.converted(to: UnitConcentrationMass.milligramsPerDeciliter).description
// You can even compare two different glucose values that are in different units. In this example however there is a `-0.200000000000022 mg/dL` difference between the two values."
var newWayDifference : String = "Nothing has been compared yet."
gvInMmol
gvInMgdL
if (gvInMgdL > gvInMmol) {
newWayDifference = "md/dL is greater than mmol/L"
} else if (gvInMgdL < gvInMmol ){
newWayDifference = "mmol/L is greater than md/dL"
} else {
newWayDifference = "They are the same"
}
newWayDifference
/*
Measurements have a dedicated formatter with the `MeasurementFormatter()` class.
This class translates units into appropriate languages and can shorten them when appropriate.
*/
let fmt = MeasurementFormatter()
fmt.unitOptions = [.naturalScale] // Natural scale allows the formatter to adjust the length of the units depending on the space.
let stringOfNumber = fmt.numberFormatter.string(from: gvInMmol.value)
print(fmt.string(from: gvInMmol.unit))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment