HomeRootView.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. formatter.maximumFractionDigits = 2
  11. return formatter
  12. }
  13. var header: some View {
  14. HStack(alignment: .bottom) {
  15. VStack(alignment: .leading, spacing: 12) {
  16. HStack {
  17. Text("IOB").font(.caption2).foregroundColor(.secondary)
  18. Text((numberFormatter.string(from: (viewModel.suggestion?.iob ?? 0) as NSNumber) ?? "0") + " U")
  19. .font(.system(size: 12, weight: .bold))
  20. }
  21. HStack {
  22. Text("COB").font(.caption2).foregroundColor(.secondary)
  23. Text((numberFormatter.string(from: (viewModel.suggestion?.cob ?? 0) as NSNumber) ?? "0") + " g")
  24. .font(.system(size: 12, weight: .bold))
  25. }
  26. }
  27. .padding(.leading, 4)
  28. Spacer()
  29. CurrentGlucoseView(
  30. recentGlucose: $viewModel.recentGlucose,
  31. delta: $viewModel.glucoseDelta,
  32. units: viewModel.units
  33. )
  34. Spacer()
  35. PumpView(
  36. reservoir: $viewModel.reservoir,
  37. battery: $viewModel.battery,
  38. name: $viewModel.pumpName,
  39. expiresAtDate: $viewModel.pumpExpiresAtDate,
  40. timerDate: $viewModel.timerDate
  41. )
  42. Spacer()
  43. LoopView(
  44. suggestion: $viewModel.suggestion,
  45. enactedSuggestion: $viewModel.enactedSuggestion,
  46. closedLoop: $viewModel.closedLoop,
  47. timerDate: $viewModel.timerDate,
  48. isLooping: $viewModel.isLooping,
  49. lastLoopDate: $viewModel.lastLoopDate
  50. ).onTapGesture {
  51. isStatusPopupPresented = true
  52. }.onLongPressGesture {
  53. viewModel.runLoop()
  54. }
  55. }.frame(maxWidth: .infinity)
  56. }
  57. var infoPanal: some View {
  58. HStack(alignment: .firstTextBaseline) {
  59. if let tempRate = viewModel.tempRate {
  60. Text((numberFormatter.string(from: tempRate as NSNumber) ?? "0") + " U/hr")
  61. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  62. .padding(.leading, 4)
  63. }
  64. if let tepmTargetName = viewModel.tempTargetName {
  65. Text(tepmTargetName).font(.caption).foregroundColor(.secondary)
  66. }
  67. Spacer()
  68. }
  69. .frame(maxWidth: .infinity, maxHeight: 30)
  70. }
  71. var body: some View {
  72. GeometryReader { geo in
  73. VStack(spacing: 0) {
  74. header
  75. .frame(maxHeight: 70)
  76. .padding(.top, geo.safeAreaInsets.top)
  77. .background(Color.gray.opacity(0.2))
  78. infoPanal
  79. MainChartView(
  80. glucose: $viewModel.glucose,
  81. suggestion: $viewModel.suggestion,
  82. tempBasals: $viewModel.tempBasals,
  83. boluses: $viewModel.boluses,
  84. hours: .constant(viewModel.filteredHours),
  85. maxBasal: $viewModel.maxBasal,
  86. basalProfile: $viewModel.basalProfile,
  87. tempTargets: $viewModel.tempTargets,
  88. carbs: $viewModel.carbs,
  89. units: viewModel.units
  90. )
  91. .padding(.bottom)
  92. ZStack {
  93. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  94. HStack {
  95. Button { viewModel.showModal(for: .addCarbs) }
  96. label: {
  97. Image("carbs")
  98. .renderingMode(.template)
  99. .resizable()
  100. .frame(width: 24, height: 24)
  101. }.foregroundColor(.loopGreen)
  102. Spacer()
  103. Button { viewModel.showModal(for: .addTempTarget) }
  104. label: {
  105. Image("target")
  106. .renderingMode(.template)
  107. .resizable()
  108. .frame(width: 24, height: 24)
  109. }.foregroundColor(.loopYellow)
  110. Spacer()
  111. Button { viewModel.showModal(for: .bolus) }
  112. label: {
  113. Image("bolus")
  114. .renderingMode(.template)
  115. .resizable()
  116. .frame(width: 24, height: 24)
  117. }.foregroundColor(.insulin)
  118. Spacer()
  119. if viewModel.allowManualTemp {
  120. Button { viewModel.showModal(for: .manualTempBasal) }
  121. label: {
  122. Image("bolus1")
  123. .renderingMode(.template)
  124. .resizable()
  125. .frame(width: 24, height: 24)
  126. }.foregroundColor(.insulin)
  127. Spacer()
  128. }
  129. Button { viewModel.showModal(for: .settings) }
  130. label: {
  131. Image("settings1")
  132. .renderingMode(.template)
  133. .resizable()
  134. .frame(width: 24, height: 24)
  135. }.foregroundColor(.loopGray)
  136. }
  137. .padding(.horizontal, 24)
  138. .padding(.bottom, geo.safeAreaInsets.bottom)
  139. }
  140. }
  141. .edgesIgnoringSafeArea(.vertical)
  142. }
  143. .navigationTitle("Home")
  144. .navigationBarHidden(true)
  145. .ignoresSafeArea(.keyboard)
  146. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  147. VStack(alignment: .leading) {
  148. Text(viewModel.statusTitle).foregroundColor(.white)
  149. .padding(.bottom, 4)
  150. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  151. }
  152. .padding()
  153. .background(
  154. RoundedRectangle(cornerRadius: 8, style: .continuous)
  155. .fill(Color(UIColor.darkGray))
  156. )
  157. .onTapGesture {
  158. isStatusPopupPresented = false
  159. }
  160. .gesture(
  161. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  162. .onEnded { value in
  163. if value.translation.height < 0 {
  164. isStatusPopupPresented = false
  165. }
  166. }
  167. )
  168. }
  169. }
  170. }
  171. }