HomeRootView.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import SwiftDate
  2. import SwiftUI
  3. extension Home {
  4. struct RootView: BaseView {
  5. @EnvironmentObject var viewModel: ViewModel<Provider>
  6. @State var isStatusPopupPresented = 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(
  35. suggestion: $viewModel.suggestion,
  36. enactedSuggestion: $viewModel.enactedSuggestion,
  37. closedLoop: $viewModel.closedLoop,
  38. timerDate: $viewModel.timerDate,
  39. isLooping: $viewModel.isLooping,
  40. lastLoopDate: $viewModel.lastLoopDate
  41. ).onTapGesture {
  42. isStatusPopupPresented = true
  43. }.onLongPressGesture {
  44. viewModel.runLoop()
  45. }
  46. }.frame(maxWidth: .infinity)
  47. }
  48. var body: some View {
  49. viewModel.setFilteredGlucoseHours(hours: 24)
  50. return GeometryReader { geo in
  51. VStack {
  52. header.padding().frame(maxHeight: 70)
  53. MainChartView(
  54. glucose: $viewModel.glucose,
  55. suggestion: $viewModel.suggestion,
  56. tempBasals: $viewModel.tempBasals,
  57. boluses: $viewModel.boluses,
  58. hours: .constant(viewModel.filteredHours),
  59. maxBasal: $viewModel.maxBasal,
  60. basalProfile: $viewModel.basalProfile,
  61. tempTargets: $viewModel.tempTargets,
  62. carbs: $viewModel.carbs,
  63. units: viewModel.units
  64. )
  65. ZStack {
  66. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  67. HStack {
  68. Button { viewModel.showModal(for: .addCarbs) }
  69. label: {
  70. Image(systemName: "circlebadge.2.fill")
  71. }.foregroundColor(.green)
  72. Spacer()
  73. Button { viewModel.showModal(for: .addTempTarget) }
  74. label: {
  75. Image(systemName: "target")
  76. }.foregroundColor(.green)
  77. Spacer()
  78. Button { viewModel.showModal(for: .bolus) }
  79. label: {
  80. Image(systemName: "drop.fill")
  81. }.foregroundColor(.orange)
  82. Spacer()
  83. if viewModel.allowManualTemp {
  84. Button { viewModel.showModal(for: .manualTempBasal) }
  85. label: {
  86. Image(systemName: "circle.bottomhalf.fill")
  87. }.foregroundColor(.blue)
  88. Spacer()
  89. }
  90. Button { viewModel.showModal(for: .settings) }
  91. label: {
  92. Image(systemName: "gearshape")
  93. }.foregroundColor(.gray)
  94. }
  95. .padding(.horizontal, 24)
  96. .padding(.bottom, geo.safeAreaInsets.bottom)
  97. }
  98. }
  99. .edgesIgnoringSafeArea(.bottom)
  100. }
  101. .navigationTitle("Home")
  102. .navigationBarHidden(true)
  103. .ignoresSafeArea(.keyboard)
  104. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  105. VStack(alignment: .leading) {
  106. Text(viewModel.statusTitle).foregroundColor(.white)
  107. .padding(.bottom, 4)
  108. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  109. }
  110. .padding()
  111. .background(
  112. RoundedRectangle(cornerRadius: 8, style: .continuous)
  113. .fill(Color(UIColor.darkGray))
  114. )
  115. .onTapGesture {
  116. isStatusPopupPresented = false
  117. }
  118. }
  119. }
  120. }
  121. }