CurrentGlucoseView.swift 4.0 KB

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