TreatmentsRootView.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. import Charts
  2. import CoreData
  3. import LoopKitUI
  4. import SwiftUI
  5. import Swinject
  6. extension Treatments {
  7. struct RootView: BaseView {
  8. enum FocusedField {
  9. case carbs
  10. case fat
  11. case protein
  12. case bolus
  13. }
  14. @FocusState private var focusedField: FocusedField?
  15. let resolver: Resolver
  16. @State var state = StateModel()
  17. @State private var showPresetSheet = false
  18. @State private var autofocus: Bool = true
  19. @State private var calculatorDetent = PresentationDetent.large
  20. @State private var pushed: Bool = false
  21. @State private var debounce: DispatchWorkItem?
  22. private enum Config {
  23. static let dividerHeight: CGFloat = 2
  24. static let spacing: CGFloat = 3
  25. }
  26. @Environment(\.colorScheme) var colorScheme
  27. @Environment(AppState.self) var appState
  28. private var formatter: NumberFormatter {
  29. let formatter = NumberFormatter()
  30. formatter.numberStyle = .decimal
  31. formatter.maximumIntegerDigits = 2
  32. formatter.maximumFractionDigits = 3
  33. return formatter
  34. }
  35. private var mealFormatter: NumberFormatter {
  36. let formatter = NumberFormatter()
  37. formatter.numberStyle = .decimal
  38. formatter.maximumIntegerDigits = 3
  39. formatter.maximumFractionDigits = 0
  40. return formatter
  41. }
  42. private var gluoseFormatter: NumberFormatter {
  43. let formatter = NumberFormatter()
  44. formatter.numberStyle = .decimal
  45. if state.units == .mmolL {
  46. formatter.maximumIntegerDigits = 2
  47. formatter.maximumFractionDigits = 1
  48. } else {
  49. formatter.maximumIntegerDigits = 3
  50. formatter.maximumFractionDigits = 0
  51. }
  52. return formatter
  53. }
  54. private var fractionDigits: Int {
  55. if state.units == .mmolL {
  56. return 1
  57. } else { return 0 }
  58. }
  59. /// Handles macro input (carb, fat, protein) in a debounced fashion.
  60. func handleDebouncedInput() {
  61. debounce?.cancel()
  62. debounce = DispatchWorkItem { [self] in
  63. Task {
  64. await state.updateForecasts()
  65. state.insulinCalculated = await state.calculateInsulin()
  66. }
  67. }
  68. if let debounce = debounce {
  69. DispatchQueue.main.asyncAfter(deadline: .now() + 0.35, execute: debounce)
  70. }
  71. }
  72. @ViewBuilder private func proteinAndFat() -> some View {
  73. HStack {
  74. HStack {
  75. Text("Fat")
  76. TextFieldWithToolBar(
  77. text: $state.fat,
  78. placeholder: "0",
  79. keyboardType: .numberPad,
  80. numberFormatter: mealFormatter,
  81. showArrows: true,
  82. previousTextField: { focusedField = previousField(from: .fat) },
  83. nextTextField: { focusedField = nextField(from: .fat) },
  84. unitsText: String(localized: "g", comment: "Units for carbs")
  85. )
  86. .focused($focusedField, equals: .fat)
  87. }
  88. Divider().foregroundStyle(.primary).fontWeight(.bold).frame(width: 10)
  89. HStack {
  90. Text("Protein")
  91. TextFieldWithToolBar(
  92. text: $state.protein,
  93. placeholder: "0",
  94. keyboardType: .numberPad,
  95. numberFormatter: mealFormatter,
  96. showArrows: true,
  97. previousTextField: { focusedField = previousField(from: .protein) },
  98. nextTextField: { focusedField = nextField(from: .protein) },
  99. unitsText: String(localized: "g", comment: "Units for carbs")
  100. )
  101. .focused($focusedField, equals: .protein)
  102. }
  103. }
  104. }
  105. @ViewBuilder private func carbsTextField() -> some View {
  106. HStack {
  107. Text("Carbs")
  108. Spacer()
  109. TextFieldWithToolBar(
  110. text: $state.carbs,
  111. placeholder: "0",
  112. keyboardType: .numberPad,
  113. numberFormatter: mealFormatter,
  114. showArrows: true,
  115. previousTextField: { focusedField = previousField(from: .carbs) },
  116. nextTextField: { focusedField = nextField(from: .carbs) },
  117. unitsText: String(localized: "g", comment: "Units for carbs")
  118. )
  119. .focused($focusedField, equals: .carbs)
  120. .onChange(of: state.carbs) {
  121. handleDebouncedInput()
  122. }
  123. }
  124. }
  125. /// Determines the next field to focus on based on the current focused field.
  126. ///
  127. /// This function handles the tab order navigation between input fields,
  128. /// taking into account whether fat/protein fields are visible based on user settings.
  129. ///
  130. /// - Parameter current: The currently focused field
  131. /// - Returns: The next field that should receive focus, or nil if there is no next field
  132. private func nextField(from current: FocusedField) -> FocusedField? {
  133. // If fat/protein fields are hidden, skip them in navigation
  134. let showFPU = state.useFPUconversion
  135. switch current {
  136. case .fat:
  137. return .bolus
  138. case .protein:
  139. return .fat
  140. case .carbs:
  141. return showFPU ? .protein : .bolus
  142. case .bolus:
  143. return .carbs
  144. }
  145. }
  146. /// Determines the previous field to focus on based on the current focused field.
  147. ///
  148. /// This function handles the reverse tab order navigation between input fields,
  149. /// taking into account whether fat/protein fields are visible based on user settings.
  150. ///
  151. /// - Parameter current: The currently focused field
  152. /// - Returns: The previous field that should receive focus, or nil if there is no previous field
  153. private func previousField(from current: FocusedField) -> FocusedField? {
  154. let showFPU = state.useFPUconversion
  155. switch current {
  156. case .fat:
  157. return .protein
  158. case .protein:
  159. return .carbs
  160. case .carbs:
  161. return .bolus
  162. case .bolus:
  163. return showFPU ? .fat : .carbs
  164. }
  165. }
  166. var body: some View {
  167. ZStack(alignment: .center) {
  168. VStack {
  169. List {
  170. Section {
  171. ForecastChart(state: state)
  172. .padding(.vertical)
  173. }.listRowBackground(Color.chart)
  174. Section {
  175. carbsTextField()
  176. if state.useFPUconversion {
  177. proteinAndFat()
  178. }
  179. // Time
  180. HStack {
  181. // Semi-hacky workaround to make sure the List renders the horizontal divider properly between the `Time` and `Note` rows within the Section
  182. HStack {
  183. Text("")
  184. Image(systemName: "clock").padding(.leading, -7)
  185. }
  186. Spacer()
  187. if !pushed {
  188. Button {
  189. pushed = true
  190. } label: { Text("Now") }.buttonStyle(.borderless).foregroundColor(.secondary)
  191. .padding(.trailing, 5)
  192. } else {
  193. Button { state.date = state.date.addingTimeInterval(-15.minutes.timeInterval) }
  194. label: { Image(systemName: "minus.circle") }.tint(.blue).buttonStyle(.borderless)
  195. DatePicker(
  196. "Time",
  197. selection: $state.date,
  198. displayedComponents: [.hourAndMinute]
  199. ).controlSize(.mini)
  200. .labelsHidden()
  201. .onChange(of: state.date) { _, _ in
  202. // Trigger simulation when date changes to update forecasts for backdated carbs
  203. Task {
  204. // `updateForecasts()` does update the `simulatedDetermination` of type `Determination?` var on the main thread, so I can use this to pass its cob value into the bolus calc manager
  205. await state.updateForecasts()
  206. state.insulinCalculated = await state.calculateInsulin()
  207. }
  208. }
  209. Button {
  210. state.date = state.date.addingTimeInterval(15.minutes.timeInterval)
  211. }
  212. label: { Image(systemName: "plus.circle") }.tint(.blue).buttonStyle(.borderless)
  213. }
  214. }
  215. // Notes
  216. HStack {
  217. Image(systemName: "square.and.pencil")
  218. TextFieldWithToolBarString(
  219. text: $state.note,
  220. placeholder: String(localized: "Note..."),
  221. maxLength: 25
  222. )
  223. }
  224. }.listRowBackground(Color.chart)
  225. Section {
  226. if state.fattyMeals || state.sweetMeals {
  227. HStack(spacing: 10) {
  228. if state.fattyMeals {
  229. Toggle(isOn: $state.useFattyMealCorrectionFactor) {
  230. Text("Reduced Bolus")
  231. }
  232. .toggleStyle(RadioButtonToggleStyle())
  233. .font(.footnote)
  234. .onChange(of: state.useFattyMealCorrectionFactor) {
  235. Task {
  236. state.insulinCalculated = await state.calculateInsulin()
  237. if state.useFattyMealCorrectionFactor {
  238. state.useSuperBolus = false
  239. }
  240. }
  241. }
  242. }
  243. if state.sweetMeals {
  244. Toggle(isOn: $state.useSuperBolus) {
  245. Text("Super Bolus")
  246. }
  247. .toggleStyle(RadioButtonToggleStyle())
  248. .font(.footnote)
  249. .onChange(of: state.useSuperBolus) {
  250. Task {
  251. state.insulinCalculated = await state.calculateInsulin()
  252. if state.useSuperBolus {
  253. state.useFattyMealCorrectionFactor = false
  254. }
  255. }
  256. }
  257. }
  258. }
  259. }
  260. HStack {
  261. HStack {
  262. Text("Recommendation")
  263. Button(action: {
  264. state.showInfo.toggle()
  265. }, label: {
  266. Image(systemName: "info.circle")
  267. })
  268. .foregroundStyle(.blue)
  269. .buttonStyle(PlainButtonStyle())
  270. }
  271. Spacer()
  272. Button {
  273. state.amount = state.insulinCalculated
  274. } label: {
  275. HStack {
  276. Text(
  277. formatter
  278. .string(from: Double(state.insulinCalculated) as NSNumber) ?? ""
  279. )
  280. Text(
  281. String(
  282. localized:
  283. " U",
  284. comment: "Unit in number of units delivered (keep the space character!)"
  285. )
  286. ).foregroundColor(.secondary)
  287. }
  288. }
  289. .disabled(state.insulinCalculated == 0 || state.amount == state.insulinCalculated)
  290. .buttonStyle(.bordered).padding(.trailing, -10)
  291. }
  292. HStack {
  293. Text("Bolus")
  294. Spacer()
  295. TextFieldWithToolBar(
  296. text: $state.amount,
  297. placeholder: "0",
  298. textColor: colorScheme == .dark ? .white : .blue,
  299. maxLength: 5,
  300. numberFormatter: formatter,
  301. showArrows: true,
  302. previousTextField: { focusedField = previousField(from: .bolus) },
  303. nextTextField: { focusedField = nextField(from: .bolus) },
  304. unitsText: String(localized: "U", comment: "Units for bolus amount")
  305. ).focused($focusedField, equals: .bolus)
  306. .onChange(of: state.amount) {
  307. Task {
  308. await state.updateForecasts()
  309. }
  310. }
  311. }
  312. HStack {
  313. Text("External Insulin")
  314. Spacer()
  315. Toggle("", isOn: $state.externalInsulin).toggleStyle(CheckboxToggleStyle())
  316. }
  317. }.listRowBackground(Color.chart)
  318. treatmentButton
  319. }
  320. .listSectionSpacing(sectionSpacing)
  321. }
  322. .blur(radius: state.isAwaitingDeterminationResult ? 5 : 0)
  323. if state.isAwaitingDeterminationResult {
  324. CustomProgressView(text: progressText.displayName)
  325. }
  326. }
  327. .padding(.top)
  328. .ignoresSafeArea(edges: .top)
  329. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  330. .blur(radius: state.showInfo ? 3 : 0)
  331. .navigationTitle("Treatments")
  332. .navigationBarTitleDisplayMode(.inline)
  333. .toolbar(content: {
  334. ToolbarItem(placement: .topBarLeading) {
  335. Button {
  336. state.hideModal()
  337. } label: {
  338. Text("Close")
  339. }
  340. }
  341. if state.displayPresets {
  342. ToolbarItem(placement: .topBarTrailing) {
  343. Button(action: {
  344. showPresetSheet = true
  345. }, label: {
  346. HStack {
  347. Text("Presets")
  348. Image(systemName: "plus")
  349. }
  350. })
  351. }
  352. }
  353. })
  354. .onAppear {
  355. configureView {
  356. state.isActive = true
  357. Task { @MainActor in
  358. state.insulinCalculated = await state.calculateInsulin()
  359. }
  360. }
  361. }
  362. .onDisappear {
  363. state.isActive = false
  364. state.addButtonPressed = false
  365. // Cancel all Combine subscriptions and unregister State from broadcaster
  366. state.cleanupTreatmentState()
  367. }
  368. .sheet(isPresented: $state.showInfo) {
  369. PopupView(state: state)
  370. }
  371. .sheet(isPresented: $showPresetSheet, onDismiss: {
  372. showPresetSheet = false
  373. }) {
  374. MealPresetView(state: state)
  375. }
  376. .alert("Error while processing Treatment", isPresented: $state.showDeterminationFailureAlert) {
  377. Button("OK", role: .cancel) {
  378. state.hideModal()
  379. }
  380. } message: {
  381. Text("\(state.determinationFailureMessage)")
  382. }
  383. }
  384. var progressText: ProgressText {
  385. switch (state.amount > 0, state.carbs > 0) {
  386. case (true, true):
  387. return .updatingIOBandCOB
  388. case (false, true):
  389. return .updatingCOB
  390. case (true, false):
  391. return .updatingIOB
  392. default:
  393. return .updatingTreatments
  394. }
  395. }
  396. @State private var showConfirmDialogForBolusing = false
  397. private var bolusWarning: (shouldConfirm: Bool, warningMessage: String, color: Color) {
  398. let isGlucoseVeryLow = state.currentBG < 54
  399. let isForecastVeryLow = state.minPredBG < 54
  400. // Only warn when enacting a bolus via pump
  401. guard !state.externalInsulin, state.amount > 0 else {
  402. return (false, "", .primary)
  403. }
  404. let warningMessage = isGlucoseVeryLow ? String(localized: "Glucose is very low.") :
  405. isForecastVeryLow ? String(localized: "Glucose forecast is very low.") :
  406. ""
  407. let warningColor: Color = isGlucoseVeryLow ? .red : colorScheme == .dark ? .orange : .accentColor
  408. let shouldConfirm = state.confirmBolus && (isGlucoseVeryLow || isForecastVeryLow)
  409. return (shouldConfirm, warningMessage, warningColor)
  410. }
  411. var treatmentButton: some View {
  412. var treatmentButtonBackground = Color(.systemBlue)
  413. if limitExceeded {
  414. treatmentButtonBackground = Color(.systemRed)
  415. } else if disableTaskButton {
  416. treatmentButtonBackground = Color(.systemGray)
  417. }
  418. return Section {
  419. Button {
  420. if bolusWarning.shouldConfirm {
  421. showConfirmDialogForBolusing = true
  422. } else {
  423. state.invokeTreatmentsTask()
  424. }
  425. } label: {
  426. HStack {
  427. if state.isBolusInProgress && state.amount > 0 &&
  428. !state.externalInsulin && (state.carbs == 0 || state.fat == 0 || state.protein == 0)
  429. {
  430. ProgressView()
  431. }
  432. taskButtonLabel
  433. }
  434. .font(.headline)
  435. .foregroundStyle(Color.white)
  436. .frame(maxWidth: .infinity, alignment: .center)
  437. .frame(height: 35)
  438. }
  439. .disabled(disableTaskButton)
  440. .listRowBackground(treatmentButtonBackground)
  441. .shadow(radius: 3)
  442. .clipShape(RoundedRectangle(cornerRadius: 8))
  443. .confirmationDialog(
  444. bolusWarning.warningMessage + " Bolus \(state.amount.description) U?",
  445. isPresented: $showConfirmDialogForBolusing,
  446. titleVisibility: .visible
  447. ) {
  448. Button("Cancel", role: .cancel) {}
  449. Button(
  450. bolusWarning.warningMessage.isEmpty ? "Enact Bolus" : "Ignore Warning and Enact Bolus",
  451. role: bolusWarning.warningMessage.isEmpty ? nil : .destructive
  452. ) {
  453. state.invokeTreatmentsTask()
  454. }
  455. }
  456. } header: {
  457. if !bolusWarning.warningMessage.isEmpty {
  458. Text(bolusWarning.warningMessage)
  459. .textCase(nil)
  460. .font(.subheadline)
  461. .foregroundColor(bolusWarning.color)
  462. .frame(maxWidth: .infinity, alignment: .center)
  463. .padding(.top, -22)
  464. }
  465. }
  466. }
  467. private var taskButtonLabel: some View {
  468. if pumpBolusLimitExceeded {
  469. return Text("Max Bolus of \(state.maxBolus.description) U Exceeded")
  470. } else if externalBolusLimitExceeded {
  471. return Text("Max External Bolus of \(state.maxExternal.description) U Exceeded")
  472. } else if carbLimitExceeded {
  473. return Text("Max Carbs of \(state.maxCarbs.description) g Exceeded")
  474. } else if fatLimitExceeded {
  475. return Text("Max Fat of \(state.maxFat.description) g Exceeded")
  476. } else if proteinLimitExceeded {
  477. return Text("Max Protein of \(state.maxProtein.description) g Exceeded")
  478. }
  479. let hasInsulin = state.amount > 0
  480. let hasCarbs = state.carbs > 0
  481. let hasFatOrProtein = state.fat > 0 || state.protein > 0
  482. let bolusString = state.externalInsulin ? String(localized: "External Insulin") : String(localized: "Enact Bolus")
  483. if state.isBolusInProgress && hasInsulin && !state.externalInsulin && (!hasCarbs || !hasFatOrProtein) {
  484. return Text("Bolus In Progress...")
  485. }
  486. switch (hasInsulin, hasCarbs, hasFatOrProtein) {
  487. case (true, true, true):
  488. return Text("Log Meal and \(bolusString)")
  489. case (true, true, false):
  490. return Text("Log Carbs and \(bolusString)")
  491. case (true, false, true):
  492. return Text("Log FPU and \(bolusString)")
  493. case (true, false, false):
  494. return Text(state.externalInsulin ? "Log External Insulin" : "Enact Bolus")
  495. case (false, true, true):
  496. return Text("Log Meal")
  497. case (false, true, false):
  498. return Text("Log Carbs")
  499. case (false, false, true):
  500. return Text("Log FPU")
  501. default:
  502. return Text("Continue Without Treatment")
  503. }
  504. }
  505. private var pumpBolusLimitExceeded: Bool {
  506. !state.externalInsulin && state.amount > state.maxBolus
  507. }
  508. private var externalBolusLimitExceeded: Bool {
  509. state.externalInsulin && state.amount > state.maxExternal
  510. }
  511. private var carbLimitExceeded: Bool {
  512. state.carbs > state.maxCarbs
  513. }
  514. private var fatLimitExceeded: Bool {
  515. state.fat > state.maxFat
  516. }
  517. private var proteinLimitExceeded: Bool {
  518. state.protein > state.maxProtein
  519. }
  520. private var limitExceeded: Bool {
  521. pumpBolusLimitExceeded || externalBolusLimitExceeded || carbLimitExceeded || fatLimitExceeded || proteinLimitExceeded
  522. }
  523. private var disableTaskButton: Bool {
  524. (
  525. state.isBolusInProgress && state
  526. .amount > 0 && !state.externalInsulin && (state.carbs == 0 || state.fat == 0 || state.protein == 0)
  527. ) || state
  528. .addButtonPressed || limitExceeded
  529. }
  530. }
  531. struct DividerDouble: View {
  532. var body: some View {
  533. VStack(spacing: 2) {
  534. Rectangle()
  535. .frame(height: 1)
  536. .foregroundColor(.gray.opacity(0.65))
  537. Rectangle()
  538. .frame(height: 1)
  539. .foregroundColor(.gray.opacity(0.65))
  540. }
  541. .frame(height: 4)
  542. .padding(.vertical)
  543. }
  544. }
  545. struct DividerCustom: View {
  546. var body: some View {
  547. Rectangle()
  548. .frame(height: 1)
  549. .foregroundColor(.gray.opacity(0.65))
  550. .padding(.vertical)
  551. }
  552. }
  553. }