HomeRootView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import SwiftDate
  2. import SwiftUI
  3. extension Home {
  4. struct RootView: BaseView {
  5. @EnvironmentObject var viewModel: ViewModel<Provider>
  6. @State var isPopupPresented = false
  7. private var numberFormatter: NumberFormatter {
  8. let formatter = NumberFormatter()
  9. formatter.numberStyle = .decimal
  10. return formatter
  11. }
  12. var header: some View {
  13. HStack {
  14. VStack(alignment: .leading) {
  15. HStack {
  16. Text("IOB").font(.caption)
  17. Text((numberFormatter.string(from: (viewModel.suggestion?.iob ?? 0) as NSNumber) ?? "0") + " U")
  18. .font(.caption2)
  19. }.padding(.top, 16)
  20. Spacer()
  21. HStack {
  22. Text("COB").font(.caption)
  23. Text((numberFormatter.string(from: (viewModel.suggestion?.cob ?? 0) as NSNumber) ?? "0") + " g")
  24. .font(.caption2)
  25. }
  26. }
  27. Spacer()
  28. CurrentGlucoseView(
  29. recentGlucose: $viewModel.recentGlucose,
  30. delta: $viewModel.glucoseDelta,
  31. units: viewModel.units
  32. )
  33. .padding(.horizontal)
  34. LoopView(suggestion: $viewModel.suggestion).onTapGesture {
  35. isPopupPresented = true
  36. }.onLongPressGesture {
  37. viewModel.runLoop()
  38. }
  39. }.frame(maxWidth: .infinity)
  40. }
  41. var body: some View {
  42. viewModel.setFilteredGlucoseHours(hours: 24)
  43. return GeometryReader { geo in
  44. VStack {
  45. header.padding().frame(maxHeight: 70)
  46. MainChartView(
  47. glucose: $viewModel.glucose,
  48. suggestion: $viewModel.suggestion,
  49. basals: $viewModel.basals,
  50. hours: .constant(viewModel.filteredHours),
  51. maxBasal: $viewModel.maxBasal,
  52. units: viewModel.units
  53. )
  54. ZStack {
  55. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  56. HStack {
  57. Button { viewModel.showModal(for: .addCarbs) }
  58. label: {
  59. Image(systemName: "circlebadge.2.fill")
  60. }.foregroundColor(.green)
  61. Spacer()
  62. Button { viewModel.showModal(for: .addTempTarget) }
  63. label: {
  64. Image(systemName: "target")
  65. }.foregroundColor(.green)
  66. Spacer()
  67. Button { viewModel.showModal(for: .bolus) }
  68. label: {
  69. Image(systemName: "drop.fill")
  70. }.foregroundColor(.orange)
  71. Spacer()
  72. if viewModel.allowManualTemp {
  73. Button { viewModel.showModal(for: .manualTempBasal) }
  74. label: {
  75. Image(systemName: "circle.bottomhalf.fill")
  76. }.foregroundColor(.blue)
  77. Spacer()
  78. }
  79. Button { viewModel.showModal(for: .settings) }
  80. label: {
  81. Image(systemName: "gearshape")
  82. }.foregroundColor(.gray)
  83. }
  84. .padding(.horizontal, 24)
  85. .padding(.bottom, geo.safeAreaInsets.bottom)
  86. }
  87. }
  88. .edgesIgnoringSafeArea(.bottom)
  89. }
  90. .navigationTitle("Home")
  91. .navigationBarHidden(true)
  92. .ignoresSafeArea(.keyboard)
  93. .popup(isPresented: isPopupPresented, alignment: .top, direction: .top) {
  94. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).padding().foregroundColor(.white)
  95. .background(
  96. RoundedRectangle(cornerRadius: 8, style: .continuous)
  97. .fill(Color(UIColor.darkGray))
  98. )
  99. .onTapGesture {
  100. isPopupPresented = false
  101. }
  102. }
  103. }
  104. }
  105. }