HomeRootView.swift 35 KB

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