CurrentGlucoseView.swift 3.0 KB

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