HomeRootView.swift 32 KB

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