HomeRootView.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. private var numberFormatter: NumberFormatter {
  48. let formatter = NumberFormatter()
  49. formatter.numberStyle = .decimal
  50. formatter.maximumFractionDigits = 2
  51. return formatter
  52. }
  53. private var fetchedTargetFormatter: NumberFormatter {
  54. let formatter = NumberFormatter()
  55. formatter.numberStyle = .decimal
  56. if state.units == .mmolL {
  57. formatter.maximumFractionDigits = 1
  58. } else { formatter.maximumFractionDigits = 0 }
  59. return formatter
  60. }
  61. private var targetFormatter: NumberFormatter {
  62. let formatter = NumberFormatter()
  63. formatter.numberStyle = .decimal
  64. formatter.maximumFractionDigits = 1
  65. return formatter
  66. }
  67. private var tirFormatter: NumberFormatter {
  68. let formatter = NumberFormatter()
  69. formatter.numberStyle = .decimal
  70. formatter.maximumFractionDigits = 0
  71. return formatter
  72. }
  73. private var dateFormatter: DateFormatter {
  74. let dateFormatter = DateFormatter()
  75. dateFormatter.timeStyle = .short
  76. return dateFormatter
  77. }
  78. private var spriteScene: SKScene {
  79. let scene = SnowScene()
  80. scene.scaleMode = .resizeFill
  81. scene.backgroundColor = .clear
  82. return scene
  83. }
  84. @ViewBuilder func header(_: GeometryProxy) -> some View {
  85. HStack {
  86. Spacer()
  87. pumpView
  88. Spacer()
  89. }
  90. }
  91. var cobIobView: some View {
  92. VStack(alignment: .leading, spacing: 12) {
  93. HStack {
  94. Text("IOB").font(.footnote).foregroundColor(.secondary)
  95. Text(
  96. (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
  97. NSLocalizedString(" U", comment: "Insulin unit")
  98. )
  99. .font(.footnote).fontWeight(.bold)
  100. }.frame(alignment: .top)
  101. HStack {
  102. Text("COB").font(.footnote).foregroundColor(.secondary)
  103. Text(
  104. (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
  105. NSLocalizedString(" g", comment: "gram of carbs")
  106. )
  107. .font(.footnote).fontWeight(.bold)
  108. }.frame(alignment: .bottom)
  109. }
  110. }
  111. var cobIobView2: some View {
  112. HStack {
  113. Text("IOB").font(.callout).foregroundColor(.secondary)
  114. Text(
  115. (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
  116. NSLocalizedString(" U", comment: "Insulin unit")
  117. )
  118. .font(.callout).fontWeight(.bold)
  119. Spacer()
  120. Text("COB").font(.callout).foregroundColor(.secondary)
  121. Text(
  122. (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
  123. NSLocalizedString(" g", comment: "gram of carbs")
  124. )
  125. .font(.callout).fontWeight(.bold)
  126. Spacer()
  127. }
  128. }
  129. var glucoseView: some View {
  130. CurrentGlucoseView(
  131. recentGlucose: $state.recentGlucose,
  132. timerDate: $state.timerDate,
  133. delta: $state.glucoseDelta,
  134. units: $state.units,
  135. alarm: $state.alarm,
  136. lowGlucose: $state.lowGlucose,
  137. highGlucose: $state.highGlucose
  138. )
  139. .onTapGesture {
  140. if state.alarm == nil {
  141. state.openCGM()
  142. } else {
  143. state.showModal(for: .snooze)
  144. }
  145. }
  146. .onLongPressGesture {
  147. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  148. impactHeavy.impactOccurred()
  149. if state.alarm == nil {
  150. state.showModal(for: .snooze)
  151. } else {
  152. state.openCGM()
  153. }
  154. }
  155. }
  156. var pumpView: some View {
  157. PumpView(
  158. reservoir: $state.reservoir,
  159. battery: $state.battery,
  160. name: $state.pumpName,
  161. expiresAtDate: $state.pumpExpiresAtDate,
  162. timerDate: $state.timerDate,
  163. state: state
  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. Text(
  289. "TINS: \(state.calculateTINS())" +
  290. NSLocalizedString(" U", comment: "Unit in number of units delivered (keep the space character!)")
  291. )
  292. .font(.system(size: 12, weight: .bold))
  293. .foregroundColor(.insulin)
  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. Spacer()
  327. Group {
  328. Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
  329. Text("BG")
  330. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
  331. }
  332. Group {
  333. Circle().fill(Color.insulin).frame(width: 8, height: 8)
  334. .padding(.leading, 8)
  335. Text("IOB")
  336. .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
  337. }
  338. Group {
  339. Circle().fill(Color.zt).frame(width: 8, height: 8)
  340. .padding(.leading, 8)
  341. Text("ZT")
  342. .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
  343. }
  344. // Spacer()
  345. //
  346. //// loopView.padding(.top, 16)
  347. //
  348. // Spacer()
  349. Group {
  350. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  351. Text("COB")
  352. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  353. }
  354. Group {
  355. Circle().fill(Color.uam).frame(width: 8, height: 8)
  356. .padding(.leading, 8)
  357. Text("UAM")
  358. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  359. }
  360. if let eventualBG = state.eventualBG {
  361. Text(
  362. "⇢ " + numberFormatter.string(
  363. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  364. )!
  365. )
  366. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  367. }
  368. Spacer()
  369. }
  370. .frame(maxWidth: .infinity)
  371. }
  372. }
  373. var timeInterval: some View {
  374. HStack {
  375. ForEach(timeButtons) { button in
  376. Text(button.active ? NSLocalizedString(button.label, comment: "") : button.number).onTapGesture {
  377. let index = timeButtons.firstIndex(where: { $0.label == button.label }) ?? 0
  378. highlightButtons(index)
  379. state.hours = button.hours
  380. }
  381. .foregroundStyle(button.active ? (colorScheme == .dark ? Color.white : Color.black).opacity(0.9) : .secondary)
  382. .frame(maxHeight: 30).padding(.horizontal, 8)
  383. .background(
  384. button.active ?
  385. // RGB(30, 60, 95)
  386. (
  387. colorScheme == .dark ? Color(red: 0.1176470588, green: 0.2352941176, blue: 0.3725490196) :
  388. Color.white
  389. ) :
  390. Color
  391. .clear
  392. )
  393. .cornerRadius(20)
  394. }
  395. }
  396. .shadow(
  397. color: Color.black.opacity(colorScheme == .dark ? 0.75 : 0.33),
  398. radius: colorScheme == .dark ? 5 : 3
  399. )
  400. .font(buttonFont)
  401. }
  402. var mainChart: some View {
  403. ZStack {
  404. if state.animatedBackground {
  405. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  406. .ignoresSafeArea()
  407. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  408. }
  409. MainChartView(
  410. glucose: $state.glucose,
  411. isManual: $state.isManual,
  412. suggestion: $state.suggestion,
  413. tempBasals: $state.tempBasals,
  414. boluses: $state.boluses,
  415. suspensions: $state.suspensions,
  416. announcement: $state.announcement,
  417. hours: .constant(state.filteredHours),
  418. maxBasal: $state.maxBasal,
  419. autotunedBasalProfile: $state.autotunedBasalProfile,
  420. basalProfile: $state.basalProfile,
  421. tempTargets: $state.tempTargets,
  422. carbs: $state.carbs,
  423. timerDate: $state.timerDate,
  424. units: $state.units,
  425. smooth: $state.smooth,
  426. highGlucose: $state.highGlucose,
  427. lowGlucose: $state.lowGlucose,
  428. screenHours: $state.hours,
  429. displayXgridLines: $state.displayXgridLines,
  430. displayYgridLines: $state.displayYgridLines,
  431. thresholdLines: $state.thresholdLines
  432. )
  433. }
  434. .padding(.bottom)
  435. .modal(for: .dataTable, from: self)
  436. }
  437. private func selectedProfile() -> (name: String, isOn: Bool) {
  438. var profileString = ""
  439. var display: Bool = false
  440. let duration = (fetchedPercent.first?.duration ?? 0) as Decimal
  441. let indefinite = fetchedPercent.first?.indefinite ?? false
  442. let addedMinutes = Int(duration)
  443. let date = fetchedPercent.first?.date ?? Date()
  444. if date.addingTimeInterval(addedMinutes.minutes.timeInterval) > Date() || indefinite {
  445. display.toggle()
  446. }
  447. if fetchedPercent.first?.enabled ?? false, !(fetchedPercent.first?.isPreset ?? false), display {
  448. profileString = NSLocalizedString("Custom Profile", comment: "Custom but unsaved Profile")
  449. } else if !(fetchedPercent.first?.enabled ?? false) || !display {
  450. profileString = NSLocalizedString("Normal Profile", comment: "Your normal Profile. Use a short string")
  451. } else {
  452. let id_ = fetchedPercent.first?.id ?? ""
  453. let profile = fetchedProfiles.filter({ $0.id == id_ }).first
  454. if profile != nil {
  455. profileString = profile?.name?.description ?? ""
  456. }
  457. }
  458. return (name: profileString, isOn: display)
  459. }
  460. func highlightButtons(_ int: Int) {
  461. var index = 0
  462. repeat {
  463. if index == int {
  464. timeButtons[index].active = true
  465. } else {
  466. timeButtons[index].active = false
  467. }
  468. index += 1
  469. } while index < timeButtons.count
  470. }
  471. @ViewBuilder private func bottomPanel(_: GeometryProxy) -> some View {
  472. let colorRectangle: Color = colorScheme == .dark ? Color.black.opacity(0.8) : Color.white
  473. let colorIcon: Color = (colorScheme == .dark ? Color.white : Color.black).opacity(0.9)
  474. ZStack {
  475. Rectangle()
  476. .fill(colorRectangle)
  477. .frame(height: UIScreen.main.bounds.height / 13)
  478. .cornerRadius(15)
  479. .shadow(
  480. color: colorScheme == .dark ? Color(red: 0.02745098039, green: 0.1098039216, blue: 0.1411764706) : Color
  481. .black.opacity(0.33),
  482. radius: 3
  483. )
  484. .padding([.leading, .trailing], 10)
  485. HStack {
  486. Button { state.showModal(for: .addCarbs(editMode: false, override: false)) }
  487. label: {
  488. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  489. Image("carbs")
  490. .renderingMode(.template)
  491. .resizable()
  492. .frame(width: 24, height: 24)
  493. .foregroundColor(colorIcon)
  494. .padding(8)
  495. if let carbsReq = state.carbsRequired {
  496. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  497. .font(.caption)
  498. .foregroundColor(.white)
  499. .padding(4)
  500. .background(Capsule().fill(Color.red))
  501. }
  502. }
  503. }.buttonStyle(.borderless)
  504. Spacer()
  505. Button { state.showModal(for: .addTempTarget) }
  506. label: {
  507. Image("target")
  508. .renderingMode(.template)
  509. .resizable()
  510. .frame(width: 24, height: 24)
  511. .padding(8)
  512. }
  513. .foregroundColor(colorIcon)
  514. .buttonStyle(.borderless)
  515. Spacer()
  516. Button {
  517. state.showModal(for: .bolus(
  518. waitForSuggestion: true,
  519. fetch: false
  520. ))
  521. }
  522. label: {
  523. Image("bolus")
  524. .renderingMode(.template)
  525. .resizable()
  526. .frame(width: 24, height: 24)
  527. .padding(8)
  528. }
  529. .foregroundColor(colorIcon)
  530. .buttonStyle(.borderless)
  531. Spacer()
  532. if state.allowManualTemp {
  533. Button { state.showModal(for: .manualTempBasal) }
  534. label: {
  535. Image("bolus1")
  536. .renderingMode(.template)
  537. .resizable()
  538. .frame(width: 24, height: 24)
  539. .padding(8)
  540. }
  541. .foregroundColor(colorIcon)
  542. .buttonStyle(.borderless)
  543. Spacer()
  544. }
  545. // MARK: CANCEL OF PROFILE HAS TO BE IMPLEMENTED
  546. // MAYBE WITH A SMALL INDICATOR AT THE SYMBOL
  547. Button {
  548. state.showModal(for: .overrideProfilesConfig)
  549. } label: {
  550. Image(systemName: "person")
  551. .renderingMode(.template)
  552. .resizable()
  553. .frame(width: 24, height: 24)
  554. .padding(8)
  555. }
  556. .foregroundColor(colorIcon)
  557. .buttonStyle(.borderless)
  558. Spacer()
  559. Button { state.showModal(for: .statistics)
  560. }
  561. label: {
  562. Image(systemName: "chart.xyaxis.line")
  563. .renderingMode(.template)
  564. .resizable()
  565. .frame(width: 24, height: 24)
  566. .padding(8)
  567. }
  568. .foregroundColor(colorIcon)
  569. .buttonStyle(.borderless)
  570. Spacer()
  571. Button { state.showModal(for: .settings) }
  572. label: {
  573. Image("settings1")
  574. .renderingMode(.template)
  575. .resizable()
  576. .frame(width: 24, height: 24)
  577. .padding(8)
  578. }
  579. .foregroundColor(colorIcon)
  580. .buttonStyle(.borderless)
  581. }
  582. .padding(.horizontal, 24)
  583. .padding(.bottom, 16)
  584. }
  585. }
  586. var body: some View {
  587. let colorBackground = colorScheme == .dark ? LinearGradient(
  588. gradient: Gradient(colors: [
  589. // RGB(3, 15, 28)
  590. Color(red: 0.011, green: 0.058, blue: 0.109),
  591. // RGB(10, 34, 55)
  592. Color(red: 0.03921568627, green: 0.1333333333, blue: 0.2156862745)
  593. ]),
  594. startPoint: .bottom,
  595. endPoint: .top
  596. )
  597. :
  598. LinearGradient(gradient: Gradient(colors: [Color.gray.opacity(0.1)]), startPoint: .top, endPoint: .bottom)
  599. let colourChart: Color = colorScheme == .dark ? Color(
  600. red: 0.05490196078,
  601. green: 0.05490196078,
  602. blue: 0.05490196078
  603. ) : .white
  604. GeometryReader { geo in
  605. VStack(spacing: 0) {
  606. Spacer()
  607. // ZStack {
  608. glucoseView.padding(.top, 75)
  609. // loopView
  610. // /// circles width is 110, loops width is 35 -> (110/2) - (35/2) = 55 - 17.5 = 37.5
  611. // .offset(x: UIScreen.main.bounds.width * 0.43, y: -37.5)
  612. // .padding(.trailing, 10)
  613. // }
  614. // .padding(.top, 75)
  615. Spacer()
  616. header(geo)
  617. .padding(.top, 15)
  618. .padding(.horizontal, 10)
  619. Spacer()
  620. infoPanel
  621. .padding(.horizontal, 10)
  622. RoundedRectangle(cornerRadius: 15)
  623. .fill(colourChart)
  624. .overlay(mainChart)
  625. .clipShape(RoundedRectangle(cornerRadius: 15))
  626. .shadow(
  627. color: colorScheme == .dark ? Color(red: 0.02745098039, green: 0.1098039216, blue: 0.1411764706) :
  628. Color.black.opacity(0.33),
  629. radius: 3
  630. )
  631. .padding(.horizontal, 10)
  632. .frame(maxHeight: UIScreen.main.bounds.height / 2.2)
  633. Spacer()
  634. timeInterval
  635. Spacer()
  636. legendPanel
  637. Spacer()
  638. bottomPanel(geo)
  639. }
  640. .background(colorBackground)
  641. .edgesIgnoringSafeArea(.all)
  642. }
  643. .onAppear(perform: configureView)
  644. .navigationTitle("Home")
  645. .navigationBarHidden(true)
  646. .ignoresSafeArea(.keyboard)
  647. .popup(isPresented: state.isStatusPopupPresented, alignment: .top, direction: .top) {
  648. popup
  649. .padding()
  650. .background(
  651. RoundedRectangle(cornerRadius: 8, style: .continuous)
  652. .fill(colorScheme == .dark ? Color(
  653. red: 0.05490196078,
  654. green: 0.05490196078,
  655. blue: 0.05490196078
  656. ) : Color(UIColor.darkGray))
  657. )
  658. .onTapGesture {
  659. state.isStatusPopupPresented = false
  660. }
  661. .gesture(
  662. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  663. .onEnded { value in
  664. if value.translation.height < 0 {
  665. state.isStatusPopupPresented = false
  666. }
  667. }
  668. )
  669. }
  670. }
  671. private var popup: some View {
  672. VStack(alignment: .leading, spacing: 4) {
  673. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  674. .padding(.bottom, 4)
  675. if let suggestion = state.suggestion {
  676. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  677. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  678. } else {
  679. Text("No sugestion found").font(.body).foregroundColor(.white)
  680. }
  681. if let errorMessage = state.errorMessage, let date = state.errorDate {
  682. Text(NSLocalizedString("Error at", comment: "") + " " + dateFormatter.string(from: date))
  683. .foregroundColor(.white)
  684. .font(.headline)
  685. .padding(.bottom, 4)
  686. .padding(.top, 8)
  687. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  688. } else if let suggestion = state.suggestion, (suggestion.bg ?? 100) == 400 {
  689. Text("Invalid CGM reading (HIGH).").font(.callout).bold().foregroundColor(.loopRed).padding(.top, 8)
  690. Text("SMBs and High Temps Disabled.").font(.caption).foregroundColor(.white).padding(.bottom, 4)
  691. }
  692. }
  693. }
  694. }
  695. }