HomeRootView.swift 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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. 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. loopView.padding(.top, 16)
  346. Spacer()
  347. Group {
  348. Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
  349. Text("COB")
  350. .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
  351. }
  352. Group {
  353. Circle().fill(Color.uam).frame(width: 8, height: 8)
  354. .padding(.leading, 8)
  355. Text("UAM")
  356. .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
  357. }
  358. if let eventualBG = state.eventualBG {
  359. Text(
  360. "⇢ " + numberFormatter.string(
  361. from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
  362. )!
  363. )
  364. .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
  365. }
  366. Spacer()
  367. }
  368. .frame(maxWidth: .infinity)
  369. }
  370. }
  371. var timeInterval: some View {
  372. HStack(alignment: .center) {
  373. let saveButton = UXSettings(context: moc)
  374. ForEach(timeButtons) { button in
  375. Text(button.active ? NSLocalizedString(button.label, comment: "") : button.number).onTapGesture {
  376. let index = timeButtons.firstIndex(where: { $0.label == button.label }) ?? 0
  377. highlightButtons(index, onAppear: false)
  378. saveButton.hours = button.hours
  379. saveButton.date = Date.now
  380. try? moc.save()
  381. state.hours = button.hours
  382. }
  383. .foregroundStyle(button.active ? (colorScheme == .dark ? Color.white : Color.black).opacity(0.9) : .secondary)
  384. .frame(maxHeight: 30).padding(.horizontal, 8)
  385. .background(
  386. button.active ?
  387. // RGB(30, 60, 95)
  388. (
  389. colorScheme == .dark ? Color(red: 0.1176470588, green: 0.2352941176, blue: 0.3725490196) :
  390. Color.white
  391. ) :
  392. Color
  393. .clear
  394. )
  395. .cornerRadius(20)
  396. }
  397. }
  398. .shadow(
  399. color: colorScheme == .dark ? Color(
  400. red: 0.02745098039,
  401. green: 0.1098039216,
  402. blue: 0.1411764706
  403. ) : Color
  404. .black.opacity(0.33),
  405. radius: 3
  406. )
  407. .font(buttonFont)
  408. }
  409. var mainChart: some View {
  410. ZStack {
  411. if state.animatedBackground {
  412. SpriteView(scene: spriteScene, options: [.allowsTransparency])
  413. .ignoresSafeArea()
  414. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  415. }
  416. MainChartView(
  417. glucose: $state.glucose,
  418. isManual: $state.isManual,
  419. suggestion: $state.suggestion,
  420. tempBasals: $state.tempBasals,
  421. boluses: $state.boluses,
  422. suspensions: $state.suspensions,
  423. announcement: $state.announcement,
  424. hours: .constant(state.filteredHours),
  425. maxBasal: $state.maxBasal,
  426. autotunedBasalProfile: $state.autotunedBasalProfile,
  427. basalProfile: $state.basalProfile,
  428. tempTargets: $state.tempTargets,
  429. carbs: $state.carbs,
  430. timerDate: $state.timerDate,
  431. units: $state.units,
  432. smooth: $state.smooth,
  433. highGlucose: $state.highGlucose,
  434. lowGlucose: $state.lowGlucose,
  435. screenHours: $state.hours,
  436. displayXgridLines: $state.displayXgridLines,
  437. displayYgridLines: $state.displayYgridLines,
  438. thresholdLines: $state.thresholdLines
  439. )
  440. }
  441. .padding(.bottom)
  442. .modal(for: .dataTable, from: self)
  443. }
  444. private func selectedProfile() -> (name: String, isOn: Bool) {
  445. var profileString = ""
  446. var display: Bool = false
  447. let duration = (fetchedPercent.first?.duration ?? 0) as Decimal
  448. let indefinite = fetchedPercent.first?.indefinite ?? false
  449. let addedMinutes = Int(duration)
  450. let date = fetchedPercent.first?.date ?? Date()
  451. if date.addingTimeInterval(addedMinutes.minutes.timeInterval) > Date() || indefinite {
  452. display.toggle()
  453. }
  454. if fetchedPercent.first?.enabled ?? false, !(fetchedPercent.first?.isPreset ?? false), display {
  455. profileString = NSLocalizedString("Custom Profile", comment: "Custom but unsaved Profile")
  456. } else if !(fetchedPercent.first?.enabled ?? false) || !display {
  457. profileString = NSLocalizedString("Normal Profile", comment: "Your normal Profile. Use a short string")
  458. } else {
  459. let id_ = fetchedPercent.first?.id ?? ""
  460. let profile = fetchedProfiles.filter({ $0.id == id_ }).first
  461. if profile != nil {
  462. profileString = profile?.name?.description ?? ""
  463. }
  464. }
  465. return (name: profileString, isOn: display)
  466. }
  467. func highlightButtons(_ int: Int?, onAppear: Bool) {
  468. var index = 0
  469. if let integer = int, !onAppear {
  470. repeat {
  471. if index == integer {
  472. timeButtons[index].active = true
  473. } else {
  474. timeButtons[index].active = false
  475. }
  476. index += 1
  477. } while index < timeButtons.count
  478. } else if onAppear {
  479. let i = timeButtons.firstIndex(where: { $0.hours == (fetchedSettings.first?.hours ?? 6) }) ?? 2
  480. index = 0
  481. repeat {
  482. if index == i {
  483. timeButtons[index].active = true
  484. } else {
  485. timeButtons[index].active = false
  486. }
  487. index += 1
  488. } while index < timeButtons.count
  489. }
  490. }
  491. @ViewBuilder private func bottomPanel(_: GeometryProxy) -> some View {
  492. let colorRectangle: Color = colorScheme == .dark ? Color.black.opacity(0.8) : Color.white
  493. let colorIcon: Color = (colorScheme == .dark ? Color.white : Color.black).opacity(0.9)
  494. ZStack {
  495. Rectangle()
  496. .fill(colorRectangle)
  497. .frame(height: UIScreen.main.bounds.height / 13)
  498. .cornerRadius(15)
  499. .shadow(
  500. color: colorScheme == .dark ? Color(red: 0.02745098039, green: 0.1098039216, blue: 0.1411764706) : Color
  501. .black.opacity(0.33),
  502. radius: 3
  503. )
  504. .padding([.leading, .trailing], 10)
  505. HStack {
  506. Button { state.showModal(for: .addCarbs(editMode: false, override: false)) }
  507. label: {
  508. ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
  509. Image("carbs")
  510. .renderingMode(.template)
  511. .resizable()
  512. .frame(width: 24, height: 24)
  513. .foregroundColor(colorIcon)
  514. .padding(8)
  515. if let carbsReq = state.carbsRequired {
  516. Text(numberFormatter.string(from: carbsReq as NSNumber)!)
  517. .font(.caption)
  518. .foregroundColor(.white)
  519. .padding(4)
  520. .background(Capsule().fill(Color.red))
  521. }
  522. }
  523. }.buttonStyle(.borderless)
  524. Spacer()
  525. Button { state.showModal(for: .addTempTarget) }
  526. label: {
  527. Image("target")
  528. .renderingMode(.template)
  529. .resizable()
  530. .frame(width: 24, height: 24)
  531. .padding(8)
  532. }
  533. .foregroundColor(colorIcon)
  534. .buttonStyle(.borderless)
  535. Spacer()
  536. Button {
  537. state.showModal(for: .bolus(
  538. waitForSuggestion: true,
  539. fetch: false
  540. ))
  541. }
  542. label: {
  543. Image("bolus")
  544. .renderingMode(.template)
  545. .resizable()
  546. .frame(width: 24, height: 24)
  547. .padding(8)
  548. }
  549. .foregroundColor(colorIcon)
  550. .buttonStyle(.borderless)
  551. Spacer()
  552. if state.allowManualTemp {
  553. Button { state.showModal(for: .manualTempBasal) }
  554. label: {
  555. Image("bolus1")
  556. .renderingMode(.template)
  557. .resizable()
  558. .frame(width: 24, height: 24)
  559. .padding(8)
  560. }
  561. .foregroundColor(colorIcon)
  562. .buttonStyle(.borderless)
  563. Spacer()
  564. }
  565. // MARK: CANCEL OF PROFILE HAS TO BE IMPLEMENTED
  566. // MAYBE WITH A SMALL INDICATOR AT THE SYMBOL
  567. Button {
  568. state.showModal(for: .overrideProfilesConfig)
  569. } label: {
  570. Image(systemName: "person")
  571. .renderingMode(.template)
  572. .resizable()
  573. .frame(width: 24, height: 24)
  574. .padding(8)
  575. }
  576. .foregroundColor(colorIcon)
  577. .buttonStyle(.borderless)
  578. Spacer()
  579. Button { state.showModal(for: .statistics)
  580. }
  581. label: {
  582. Image(systemName: "chart.xyaxis.line")
  583. .renderingMode(.template)
  584. .resizable()
  585. .frame(width: 24, height: 24)
  586. .padding(8)
  587. }
  588. .foregroundColor(colorIcon)
  589. .buttonStyle(.borderless)
  590. Spacer()
  591. Button { state.showModal(for: .settings) }
  592. label: {
  593. Image("settings1")
  594. .renderingMode(.template)
  595. .resizable()
  596. .frame(width: 24, height: 24)
  597. .padding(8)
  598. }
  599. .foregroundColor(colorIcon)
  600. .buttonStyle(.borderless)
  601. }
  602. .padding(.horizontal, 24)
  603. .padding(.bottom, 16)
  604. }
  605. }
  606. var body: some View {
  607. let colorBackground = colorScheme == .dark ? LinearGradient(
  608. gradient: Gradient(colors: [
  609. // RGB(3, 15, 28)
  610. Color(red: 0.011, green: 0.058, blue: 0.109),
  611. Color(red: 0.011, green: 0.058, blue: 0.109),
  612. // RGB(1, 3, 8)
  613. Color(red: 0.003, green: 0.011, blue: 0.031)
  614. ]),
  615. startPoint: .bottom,
  616. endPoint: .top
  617. )
  618. :
  619. LinearGradient(gradient: Gradient(colors: [Color.gray.opacity(0.1)]), startPoint: .top, endPoint: .bottom)
  620. let colourChart: Color = colorScheme == .dark ? .black.opacity(0.8) : .white
  621. GeometryReader { geo in
  622. VStack(spacing: 0) {
  623. Spacer()
  624. // ZStack {
  625. glucoseView.padding(.top, 75)
  626. // loopView
  627. // /// circles width is 110, loops width is 35 -> (110/2) - (35/2) = 55 - 17.5 = 37.5
  628. // .offset(x: UIScreen.main.bounds.width * 0.43, y: -37.5)
  629. // .padding(.trailing, 10)
  630. // }
  631. // .padding(.top, 75)
  632. Spacer()
  633. header(geo)
  634. .padding(.top, 15)
  635. .padding(.horizontal, 10)
  636. Spacer()
  637. infoPanel
  638. .padding(.horizontal, 10)
  639. RoundedRectangle(cornerRadius: 15)
  640. .fill(colourChart)
  641. .overlay(mainChart)
  642. .clipShape(RoundedRectangle(cornerRadius: 15))
  643. .shadow(
  644. color: colorScheme == .dark ? Color(red: 0.02745098039, green: 0.1098039216, blue: 0.1411764706) :
  645. Color.black.opacity(0.33),
  646. radius: 3
  647. )
  648. .padding(.horizontal, 10)
  649. .frame(maxHeight: UIScreen.main.bounds.height / 2.2)
  650. Spacer()
  651. timeInterval
  652. legendPanel
  653. Spacer()
  654. bottomPanel(geo)
  655. }
  656. .background(colorBackground)
  657. .edgesIgnoringSafeArea(.all)
  658. }
  659. .onAppear(perform: configureView)
  660. .navigationTitle("Home")
  661. .navigationBarHidden(true)
  662. .ignoresSafeArea(.keyboard)
  663. .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
  664. popup
  665. .padding()
  666. .background(
  667. RoundedRectangle(cornerRadius: 8, style: .continuous)
  668. .fill(Color(UIColor.darkGray))
  669. )
  670. .onTapGesture {
  671. isStatusPopupPresented = false
  672. }
  673. .gesture(
  674. DragGesture(minimumDistance: 10, coordinateSpace: .local)
  675. .onEnded { value in
  676. if value.translation.height < 0 {
  677. isStatusPopupPresented = false
  678. }
  679. }
  680. )
  681. }
  682. }
  683. private var popup: some View {
  684. VStack(alignment: .leading, spacing: 4) {
  685. Text(state.statusTitle).font(.headline).foregroundColor(.white)
  686. .padding(.bottom, 4)
  687. if let suggestion = state.suggestion {
  688. TagCloudView(tags: suggestion.reasonParts).animation(.none, value: false)
  689. Text(suggestion.reasonConclusion.capitalizingFirstLetter()).font(.caption).foregroundColor(.white)
  690. } else {
  691. Text("No sugestion found").font(.body).foregroundColor(.white)
  692. }
  693. if let errorMessage = state.errorMessage, let date = state.errorDate {
  694. Text(NSLocalizedString("Error at", comment: "") + " " + dateFormatter.string(from: date))
  695. .foregroundColor(.white)
  696. .font(.headline)
  697. .padding(.bottom, 4)
  698. .padding(.top, 8)
  699. Text(errorMessage).font(.caption).foregroundColor(.loopRed)
  700. } else if let suggestion = state.suggestion, (suggestion.bg ?? 100) == 400 {
  701. Text("Invalid CGM reading (HIGH).").font(.callout).bold().foregroundColor(.loopRed).padding(.top, 8)
  702. Text("SMBs and High Temps Disabled.").font(.caption).foregroundColor(.white).padding(.bottom, 4)
  703. }
  704. }
  705. }
  706. }
  707. }