CurrentGlucoseView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import SwiftUI
  2. struct CurrentGlucoseView: View {
  3. @Binding var recentGlucose: BloodGlucose?
  4. @Binding var delta: Int?
  5. @Binding var units: GlucoseUnits
  6. private var glucoseFormatter: NumberFormatter {
  7. let formatter = NumberFormatter()
  8. formatter.numberStyle = .decimal
  9. formatter.maximumFractionDigits = 0
  10. if units == .mmolL {
  11. formatter.minimumFractionDigits = 1
  12. formatter.maximumFractionDigits = 1
  13. }
  14. return formatter
  15. }
  16. private var deltaFormatter: NumberFormatter {
  17. let formatter = NumberFormatter()
  18. formatter.numberStyle = .decimal
  19. formatter.maximumFractionDigits = 1
  20. formatter.positivePrefix = "+"
  21. return formatter
  22. }
  23. private var dateFormatter: DateFormatter {
  24. let formatter = DateFormatter()
  25. formatter.timeStyle = .short
  26. return formatter
  27. }
  28. var body: some View {
  29. VStack(alignment: .center, spacing: 6) {
  30. HStack(spacing: 8) {
  31. Text(
  32. recentGlucose?.glucose
  33. .map {
  34. glucoseFormatter
  35. .string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)! }
  36. ?? "--"
  37. )
  38. .font(.system(size: 24, weight: .bold))
  39. .fixedSize()
  40. .foregroundColor(colorOfGlucose)
  41. image.padding(.bottom, 2)
  42. }.padding(.leading, 4)
  43. HStack(alignment: .lastTextBaseline, spacing: 2) {
  44. Text(
  45. "\(minutesAgo)m "
  46. ).font(.caption2).foregroundColor(colorOfMinutesAgo(minutesAgo))
  47. Text(
  48. delta
  49. .map { deltaFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)!
  50. } ??
  51. "--"
  52. ).font(.system(size: 12, weight: .bold)) }
  53. }
  54. }
  55. var colorOfGlucose: Color {
  56. guard var recentBG = recentGlucose?.glucose
  57. else { return .loopYellow }
  58. recentBG = Int(recentBG.asMmolL) // convert to mmol/l for calculation
  59. switch recentBG {
  60. case 3 ... 4:
  61. return .loopOrange
  62. case 4 ... 7:
  63. return .loopGreen
  64. case 8 ... 9:
  65. return .loopYellow
  66. default:
  67. return .loopRed
  68. }
  69. }
  70. var minutesAgo: Int {
  71. let lastGlucoseDateString = recentGlucose.map { dateFormatter.string(from: $0.dateString) } ?? "--"
  72. let LastGlucoseDate = Date(lastGlucoseDateString) ?? Date()
  73. let now = Date()
  74. let diff = Int(now.timeIntervalSince1970 - LastGlucoseDate.timeIntervalSince1970)
  75. let hoursDiff = diff / 3600
  76. let minutesDiff = (diff - hoursDiff * 3600) / 60
  77. return minutesDiff
  78. }
  79. func colorOfMinutesAgo(_ minutes: Int) -> Color {
  80. switch minutes {
  81. case 0 ... 5:
  82. return .loopGreen
  83. case 6 ... 9:
  84. return .loopYellow
  85. default:
  86. return .loopRed
  87. }
  88. }
  89. var image: Image {
  90. guard let direction = recentGlucose?.direction else {
  91. return Image(systemName: "arrow.left.and.right")
  92. }
  93. switch direction {
  94. case .doubleUp,
  95. .singleUp,
  96. .tripleUp:
  97. return Image(systemName: "arrow.up")
  98. case .fortyFiveUp:
  99. return Image(systemName: "arrow.up.right")
  100. case .flat:
  101. return Image(systemName: "arrow.forward")
  102. case .fortyFiveDown:
  103. return Image(systemName: "arrow.down.forward")
  104. case .doubleDown,
  105. .singleDown,
  106. .tripleDown:
  107. return Image(systemName: "arrow.down")
  108. case .none,
  109. .notComputable,
  110. .rateOutOfRange:
  111. return Image(systemName: "arrow.left.and.right")
  112. }
  113. }
  114. }