LiveActivityAttributes+Helper.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import Foundation
  2. extension LiveActivityAttributes.ContentState {
  3. static func formatGlucose(_ value: Int, mmol: Bool, forceSign: Bool) -> String {
  4. let formatter = NumberFormatter()
  5. formatter.numberStyle = .decimal
  6. formatter.maximumFractionDigits = 0
  7. if mmol {
  8. formatter.minimumFractionDigits = 1
  9. formatter.maximumFractionDigits = 1
  10. }
  11. if forceSign {
  12. formatter.positivePrefix = formatter.plusSign
  13. }
  14. formatter.roundingMode = .halfUp
  15. return formatter
  16. .string(from: mmol ? value.asMmolL as NSNumber : NSNumber(value: value))!
  17. }
  18. static func calculateChange(chart: [GlucoseData]) -> String {
  19. guard chart.count > 2 else { return "" }
  20. let lastGlucose = chart.first?.glucose ?? 0
  21. let secondLastGlucose = chart.dropFirst().first?.glucose ?? 0
  22. let delta = lastGlucose - secondLastGlucose
  23. let deltaAsDecimal = Decimal(delta)
  24. let formatter = NumberFormatter()
  25. formatter.numberStyle = .decimal
  26. formatter.maximumFractionDigits = 1
  27. formatter.positivePrefix = " +"
  28. formatter.negativePrefix = " -"
  29. return formatter.string(from: deltaAsDecimal as NSNumber) ?? "--"
  30. }
  31. init?(
  32. new bg: GlucoseData,
  33. prev _: GlucoseData?,
  34. mmol: Bool,
  35. chart: [GlucoseData],
  36. settings: FreeAPSSettings,
  37. determination: DeterminationData?
  38. ) {
  39. let glucose = bg.glucose
  40. let formattedBG = Self.formatGlucose(Int(glucose), mmol: mmol, forceSign: false)
  41. var rotationDegrees: Double = 0.0
  42. switch bg.direction {
  43. case .doubleUp,
  44. .singleUp,
  45. .tripleUp:
  46. rotationDegrees = -90
  47. case .fortyFiveUp:
  48. rotationDegrees = -45
  49. case .flat:
  50. rotationDegrees = 0
  51. case .fortyFiveDown:
  52. rotationDegrees = 45
  53. case .doubleDown,
  54. .singleDown,
  55. .tripleDown:
  56. rotationDegrees = 90
  57. case nil,
  58. .notComputable,
  59. .rateOutOfRange:
  60. rotationDegrees = 0
  61. default:
  62. rotationDegrees = 0
  63. }
  64. let trendString = bg.direction?.symbol as? String
  65. let change = Self.calculateChange(chart: chart)
  66. let chartBG = chart.map(\.glucose)
  67. let conversionFactor: Double = settings.units == .mmolL ? 18.0 : 1.0
  68. let convertedChartBG = chartBG.map { Double($0) / conversionFactor }
  69. let chartDate = chart.map(\.date)
  70. /// glucose limits from UI settings, not from notifications settings
  71. let highGlucose = settings.high / Decimal(conversionFactor)
  72. let lowGlucose = settings.low / Decimal(conversionFactor)
  73. let cob = determination?.cob ?? 0
  74. let iob = determination?.iob ?? 0
  75. let lockScreenView = settings.lockScreenView.displayName
  76. let unit = settings.units == .mmolL ? " mmol/L" : " mg/dL"
  77. self.init(
  78. bg: formattedBG,
  79. direction: trendString,
  80. change: change,
  81. date: bg.date,
  82. chart: convertedChartBG,
  83. chartDate: chartDate,
  84. rotationDegrees: rotationDegrees,
  85. highGlucose: Double(highGlucose),
  86. lowGlucose: Double(lowGlucose),
  87. cob: Decimal(cob),
  88. iob: iob as Decimal,
  89. lockScreenView: lockScreenView,
  90. unit: unit
  91. )
  92. }
  93. }