HomeRootView.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. }.frame(minWidth: 0, maxWidth: .infinity)
  27. Spacer()
  28. CurrentGlucoseView(
  29. recentGlucose: $viewModel.recentGlucose,
  30. delta: $viewModel.glucoseDelta,
  31. units: viewModel.units
  32. ).frame(minWidth: 0, maxWidth: .infinity)
  33. Spacer()
  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. }.frame(minWidth: 0, maxWidth: .infinity)
  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(.vertical).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("carbs")
  71. .renderingMode(.template)
  72. .resizable()
  73. .frame(width: 24, height: 24)
  74. }.foregroundColor(.green)
  75. Spacer()
  76. Button { viewModel.showModal(for: .addTempTarget) }
  77. label: {
  78. Image("target")
  79. .renderingMode(.template)
  80. .resizable()
  81. .frame(width: 24, height: 24)
  82. }.foregroundColor(.green)
  83. Spacer()
  84. Button { viewModel.showModal(for: .bolus) }
  85. label: {
  86. Image("bolus")
  87. .renderingMode(.template)
  88. .resizable()
  89. .frame(width: 24, height: 24)
  90. }.foregroundColor(.orange)
  91. Spacer()
  92. if viewModel.allowManualTemp {
  93. Button { viewModel.showModal(for: .manualTempBasal) }
  94. label: {
  95. Image("bolus1")
  96. .renderingMode(.template)
  97. .resizable()
  98. .frame(width: 24, height: 24)
  99. }.foregroundColor(.blue)
  100. Spacer()
  101. }
  102. Button { viewModel.showModal(for: .settings) }
  103. label: {
  104. Image("settings1")
  105. .renderingMode(.template)
  106. .resizable()
  107. .frame(width: 24, height: 24)
  108. }.foregroundColor(.gray)
  109. }
  110. .padding(.horizontal, 24)
  111. .padding(.bottom, geo.safeAreaInsets.bottom)
  112. }
  113. }
  114. .edgesIgnoringSafeArea(.bottom)
  115. }
  116. .navigationTitle("Home")
  117. .navigationBarHidden(true)
  118. .ignoresSafeArea(.keyboard)
  119. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  120. VStack(alignment: .leading) {
  121. Text(viewModel.statusTitle).foregroundColor(.white)
  122. .padding(.bottom, 4)
  123. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  124. }
  125. .padding()
  126. .background(
  127. RoundedRectangle(cornerRadius: 8, style: .continuous)
  128. .fill(Color(UIColor.darkGray))
  129. )
  130. .onTapGesture {
  131. isStatusPopupPresented = false
  132. }
  133. }
  134. }
  135. }
  136. }