HomeRootView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. private var targetFormatter: NumberFormatter {
  14. let formatter = NumberFormatter()
  15. formatter.numberStyle = .decimal
  16. formatter.maximumFractionDigits = 1
  17. return formatter
  18. }
  19. var header: some View {
  20. HStack(alignment: .bottom) {
  21. Spacer()
  22. VStack(alignment: .leading, spacing: 12) {
  23. HStack {
  24. Text("IOB").font(.caption2).foregroundColor(.secondary)
  25. Text((numberFormatter.string(from: (viewModel.suggestion?.iob ?? 0) as NSNumber) ?? "0") + " U")
  26. .font(.system(size: 12, weight: .bold))
  27. }
  28. HStack {
  29. Text("COB").font(.caption2).foregroundColor(.secondary)
  30. Text((numberFormatter.string(from: (viewModel.suggestion?.cob ?? 0) as NSNumber) ?? "0") + " g")
  31. .font(.system(size: 12, weight: .bold))
  32. }
  33. }
  34. Spacer()
  35. CurrentGlucoseView(
  36. recentGlucose: $viewModel.recentGlucose,
  37. delta: $viewModel.glucoseDelta,
  38. units: viewModel.units
  39. )
  40. .onTapGesture {
  41. viewModel.openCGM()
  42. }
  43. Spacer()
  44. PumpView(
  45. reservoir: $viewModel.reservoir,
  46. battery: $viewModel.battery,
  47. name: $viewModel.pumpName,
  48. expiresAtDate: $viewModel.pumpExpiresAtDate,
  49. timerDate: $viewModel.timerDate
  50. )
  51. .onTapGesture {
  52. viewModel.setupPump = true
  53. }
  54. .popover(isPresented: $viewModel.setupPump) {
  55. if let pumpManager = viewModel.provider.apsManager.pumpManager {
  56. PumpConfig.PumpSettingsView(pumpManager: pumpManager, completionDelegate: viewModel)
  57. }
  58. }
  59. Spacer()
  60. LoopView(
  61. suggestion: $viewModel.suggestion,
  62. enactedSuggestion: $viewModel.enactedSuggestion,
  63. closedLoop: $viewModel.closedLoop,
  64. timerDate: $viewModel.timerDate,
  65. isLooping: $viewModel.isLooping,
  66. lastLoopDate: $viewModel.lastLoopDate
  67. ).onTapGesture {
  68. isStatusPopupPresented = true
  69. }.onLongPressGesture {
  70. viewModel.runLoop()
  71. }
  72. Spacer()
  73. }.frame(maxWidth: .infinity)
  74. }
  75. var infoPanal: some View {
  76. HStack(alignment: .firstTextBaseline) {
  77. if let tempRate = viewModel.tempRate {
  78. Text((numberFormatter.string(from: tempRate as NSNumber) ?? "0") + " U/hr")
  79. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  80. .padding(.leading, 8)
  81. }
  82. if let tepmTarget = viewModel.tempTarget {
  83. Text(tepmTarget.name).font(.caption).foregroundColor(.secondary)
  84. if viewModel.units == .mmolL {
  85. Text(
  86. targetFormatter
  87. .string(from: tepmTarget.targetBottom.asMmolL as NSNumber)!
  88. )
  89. .font(.caption)
  90. .foregroundColor(.secondary)
  91. if tepmTarget.targetBottom != tepmTarget.targetTop {
  92. Text("-").font(.caption)
  93. .foregroundColor(.secondary)
  94. Text(
  95. targetFormatter
  96. .string(from: tepmTarget.targetTop.asMmolL as NSNumber)! + " \(viewModel.units.rawValue)"
  97. )
  98. .font(.caption)
  99. .foregroundColor(.secondary)
  100. } else {
  101. Text(viewModel.units.rawValue).font(.caption)
  102. .foregroundColor(.secondary)
  103. }
  104. } else {
  105. Text(targetFormatter.string(from: tepmTarget.targetBottom as NSNumber)!)
  106. .font(.caption)
  107. .foregroundColor(.secondary)
  108. if tepmTarget.targetBottom != tepmTarget.targetTop {
  109. Text("-").font(.caption)
  110. .foregroundColor(.secondary)
  111. Text(targetFormatter.string(from: tepmTarget.targetTop as NSNumber)! + " \(viewModel.units.rawValue)")
  112. .font(.caption)
  113. .foregroundColor(.secondary)
  114. } else {
  115. Text(viewModel.units.rawValue).font(.caption)
  116. .foregroundColor(.secondary)
  117. }
  118. }
  119. }
  120. Spacer()
  121. }
  122. .frame(maxWidth: .infinity, maxHeight: 30)
  123. }
  124. var legendPanal: some View {
  125. HStack(alignment: .firstTextBaseline) {
  126. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  127. .padding(.leading, 8)
  128. Text("BG")
  129. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  130. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  131. .padding(.leading, 8)
  132. Text("IOB")
  133. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  134. Circle().fill(Color.zt).frame(width: 8, height: 8)
  135. .padding(.leading, 8)
  136. Text("ZT")
  137. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  138. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  139. .padding(.leading, 8)
  140. Text("COB")
  141. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  142. Circle().fill(Color.uam).frame(width: 8, height: 8)
  143. .padding(.leading, 8)
  144. Text("UAM")
  145. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  146. }
  147. .frame(maxWidth: .infinity, maxHeight: 30)
  148. }
  149. var body: some View {
  150. GeometryReader { geo in
  151. VStack(spacing: 0) {
  152. header
  153. .frame(maxHeight: 70)
  154. .padding(.top, geo.safeAreaInsets.top)
  155. .background(Color.gray.opacity(0.2))
  156. infoPanal
  157. MainChartView(
  158. glucose: $viewModel.glucose,
  159. suggestion: $viewModel.suggestion,
  160. tempBasals: $viewModel.tempBasals,
  161. boluses: $viewModel.boluses,
  162. hours: .constant(viewModel.filteredHours),
  163. maxBasal: $viewModel.maxBasal,
  164. basalProfile: $viewModel.basalProfile,
  165. tempTargets: $viewModel.tempTargets,
  166. carbs: $viewModel.carbs,
  167. units: viewModel.units
  168. )
  169. .padding(.bottom)
  170. legendPanal
  171. ZStack {
  172. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  173. HStack {
  174. Button { viewModel.showModal(for: .addCarbs) }
  175. label: {
  176. Image("carbs")
  177. .renderingMode(.template)
  178. .resizable()
  179. .frame(width: 24, height: 24)
  180. }.foregroundColor(.loopGreen)
  181. Spacer()
  182. Button { viewModel.showModal(for: .addTempTarget) }
  183. label: {
  184. Image("target")
  185. .renderingMode(.template)
  186. .resizable()
  187. .frame(width: 24, height: 24)
  188. }.foregroundColor(.loopYellow)
  189. Spacer()
  190. Button { viewModel.showModal(for: .bolus) }
  191. label: {
  192. Image("bolus")
  193. .renderingMode(.template)
  194. .resizable()
  195. .frame(width: 24, height: 24)
  196. }.foregroundColor(.insulin)
  197. Spacer()
  198. if viewModel.allowManualTemp {
  199. Button { viewModel.showModal(for: .manualTempBasal) }
  200. label: {
  201. Image("bolus1")
  202. .renderingMode(.template)
  203. .resizable()
  204. .frame(width: 24, height: 24)
  205. }.foregroundColor(.insulin)
  206. Spacer()
  207. }
  208. Button { viewModel.showModal(for: .settings) }
  209. label: {
  210. Image("settings1")
  211. .renderingMode(.template)
  212. .resizable()
  213. .frame(width: 24, height: 24)
  214. }.foregroundColor(.loopGray)
  215. }
  216. .padding(.horizontal, 24)
  217. .padding(.bottom, geo.safeAreaInsets.bottom)
  218. }
  219. }
  220. .edgesIgnoringSafeArea(.vertical)
  221. }
  222. .navigationTitle("Home")
  223. .navigationBarHidden(true)
  224. .ignoresSafeArea(.keyboard)
  225. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  226. VStack(alignment: .leading) {
  227. Text(viewModel.statusTitle).foregroundColor(.white)
  228. .padding(.bottom, 4)
  229. Text(viewModel.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
  230. }
  231. .padding()
  232. .background(
  233. RoundedRectangle(cornerRadius: 8, style: .continuous)
  234. .fill(Color(UIColor.darkGray))
  235. )
  236. .onTapGesture {
  237. isStatusPopupPresented = false
  238. }
  239. .gesture(
  240. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  241. .onEnded { value in
  242. if value.translation.height < 0 {
  243. isStatusPopupPresented = false
  244. }
  245. }
  246. )
  247. }
  248. }
  249. }
  250. }