HomeRootView.swift 32 KB

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