CurrentGlucoseView.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 eventualBG: Int?
  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. return formatter
  16. }
  17. private var deltaFormatter: NumberFormatter {
  18. let formatter = NumberFormatter()
  19. formatter.numberStyle = .decimal
  20. formatter.maximumFractionDigits = 2
  21. formatter.positivePrefix = "+"
  22. return formatter
  23. }
  24. private var dateFormatter: DateFormatter {
  25. let formatter = DateFormatter()
  26. formatter.timeStyle = .short
  27. return formatter
  28. }
  29. var body: some View {
  30. VStack(alignment: .center, spacing: 6) {
  31. HStack(spacing: 8) {
  32. Text(
  33. recentGlucose?.glucose
  34. .map {
  35. glucoseFormatter
  36. .string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)! }
  37. ?? "--"
  38. )
  39. .font(.system(size: 24, weight: .bold))
  40. .fixedSize()
  41. VStack {
  42. image
  43. if let eventualBG = eventualBG {
  44. if units == .mmolL {
  45. Text(
  46. glucoseFormatter
  47. .string(from: Decimal(eventualBG).asMmolL as NSNumber)!
  48. )
  49. .font(.system(size: 10, weight: .regular)).foregroundColor(.secondary)
  50. } else {
  51. Text("\(eventualBG)").font(.system(size: 10, weight: .regular)).foregroundColor(.secondary)
  52. }
  53. }
  54. }
  55. }.padding(.leading, 4)
  56. HStack(alignment: .lastTextBaseline, spacing: 2) {
  57. Text(
  58. recentGlucose.map { dateFormatter.string(from: $0.dateString) } ?? "--"
  59. ).font(.caption2).foregroundColor(.secondary)
  60. Text(
  61. delta
  62. .map { deltaFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)!
  63. } ??
  64. "--"
  65. ).font(.system(size: 12, weight: .bold))
  66. }
  67. }
  68. }
  69. var image: Image {
  70. guard let direction = recentGlucose?.direction else {
  71. return Image(systemName: "arrow.left.and.right")
  72. }
  73. switch direction {
  74. case .doubleUp,
  75. .singleUp,
  76. .tripleUp:
  77. return Image(systemName: "arrow.up")
  78. case .fortyFiveUp:
  79. return Image(systemName: "arrow.up.right")
  80. case .flat:
  81. return Image(systemName: "arrow.forward")
  82. case .fortyFiveDown:
  83. return Image(systemName: "arrow.down.forward")
  84. case .doubleDown,
  85. .singleDown,
  86. .tripleDown:
  87. return Image(systemName: "arrow.down")
  88. case .none,
  89. .notComputable,
  90. .rateOutOfRange:
  91. return Image(systemName: "arrow.left.and.right")
  92. }
  93. }
  94. }