HomeRootView.swift 14 KB

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