HomeRootView.swift 4.8 KB

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