CurrentGlucoseView.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. formatter.roundingMode = .halfUp
  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. image.padding(.bottom, 2)
  42. }.padding(.leading, 4)
  43. HStack(alignment: .lastTextBaseline, spacing: 2) {
  44. Text(
  45. recentGlucose.map { dateFormatter.string(from: $0.dateString) } ?? "--"
  46. ).font(.caption2).foregroundColor(.secondary)
  47. Text(
  48. delta
  49. .map { deltaFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)!
  50. } ??
  51. "--"
  52. ).font(.system(size: 12, weight: .bold))
  53. }
  54. }
  55. }
  56. var image: Image {
  57. guard let direction = recentGlucose?.direction else {
  58. return Image(systemName: "arrow.left.and.right")
  59. }
  60. switch direction {
  61. case .doubleUp,
  62. .singleUp,
  63. .tripleUp:
  64. return Image(systemName: "arrow.up")
  65. case .fortyFiveUp:
  66. return Image(systemName: "arrow.up.right")
  67. case .flat:
  68. return Image(systemName: "arrow.forward")
  69. case .fortyFiveDown:
  70. return Image(systemName: "arrow.down.forward")
  71. case .doubleDown,
  72. .singleDown,
  73. .tripleDown:
  74. return Image(systemName: "arrow.down")
  75. case .none,
  76. .notComputable,
  77. .rateOutOfRange:
  78. return Image(systemName: "arrow.left.and.right")
  79. }
  80. }
  81. }