CurrentGlucoseView.swift 4.0 KB

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