CurrentGlucoseView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import SwiftUI
  2. struct CurrentGlucoseView: View {
  3. @Binding var recentGlucose: BloodGlucose?
  4. @Binding var delta: Int?
  5. @Binding var units: GlucoseUnits
  6. @Binding var alarm: GlucoseAlarm?
  7. private var glucoseFormatter: NumberFormatter {
  8. let formatter = NumberFormatter()
  9. formatter.numberStyle = .decimal
  10. formatter.maximumFractionDigits = 0
  11. if units == .mmolL {
  12. formatter.minimumFractionDigits = 1
  13. formatter.maximumFractionDigits = 1
  14. }
  15. formatter.roundingMode = .halfUp
  16. return formatter
  17. }
  18. private var deltaFormatter: NumberFormatter {
  19. let formatter = NumberFormatter()
  20. formatter.numberStyle = .decimal
  21. formatter.maximumFractionDigits = 1
  22. formatter.positivePrefix = " +"
  23. formatter.negativePrefix = " -"
  24. return formatter
  25. }
  26. private var timaAgoFormatter: NumberFormatter {
  27. let formatter = NumberFormatter()
  28. formatter.numberStyle = .decimal
  29. formatter.maximumFractionDigits = 0
  30. formatter.negativePrefix = ""
  31. return formatter
  32. }
  33. private var dateFormatter: DateFormatter {
  34. let formatter = DateFormatter()
  35. formatter.timeStyle = .short
  36. return formatter
  37. }
  38. var body: some View {
  39. VStack(alignment: .center) {
  40. HStack {
  41. Text(
  42. recentGlucose?.glucose
  43. .map {
  44. glucoseFormatter
  45. .string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)! }
  46. ?? "--"
  47. )
  48. .font(.title).fontWeight(.bold)
  49. .foregroundColor(alarm == nil ? colorOfGlucose : .loopRed)
  50. image
  51. }
  52. HStack {
  53. let minutes = (recentGlucose?.dateString.timeIntervalSinceNow ?? 0) / 60
  54. let text = timaAgoFormatter.string(for: Double(minutes)) ?? ""
  55. Text(
  56. text == "0" ? "< 1 " + NSLocalizedString("min", comment: "Short form for minutes") : (
  57. text + " " +
  58. NSLocalizedString("min", comment: "Short form for minutes") + " "
  59. )
  60. )
  61. .font(.caption2).foregroundColor(.secondary)
  62. Text(
  63. delta
  64. .map {
  65. deltaFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)!
  66. } ?? "--"
  67. )
  68. .font(.caption2).foregroundColor(.secondary)
  69. }.frame(alignment: .top)
  70. }
  71. }
  72. var image: Image {
  73. guard let direction = recentGlucose?.direction else {
  74. return Image(systemName: "arrow.left.and.right")
  75. }
  76. switch direction {
  77. case .doubleUp,
  78. .singleUp,
  79. .tripleUp:
  80. return Image(systemName: "arrow.up")
  81. case .fortyFiveUp:
  82. return Image(systemName: "arrow.up.right")
  83. case .flat:
  84. return Image(systemName: "arrow.forward")
  85. case .fortyFiveDown:
  86. return Image(systemName: "arrow.down.forward")
  87. case .doubleDown,
  88. .singleDown,
  89. .tripleDown:
  90. return Image(systemName: "arrow.down")
  91. case .none,
  92. .notComputable,
  93. .rateOutOfRange:
  94. return Image(systemName: "arrow.left.and.right")
  95. }
  96. }
  97. var colorOfGlucose: Color {
  98. let whichGlucose = recentGlucose?.glucose ?? 0
  99. switch whichGlucose {
  100. case 71 ... 145:
  101. return .loopGreen
  102. case 1 ... 55,
  103. 217...:
  104. return .loopRed
  105. case 56 ... 70,
  106. 146 ... 216:
  107. return .loopYellow
  108. default:
  109. return .primary
  110. }
  111. }
  112. }