HomeRootView.swift 34 KB

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