HomeRootView.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 tirFormatter: NumberFormatter {
  23. let formatter = NumberFormatter()
  24. formatter.numberStyle = .decimal
  25. formatter.maximumFractionDigits = 0
  26. return formatter
  27. }
  28. private var dateFormatter: DateFormatter {
  29. let dateFormatter = DateFormatter()
  30. dateFormatter.timeStyle = .short
  31. return dateFormatter
  32. }
  33. private var spriteScene: SKScene {
  34. let scene = SnowScene()
  35. scene.scaleMode = .resizeFill
  36. scene.backgroundColor = .clear
  37. return scene
  38. }
  39. @ViewBuilder func header(_ geo: GeometryProxy) -> some View {
  40. HStack(alignment: .bottom) {
  41. Spacer()
  42. cobIobView
  43. Spacer()
  44. glucoseView
  45. Spacer()
  46. pumpView
  47. Spacer()
  48. loopView
  49. Spacer()
  50. }
  51. .frame(maxWidth: .infinity)
  52. .frame(maxHeight: 70)
  53. .padding(.top, geo.safeAreaInsets.top)
  54. .background(Color.gray.opacity(0.2))
  55. }
  56. var cobIobView: some View {
  57. VStack(alignment: .leading, spacing: 12) {
  58. HStack {
  59. Text("IOB").font(.caption2).foregroundColor(.secondary)
  60. Text(
  61. (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
  62. NSLocalizedString(" U", comment: "Insulin unit")
  63. )
  64. .font(.system(size: 12, weight: .bold))
  65. }
  66. HStack {
  67. Text("COB").font(.caption2).foregroundColor(.secondary)
  68. Text(
  69. (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
  70. NSLocalizedString(" g", comment: "gram of carbs")
  71. )
  72. .font(.system(size: 12, weight: .bold))
  73. }
  74. }
  75. }
  76. var glucoseView: some View {
  77. CurrentGlucoseView(
  78. recentGlucose: $state.recentGlucose,
  79. delta: $state.glucoseDelta,
  80. units: $state.units,
  81. alarm: $state.alarm
  82. )
  83. .onTapGesture {
  84. if state.alarm == nil {
  85. state.openCGM()
  86. } else {
  87. state.showModal(for: .snooze)
  88. }
  89. }
  90. .onLongPressGesture {
  91. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  92. impactHeavy.impactOccurred()
  93. if state.alarm == nil {
  94. state.showModal(for: .snooze)
  95. } else {
  96. state.openCGM()
  97. }
  98. }
  99. }
  100. var pumpView: some View {
  101. PumpView(
  102. reservoir: $state.reservoir,
  103. battery: $state.battery,
  104. name: $state.pumpName,
  105. expiresAtDate: $state.pumpExpiresAtDate,
  106. timerDate: $state.timerDate
  107. )
  108. .onTapGesture {
  109. if state.pumpDisplayState != nil {
  110. state.setupPump = true
  111. }
  112. }
  113. }
  114. var loopView: some View {
  115. LoopView(
  116. suggestion: $state.suggestion,
  117. enactedSuggestion: $state.enactedSuggestion,
  118. closedLoop: $state.closedLoop,
  119. timerDate: $state.timerDate,
  120. isLooping: $state.isLooping,
  121. lastLoopDate: $state.lastLoopDate,
  122. manualTempBasal: $state.manualTempBasal
  123. ).onTapGesture {
  124. isStatusPopupPresented = true
  125. }.onLongPressGesture {
  126. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  127. impactHeavy.impactOccurred()
  128. state.runLoop()
  129. }
  130. }
  131. var infoPanel: some View {
  132. HStack(alignment: .center) {
  133. if state.pumpSuspended {
  134. Text("Pump suspended")
  135. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGray)
  136. .padding(.leading, 8)
  137. } else if let tempRate = state.tempRate {
  138. if state.apsManager.isManualTempBasal {
  139. Text(
  140. (numberFormatter.string(from: tempRate as NSNumber) ?? "0") +
  141. NSLocalizedString(" U/hr", comment: "Unit per hour with space") +
  142. NSLocalizedString(" - Manual Basal ⚠️", comment: "Manual Temp basal")
  143. )
  144. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  145. .padding(.leading, 8)
  146. } else {
  147. Text(
  148. (numberFormatter.string(from: tempRate as NSNumber) ?? "0") +
  149. NSLocalizedString(" U/hr", comment: "Unit per hour with space")
  150. )
  151. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  152. .padding(.leading, 8)
  153. }
  154. }
  155. if let tempTarget = state.tempTarget {
  156. Text(tempTarget.displayName).font(.caption).foregroundColor(.secondary)
  157. if state.units == .mmolL {
  158. Text(
  159. targetFormatter
  160. .string(from: (tempTarget.targetBottom?.asMmolL ?? 0) as NSNumber)!
  161. )
  162. .font(.caption)
  163. .foregroundColor(.secondary)
  164. if tempTarget.targetBottom != tempTarget.targetTop {
  165. Text("-").font(.caption)
  166. .foregroundColor(.secondary)
  167. Text(
  168. targetFormatter
  169. .string(from: (tempTarget.targetTop?.asMmolL ?? 0) as NSNumber)! +
  170. " \(state.units.rawValue)"
  171. )
  172. .font(.caption)
  173. .foregroundColor(.secondary)
  174. } else {
  175. Text(state.units.rawValue).font(.caption)
  176. .foregroundColor(.secondary)
  177. }
  178. } else {
  179. Text(targetFormatter.string(from: (tempTarget.targetBottom ?? 0) as NSNumber)!)
  180. .font(.caption)
  181. .foregroundColor(.secondary)
  182. if tempTarget.targetBottom != tempTarget.targetTop {
  183. Text("-").font(.caption)
  184. .foregroundColor(.secondary)
  185. Text(
  186. targetFormatter
  187. .string(from: (tempTarget.targetTop ?? 0) as NSNumber)! + " \(state.units.rawValue)"
  188. )
  189. .font(.caption)
  190. .foregroundColor(.secondary)
  191. } else {
  192. Text(state.units.rawValue).font(.caption)
  193. .foregroundColor(.secondary)
  194. }
  195. }
  196. }
  197. Spacer()
  198. if let progress = state.bolusProgress {
  199. Text("Bolusing")
  200. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  201. ProgressView(value: Double(progress))
  202. .progressViewStyle(BolusProgressViewStyle())
  203. .padding(.trailing, 8)
  204. .onTapGesture {
  205. state.cancelBolus()
  206. }
  207. }
  208. }
  209. .frame(maxWidth: .infinity, maxHeight: 30)
  210. }
  211. @ViewBuilder private func statPanel() -> some View {
  212. if state.displayStatistics {
  213. VStack(alignment: .center, spacing: 6) {
  214. HStack {
  215. Group {
  216. Text("Updated").font(.caption2)
  217. .foregroundColor(.secondary)
  218. Text(
  219. dateFormatter.string(from: state.statistics?.created_at ?? Date())
  220. ).font(.system(size: 12))
  221. Text(
  222. NSLocalizedString("Average", comment: "") + " " + state.settingsManager.settings.units.rawValue
  223. ).font(.caption2).foregroundColor(.secondary)
  224. if state.units == .mmolL {
  225. Text(
  226. targetFormatter
  227. .string(from: (state.statistics?.Statistics.Glucose.Average.day ?? 0) as NSNumber) ??
  228. ""
  229. ).font(.system(size: 12))
  230. } else {
  231. Text(
  232. tirFormatter
  233. .string(from: (state.statistics?.Statistics.Glucose.Average.day ?? 0) as NSNumber) ??
  234. ""
  235. ).font(.system(size: 12))
  236. }
  237. Text("Median")
  238. .font(.caption2).foregroundColor(.secondary)
  239. if state.units == .mmolL {
  240. Text(
  241. targetFormatter
  242. .string(from: (state.statistics?.Statistics.Glucose.Median.day ?? 0) as NSNumber) ??
  243. ""
  244. ).font(.system(size: 12))
  245. } else {
  246. Text(
  247. tirFormatter
  248. .string(from: (state.statistics?.Statistics.Glucose.Median.day ?? 0) as NSNumber) ??
  249. ""
  250. ).font(.system(size: 12))
  251. }
  252. }
  253. }
  254. HStack {
  255. Group {
  256. Text(
  257. NSLocalizedString("Low (<", comment: " ") +
  258. (targetFormatter.string(from: state.settingsManager.preferences.low as NSNumber) ?? "") + ")"
  259. ).font(.caption2).foregroundColor(.secondary)
  260. Text(
  261. (
  262. tirFormatter
  263. .string(from: (
  264. state.statistics?.Statistics.Distribution.Hypos.day ?? 0
  265. ) as NSNumber) ??
  266. "0"
  267. ) + " %"
  268. ).font(.system(size: 12)).foregroundColor(.loopRed)
  269. Text("Normal (24h)").font(.caption2).foregroundColor(.secondary)
  270. Text(
  271. (
  272. tirFormatter
  273. .string(from: (state.statistics?.Statistics.Distribution.TIR.day ?? 0) as NSNumber) ??
  274. "0"
  275. ) + " %"
  276. ).font(.system(size: 12)).foregroundColor(.loopGreen)
  277. Text(
  278. NSLocalizedString("High (>", comment: " ") +
  279. (targetFormatter.string(from: state.settingsManager.preferences.high as NSNumber) ?? "") + ")"
  280. ).font(.caption2).foregroundColor(.secondary)
  281. Text(
  282. (
  283. tirFormatter
  284. .string(from: (
  285. state.statistics?.Statistics.Distribution.Hypers.day ?? 0
  286. ) as NSNumber) ??
  287. "0"
  288. ) + " %"
  289. ).font(.system(size: 12)).foregroundColor(.loopYellow)
  290. }
  291. }
  292. HStack {
  293. Group {
  294. Text("HbA1c (24h)").font(.caption2).foregroundColor(.secondary)
  295. Text(
  296. targetFormatter
  297. .string(from: (state.statistics?.Statistics.HbA1c.day ?? 0) as NSNumber) ??
  298. ""
  299. ).font(.system(size: 12))
  300. Text(
  301. NSLocalizedString("All ", comment: "") +
  302. (
  303. targetFormatter
  304. .string(from: (state.statistics?.GlucoseStorage_Days ?? 0) as NSNumber) ?? ""
  305. ) +
  306. NSLocalizedString(" days", comment: "")
  307. ).font(.caption2).foregroundColor(.secondary)
  308. Text(
  309. targetFormatter
  310. .string(from: (state.statistics?.Statistics.HbA1c.total ?? 0) as NSNumber) ??
  311. ""
  312. ).font(.system(size: 12))
  313. if !state.settingsManager.preferences.displaySD {
  314. Text(
  315. NSLocalizedString("CV (%)", comment: "CV")
  316. ).font(.caption2).foregroundColor(.secondary)
  317. Text(
  318. tirFormatter
  319. .string(from: (state.statistics?.Statistics.Variance.CV.total ?? 0) as NSNumber) ??
  320. ""
  321. ).font(.system(size: 12))
  322. } else {
  323. Text(
  324. NSLocalizedString("SD (", comment: "SD") + state.settingsManager.settings.units.rawValue + ")"
  325. ).font(.caption2).foregroundColor(.secondary)
  326. if state.units == .mmolL {
  327. Text(
  328. targetFormatter
  329. .string(from: (state.statistics?.Statistics.Variance.SD.total ?? 0) as NSNumber) ??
  330. ""
  331. ).font(.system(size: 12))
  332. } else {
  333. Text(
  334. tirFormatter
  335. .string(from: (state.statistics?.Statistics.Variance.SD.total ?? 0) as NSNumber) ??
  336. ""
  337. ).font(.system(size: 12))
  338. }
  339. }
  340. }
  341. }
  342. HStack {
  343. Group {
  344. Text("Loops").font(.caption2).foregroundColor(.secondary)
  345. Text(
  346. numberFormatter
  347. .string(from: (state.statistics?.Statistics.LoopCycles.loops ?? 0) as NSNumber) ??
  348. "0"
  349. ).font(.system(size: 12))
  350. Text("Average Interval").font(.caption2).foregroundColor(.secondary)
  351. Text(
  352. targetFormatter
  353. .string(from: (state.statistics?.Statistics.LoopCycles.avg_interval ?? 0) as NSNumber) ??
  354. "0"
  355. ).font(.system(size: 12))
  356. Text("Median Duration").font(.caption2).foregroundColor(.secondary)
  357. Text(
  358. numberFormatter
  359. .string(from: (
  360. state.statistics?.Statistics.LoopCycles
  361. .median_duration ?? 0
  362. ) as NSNumber) ??
  363. "0"
  364. ).font(.system(size: 12))
  365. }
  366. }
  367. }
  368. .frame(maxWidth: .infinity, maxHeight: 100, alignment: .center)
  369. }
  370. }
  371. var legendPanel: some View {
  372. HStack(alignment: .center) {
  373. Group {
  374. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  375. Text("BG")
  376. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  377. }
  378. Group {
  379. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  380. .padding(.leading, 8)
  381. Text("IOB")
  382. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  383. }
  384. Group {
  385. Circle().fill(Color.zt).frame(width: 8, height: 8)
  386. .padding(.leading, 8)
  387. Text("ZT")
  388. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  389. }
  390. Group {
  391. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  392. .padding(.leading, 8)
  393. Text("COB")
  394. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  395. }
  396. Group {
  397. Circle().fill(Color.uam).frame(width: 8, height: 8)
  398. .padding(.leading, 8)
  399. Text("UAM")
  400. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  401. }
  402. if let eventualBG = state.eventualBG {
  403. Text(
  404. "⇢ " + numberFormatter.string(
  405. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  406. )!
  407. )
  408. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  409. }
  410. }
  411. .frame(maxWidth: .infinity, maxHeight: 30)
  412. }
  413. var mainChart: some View {
  414. ZStack {
  415. if state.animatedBackground {
  416. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  417. .ignoresSafeArea()
  418. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  419. }
  420. MainChartView(
  421. glucose: $state.glucose,
  422. suggestion: $state.suggestion,
  423. statistcs: $state.statistics,
  424. tempBasals: $state.tempBasals,
  425. boluses: $state.boluses,
  426. suspensions: $state.suspensions,
  427. hours: .constant(state.filteredHours),
  428. maxBasal: $state.maxBasal,
  429. autotunedBasalProfile: $state.autotunedBasalProfile,
  430. basalProfile: $state.basalProfile,
  431. tempTargets: $state.tempTargets,
  432. carbs: $state.carbs,
  433. timerDate: $state.timerDate,
  434. units: $state.units
  435. )
  436. }
  437. .padding(.bottom)
  438. .modal(for: .dataTable, from: self)
  439. }
  440. @ViewBuilder private func bottomPanel(_ geo: GeometryProxy) -> some View {
  441. ZStack {
  442. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  443. HStack {
  444. Button { state.showModal(for: .addCarbs) }
  445. label: {
  446. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  447. Image("carbs")
  448. .renderingMode(.template)
  449. .resizable()
  450. .frame(width: 24, height: 24)
  451. .foregroundColor(.loopGreen)
  452. .padding(8)
  453. if let carbsReq = state.carbsRequired {
  454. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  455. .font(.caption)
  456. .foregroundColor(.white)
  457. .padding(4)
  458. .background(Capsule().fill(Color.red))
  459. }
  460. }
  461. }
  462. Spacer()
  463. Button { state.showModal(for: .addTempTarget) }
  464. label: {
  465. Image("target")
  466. .renderingMode(.template)
  467. .resizable()
  468. .frame(width: 24, height: 24)
  469. .padding(8)
  470. }.foregroundColor(.loopYellow)
  471. Spacer()
  472. Button { state.showModal(for: .bolus(waitForSuggestion: false)) }
  473. label: {
  474. Image("bolus")
  475. .renderingMode(.template)
  476. .resizable()
  477. .frame(width: 24, height: 24)
  478. .padding(8)
  479. }.foregroundColor(.insulin)
  480. Spacer()
  481. if state.allowManualTemp {
  482. Button { state.showModal(for: .manualTempBasal) }
  483. label: {
  484. Image("bolus1")
  485. .renderingMode(.template)
  486. .resizable()
  487. .frame(width: 24, height: 24)
  488. .padding(8)
  489. }.foregroundColor(.insulin)
  490. Spacer()
  491. }
  492. Button { state.showModal(for: .settings) }
  493. label: {
  494. Image("settings1")
  495. .renderingMode(.template)
  496. .resizable()
  497. .frame(width: 24, height: 24)
  498. .padding(8)
  499. }.foregroundColor(.loopGray)
  500. }
  501. .padding(.horizontal, 24)
  502. .padding(.bottom, geo.safeAreaInsets.bottom)
  503. }
  504. }
  505. var body: some View {
  506. GeometryReader { geo in
  507. VStack(spacing: 0) {
  508. header(geo)
  509. infoPanel
  510. mainChart
  511. legendPanel
  512. statPanel()
  513. bottomPanel(geo)
  514. }
  515. .edgesIgnoringSafeArea(.vertical)
  516. }
  517. .onAppear(perform: configureView)
  518. .navigationTitle("Home")
  519. .navigationBarHidden(true)
  520. .ignoresSafeArea(.keyboard)
  521. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  522. popup
  523. .padding()
  524. .background(
  525. RoundedRectangle(cornerRadius: 8, style: .continuous)
  526. .fill(Color(UIColor.darkGray))
  527. )
  528. .onTapGesture {
  529. isStatusPopupPresented = false
  530. }
  531. .gesture(
  532. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  533. .onEnded { value in
  534. if value.translation.height < 0 {
  535. isStatusPopupPresented = false
  536. }
  537. }
  538. )
  539. }
  540. }
  541. private var popup: some View {
  542. VStack(alignment: .leading, spacing: 4) {
  543. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  544. .padding(.bottom, 4)
  545. if let suggestion = state.suggestion {
  546. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  547. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  548. } else {
  549. Text("No sugestion found").font(.body).foregroundColor(.white)
  550. }
  551. if let errorMessage = state.errorMessage, let date = state.errorDate {
  552. Text("Error at \(dateFormatter.string(from: date))")
  553. .foregroundColor(.white)
  554. .font(.headline)
  555. .padding(.bottom, 4)
  556. .padding(.top, 8)
  557. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  558. }
  559. }
  560. }
  561. private func colorOfGlucose(_ glucose: Decimal) -> Color {
  562. switch glucose {
  563. case 4 ... 8,
  564. 30 ... 46,
  565. 72 ... 144:
  566. return .loopGreen
  567. case 0 ... 4,
  568. 20 ... 71:
  569. return .loopRed
  570. default:
  571. return .loopYellow
  572. }
  573. }
  574. }
  575. }