HomeRootView.swift 32 KB

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