HomeRootView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. tempBasals: $viewModel.tempBasals,
  50. hours: .constant(viewModel.filteredHours),
  51. maxBasal: $viewModel.maxBasal,
  52. basalProfile: $viewModel.basalProfile,
  53. units: viewModel.units
  54. )
  55. ZStack {
  56. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  57. HStack {
  58. Button { viewModel.showModal(for: .addCarbs) }
  59. label: {
  60. Image(systemName: "circlebadge.2.fill")
  61. }.foregroundColor(.green)
  62. Spacer()
  63. Button { viewModel.showModal(for: .addTempTarget) }
  64. label: {
  65. Image(systemName: "target")
  66. }.foregroundColor(.green)
  67. Spacer()
  68. Button { viewModel.showModal(for: .bolus) }
  69. label: {
  70. Image(systemName: "drop.fill")
  71. }.foregroundColor(.orange)
  72. Spacer()
  73. if viewModel.allowManualTemp {
  74. Button { viewModel.showModal(for: .manualTempBasal) }
  75. label: {
  76. Image(systemName: "circle.bottomhalf.fill")
  77. }.foregroundColor(.blue)
  78. Spacer()
  79. }
  80. Button { viewModel.showModal(for: .settings) }
  81. label: {
  82. Image(systemName: "gearshape")
  83. }.foregroundColor(.gray)
  84. }
  85. .padding(.horizontal, 24)
  86. .padding(.bottom, geo.safeAreaInsets.bottom)
  87. }
  88. }
  89. .edgesIgnoringSafeArea(.bottom)
  90. }
  91. .navigationTitle("Home")
  92. .navigationBarHidden(true)
  93. .ignoresSafeArea(.keyboard)
  94. .popup(isPresented: isPopupPresented, alignment: .top, direction: .top) {
  95. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).padding().foregroundColor(.white)
  96. .background(
  97. RoundedRectangle(cornerRadius: 8, style: .continuous)
  98. .fill(Color(UIColor.darkGray))
  99. )
  100. .onTapGesture {
  101. isPopupPresented = false
  102. }
  103. }
  104. }
  105. }
  106. }