HomeRootView.swift 31 KB

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