HomeRootView.swift 4.7 KB

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