CurrentGlucoseView.swift 4.1 KB

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