HomeRootView.swift 32 KB

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