CurrentGlucoseView.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 = 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 latestGlucose: BloodGlucose?
  29. var colorOfGlucose: Color {
  30. var glucoseString =
  31. " \(recentGlucose?.glucose.map { glucoseFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)! })"
  32. var glucoseStringClipped = String(glucoseString.dropFirst(11)) // Drop first 11 characters
  33. var numberOfCharactersToDrop = 3
  34. if glucoseStringClipped.prefix(2).contains(",") {
  35. numberOfCharactersToDrop = 2
  36. } else {
  37. numberOfCharactersToDrop = 3
  38. }
  39. var glucoseStringTrimmed = String(glucoseStringClipped.dropLast(3)) // Drop last 3 characters
  40. print(glucoseStringTrimmed)
  41. switch glucoseStringTrimmed {
  42. case "10",
  43. "11",
  44. "12",
  45. "13",
  46. "14",
  47. "15",
  48. "16",
  49. "17",
  50. "18",
  51. "19",
  52. "20",
  53. "21",
  54. "22",
  55. "23",
  56. "24",
  57. "25":
  58. return .loopRed
  59. case "1,",
  60. "2,",
  61. "3,":
  62. return .loopRed
  63. case "4,",
  64. "5,",
  65. "6,",
  66. "7,":
  67. return .loopGreen
  68. case "8,",
  69. "9,":
  70. return .loopYellow
  71. default:
  72. return .white
  73. }
  74. }
  75. var body: some View {
  76. VStack(alignment: .center, spacing: 6) {
  77. HStack(spacing: 8) {
  78. Text(
  79. recentGlucose?.glucose
  80. .map {
  81. glucoseFormatter
  82. .string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)! }
  83. ?? "--"
  84. )
  85. .font(.system(size: 24, weight: .bold))
  86. .fixedSize()
  87. .foregroundColor(colorOfGlucose)
  88. image.padding(.bottom, 2)
  89. }.padding(.leading, 4)
  90. HStack(alignment: .lastTextBaseline, spacing: 2) {
  91. Text(
  92. recentGlucose.map { dateFormatter.string(from: $0.dateString) } ?? "--"
  93. ).font(.caption2).foregroundColor(.secondary)
  94. Text(
  95. delta
  96. .map { deltaFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)!
  97. } ??
  98. "--"
  99. ).font(.caption2).foregroundColor(.secondary)
  100. }
  101. }
  102. }
  103. var image: Image {
  104. guard let direction = recentGlucose?.direction else {
  105. return Image(systemName: "arrow.left.and.right")
  106. }
  107. switch direction {
  108. case .doubleUp,
  109. .singleUp,
  110. .tripleUp:
  111. return Image(systemName: "arrow.up")
  112. case .fortyFiveUp:
  113. return Image(systemName: "arrow.up.right")
  114. case .flat:
  115. return Image(systemName: "arrow.forward")
  116. case .fortyFiveDown:
  117. return Image(systemName: "arrow.down.forward")
  118. case .doubleDown,
  119. .singleDown,
  120. .tripleDown:
  121. return Image(systemName: "arrow.down")
  122. case .none,
  123. .notComputable,
  124. .rateOutOfRange:
  125. return Image(systemName: "arrow.left.and.right")
  126. }
  127. }
  128. }