HomeRootView.swift 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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_ = numberFormatter
  227. .string(from: (state.statistics?.Statistics.Glucose.Average.day ?? 0) as NSNumber) ?? ""
  228. let median_ = numberFormatter
  229. .string(from: (state.statistics?.Statistics.Glucose.Median.day ?? 0) as NSNumber) ?? ""
  230. let tir_low = numberFormatter
  231. .string(from: (state.statistics?.Statistics.Distribution.Hypos.day ?? 0) as NSNumber) ?? ""
  232. let tir_high = numberFormatter
  233. .string(from: (state.statistics?.Statistics.Distribution.Hypers.day ?? 0) as NSNumber) ?? ""
  234. let tir_ = numberFormatter
  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_ = numberFormatter
  247. .string(from: (state.statistics?.Statistics.Glucose.Average.week ?? 0) as NSNumber) ?? ""
  248. let median_ = numberFormatter
  249. .string(from: (state.statistics?.Statistics.Glucose.Median.week ?? 0) as NSNumber) ?? ""
  250. let tir_low = numberFormatter
  251. .string(from: (state.statistics?.Statistics.Distribution.Hypos.week ?? 0) as NSNumber) ?? ""
  252. let tir_high = numberFormatter
  253. .string(from: (state.statistics?.Statistics.Distribution.Hypers.week ?? 0) as NSNumber) ?? ""
  254. let tir_ = numberFormatter
  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_ = numberFormatter
  267. .string(from: (state.statistics?.Statistics.Glucose.Average.month ?? 0) as NSNumber) ?? ""
  268. let median_ = numberFormatter
  269. .string(from: (state.statistics?.Statistics.Glucose.Median.month ?? 0) as NSNumber) ?? ""
  270. let tir_low = numberFormatter
  271. .string(from: (state.statistics?.Statistics.Distribution.Hypos.month ?? 0) as NSNumber) ?? ""
  272. let tir_high = numberFormatter
  273. .string(from: (state.statistics?.Statistics.Distribution.Hypers.month ?? 0) as NSNumber) ??
  274. ""
  275. let tir_ = numberFormatter
  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_ = numberFormatter
  288. .string(from: (state.statistics?.Statistics.Glucose.Average.ninetyDays ?? 0) as NSNumber) ??
  289. ""
  290. let median_ = numberFormatter
  291. .string(from: (state.statistics?.Statistics.Glucose.Median.ninetyDays ?? 0) as NSNumber) ??
  292. ""
  293. let tir_low = numberFormatter
  294. .string(
  295. from: (state.statistics?.Statistics.Distribution.Hypos.ninetyDays ?? 0) as NSNumber
  296. ) ??
  297. ""
  298. let tir_high = numberFormatter
  299. .string(
  300. from: (state.statistics?.Statistics.Distribution.Hypers.ninetyDays ?? 0) as NSNumber
  301. ) ??
  302. ""
  303. let tir_ = numberFormatter
  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_ = numberFormatter
  317. .string(from: (state.statistics?.Statistics.Glucose.Average.total ?? 0) as NSNumber) ?? ""
  318. let median_ = numberFormatter
  319. .string(from: (state.statistics?.Statistics.Glucose.Median.total ?? 0) as NSNumber) ?? ""
  320. let tir_low = numberFormatter
  321. .string(from: (state.statistics?.Statistics.Distribution.Hypos.total ?? 0) as NSNumber) ?? ""
  322. let tir_high = numberFormatter
  323. .string(from: (state.statistics?.Statistics.Distribution.Hypers.total ?? 0) as NSNumber) ??
  324. ""
  325. let tir_ = numberFormatter
  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: 120, 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. }
  358. }
  359. HStack {
  360. Group {
  361. Text(
  362. NSLocalizedString("Low (<", comment: " ") +
  363. (
  364. targetFormatter
  365. .string(from: state.settingsManager.preferences.low as NSNumber) ?? ""
  366. ) + ")"
  367. ).font(.caption2)
  368. .foregroundColor(.secondary)
  369. Text(tir_low + " %").font(.system(size: 12)).foregroundColor(.loopRed)
  370. Text("Normal").font(.caption2).foregroundColor(.secondary)
  371. Text(tir_ + " %").font(.system(size: 12)).foregroundColor(.loopGreen)
  372. Text(
  373. NSLocalizedString("High (>", comment: " ") +
  374. (
  375. targetFormatter
  376. .string(from: state.settingsManager.preferences.high as NSNumber) ?? ""
  377. ) + ")"
  378. )
  379. .font(.caption2).foregroundColor(.secondary)
  380. Text(tir_high + " %").font(.system(size: 12)).foregroundColor(.loopYellow)
  381. }
  382. }
  383. HStack {
  384. Group {
  385. Text("HbA1c").font(.caption2).foregroundColor(.secondary)
  386. Text(hba1c_).font(.system(size: 12))
  387. if !state.settingsManager.preferences.displaySD {
  388. Text(
  389. NSLocalizedString("CV", comment: "CV")
  390. ).font(.caption2).foregroundColor(.secondary)
  391. Text(cv_).font(.system(size: 12))
  392. } else {
  393. Text(
  394. NSLocalizedString("SD", comment: "SD")
  395. ).font(.caption2).foregroundColor(.secondary)
  396. Text(sd_).font(.system(size: 12))
  397. }
  398. Text(
  399. NSLocalizedString("All ", comment: "") +
  400. // getString(state.statistics?.GlucoseStorage_Days, false) +
  401. NSLocalizedString(" days", comment: "")
  402. ).font(.caption2).foregroundColor(.secondary)
  403. Text(hba1c_all).font(.system(size: 12))
  404. }
  405. }
  406. if state.settingsManager.preferences.displayLoops {
  407. HStack {
  408. Group {
  409. Text("Loops").font(.caption2).foregroundColor(.secondary)
  410. Text(
  411. tirFormatter
  412. .string(from: (state.statistics?.Statistics.LoopCycles.loops ?? 0) as NSNumber) ?? ""
  413. ).font(.system(size: 12))
  414. Text("Average Interval").font(.caption2)
  415. .foregroundColor(.secondary)
  416. Text(
  417. targetFormatter
  418. .string(from: (state.statistics?.Statistics.LoopCycles.avg_interval ?? 0) as NSNumber) ??
  419. ""
  420. ).font(.system(size: 12))
  421. Text("Median Duration").font(.caption2)
  422. .foregroundColor(.secondary)
  423. Text(
  424. numberFormatter
  425. .string(
  426. from: (state.statistics?.Statistics.LoopCycles.median_duration ?? 0) as NSNumber
  427. ) ?? ""
  428. ).font(.system(size: 12))
  429. }
  430. }
  431. }
  432. }
  433. var legendPanel: some View {
  434. ZStack {
  435. HStack(alignment: .center) {
  436. Group {
  437. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  438. Text("BG")
  439. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  440. }
  441. Group {
  442. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  443. .padding(.leading, 8)
  444. Text("IOB")
  445. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  446. }
  447. Group {
  448. Circle().fill(Color.zt).frame(width: 8, height: 8)
  449. .padding(.leading, 8)
  450. Text("ZT")
  451. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  452. }
  453. Group {
  454. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  455. .padding(.leading, 8)
  456. Text("COB")
  457. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  458. }
  459. Group {
  460. Circle().fill(Color.uam).frame(width: 8, height: 8)
  461. .padding(.leading, 8)
  462. Text("UAM")
  463. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  464. }
  465. if let eventualBG = state.eventualBG {
  466. Text(
  467. "⇢ " + numberFormatter.string(
  468. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  469. )!
  470. )
  471. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  472. }
  473. }
  474. .frame(maxWidth: .infinity)
  475. }
  476. }
  477. var mainChart: some View {
  478. ZStack {
  479. if state.animatedBackground {
  480. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  481. .ignoresSafeArea()
  482. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  483. }
  484. MainChartView(
  485. glucose: $state.glucose,
  486. suggestion: $state.suggestion,
  487. statistcs: $state.statistics,
  488. tempBasals: $state.tempBasals,
  489. boluses: $state.boluses,
  490. suspensions: $state.suspensions,
  491. hours: .constant(state.filteredHours),
  492. maxBasal: $state.maxBasal,
  493. autotunedBasalProfile: $state.autotunedBasalProfile,
  494. basalProfile: $state.basalProfile,
  495. tempTargets: $state.tempTargets,
  496. carbs: $state.carbs,
  497. timerDate: $state.timerDate,
  498. units: $state.units
  499. )
  500. }
  501. // .padding(.bottom)
  502. .modal(for: .dataTable, from: self)
  503. }
  504. @ViewBuilder private func bottomPanel(_ geo: GeometryProxy) -> some View {
  505. ZStack {
  506. Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
  507. HStack {
  508. Button { state.showModal(for: .addCarbs) }
  509. label: {
  510. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  511. Image("carbs")
  512. .renderingMode(.template)
  513. .resizable()
  514. .frame(width: 24, height: 24)
  515. .foregroundColor(.loopGreen)
  516. .padding(8)
  517. if let carbsReq = state.carbsRequired {
  518. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  519. .font(.caption)
  520. .foregroundColor(.white)
  521. .padding(4)
  522. .background(Capsule().fill(Color.red))
  523. }
  524. }
  525. }
  526. Spacer()
  527. Button { state.showModal(for: .addTempTarget) }
  528. label: {
  529. Image("target")
  530. .renderingMode(.template)
  531. .resizable()
  532. .frame(width: 24, height: 24)
  533. .padding(8)
  534. }.foregroundColor(.loopYellow)
  535. Spacer()
  536. Button { state.showModal(for: .bolus(waitForSuggestion: false)) }
  537. label: {
  538. Image("bolus")
  539. .renderingMode(.template)
  540. .resizable()
  541. .frame(width: 24, height: 24)
  542. .padding(8)
  543. }.foregroundColor(.insulin)
  544. Spacer()
  545. if state.allowManualTemp {
  546. Button { state.showModal(for: .manualTempBasal) }
  547. label: {
  548. Image("bolus1")
  549. .renderingMode(.template)
  550. .resizable()
  551. .frame(width: 24, height: 24)
  552. .padding(8)
  553. }.foregroundColor(.insulin)
  554. Spacer()
  555. }
  556. Button { state.showModal(for: .settings) }
  557. label: {
  558. Image("settings1")
  559. .renderingMode(.template)
  560. .resizable()
  561. .frame(width: 24, height: 24)
  562. .padding(8)
  563. }.foregroundColor(.loopGray)
  564. }
  565. .padding(.horizontal, 24)
  566. .padding(.bottom, geo.safeAreaInsets.bottom)
  567. }
  568. }
  569. var body: some View {
  570. GeometryReader { geo in
  571. VStack(spacing: 0) {
  572. header(geo)
  573. infoPanel
  574. mainChart
  575. legendPanel
  576. statPanel()
  577. bottomPanel(geo)
  578. }
  579. .edgesIgnoringSafeArea(.vertical)
  580. }
  581. .onAppear(perform: configureView)
  582. .navigationTitle("Home")
  583. .navigationBarHidden(true)
  584. .ignoresSafeArea(.keyboard)
  585. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  586. popup
  587. .padding()
  588. .background(
  589. RoundedRectangle(cornerRadius: 8, style: .continuous)
  590. .fill(Color(UIColor.darkGray))
  591. )
  592. .onTapGesture {
  593. isStatusPopupPresented = false
  594. }
  595. .gesture(
  596. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  597. .onEnded { value in
  598. if value.translation.height < 0 {
  599. isStatusPopupPresented = false
  600. }
  601. }
  602. )
  603. }
  604. }
  605. private var popup: some View {
  606. VStack(alignment: .leading, spacing: 4) {
  607. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  608. .padding(.bottom, 4)
  609. if let suggestion = state.suggestion {
  610. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  611. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  612. } else {
  613. Text("No sugestion found").font(.body).foregroundColor(.white)
  614. }
  615. if let errorMessage = state.errorMessage, let date = state.errorDate {
  616. Text("Error at \(dateFormatter.string(from: date))")
  617. .foregroundColor(.white)
  618. .font(.headline)
  619. .padding(.bottom, 4)
  620. .padding(.top, 8)
  621. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  622. }
  623. }
  624. }
  625. private func colorOfGlucose(_ glucose: Decimal) -> Color {
  626. switch glucose {
  627. case 4 ... 8,
  628. 30 ... 46,
  629. 72 ... 144:
  630. return .loopGreen
  631. case 0 ... 4,
  632. 20 ... 71:
  633. return .loopRed
  634. default:
  635. return .loopYellow
  636. }
  637. }
  638. }
  639. }