CurrentGlucoseView.swift 3.9 KB

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