HomeRootView.swift 5.3 KB

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