HomeRootView.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. import SpriteKit
  2. import SwiftDate
  3. import SwiftUI
  4. import Swinject
  5. extension Home {
  6. struct RootView: BaseView {
  7. let resolver: Resolver
  8. @StateObject var state = StateModel()
  9. @State var isStatusPopupPresented = false
  10. private var numberFormatter: NumberFormatter {
  11. let formatter = NumberFormatter()
  12. formatter.numberStyle = .decimal
  13. formatter.maximumFractionDigits = 2
  14. return formatter
  15. }
  16. private var targetFormatter: NumberFormatter {
  17. let formatter = NumberFormatter()
  18. formatter.numberStyle = .decimal
  19. formatter.maximumFractionDigits = 1
  20. return formatter
  21. }
  22. private var dateFormatter: DateFormatter {
  23. let dateFormatter = DateFormatter()
  24. dateFormatter.timeStyle = .short
  25. return dateFormatter
  26. }
  27. private var spriteScene: SKScene {
  28. let scene = SnowScene()
  29. scene.scaleMode = .resizeFill
  30. scene.backgroundColor = .clear
  31. return scene
  32. }
  33. @ViewBuilder func header(_ geo: GeometryProxy) -> some View {
  34. HStack(alignment: .bottom) {
  35. Spacer()
  36. cobIobView
  37. Spacer()
  38. glucoseView
  39. Spacer()
  40. pumpView
  41. Spacer()
  42. loopView
  43. Spacer()
  44. }
  45. .frame(maxWidth: .infinity)
  46. .frame(maxHeight: 70)
  47. .padding(.top, geo.safeAreaInsets.top)
  48. .background(Color.gray.opacity(0.2))
  49. }
  50. var cobIobView: some View {
  51. VStack(alignment: .leading, spacing: 12) {
  52. HStack {
  53. Text("IOB").font(.caption2).foregroundColor(.secondary)
  54. Text(
  55. (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
  56. NSLocalizedString(" U", comment: "Insulin unit")
  57. )
  58. .font(.system(size: 12, weight: .bold))
  59. }
  60. HStack {
  61. Text("COB").font(.caption2).foregroundColor(.secondary)
  62. Text(
  63. (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
  64. NSLocalizedString(" g", comment: "gram of carbs")
  65. )
  66. .font(.system(size: 12, weight: .bold))
  67. }
  68. }
  69. }
  70. var glucoseView: some View {
  71. CurrentGlucoseView(
  72. recentGlucose: $state.recentGlucose,
  73. delta: $state.glucoseDelta,
  74. units: $state.units,
  75. alarm: $state.alarm
  76. )
  77. .onTapGesture {
  78. if state.alarm == nil {
  79. state.openCGM()
  80. } else {
  81. state.showModal(for: .snooze)
  82. }
  83. }
  84. .onLongPressGesture {
  85. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  86. impactHeavy.impactOccurred()
  87. if state.alarm == nil {
  88. state.showModal(for: .snooze)
  89. } else {
  90. state.openCGM()
  91. }
  92. }
  93. }
  94. var pumpView: some View {
  95. PumpView(
  96. reservoir: $state.reservoir,
  97. battery: $state.battery,
  98. name: $state.pumpName,
  99. expiresAtDate: $state.pumpExpiresAtDate,
  100. timerDate: $state.timerDate
  101. )
  102. .onTapGesture {
  103. if state.pumpDisplayState != nil {
  104. state.setupPump = true
  105. }
  106. }
  107. }
  108. var loopView: some View {
  109. LoopView(
  110. suggestion: $state.suggestion,
  111. enactedSuggestion: $state.enactedSuggestion,
  112. closedLoop: $state.closedLoop,
  113. timerDate: $state.timerDate,
  114. isLooping: $state.isLooping,
  115. lastLoopDate: $state.lastLoopDate,
  116. manualTempBasal: $state.manualTempBasal
  117. ).onTapGesture {
  118. isStatusPopupPresented = true
  119. }.onLongPressGesture {
  120. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  121. impactHeavy.impactOccurred()
  122. state.runLoop()
  123. }
  124. }
  125. var infoPanel: some View {
  126. HStack(alignment: .center) {
  127. if state.pumpSuspended {
  128. Text("Pump suspended")
  129. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGray)
  130. .padding(.leading, 8)
  131. } else if let tempRate = state.tempRate {
  132. if state.apsManager.isManualTempBasal {
  133. Text(
  134. (numberFormatter.string(from: tempRate as NSNumber) ?? "0") +
  135. NSLocalizedString(" U/hr", comment: "Unit per hour with space") +
  136. NSLocalizedString(" - Manual Basal ⚠️", comment: "Manual Temp basal")
  137. )
  138. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  139. .padding(.leading, 8)
  140. } else {
  141. Text(
  142. (numberFormatter.string(from: tempRate as NSNumber) ?? "0") +
  143. NSLocalizedString(" U/hr", comment: "Unit per hour with space")
  144. )
  145. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  146. .padding(.leading, 8)
  147. }
  148. }
  149. if let tempTarget = state.tempTarget {
  150. Text(tempTarget.displayName).font(.caption).foregroundColor(.secondary)
  151. if state.units == .mmolL {
  152. Text(
  153. targetFormatter
  154. .string(from: (tempTarget.targetBottom?.asMmolL ?? 0) as NSNumber)!
  155. )
  156. .font(.caption)
  157. .foregroundColor(.secondary)
  158. if tempTarget.targetBottom != tempTarget.targetTop {
  159. Text("-").font(.caption)
  160. .foregroundColor(.secondary)
  161. Text(
  162. targetFormatter
  163. .string(from: (tempTarget.targetTop?.asMmolL ?? 0) as NSNumber)! +
  164. " \(state.units.rawValue)"
  165. )
  166. .font(.caption)
  167. .foregroundColor(.secondary)
  168. } else {
  169. Text(state.units.rawValue).font(.caption)
  170. .foregroundColor(.secondary)
  171. }
  172. } else {
  173. Text(targetFormatter.string(from: (tempTarget.targetBottom ?? 0) as NSNumber)!)
  174. .font(.caption)
  175. .foregroundColor(.secondary)
  176. if tempTarget.targetBottom != tempTarget.targetTop {
  177. Text("-").font(.caption)
  178. .foregroundColor(.secondary)
  179. Text(
  180. targetFormatter
  181. .string(from: (tempTarget.targetTop ?? 0) as NSNumber)! + " \(state.units.rawValue)"
  182. )
  183. .font(.caption)
  184. .foregroundColor(.secondary)
  185. } else {
  186. Text(state.units.rawValue).font(.caption)
  187. .foregroundColor(.secondary)
  188. }
  189. }
  190. }
  191. Spacer()
  192. if let progress = state.bolusProgress {
  193. Text("Bolusing")
  194. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  195. ProgressView(value: Double(progress))
  196. .progressViewStyle(BolusProgressViewStyle())
  197. .padding(.trailing, 8)
  198. .onTapGesture {
  199. state.cancelBolus()
  200. }
  201. }
  202. }
  203. .frame(maxWidth: .infinity, maxHeight: 30)
  204. }
  205. var legendPanal: some View {
  206. HStack(alignment: .center) {
  207. Group {
  208. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  209. Text("BG")
  210. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  211. }
  212. Group {
  213. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  214. .padding(.leading, 8)
  215. Text("IOB")
  216. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  217. }
  218. Group {
  219. Circle().fill(Color.zt).frame(width: 8, height: 8)
  220. .padding(.leading, 8)
  221. Text("ZT")
  222. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  223. }
  224. Group {
  225. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  226. .padding(.leading, 8)
  227. Text("COB")
  228. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  229. }
  230. Group {
  231. Circle().fill(Color.uam).frame(width: 8, height: 8)
  232. .padding(.leading, 8)
  233. Text("UAM")
  234. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  235. }
  236. if let eventualBG = state.eventualBG {
  237. Text(
  238. "⇢ " + numberFormatter.string(
  239. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  240. )!
  241. )
  242. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  243. }
  244. }
  245. .frame(maxWidth: .infinity, maxHeight: 30)
  246. }
  247. var mainChart: some View {
  248. ZStack {
  249. if state.animatedBackground {
  250. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  251. .ignoresSafeArea()
  252. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  253. }
  254. MainChartView(
  255. glucose: $state.glucose,
  256. suggestion: $state.suggestion,
  257. tempBasals: $state.tempBasals,
  258. boluses: $state.boluses,
  259. suspensions: $state.suspensions,
  260. hours: .constant(state.filteredHours),
  261. maxBasal: $state.maxBasal,
  262. autotunedBasalProfile: $state.autotunedBasalProfile,
  263. basalProfile: $state.basalProfile,
  264. tempTargets: $state.tempTargets,
  265. carbs: $state.carbs,
  266. timerDate: $state.timerDate,
  267. units: $state.units
  268. )
  269. }
  270. .padding(.bottom)
  271. .modal(for: .dataTable, from: self)
  272. }
  273. @ViewBuilder private func bottomPanel(_ geo: GeometryProxy) -> some View {
  274. ZStack {
  275. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  276. HStack {
  277. Button { state.showModal(for: .addCarbs) }
  278. label: {
  279. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  280. Image("carbs")
  281. .renderingMode(.template)
  282. .resizable()
  283. .frame(width: 24, height: 24)
  284. .foregroundColor(.loopGreen)
  285. .padding(8)
  286. if let carbsReq = state.carbsRequired {
  287. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  288. .font(.caption)
  289. .foregroundColor(.white)
  290. .padding(4)
  291. .background(Capsule().fill(Color.red))
  292. }
  293. }
  294. }
  295. Spacer()
  296. Button { state.showModal(for: .addTempTarget) }
  297. label: {
  298. Image("target")
  299. .renderingMode(.template)
  300. .resizable()
  301. .frame(width: 24, height: 24)
  302. .padding(8)
  303. }.foregroundColor(.loopYellow)
  304. Spacer()
  305. Button { state.showModal(for: .bolus(waitForSuggestion: false)) }
  306. label: {
  307. Image("bolus")
  308. .renderingMode(.template)
  309. .resizable()
  310. .frame(width: 24, height: 24)
  311. .padding(8)
  312. }.foregroundColor(.insulin)
  313. Spacer()
  314. if state.allowManualTemp {
  315. Button { state.showModal(for: .manualTempBasal) }
  316. label: {
  317. Image("bolus1")
  318. .renderingMode(.template)
  319. .resizable()
  320. .frame(width: 24, height: 24)
  321. .padding(8)
  322. }.foregroundColor(.insulin)
  323. Spacer()
  324. }
  325. Button { state.showModal(for: .settings) }
  326. label: {
  327. Image("settings1")
  328. .renderingMode(.template)
  329. .resizable()
  330. .frame(width: 24, height: 24)
  331. .padding(8)
  332. }.foregroundColor(.loopGray)
  333. }
  334. .padding(.horizontal, 24)
  335. .padding(.bottom, geo.safeAreaInsets.bottom)
  336. }
  337. }
  338. var body: some View {
  339. GeometryReader { geo in
  340. VStack(spacing: 0) {
  341. header(geo)
  342. infoPanel
  343. mainChart
  344. legendPanal
  345. bottomPanel(geo)
  346. }
  347. .edgesIgnoringSafeArea(.vertical)
  348. }
  349. .onAppear(perform: configureView)
  350. .navigationTitle("Home")
  351. .navigationBarHidden(true)
  352. .ignoresSafeArea(.keyboard)
  353. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  354. popup
  355. .padding()
  356. .background(
  357. RoundedRectangle(cornerRadius: 8, style: .continuous)
  358. .fill(Color(UIColor.darkGray))
  359. )
  360. .onTapGesture {
  361. isStatusPopupPresented = false
  362. }
  363. .gesture(
  364. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  365. .onEnded { value in
  366. if value.translation.height < 0 {
  367. isStatusPopupPresented = false
  368. }
  369. }
  370. )
  371. }
  372. }
  373. private var popup: some View {
  374. VStack(alignment: .leading, spacing: 4) {
  375. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  376. .padding(.bottom, 4)
  377. if let suggestion = state.suggestion {
  378. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  379. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  380. } else {
  381. Text("No sugestion found").font(.body).foregroundColor(.white)
  382. }
  383. if let errorMessage = state.errorMessage, let date = state.errorDate {
  384. Text("Error at \(dateFormatter.string(from: date))")
  385. .foregroundColor(.white)
  386. .font(.headline)
  387. .padding(.bottom, 4)
  388. .padding(.top, 8)
  389. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  390. }
  391. }
  392. }
  393. }
  394. }