BolusRootView.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. import Charts
  2. import CoreData
  3. import LoopKitUI
  4. import SwiftUI
  5. import Swinject
  6. extension Bolus {
  7. struct RootView: BaseView {
  8. enum FocusedField {
  9. case carbs
  10. case fat
  11. case protein
  12. }
  13. @FocusState private var focusedField: FocusedField?
  14. let resolver: Resolver
  15. @StateObject var state = StateModel()
  16. @State private var showAlert = false
  17. @State private var autofocus: Bool = true
  18. @State private var calculatorDetent = PresentationDetent.medium
  19. @State private var pushed: Bool = false
  20. @State private var isPromptPresented: Bool = false
  21. @State private var dish: String = ""
  22. @State private var saved: Bool = false
  23. @State private var debounce: DispatchWorkItem?
  24. @Environment(\.managedObjectContext) var moc
  25. private enum Config {
  26. static let dividerHeight: CGFloat = 2
  27. static let spacing: CGFloat = 3
  28. }
  29. @Environment(\.colorScheme) var colorScheme
  30. @FetchRequest(
  31. entity: MealPresetStored.entity(),
  32. sortDescriptors: [NSSortDescriptor(key: "dish", ascending: true)]
  33. ) var carbPresets: FetchedResults<MealPresetStored>
  34. private var formatter: NumberFormatter {
  35. let formatter = NumberFormatter()
  36. formatter.numberStyle = .decimal
  37. formatter.maximumFractionDigits = 2
  38. return formatter
  39. }
  40. private var mealFormatter: NumberFormatter {
  41. let formatter = NumberFormatter()
  42. formatter.numberStyle = .decimal
  43. formatter.maximumFractionDigits = 1
  44. return formatter
  45. }
  46. private var gluoseFormatter: NumberFormatter {
  47. let formatter = NumberFormatter()
  48. formatter.numberStyle = .decimal
  49. if state.units == .mmolL {
  50. formatter.maximumFractionDigits = 1
  51. } else { formatter.maximumFractionDigits = 0 }
  52. return formatter
  53. }
  54. private var fractionDigits: Int {
  55. if state.units == .mmolL {
  56. return 1
  57. } else { return 0 }
  58. }
  59. private var color: LinearGradient {
  60. colorScheme == .dark ? LinearGradient(
  61. gradient: Gradient(colors: [
  62. Color.bgDarkBlue,
  63. Color.bgDarkerDarkBlue
  64. ]),
  65. startPoint: .top,
  66. endPoint: .bottom
  67. )
  68. :
  69. LinearGradient(
  70. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  71. startPoint: .top,
  72. endPoint: .bottom
  73. )
  74. }
  75. private var empty: Bool {
  76. state.useFPUconversion ? (state.carbs <= 0 && state.fat <= 0 && state.protein <= 0) : (state.carbs <= 0)
  77. }
  78. /// Handles macro input (carb, fat, protein) in a debounced fashion.
  79. func handleDebouncedInput() {
  80. debounce?.cancel()
  81. debounce = DispatchWorkItem { [self] in
  82. state.insulinCalculated = state.calculateInsulin()
  83. Task {
  84. await state.updateForecasts()
  85. }
  86. }
  87. if let debounce = debounce {
  88. DispatchQueue.main.asyncAfter(deadline: .now() + 0.35, execute: debounce)
  89. }
  90. }
  91. private var presetPopover: some View {
  92. Form {
  93. Section {
  94. TextField("Name Of Dish", text: $dish)
  95. Button {
  96. saved = true
  97. if dish != "", saved {
  98. let preset = MealPresetStored(context: moc)
  99. preset.dish = dish
  100. preset.fat = state.fat as NSDecimalNumber
  101. preset.protein = state.protein as NSDecimalNumber
  102. preset.carbs = state.carbs as NSDecimalNumber
  103. if self.moc.hasChanges {
  104. try? moc.save()
  105. }
  106. state.addNewPresetToWaitersNotepad(dish)
  107. saved = false
  108. isPromptPresented = false
  109. }
  110. }
  111. label: { Text("Save") }
  112. Button {
  113. dish = ""
  114. saved = false
  115. isPromptPresented = false }
  116. label: { Text("Cancel") }
  117. } header: { Text("Enter Meal Preset Name") }
  118. }
  119. }
  120. private var minusButton: some View {
  121. Button {
  122. if state.carbs != 0,
  123. (state.carbs - (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
  124. {
  125. state.carbs -= (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal)
  126. } else { state.carbs = 0 }
  127. if state.fat != 0,
  128. (state.fat - (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
  129. {
  130. state.fat -= (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal)
  131. } else { state.fat = 0 }
  132. if state.protein != 0,
  133. (state.protein - (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
  134. {
  135. state.protein -= (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal)
  136. } else { state.protein = 0 }
  137. state.removePresetFromNewMeal()
  138. if state.carbs == 0, state.fat == 0, state.protein == 0 { state.summation = [] }
  139. }
  140. label: { Image(systemName: "minus.circle.fill")
  141. .font(.system(size: 20))
  142. }
  143. .disabled(
  144. state
  145. .selection == nil ||
  146. (
  147. !state.summation
  148. .contains(state.selection?.dish ?? "") && (state.selection?.dish ?? "") != ""
  149. )
  150. )
  151. .buttonStyle(.borderless)
  152. .tint(.blue)
  153. }
  154. private var plusButton: some View {
  155. Button {
  156. state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
  157. state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
  158. state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
  159. state.addPresetToNewMeal()
  160. }
  161. label: { Image(systemName: "plus.circle.fill")
  162. .font(.system(size: 20))
  163. }
  164. .disabled(state.selection == nil)
  165. .buttonStyle(.borderless)
  166. .tint(.blue)
  167. }
  168. private var mealPresets: some View {
  169. Section {
  170. HStack {
  171. if state.selection != nil {
  172. minusButton
  173. }
  174. Picker("Preset", selection: $state.selection) {
  175. Text("Saved Food").tag(nil as MealPresetStored?)
  176. ForEach(carbPresets, id: \.self) { (preset: MealPresetStored) in
  177. Text(preset.dish ?? "").tag(preset as MealPresetStored?)
  178. }
  179. }
  180. .labelsHidden()
  181. .frame(maxWidth: .infinity, alignment: .center)
  182. ._onBindingChange($state.selection) { _ in
  183. state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
  184. state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
  185. state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
  186. state.addToSummation()
  187. }
  188. if state.selection != nil {
  189. plusButton
  190. }
  191. }
  192. HStack {
  193. Button("Delete Preset") {
  194. showAlert.toggle()
  195. }
  196. .disabled(state.selection == nil)
  197. .tint(.orange)
  198. .buttonStyle(.borderless)
  199. .alert(
  200. "Delete preset '\(state.selection?.dish ?? "")'?",
  201. isPresented: $showAlert,
  202. actions: {
  203. Button("No", role: .cancel) {}
  204. Button("Yes", role: .destructive) {
  205. state.deletePreset()
  206. state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
  207. state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
  208. state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
  209. state.addPresetToNewMeal()
  210. }
  211. }
  212. )
  213. Spacer()
  214. Button {
  215. isPromptPresented = true
  216. }
  217. label: { Text("Save as Preset") }
  218. .buttonStyle(.borderless)
  219. .disabled(
  220. empty ||
  221. (
  222. (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal) == state
  223. .carbs && (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal) == state
  224. .fat && (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal) == state
  225. .protein
  226. )
  227. )
  228. }
  229. }
  230. }
  231. @ViewBuilder private func proteinAndFat() -> some View {
  232. HStack {
  233. Text("Fat").foregroundColor(.orange)
  234. Spacer()
  235. TextFieldWithToolBar(text: $state.fat, placeholder: "0", keyboardType: .numberPad, numberFormatter: mealFormatter)
  236. Text("g").foregroundColor(.secondary)
  237. }
  238. HStack {
  239. Text("Protein").foregroundColor(.red)
  240. Spacer()
  241. TextFieldWithToolBar(
  242. text: $state.protein,
  243. placeholder: "0",
  244. keyboardType: .numberPad,
  245. numberFormatter: mealFormatter
  246. )
  247. Text("g").foregroundColor(.secondary)
  248. }
  249. }
  250. @ViewBuilder private func carbsTextField() -> some View {
  251. HStack {
  252. Text("Carbs").fontWeight(.semibold)
  253. Spacer()
  254. TextFieldWithToolBar(
  255. text: $state.carbs,
  256. placeholder: "0",
  257. keyboardType: .numberPad,
  258. numberFormatter: mealFormatter
  259. )
  260. .onChange(of: state.carbs) { _ in
  261. if state.carbs > 0 {
  262. handleDebouncedInput()
  263. }
  264. }
  265. Text("g").foregroundColor(.secondary)
  266. }
  267. }
  268. var body: some View {
  269. ZStack(alignment: .center) {
  270. VStack {
  271. Form {
  272. Section {
  273. carbsTextField()
  274. if state.useFPUconversion {
  275. proteinAndFat()
  276. }
  277. // Summary when combining presets
  278. if state.waitersNotepad() != "" {
  279. HStack {
  280. Text("Total")
  281. let test = state.waitersNotepad().components(separatedBy: ", ").removeDublicates()
  282. HStack(spacing: 0) {
  283. ForEach(test, id: \.self) {
  284. Text($0).foregroundStyle(Color.blue).font(.footnote)
  285. Text($0 == test[test.count - 1] ? "" : ", ")
  286. }
  287. }.frame(maxWidth: .infinity, alignment: .trailing)
  288. }
  289. }
  290. // Time
  291. HStack {
  292. Text("Time").foregroundStyle(Color.secondary)
  293. Spacer()
  294. if !pushed {
  295. Button {
  296. pushed = true
  297. } label: { Text("Now") }.buttonStyle(.borderless).foregroundColor(.secondary)
  298. .padding(.trailing, 5)
  299. } else {
  300. Button { state.date = state.date.addingTimeInterval(-15.minutes.timeInterval) }
  301. label: { Image(systemName: "minus.circle") }.tint(.blue).buttonStyle(.borderless)
  302. DatePicker(
  303. "Time",
  304. selection: $state.date,
  305. displayedComponents: [.hourAndMinute]
  306. ).controlSize(.mini)
  307. .labelsHidden()
  308. Button {
  309. state.date = state.date.addingTimeInterval(15.minutes.timeInterval)
  310. }
  311. label: { Image(systemName: "plus.circle") }.tint(.blue).buttonStyle(.borderless)
  312. }
  313. }
  314. .popover(isPresented: $isPromptPresented) {
  315. presetPopover
  316. }
  317. }.listRowBackground(Color.chart)
  318. if state.displayPresets {
  319. Section {
  320. mealPresets
  321. }.listRowBackground(Color.chart)
  322. }
  323. Section {
  324. HStack {
  325. Button(action: {
  326. state.showInfo.toggle()
  327. }, label: {
  328. Image(systemName: "info.circle")
  329. Text("Calculations")
  330. })
  331. .foregroundStyle(.blue)
  332. .font(.footnote)
  333. .buttonStyle(PlainButtonStyle())
  334. .frame(maxWidth: .infinity, alignment: .leading)
  335. if state.fattyMeals {
  336. Spacer()
  337. Toggle(isOn: $state.useFattyMealCorrectionFactor) {
  338. Text("Fatty Meal")
  339. }
  340. .toggleStyle(CheckboxToggleStyle())
  341. .font(.footnote)
  342. .onChange(of: state.useFattyMealCorrectionFactor) { _ in
  343. state.insulinCalculated = state.calculateInsulin()
  344. if state.useFattyMealCorrectionFactor {
  345. state.useSuperBolus = false
  346. }
  347. }
  348. }
  349. if state.sweetMeals {
  350. Spacer()
  351. Toggle(isOn: $state.useSuperBolus) {
  352. Text("Super Bolus")
  353. }
  354. .toggleStyle(CheckboxToggleStyle())
  355. .font(.footnote)
  356. .onChange(of: state.useSuperBolus) { _ in
  357. state.insulinCalculated = state.calculateInsulin()
  358. if state.useSuperBolus {
  359. state.useFattyMealCorrectionFactor = false
  360. }
  361. }
  362. }
  363. }
  364. HStack {
  365. Text("Recommended Bolus")
  366. Spacer()
  367. Text(
  368. formatter
  369. .string(from: Double(state.insulinCalculated) as NSNumber) ?? ""
  370. )
  371. Text(
  372. NSLocalizedString(
  373. " U",
  374. comment: "Unit in number of units delivered (keep the space character!)"
  375. )
  376. ).foregroundColor(.secondary)
  377. }.contentShape(Rectangle())
  378. .onTapGesture { state.amount = state.insulinCalculated }
  379. HStack {
  380. Text("Bolus")
  381. Spacer()
  382. TextFieldWithToolBar(
  383. text: $state.amount,
  384. placeholder: "0",
  385. textColor: colorScheme == .dark ? .white : .blue,
  386. maxLength: 5,
  387. numberFormatter: formatter
  388. ).onChange(of: state.amount) { _ in
  389. Task {
  390. await state.updateForecasts()
  391. }
  392. }
  393. Text(" U").foregroundColor(.secondary)
  394. }
  395. if state.amount > 0 {
  396. HStack {
  397. Text("External insulin")
  398. Spacer()
  399. Toggle("", isOn: $state.externalInsulin).toggleStyle(Checkbox())
  400. }
  401. }
  402. }.listRowBackground(Color.chart)
  403. Section {
  404. ForeCastChart(state: state, units: $state.units)
  405. .padding(.vertical)
  406. }.listRowBackground(Color.chart)
  407. }
  408. }
  409. .safeAreaInset(edge: .bottom, spacing: 0) {
  410. stickyButton
  411. }.blur(radius: state.waitForSuggestion ? 5 : 0)
  412. if state.waitForSuggestion {
  413. CustomProgressView(text: progressText.rawValue)
  414. }
  415. }
  416. .scrollContentBackground(.hidden).background(color)
  417. .blur(radius: state.showInfo ? 3 : 0)
  418. .navigationTitle("Treatments")
  419. .navigationBarTitleDisplayMode(.inline)
  420. .toolbar(content: {
  421. ToolbarItem(placement: .topBarLeading) {
  422. Button {
  423. state.hideModal()
  424. } label: {
  425. Text("Close")
  426. }
  427. }
  428. })
  429. .onAppear {
  430. configureView {
  431. state.insulinCalculated = state.calculateInsulin()
  432. }
  433. }
  434. .onDisappear {
  435. state.addButtonPressed = false
  436. }
  437. .sheet(isPresented: $state.showInfo) {
  438. PopupView(state: state)
  439. .presentationDetents(
  440. [.fraction(0.9), .large],
  441. selection: $calculatorDetent
  442. )
  443. }
  444. }
  445. var progressText: ProgressText {
  446. switch (state.amount > 0, state.carbs > 0) {
  447. case (true, true):
  448. return .updatingIOBandCOB
  449. case (false, true):
  450. return .updatingCOB
  451. case (true, false):
  452. return .updatingIOB
  453. default:
  454. return .updatingTreatments
  455. }
  456. }
  457. var stickyButton: some View {
  458. ZStack {
  459. Rectangle()
  460. .frame(width: UIScreen.main.bounds.width, height: 120).offset(y: 40)
  461. .shadow(
  462. color: colorScheme == .dark ? Color(red: 0.02745098039, green: 0.1098039216, blue: 0.1411764706) :
  463. Color.black.opacity(0.33),
  464. radius: 3
  465. )
  466. .foregroundStyle(Color.chart)
  467. Button {
  468. state.invokeTreatmentsTask()
  469. } label: {
  470. taskButtonLabel
  471. .font(.headline)
  472. .foregroundStyle(Color.white)
  473. .frame(maxWidth: .infinity, alignment: .center)
  474. .frame(minHeight: 50)
  475. }
  476. .disabled(disableTaskButton)
  477. .background(
  478. (state.externalInsulin ? externalBolusLimit : pumpBolusLimit) ? Color(.systemRed) :
  479. Color(.systemBlue)
  480. )
  481. .shadow(radius: 3)
  482. .clipShape(RoundedRectangle(cornerRadius: 8))
  483. .padding()
  484. .offset(y: 20)
  485. }
  486. }
  487. private var taskButtonLabel: some View {
  488. let hasInsulin = state.amount > 0
  489. let hasCarbs = state.carbs > 0
  490. let hasFatOrProtein = state.fat > 0 || state.protein > 0
  491. switch (hasInsulin, hasCarbs, hasFatOrProtein) {
  492. case (true, true, true):
  493. return Text(
  494. state
  495. .externalInsulin ? (
  496. externalBolusLimit ? "Manual bolus exceeds max bolus!" : "Log meal and external insulin"
  497. ) :
  498. (pumpBolusLimit ? "Pump bolus exceeds max bolus!" : "Log meal and enact bolus")
  499. )
  500. case (true, true, false):
  501. return Text(
  502. state
  503. .externalInsulin ?
  504. (externalBolusLimit ? "Manual bolus exceeds max bolus!" : "Log carbs and external insulin") :
  505. (pumpBolusLimit ? "Pump bolus exceeds max bolus!" : "Log carbs and enact bolus")
  506. )
  507. case (true, false, true):
  508. return Text(
  509. state
  510. .externalInsulin ?
  511. (externalBolusLimit ? "Manual bolus exceeds max bolus!" : "Log FPUs and external insulin") :
  512. (pumpBolusLimit ? "Pump bolus exceeds max bolus!" : "Log FPUs and enact bolus")
  513. )
  514. case (true, false, false):
  515. return Text(
  516. state
  517. .externalInsulin ? (externalBolusLimit ? "Manual bolus exceeds max bolus!" : "Log external insulin") :
  518. (pumpBolusLimit ? "Pump bolus exceeds max bolus!" : "Enact bolus")
  519. )
  520. case (false, true, true):
  521. return Text("Log meal")
  522. case (false, true, false):
  523. return Text("Log carbs")
  524. case (false, false, true):
  525. return Text("Log FPUs")
  526. default:
  527. return Text("Continue without treatment")
  528. }
  529. }
  530. private var pumpBolusLimit: Bool {
  531. state.amount > state.maxBolus
  532. }
  533. private var externalBolusLimit: Bool {
  534. state.amount > state.maxBolus * 3
  535. }
  536. private var disableTaskButton: Bool {
  537. state.amount > 0 ? (state.externalInsulin ? externalBolusLimit : pumpBolusLimit) : false
  538. }
  539. }
  540. struct DividerDouble: View {
  541. var body: some View {
  542. VStack(spacing: 2) {
  543. Rectangle()
  544. .frame(height: 1)
  545. .foregroundColor(.gray.opacity(0.65))
  546. Rectangle()
  547. .frame(height: 1)
  548. .foregroundColor(.gray.opacity(0.65))
  549. }
  550. .frame(height: 4)
  551. .padding(.vertical)
  552. }
  553. }
  554. struct DividerCustom: View {
  555. var body: some View {
  556. Rectangle()
  557. .frame(height: 1)
  558. .foregroundColor(.gray.opacity(0.65))
  559. .padding(.vertical)
  560. }
  561. }
  562. }
  563. // fix iOS 15 bug
  564. struct ActivityIndicator: UIViewRepresentable {
  565. @Binding var isAnimating: Bool
  566. let style: UIActivityIndicatorView.Style
  567. func makeUIView(context _: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
  568. UIActivityIndicatorView(style: style)
  569. }
  570. func updateUIView(_ uiView: UIActivityIndicatorView, context _: UIViewRepresentableContext<ActivityIndicator>) {
  571. isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
  572. }
  573. }