HomeRootView.swift 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 statPanel
  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. .padding(.top, geo.safeAreaInsets.top)
  67. .padding(.bottom)
  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(.footnote).foregroundColor(.secondary)
  74. Text(
  75. (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
  76. NSLocalizedString(" U", comment: "Insulin unit")
  77. )
  78. .font(.footnote).fontWeight(.bold)
  79. }.frame(alignment: .top)
  80. HStack {
  81. Text("COB").font(.footnote).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(.footnote).fontWeight(.bold)
  87. }.frame(alignment: .bottom)
  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 .total:
  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.total ?? 0) as NSNumber) ?? ""
  295. let median_ = targetFormatter
  296. .string(from: (state.statistics?.Statistics.Glucose.Median.total ?? 0) as NSNumber) ?? ""
  297. let tir_low = tirFormatter
  298. .string(from: (state.statistics?.Statistics.Distribution.Hypos.total ?? 0) as NSNumber) ?? ""
  299. let tir_high = tirFormatter
  300. .string(from: (state.statistics?.Statistics.Distribution.Hypers.total ?? 0) as NSNumber) ??
  301. ""
  302. let tir_ = tirFormatter
  303. .string(from: (state.statistics?.Statistics.Distribution.TIR.total ?? 0) as NSNumber) ?? ""
  304. let hba1c_ = numberFormatter
  305. .string(from: (state.statistics?.Statistics.HbA1c.total ?? 0) as NSNumber) ?? ""
  306. let sd_ = numberFormatter
  307. .string(from: (state.statistics?.Statistics.Variance.SD.total ?? 0) as NSNumber) ?? ""
  308. let cv_ = tirFormatter
  309. .string(from: (state.statistics?.Statistics.Variance.CV.total ?? 0) as NSNumber) ?? ""
  310. averageTIRhca1c(hba1c_all, average_, median_, tir_low, tir_high, tir_, hba1c_, sd_, cv_)
  311. }
  312. }
  313. .frame(maxWidth: .infinity)
  314. .padding([.bottom], 20)
  315. }
  316. }
  317. @ViewBuilder private func averageTIRhca1c(
  318. _ hba1c_all: String,
  319. _ average_: String,
  320. _ median_: String,
  321. _ tir_low: String,
  322. _ tir_high: String,
  323. _ tir_: String,
  324. _ hba1c_: String,
  325. _ sd_: String,
  326. _ cv_: String
  327. ) -> some View {
  328. HStack {
  329. Group {
  330. if selectedState != .total {
  331. HStack {
  332. Text("HbA1c").font(.footnote).foregroundColor(.secondary)
  333. Text(hba1c_).font(.footnote)
  334. }
  335. } else {
  336. HStack {
  337. Text(
  338. "\(NSLocalizedString("HbA1c", comment: "")) (\(targetFormatter.string(from: (state.statistics?.GlucoseStorage_Days ?? 0) as NSNumber) ?? "") \(NSLocalizedString("days", comment: "")))"
  339. )
  340. .font(.footnote).foregroundColor(.secondary)
  341. Text(hba1c_all).font(.footnote)
  342. }
  343. }
  344. // Average as default. Changes to Median when clicking.
  345. let textAverageTitle = NSLocalizedString("Average", comment: "")
  346. let textMedianTitle = NSLocalizedString("Median", comment: "")
  347. let cgmReadingsTitle = NSLocalizedString("Readings", comment: "CGM readings in statPanel")
  348. HStack {
  349. Text(averageOrMedianTitle).font(.footnote).foregroundColor(.secondary)
  350. if averageOrMedianTitle == textAverageTitle {
  351. Text(averageOrmedian == "" ? average_ : average_).font(.footnote)
  352. } else if averageOrMedianTitle == textMedianTitle {
  353. Text(averageOrmedian == "" ? median_ : median_).font(.footnote)
  354. } else if averageOrMedianTitle == cgmReadingsTitle {
  355. Text(
  356. averageOrmedian != "0" ? tirFormatter
  357. .string(from: (state.statistics?.Statistics.LoopCycles.readings ?? 0) as NSNumber) ?? "" : ""
  358. )
  359. .font(.footnote)
  360. }
  361. }.onTapGesture {
  362. if averageOrMedianTitle == textAverageTitle {
  363. averageOrMedianTitle = textMedianTitle
  364. averageOrmedian = median_
  365. } else if averageOrMedianTitle == textMedianTitle {
  366. averageOrMedianTitle = cgmReadingsTitle
  367. averageOrmedian = tirFormatter
  368. .string(from: (state.statistics?.Statistics.LoopCycles.readings ?? 0) as NSNumber) ?? ""
  369. } else if averageOrMedianTitle == cgmReadingsTitle {
  370. averageOrMedianTitle = textAverageTitle
  371. averageOrmedian = average_
  372. }
  373. }
  374. .frame(minWidth: 110)
  375. // CV as default. Changes to SD when clicking
  376. let text_CV_Title = NSLocalizedString("CV", comment: "")
  377. let text_SD_Title = NSLocalizedString("SD", comment: "")
  378. HStack {
  379. Text(CV_or_SD_Title).font(.footnote).foregroundColor(.secondary)
  380. if CV_or_SD_Title == text_CV_Title {
  381. Text(CVorSD == "" ? cv_ : cv_).font(.footnote)
  382. } else {
  383. Text(CVorSD == "" ? sd_ : sd_).font(.footnote)
  384. }
  385. }.onTapGesture {
  386. if CV_or_SD_Title == text_CV_Title {
  387. CV_or_SD_Title = text_SD_Title
  388. CVorSD = sd_
  389. } else {
  390. CV_or_SD_Title = text_CV_Title
  391. CVorSD = cv_
  392. }
  393. }
  394. }
  395. }
  396. HStack {
  397. Group {
  398. HStack {
  399. Text(
  400. NSLocalizedString("Low", comment: " ")
  401. )
  402. .font(.footnote)
  403. .foregroundColor(.secondary)
  404. Text(tir_low + " %").font(.footnote).foregroundColor(.loopRed)
  405. }
  406. HStack {
  407. Text("Normal").font(.footnote).foregroundColor(.secondary)
  408. Text(tir_ + " %").font(.footnote).foregroundColor(.loopGreen)
  409. }
  410. HStack {
  411. Text(
  412. NSLocalizedString("High", comment: " ")
  413. )
  414. .font(.footnote).foregroundColor(.secondary)
  415. Text(tir_high + " %").font(.footnote).foregroundColor(.loopYellow)
  416. }
  417. }
  418. }
  419. if state.settingsManager.preferences.displayLoops {
  420. HStack {
  421. Group {
  422. let loopTitle = NSLocalizedString("Loops", comment: "Nr of Loops in statPanel")
  423. let errorTitle = NSLocalizedString("Errors", comment: "Loop Errors in statPanel")
  424. HStack {
  425. Text(loopStatTitle).font(.footnote).foregroundColor(.secondary)
  426. Text(
  427. loopStatTitle == loopTitle ? tirFormatter
  428. .string(from: (state.statistics?.Statistics.LoopCycles.loops ?? 0) as NSNumber) ?? "" :
  429. tirFormatter
  430. .string(from: (state.statistics?.Statistics.LoopCycles.errors ?? 0) as NSNumber) ?? ""
  431. ).font(.footnote)
  432. }.onTapGesture {
  433. if loopStatTitle == loopTitle {
  434. loopStatTitle = errorTitle
  435. } else if loopStatTitle == errorTitle {
  436. loopStatTitle = loopTitle
  437. }
  438. }
  439. HStack {
  440. Text("Interval").font(.footnote)
  441. .foregroundColor(.secondary)
  442. Text(
  443. targetFormatter
  444. .string(from: (state.statistics?.Statistics.LoopCycles.avg_interval ?? 0) as NSNumber) ??
  445. ""
  446. ).font(.footnote)
  447. }
  448. HStack {
  449. Text("Duration").font(.footnote)
  450. .foregroundColor(.secondary)
  451. Text(
  452. numberFormatter
  453. .string(
  454. from: (state.statistics?.Statistics.LoopCycles.median_duration ?? 0) as NSNumber
  455. ) ?? ""
  456. ).font(.footnote)
  457. }
  458. }
  459. }
  460. }
  461. }
  462. var legendPanel: some View {
  463. ZStack {
  464. HStack(alignment: .center) {
  465. Group {
  466. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  467. Text("BG")
  468. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  469. }
  470. Group {
  471. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  472. .padding(.leading, 8)
  473. Text("IOB")
  474. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  475. }
  476. Group {
  477. Circle().fill(Color.zt).frame(width: 8, height: 8)
  478. .padding(.leading, 8)
  479. Text("ZT")
  480. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  481. }
  482. Group {
  483. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  484. .padding(.leading, 8)
  485. Text("COB")
  486. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  487. }
  488. Group {
  489. Circle().fill(Color.uam).frame(width: 8, height: 8)
  490. .padding(.leading, 8)
  491. Text("UAM")
  492. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  493. }
  494. if let eventualBG = state.eventualBG {
  495. Text(
  496. "⇢ " + numberFormatter.string(
  497. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  498. )!
  499. )
  500. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  501. }
  502. }
  503. .frame(maxWidth: .infinity)
  504. .padding([.bottom], 20)
  505. }
  506. }
  507. var mainChart: some View {
  508. ZStack {
  509. if state.animatedBackground {
  510. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  511. .ignoresSafeArea()
  512. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  513. }
  514. MainChartView(
  515. glucose: $state.glucose,
  516. suggestion: $state.suggestion,
  517. statistcs: $state.statistics,
  518. tempBasals: $state.tempBasals,
  519. boluses: $state.boluses,
  520. suspensions: $state.suspensions,
  521. hours: .constant(state.filteredHours),
  522. maxBasal: $state.maxBasal,
  523. autotunedBasalProfile: $state.autotunedBasalProfile,
  524. basalProfile: $state.basalProfile,
  525. tempTargets: $state.tempTargets,
  526. carbs: $state.carbs,
  527. timerDate: $state.timerDate,
  528. units: $state.units
  529. )
  530. }
  531. .padding(.bottom)
  532. .modal(for: .dataTable, from: self)
  533. }
  534. @ViewBuilder private func bottomPanel(_ geo: GeometryProxy) -> some View {
  535. ZStack {
  536. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  537. HStack {
  538. Button { state.showModal(for: .addCarbs) }
  539. label: {
  540. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  541. Image("carbs")
  542. .renderingMode(.template)
  543. .resizable()
  544. .frame(width: 24, height: 24)
  545. .foregroundColor(.loopYellow)
  546. .padding(8)
  547. if let carbsReq = state.carbsRequired {
  548. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  549. .font(.caption)
  550. .foregroundColor(.white)
  551. .padding(4)
  552. .background(Capsule().fill(Color.red))
  553. }
  554. }
  555. }
  556. Spacer()
  557. Button { state.showModal(for: .addTempTarget) }
  558. label: {
  559. Image("target")
  560. .renderingMode(.template)
  561. .resizable()
  562. .frame(width: 24, height: 24)
  563. .padding(8)
  564. }.foregroundColor(.loopGreen)
  565. Spacer()
  566. Button { state.showModal(for: .bolus(waitForSuggestion: false)) }
  567. label: {
  568. Image("bolus")
  569. .renderingMode(.template)
  570. .resizable()
  571. .frame(width: 24, height: 24)
  572. .padding(8)
  573. }.foregroundColor(.insulin)
  574. Spacer()
  575. if state.allowManualTemp {
  576. Button { state.showModal(for: .manualTempBasal) }
  577. label: {
  578. Image("bolus1")
  579. .renderingMode(.template)
  580. .resizable()
  581. .frame(width: 24, height: 24)
  582. .padding(8)
  583. }.foregroundColor(.insulin)
  584. Spacer()
  585. }
  586. Button { state.showModal(for: .settings) }
  587. label: {
  588. Image("settings1")
  589. .renderingMode(.template)
  590. .resizable()
  591. .frame(width: 24, height: 24)
  592. .padding(8)
  593. }.foregroundColor(.loopGray)
  594. }
  595. .padding(.horizontal, 24)
  596. .padding(.bottom, geo.safeAreaInsets.bottom)
  597. }
  598. }
  599. var body: some View {
  600. GeometryReader { geo in
  601. VStack(spacing: 0) {
  602. header(geo)
  603. infoPanel
  604. mainChart
  605. legendPanel
  606. statPanel()
  607. bottomPanel(geo)
  608. }
  609. .edgesIgnoringSafeArea(.vertical)
  610. }
  611. .onAppear(perform: configureView)
  612. .navigationTitle("Home")
  613. .navigationBarHidden(true)
  614. .ignoresSafeArea(.keyboard)
  615. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  616. popup
  617. .padding()
  618. .background(
  619. RoundedRectangle(cornerRadius: 8, style: .continuous)
  620. .fill(Color(UIColor.darkGray))
  621. )
  622. .onTapGesture {
  623. isStatusPopupPresented = false
  624. }
  625. .gesture(
  626. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  627. .onEnded { value in
  628. if value.translation.height < 0 {
  629. isStatusPopupPresented = false
  630. }
  631. }
  632. )
  633. }
  634. }
  635. private var popup: some View {
  636. VStack(alignment: .leading, spacing: 4) {
  637. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  638. .padding(.bottom, 4)
  639. if let suggestion = state.suggestion {
  640. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  641. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  642. } else {
  643. Text("No sugestion found").font(.body).foregroundColor(.white)
  644. }
  645. if let errorMessage = state.errorMessage, let date = state.errorDate {
  646. Text(NSLocalizedString("Error at", comment: "") + " " + dateFormatter.string(from: date))
  647. .foregroundColor(.white)
  648. .font(.headline)
  649. .padding(.bottom, 4)
  650. .padding(.top, 8)
  651. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  652. }
  653. }
  654. }
  655. }
  656. }