HomeRootView.swift 31 KB

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