CurrentGlucoseView.swift 2.8 KB

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