HomeRootView.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. import CoreData
  2. import SpriteKit
  3. import SwiftDate
  4. import SwiftUI
  5. import Swinject
  6. extension Home {
  7. struct RootView: BaseView {
  8. let resolver: Resolver
  9. @StateObject var state = StateModel()
  10. @State var isStatusPopupPresented = false
  11. @State var showCancelAlert = false
  12. @Environment(\.managedObjectContext) var moc
  13. @Environment(\.colorScheme) var colorScheme
  14. @FetchRequest(
  15. entity: Override.entity(),
  16. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
  17. ) var fetchedPercent: FetchedResults<Override>
  18. @FetchRequest(
  19. entity: OverridePresets.entity(),
  20. sortDescriptors: [NSSortDescriptor(key: "name", ascending: true)], predicate: NSPredicate(
  21. format: "name != %@", "" as String
  22. )
  23. ) var fetchedProfiles: FetchedResults<OverridePresets>
  24. @FetchRequest(
  25. entity: TempTargets.entity(),
  26. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
  27. ) var sliderTTpresets: FetchedResults<TempTargets>
  28. @FetchRequest(
  29. entity: TempTargetsSlider.entity(),
  30. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
  31. ) var enactedSliderTT: FetchedResults<TempTargetsSlider>
  32. private var numberFormatter: NumberFormatter {
  33. let formatter = NumberFormatter()
  34. formatter.numberStyle = .decimal
  35. formatter.maximumFractionDigits = 2
  36. return formatter
  37. }
  38. private var fetchedTargetFormatter: NumberFormatter {
  39. let formatter = NumberFormatter()
  40. formatter.numberStyle = .decimal
  41. if state.units == .mmolL {
  42. formatter.maximumFractionDigits = 1
  43. } else { formatter.maximumFractionDigits = 0 }
  44. return formatter
  45. }
  46. private var targetFormatter: NumberFormatter {
  47. let formatter = NumberFormatter()
  48. formatter.numberStyle = .decimal
  49. formatter.maximumFractionDigits = 1
  50. return formatter
  51. }
  52. private var tirFormatter: NumberFormatter {
  53. let formatter = NumberFormatter()
  54. formatter.numberStyle = .decimal
  55. formatter.maximumFractionDigits = 0
  56. return formatter
  57. }
  58. private var dateFormatter: DateFormatter {
  59. let dateFormatter = DateFormatter()
  60. dateFormatter.timeStyle = .short
  61. return dateFormatter
  62. }
  63. private var spriteScene: SKScene {
  64. let scene = SnowScene()
  65. scene.scaleMode = .resizeFill
  66. scene.backgroundColor = .clear
  67. return scene
  68. }
  69. // @ViewBuilder func header(_ geo: GeometryProxy) -> some View {
  70. // HStack(alignment: .bottom) {
  71. // Spacer()
  72. // cobIobView
  73. // Spacer()
  74. // glucoseView
  75. // Spacer()
  76. // pumpView
  77. // Spacer()
  78. // loopView
  79. // Spacer()
  80. // }
  81. // .frame(maxWidth: .infinity)
  82. // .padding(.top, 10 + geo.safeAreaInsets.top)
  83. // .padding(.bottom, 10)
  84. // .background(Color.gray.opacity(0.3))
  85. // }
  86. @ViewBuilder func header(_: GeometryProxy) -> some View {
  87. let colour: Color = colorScheme == .dark ? .gray.opacity(0.1) : .white
  88. RoundedRectangle(cornerRadius: 15)
  89. .fill(colour)
  90. // .frame(height: UIScreen.main.bounds.height / 6)
  91. .overlay(
  92. HStack(alignment: .bottom) {
  93. Spacer()
  94. cobIobView
  95. Spacer()
  96. glucoseView
  97. Spacer()
  98. pumpView
  99. Spacer()
  100. loopView
  101. Spacer()
  102. }
  103. // .frame(maxWidth: .infinity)
  104. // .padding(.top, 10 + geo.safeAreaInsets.top)
  105. // .padding(.bottom, 10)
  106. )
  107. .frame(height: UIScreen.main.bounds.height / 7)
  108. .padding([.leading, .trailing], 10)
  109. }
  110. var cobIobView: some View {
  111. VStack(alignment: .leading, spacing: 12) {
  112. HStack {
  113. Text("IOB").font(.footnote).foregroundColor(.secondary)
  114. Text(
  115. (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
  116. NSLocalizedString(" U", comment: "Insulin unit")
  117. )
  118. .font(.footnote).fontWeight(.bold)
  119. }.frame(alignment: .top)
  120. HStack {
  121. Text("COB").font(.footnote).foregroundColor(.secondary)
  122. Text(
  123. (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
  124. NSLocalizedString(" g", comment: "gram of carbs")
  125. )
  126. .font(.footnote).fontWeight(.bold)
  127. }.frame(alignment: .bottom)
  128. }
  129. }
  130. var glucoseView: some View {
  131. CurrentGlucoseView(
  132. recentGlucose: $state.recentGlucose,
  133. timerDate: $state.timerDate,
  134. delta: $state.glucoseDelta,
  135. units: $state.units,
  136. alarm: $state.alarm,
  137. lowGlucose: $state.lowGlucose,
  138. highGlucose: $state.highGlucose
  139. )
  140. .onTapGesture {
  141. if state.alarm == nil {
  142. state.openCGM()
  143. } else {
  144. state.showModal(for: .snooze)
  145. }
  146. }
  147. .onLongPressGesture {
  148. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  149. impactHeavy.impactOccurred()
  150. if state.alarm == nil {
  151. state.showModal(for: .snooze)
  152. } else {
  153. state.openCGM()
  154. }
  155. }
  156. }
  157. var pumpView: some View {
  158. PumpView(
  159. reservoir: $state.reservoir,
  160. battery: $state.battery,
  161. name: $state.pumpName,
  162. expiresAtDate: $state.pumpExpiresAtDate,
  163. timerDate: $state.timerDate
  164. )
  165. .onTapGesture {
  166. if state.pumpDisplayState != nil {
  167. state.setupPump = true
  168. }
  169. }
  170. }
  171. var loopView: some View {
  172. LoopView(
  173. suggestion: $state.suggestion,
  174. enactedSuggestion: $state.enactedSuggestion,
  175. closedLoop: $state.closedLoop,
  176. timerDate: $state.timerDate,
  177. isLooping: $state.isLooping,
  178. lastLoopDate: $state.lastLoopDate,
  179. manualTempBasal: $state.manualTempBasal
  180. ).onTapGesture {
  181. isStatusPopupPresented = true
  182. }.onLongPressGesture {
  183. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  184. impactHeavy.impactOccurred()
  185. state.runLoop()
  186. }
  187. }
  188. var tempBasalString: String? {
  189. guard let tempRate = state.tempRate else {
  190. return nil
  191. }
  192. let rateString = numberFormatter.string(from: tempRate as NSNumber) ?? "0"
  193. var manualBasalString = ""
  194. if state.apsManager.isManualTempBasal {
  195. manualBasalString = NSLocalizedString(
  196. " - Manual Basal ⚠️",
  197. comment: "Manual Temp basal"
  198. )
  199. }
  200. return rateString + NSLocalizedString(" U/hr", comment: "Unit per hour with space") + manualBasalString
  201. }
  202. var tempTargetString: String? {
  203. guard let tempTarget = state.tempTarget else {
  204. return nil
  205. }
  206. let target = tempTarget.targetBottom ?? 0
  207. let unitString = targetFormatter.string(from: (tempTarget.targetBottom?.asMmolL ?? 0) as NSNumber) ?? ""
  208. let rawString = (tirFormatter.string(from: (tempTarget.targetBottom ?? 0) as NSNumber) ?? "") + " " + state.units
  209. .rawValue
  210. var string = ""
  211. if sliderTTpresets.first?.active ?? false {
  212. let hbt = sliderTTpresets.first?.hbt ?? 0
  213. string = ", " + (tirFormatter.string(from: state.infoPanelTTPercentage(hbt, target) as NSNumber) ?? "") + " %"
  214. }
  215. let percentString = state
  216. .units == .mmolL ? (unitString + " mmol/L" + string) : (rawString + (string == "0" ? "" : string))
  217. return tempTarget.displayName + " " + percentString
  218. }
  219. var overrideString: String? {
  220. guard fetchedPercent.first?.enabled ?? false else {
  221. return nil
  222. }
  223. var percentString = "\((fetchedPercent.first?.percentage ?? 100).formatted(.number)) %"
  224. var target = (fetchedPercent.first?.target ?? 100) as Decimal
  225. let indefinite = (fetchedPercent.first?.indefinite ?? false)
  226. let unit = state.units.rawValue
  227. if state.units == .mmolL {
  228. target = target.asMmolL
  229. }
  230. var targetString = (fetchedTargetFormatter.string(from: target as NSNumber) ?? "") + " " + unit
  231. if tempTargetString != nil || target == 0 { targetString = "" }
  232. percentString = percentString == "100 %" ? "" : percentString
  233. let duration = (fetchedPercent.first?.duration ?? 0) as Decimal
  234. let addedMinutes = Int(duration)
  235. let date = fetchedPercent.first?.date ?? Date()
  236. var newDuration: Decimal = 0
  237. if date.addingTimeInterval(addedMinutes.minutes.timeInterval) > Date() {
  238. newDuration = Decimal(Date().distance(to: date.addingTimeInterval(addedMinutes.minutes.timeInterval)).minutes)
  239. }
  240. var durationString = indefinite ?
  241. "" : newDuration >= 1 ?
  242. (newDuration.formatted(.number.grouping(.never).rounded().precision(.fractionLength(0))) + " min") :
  243. (
  244. newDuration > 0 ? (
  245. (newDuration * 60).formatted(.number.grouping(.never).rounded().precision(.fractionLength(0))) + " s"
  246. ) :
  247. ""
  248. )
  249. let smbToggleString = (fetchedPercent.first?.smbIsOff ?? false) ? " \u{20e0}" : ""
  250. var comma1 = ", "
  251. var comma2 = comma1
  252. var comma3 = comma1
  253. if targetString == "" || percentString == "" { comma1 = "" }
  254. if durationString == "" { comma2 = "" }
  255. if smbToggleString == "" { comma3 = "" }
  256. if percentString == "", targetString == "" {
  257. comma1 = ""
  258. comma2 = ""
  259. }
  260. if percentString == "", targetString == "", smbToggleString == "" {
  261. durationString = ""
  262. comma1 = ""
  263. comma2 = ""
  264. comma3 = ""
  265. }
  266. if durationString == "" {
  267. comma2 = ""
  268. }
  269. if smbToggleString == "" {
  270. comma3 = ""
  271. }
  272. if durationString == "", !indefinite {
  273. return nil
  274. }
  275. return percentString + comma1 + targetString + comma2 + durationString + comma3 + smbToggleString
  276. }
  277. var infoPanel: some View {
  278. HStack(alignment: .center) {
  279. if state.pumpSuspended {
  280. Text("Pump suspended")
  281. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGray)
  282. .padding(.leading, 8)
  283. } else if let tempBasalString = tempBasalString {
  284. Text(tempBasalString)
  285. .font(.system(size: 12, weight: .bold))
  286. .foregroundColor(.insulin)
  287. .padding(.leading, 8)
  288. }
  289. if let tempTargetString = tempTargetString {
  290. Text(tempTargetString)
  291. .font(.caption)
  292. .foregroundColor(.secondary)
  293. }
  294. Spacer()
  295. if let overrideString = overrideString {
  296. Text("👤 " + overrideString)
  297. .font(.system(size: 12))
  298. .foregroundColor(.secondary)
  299. .padding(.trailing, 8)
  300. }
  301. if state.closedLoop, state.settingsManager.preferences.maxIOB == 0 {
  302. Text("Max IOB: 0").font(.callout).foregroundColor(.orange).padding(.trailing, 20)
  303. }
  304. if let progress = state.bolusProgress {
  305. Text("Bolusing")
  306. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  307. ProgressView(value: Double(progress))
  308. .progressViewStyle(BolusProgressViewStyle())
  309. .padding(.trailing, 8)
  310. .onTapGesture {
  311. state.cancelBolus()
  312. }
  313. }
  314. }
  315. .frame(maxWidth: .infinity, maxHeight: 30)
  316. }
  317. var legendPanel: some View {
  318. ZStack {
  319. HStack(alignment: .center) {
  320. Group {
  321. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  322. Text("BG")
  323. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  324. }
  325. Group {
  326. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  327. .padding(.leading, 8)
  328. Text("IOB")
  329. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  330. }
  331. Group {
  332. Circle().fill(Color.zt).frame(width: 8, height: 8)
  333. .padding(.leading, 8)
  334. Text("ZT")
  335. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  336. }
  337. Group {
  338. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  339. .padding(.leading, 8)
  340. Text("COB")
  341. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  342. }
  343. Group {
  344. Circle().fill(Color.uam).frame(width: 8, height: 8)
  345. .padding(.leading, 8)
  346. Text("UAM")
  347. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  348. }
  349. if let eventualBG = state.eventualBG {
  350. Text(
  351. "⇢ " + numberFormatter.string(
  352. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  353. )!
  354. )
  355. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  356. }
  357. }
  358. .frame(maxWidth: .infinity)
  359. .padding([.bottom], 20)
  360. }
  361. }
  362. var mainChart: some View {
  363. ZStack {
  364. if state.animatedBackground {
  365. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  366. .ignoresSafeArea()
  367. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  368. }
  369. MainChartView(
  370. glucose: $state.glucose,
  371. isManual: $state.isManual,
  372. suggestion: $state.suggestion,
  373. tempBasals: $state.tempBasals,
  374. boluses: $state.boluses,
  375. suspensions: $state.suspensions,
  376. announcement: $state.announcement,
  377. hours: .constant(state.filteredHours),
  378. maxBasal: $state.maxBasal,
  379. autotunedBasalProfile: $state.autotunedBasalProfile,
  380. basalProfile: $state.basalProfile,
  381. tempTargets: $state.tempTargets,
  382. carbs: $state.carbs,
  383. timerDate: $state.timerDate,
  384. units: $state.units,
  385. smooth: $state.smooth,
  386. highGlucose: $state.highGlucose,
  387. lowGlucose: $state.lowGlucose,
  388. screenHours: $state.screenHours,
  389. displayXgridLines: $state.displayXgridLines,
  390. displayYgridLines: $state.displayYgridLines,
  391. thresholdLines: $state.thresholdLines,
  392. state: state
  393. )
  394. }
  395. .padding(.bottom)
  396. .modal(for: .dataTable, from: self)
  397. }
  398. // MARK: PICKER IN SEGEMENTED STYLE TO CHOOSE THE X AXIS SCALE OF THE GRAPH
  399. @ViewBuilder private func pickerPanel(_: GeometryProxy) -> some View {
  400. HStack {
  401. Picker("Scale", selection: $state.scale) {
  402. ForEach(Home.StateModel.Scale.allCases) { scale in
  403. Text("\(scale.rawValue)h").tag(Optional(scale))
  404. }
  405. }
  406. .pickerStyle(.segmented)
  407. .background(Color.clear)
  408. .frame(width: UIScreen.main.bounds.width / 1.5, height: 40, alignment: .center)
  409. .disabled(state.bolusProgress != nil)
  410. }
  411. .padding(.vertical, 1)
  412. }
  413. @ViewBuilder private func profiles(_: GeometryProxy) -> some View {
  414. let colour: Color = colorScheme == .dark ? .black : .white
  415. let colourRectangle: Color = colorScheme == .dark ? .gray.opacity(0.1) : .white
  416. ZStack {
  417. Rectangle()
  418. // .fill(Color.gray.opacity(0.3))
  419. .fill(colourRectangle)
  420. .frame(maxHeight: 40)
  421. .cornerRadius(15)
  422. .padding([.leading, .trailing], 10)
  423. let cancel = fetchedPercent.first?.enabled ?? false
  424. HStack(spacing: cancel ? 25 : 15) {
  425. Text(selectedProfile().name).foregroundColor(.secondary)
  426. if cancel, selectedProfile().isOn {
  427. Button { showCancelAlert.toggle() }
  428. label: {
  429. Image(systemName: "xmark")
  430. .foregroundStyle(.secondary)
  431. }
  432. }
  433. Button { state.showModal(for: .overrideProfilesConfig) }
  434. label: {
  435. Image(systemName: "person.3.sequence.fill")
  436. .symbolRenderingMode(.palette)
  437. .foregroundStyle(
  438. !(fetchedPercent.first?.enabled ?? false) ? .green : .cyan,
  439. !(fetchedPercent.first?.enabled ?? false) ? .cyan : .green,
  440. .purple
  441. )
  442. }
  443. }
  444. }
  445. .alert(
  446. "Return to Normal?", isPresented: $showCancelAlert,
  447. actions: {
  448. Button("No", role: .cancel) {}
  449. Button("Yes", role: .destructive) {
  450. state.cancelProfile()
  451. }
  452. }, message: { Text("This will change settings back to your normal profile.") }
  453. )
  454. Rectangle().fill(colour).frame(maxHeight: 1)
  455. }
  456. private func selectedProfile() -> (name: String, isOn: Bool) {
  457. var profileString = ""
  458. var display: Bool = false
  459. let duration = (fetchedPercent.first?.duration ?? 0) as Decimal
  460. let indefinite = fetchedPercent.first?.indefinite ?? false
  461. let addedMinutes = Int(duration)
  462. let date = fetchedPercent.first?.date ?? Date()
  463. if date.addingTimeInterval(addedMinutes.minutes.timeInterval) > Date() || indefinite {
  464. display.toggle()
  465. }
  466. if fetchedPercent.first?.enabled ?? false, !(fetchedPercent.first?.isPreset ?? false), display {
  467. profileString = NSLocalizedString("Custom Profile", comment: "Custom but unsaved Profile")
  468. } else if !(fetchedPercent.first?.enabled ?? false) || !display {
  469. profileString = NSLocalizedString("Normal Profile", comment: "Your normal Profile. Use a short string")
  470. } else {
  471. let id_ = fetchedPercent.first?.id ?? ""
  472. let profile = fetchedProfiles.filter({ $0.id == id_ }).first
  473. if profile != nil {
  474. profileString = profile?.name?.description ?? ""
  475. }
  476. }
  477. return (name: profileString, isOn: display)
  478. }
  479. @ViewBuilder private func bottomPanel(_ geo: GeometryProxy) -> some View {
  480. let colourRectangle: Color = colorScheme == .dark ? .gray.opacity(0.1) : .white
  481. ZStack {
  482. Rectangle()
  483. .fill(colourRectangle)
  484. .frame(height: 50 + geo.safeAreaInsets.bottom)
  485. .cornerRadius(15)
  486. .padding([.leading, .trailing], 10)
  487. HStack {
  488. Button { state.showModal(for: .addCarbs(editMode: false, override: false)) }
  489. label: {
  490. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  491. Image("carbs")
  492. .renderingMode(.template)
  493. .resizable()
  494. .frame(width: 24, height: 24)
  495. .foregroundColor(.loopYellow)
  496. .padding(8)
  497. if let carbsReq = state.carbsRequired {
  498. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  499. .font(.caption)
  500. .foregroundColor(.white)
  501. .padding(4)
  502. .background(Capsule().fill(Color.red))
  503. }
  504. }
  505. }.buttonStyle(.borderless)
  506. Spacer()
  507. Button {
  508. state.showModal(for: .bolus(
  509. waitForSuggestion: true,
  510. fetch: false
  511. ))
  512. }
  513. label: {
  514. Image("bolus")
  515. .renderingMode(.template)
  516. .resizable()
  517. .frame(width: 24, height: 24)
  518. .padding(8)
  519. }
  520. .foregroundColor(.insulin)
  521. .buttonStyle(.borderless)
  522. Spacer()
  523. if state.allowManualTemp {
  524. Button { state.showModal(for: .manualTempBasal) }
  525. label: {
  526. Image("bolus1")
  527. .renderingMode(.template)
  528. .resizable()
  529. .frame(width: 24, height: 24)
  530. .padding(8)
  531. }
  532. .foregroundColor(.insulin)
  533. .buttonStyle(.borderless)
  534. Spacer()
  535. }
  536. Button { state.showModal(for: .addTempTarget) }
  537. label: {
  538. Image("target")
  539. .renderingMode(.template)
  540. .resizable()
  541. .frame(width: 24, height: 24)
  542. .padding(8)
  543. }
  544. .foregroundColor(.loopGreen)
  545. .buttonStyle(.borderless)
  546. Spacer()
  547. //MARK: CANCEL OF PROFILE HAS TO BE IMPLEMENTED
  548. //MAYBE WITH A SMALL INDICATOR AT THE SYMBOL
  549. Button {
  550. state.showModal(for: .overrideProfilesConfig)
  551. } label: {
  552. Image(systemName: "person.fill")
  553. .renderingMode(.template)
  554. .resizable()
  555. .frame(width: 24, height: 24)
  556. .padding(8)
  557. }
  558. .foregroundColor(.purple)
  559. .buttonStyle(.borderless)
  560. Spacer()
  561. Button { state.showModal(for: .statistics)
  562. }
  563. label: {
  564. Image(systemName: "chart.xyaxis.line")
  565. .renderingMode(.template)
  566. .resizable()
  567. .frame(width: 24, height: 24)
  568. .padding(8)
  569. }
  570. .foregroundColor(.purple)
  571. .buttonStyle(.borderless)
  572. Spacer()
  573. Button { state.showModal(for: .settings) }
  574. label: {
  575. Image("settings1")
  576. .renderingMode(.template)
  577. .resizable()
  578. .frame(width: 24, height: 24)
  579. .padding(8)
  580. }
  581. .foregroundColor(.loopGray)
  582. .buttonStyle(.borderless)
  583. }
  584. .padding(.horizontal, 24)
  585. // .padding(.bottom, geo.safeAreaInsets.bottom)
  586. }
  587. }
  588. var body: some View {
  589. let colourBackground: Color = colorScheme == .dark ? .black.opacity(0.5) : .gray.opacity(0.3)
  590. let colourChart: Color = colorScheme == .dark ? .gray.opacity(0.1) : .white
  591. GeometryReader { geo in
  592. VStack(spacing: 0) {
  593. header(geo)
  594. .padding(.top, 60)
  595. infoPanel
  596. RoundedRectangle(cornerRadius: 15)
  597. .fill(colourChart)
  598. .shadow(radius: 3)
  599. .overlay(mainChart)
  600. .padding([.leading, .trailing], 10)
  601. .frame(height: UIScreen.main.bounds.height / 2)
  602. pickerPanel(geo)
  603. legendPanel
  604. .padding(.top, 10)
  605. // profiles(geo)
  606. // .padding(.top, 2)
  607. bottomPanel(geo)
  608. .padding(.top, 40)
  609. }
  610. .edgesIgnoringSafeArea(.vertical)
  611. }
  612. .onAppear(perform: configureView)
  613. .navigationTitle("Home")
  614. .background(colourBackground)
  615. .navigationBarHidden(true)
  616. .ignoresSafeArea(.keyboard)
  617. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  618. popup
  619. .padding()
  620. .background(
  621. RoundedRectangle(cornerRadius: 8, style: .continuous)
  622. .fill(Color(UIColor.darkGray))
  623. )
  624. .onTapGesture {
  625. isStatusPopupPresented = false
  626. }
  627. .gesture(
  628. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  629. .onEnded { value in
  630. if value.translation.height < 0 {
  631. isStatusPopupPresented = false
  632. }
  633. }
  634. )
  635. }
  636. }
  637. private var popup: some View {
  638. VStack(alignment: .leading, spacing: 4) {
  639. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  640. .padding(.bottom, 4)
  641. if let suggestion = state.suggestion {
  642. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  643. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  644. } else {
  645. Text("No sugestion found").font(.body).foregroundColor(.white)
  646. }
  647. if let errorMessage = state.errorMessage, let date = state.errorDate {
  648. Text(NSLocalizedString("Error at", comment: "") + " " + dateFormatter.string(from: date))
  649. .foregroundColor(.white)
  650. .font(.headline)
  651. .padding(.bottom, 4)
  652. .padding(.top, 8)
  653. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  654. } else if let suggestion = state.suggestion, (suggestion.bg ?? 100) == 400 {
  655. Text("Invalid CGM reading (HIGH).").font(.callout).bold().foregroundColor(.loopRed).padding(.top, 8)
  656. Text("SMBs and High Temps Disabled.").font(.caption).foregroundColor(.white).padding(.bottom, 4)
  657. }
  658. }
  659. }
  660. }
  661. }