HomeRootView.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. @ViewBuilder private func statPanel() -> some View {
  206. if state.displayStatistics {
  207. VStack(alignment: .center, spacing: 6) {
  208. HStack {
  209. Group {
  210. Text("Updated").font(.caption2)
  211. .foregroundColor(.secondary)
  212. Text(
  213. dateFormatter.string(from: state.statistics?.created_at ?? Date())
  214. ).font(.system(size: 12))
  215. Text(
  216. NSLocalizedString("Average", comment: "") + " " + state.settingsManager.settings.units.rawValue
  217. ).font(.caption2).foregroundColor(.secondary)
  218. Text(
  219. numberFormatter
  220. .string(from: (state.statistics?.Statistics.Glucose.Average.day ?? 0) as NSNumber) ??
  221. ""
  222. ).font(.system(size: 12))
  223. Text("Median")
  224. .font(.caption2).foregroundColor(.secondary)
  225. Text(
  226. numberFormatter
  227. .string(from: (state.statistics?.Statistics.Glucose.Median.day ?? 0) as NSNumber) ??
  228. ""
  229. ).font(.system(size: 12))
  230. }
  231. }
  232. HStack {
  233. Group {
  234. Text("Normal (24h)").font(.caption2).foregroundColor(.secondary)
  235. Text(
  236. (
  237. numberFormatter
  238. .string(from: (state.statistics?.Statistics.Distribution.TIR.day ?? 0) as NSNumber) ??
  239. "0"
  240. ) + " %"
  241. ).font(.system(size: 12)).foregroundColor(.loopGreen)
  242. Text(
  243. NSLocalizedString("Low (<", comment: " ") +
  244. (numberFormatter.string(from: state.settingsManager.preferences.low as NSNumber) ?? "") + ")"
  245. ).font(.caption2).foregroundColor(.secondary)
  246. Text(
  247. (
  248. numberFormatter
  249. .string(from: (
  250. state.statistics?.Statistics.Distribution.Hypos.day ?? 0
  251. ) as NSNumber) ??
  252. "0"
  253. ) + " %"
  254. ).font(.system(size: 12)).foregroundColor(.loopRed)
  255. Text(
  256. NSLocalizedString("High (>", comment: " ") +
  257. (numberFormatter.string(from: state.settingsManager.preferences.high as NSNumber) ?? "") + ")"
  258. ).font(.caption2).foregroundColor(.secondary)
  259. Text(
  260. (
  261. numberFormatter
  262. .string(from: (
  263. state.statistics?.Statistics.Distribution.Hypers.day ?? 0
  264. ) as NSNumber) ??
  265. "0"
  266. ) + " %"
  267. ).font(.system(size: 12)).foregroundColor(.loopYellow)
  268. }
  269. }
  270. HStack {
  271. Group {
  272. Text("HbA1c (24h)").font(.caption2).foregroundColor(.secondary)
  273. Text(
  274. numberFormatter
  275. .string(from: (state.statistics?.Statistics.HbA1c.day ?? 0) as NSNumber) ??
  276. ""
  277. ).font(.system(size: 12))
  278. Text(
  279. NSLocalizedString("All ", comment: "") +
  280. (
  281. numberFormatter
  282. .string(from: (state.statistics?.GlucoseStorage_Days ?? 0) as NSNumber) ?? ""
  283. ) +
  284. NSLocalizedString(" days", comment: "")
  285. ).font(.caption2).foregroundColor(.secondary)
  286. Text(
  287. numberFormatter
  288. .string(from: (state.statistics?.Statistics.HbA1c.total ?? 0) as NSNumber) ??
  289. ""
  290. ).font(.system(size: 12))
  291. }
  292. }
  293. HStack {
  294. Group {
  295. Text("Loops").font(.caption2).foregroundColor(.secondary)
  296. Text(
  297. numberFormatter
  298. .string(from: (state.statistics?.Statistics.LoopCycles.loops ?? 0) as NSNumber) ??
  299. "0"
  300. ).font(.system(size: 12))
  301. Text("Average Interval").font(.caption2).foregroundColor(.secondary)
  302. Text(
  303. numberFormatter
  304. .string(from: (state.statistics?.Statistics.LoopCycles.avg_interval ?? 0) as NSNumber) ??
  305. "0"
  306. ).font(.system(size: 12))
  307. Text("Median Duration").font(.caption2).foregroundColor(.secondary)
  308. Text(
  309. numberFormatter
  310. .string(from: (
  311. state.statistics?.Statistics.LoopCycles
  312. .median_duration ?? 0
  313. ) as NSNumber) ??
  314. "0"
  315. ).font(.system(size: 12))
  316. }
  317. }
  318. }
  319. .frame(maxWidth: .infinity, maxHeight: 100, alignment: .center)
  320. }
  321. }
  322. var legendPanel: some View {
  323. HStack(alignment: .center) {
  324. Group {
  325. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  326. Text("BG")
  327. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  328. }
  329. Group {
  330. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  331. .padding(.leading, 8)
  332. Text("IOB")
  333. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  334. }
  335. Group {
  336. Circle().fill(Color.zt).frame(width: 8, height: 8)
  337. .padding(.leading, 8)
  338. Text("ZT")
  339. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  340. }
  341. Group {
  342. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  343. .padding(.leading, 8)
  344. Text("COB")
  345. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  346. }
  347. Group {
  348. Circle().fill(Color.uam).frame(width: 8, height: 8)
  349. .padding(.leading, 8)
  350. Text("UAM")
  351. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  352. }
  353. if let eventualBG = state.eventualBG {
  354. Text(
  355. "⇢ " + numberFormatter.string(
  356. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  357. )!
  358. )
  359. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  360. }
  361. }
  362. .frame(maxWidth: .infinity, maxHeight: 30)
  363. }
  364. var mainChart: some View {
  365. ZStack {
  366. if state.animatedBackground {
  367. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  368. .ignoresSafeArea()
  369. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  370. }
  371. MainChartView(
  372. glucose: $state.glucose,
  373. suggestion: $state.suggestion,
  374. statistcs: $state.statistics,
  375. tempBasals: $state.tempBasals,
  376. boluses: $state.boluses,
  377. suspensions: $state.suspensions,
  378. hours: .constant(state.filteredHours),
  379. maxBasal: $state.maxBasal,
  380. autotunedBasalProfile: $state.autotunedBasalProfile,
  381. basalProfile: $state.basalProfile,
  382. tempTargets: $state.tempTargets,
  383. carbs: $state.carbs,
  384. timerDate: $state.timerDate,
  385. units: $state.units
  386. )
  387. }
  388. .padding(.bottom)
  389. .modal(for: .dataTable, from: self)
  390. }
  391. @ViewBuilder private func bottomPanel(_ geo: GeometryProxy) -> some View {
  392. ZStack {
  393. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  394. HStack {
  395. Button { state.showModal(for: .addCarbs) }
  396. label: {
  397. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  398. Image("carbs")
  399. .renderingMode(.template)
  400. .resizable()
  401. .frame(width: 24, height: 24)
  402. .foregroundColor(.loopGreen)
  403. .padding(8)
  404. if let carbsReq = state.carbsRequired {
  405. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  406. .font(.caption)
  407. .foregroundColor(.white)
  408. .padding(4)
  409. .background(Capsule().fill(Color.red))
  410. }
  411. }
  412. }
  413. Spacer()
  414. Button { state.showModal(for: .addTempTarget) }
  415. label: {
  416. Image("target")
  417. .renderingMode(.template)
  418. .resizable()
  419. .frame(width: 24, height: 24)
  420. .padding(8)
  421. }.foregroundColor(.loopYellow)
  422. Spacer()
  423. Button { state.showModal(for: .bolus(waitForSuggestion: false)) }
  424. label: {
  425. Image("bolus")
  426. .renderingMode(.template)
  427. .resizable()
  428. .frame(width: 24, height: 24)
  429. .padding(8)
  430. }.foregroundColor(.insulin)
  431. Spacer()
  432. if state.allowManualTemp {
  433. Button { state.showModal(for: .manualTempBasal) }
  434. label: {
  435. Image("bolus1")
  436. .renderingMode(.template)
  437. .resizable()
  438. .frame(width: 24, height: 24)
  439. .padding(8)
  440. }.foregroundColor(.insulin)
  441. Spacer()
  442. }
  443. Button { state.showModal(for: .settings) }
  444. label: {
  445. Image("settings1")
  446. .renderingMode(.template)
  447. .resizable()
  448. .frame(width: 24, height: 24)
  449. .padding(8)
  450. }.foregroundColor(.loopGray)
  451. }
  452. .padding(.horizontal, 24)
  453. .padding(.bottom, geo.safeAreaInsets.bottom)
  454. }
  455. }
  456. var body: some View {
  457. GeometryReader { geo in
  458. VStack(spacing: 0) {
  459. header(geo)
  460. infoPanel
  461. mainChart
  462. legendPanel
  463. statPanel()
  464. bottomPanel(geo)
  465. }
  466. .edgesIgnoringSafeArea(.vertical)
  467. }
  468. .onAppear(perform: configureView)
  469. .navigationTitle("Home")
  470. .navigationBarHidden(true)
  471. .ignoresSafeArea(.keyboard)
  472. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  473. popup
  474. .padding()
  475. .background(
  476. RoundedRectangle(cornerRadius: 8, style: .continuous)
  477. .fill(Color(UIColor.darkGray))
  478. )
  479. .onTapGesture {
  480. isStatusPopupPresented = false
  481. }
  482. .gesture(
  483. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  484. .onEnded { value in
  485. if value.translation.height < 0 {
  486. isStatusPopupPresented = false
  487. }
  488. }
  489. )
  490. }
  491. }
  492. private var popup: some View {
  493. VStack(alignment: .leading, spacing: 4) {
  494. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  495. .padding(.bottom, 4)
  496. if let suggestion = state.suggestion {
  497. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  498. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  499. } else {
  500. Text("No sugestion found").font(.body).foregroundColor(.white)
  501. }
  502. if let errorMessage = state.errorMessage, let date = state.errorDate {
  503. Text("Error at \(dateFormatter.string(from: date))")
  504. .foregroundColor(.white)
  505. .font(.headline)
  506. .padding(.bottom, 4)
  507. .padding(.top, 8)
  508. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  509. }
  510. }
  511. }
  512. private func colorOfGlucose(_ glucose: Decimal) -> Color {
  513. switch glucose {
  514. case 4 ... 8,
  515. 30 ... 46,
  516. 72 ... 144:
  517. return .loopGreen
  518. case 0 ... 4,
  519. 20 ... 71:
  520. return .loopRed
  521. default:
  522. return .loopYellow
  523. }
  524. }
  525. }
  526. }