HomeRootView.swift 14 KB

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