CurrentGlucoseView.swift 2.8 KB

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