HKUnit+Extensions.swift 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // HKUnit+Extensions.swift
  3. // LoopFollow
  4. //
  5. // Created by Jonas Björkert on 2024-07-15.
  6. // Copyright © 2024 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. import HealthKit
  10. extension HKUnit {
  11. public static let milligramsPerDeciliter: HKUnit = {
  12. return HKUnit.gramUnit(with: .milli).unitDivided(by: .literUnit(with: .deci))
  13. }()
  14. public static let millimolesPerLiter: HKUnit = {
  15. return HKUnit.moleUnit(with: .milli, molarMass: HKUnitMolarMassBloodGlucose).unitDivided(by: .liter())
  16. }()
  17. var preferredFractionDigits: Int {
  18. if self == .milligramsPerDeciliter {
  19. return 0
  20. } else {
  21. return 1
  22. }
  23. }
  24. var localizedShortUnitString: String {
  25. if self == HKUnit.millimolesPerLiter {
  26. return NSLocalizedString("mmol/L", comment: "The short unit display string for millimoles of glucose per liter")
  27. } else if self == .milligramsPerDeciliter {
  28. return NSLocalizedString("mg/dL", comment: "The short unit display string for milligrams of glucose per decilter")
  29. } else if self == .internationalUnit() {
  30. return NSLocalizedString("U", comment: "The short unit display string for international units of insulin")
  31. } else if self == .gram() {
  32. return NSLocalizedString("g", comment: "The short unit display string for grams")
  33. } else {
  34. return String(describing: self)
  35. }
  36. }
  37. }