HomeRootView.swift 34 KB

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