BolusRootView.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. )
  389. Text(" U").foregroundColor(.secondary)
  390. }
  391. if state.amount > 0 {
  392. HStack {
  393. Text("External insulin")
  394. Spacer()
  395. Toggle("", isOn: $state.externalInsulin).toggleStyle(Checkbox())
  396. }
  397. }
  398. }.listRowBackground(Color.chart)
  399. Section {
  400. ForeCastChart(state: state, units: $state.units)
  401. .padding(.vertical)
  402. }.listRowBackground(Color.chart)
  403. }
  404. }
  405. .safeAreaInset(edge: .bottom, spacing: 0) {
  406. stickyButton
  407. }.blur(radius: state.waitForSuggestion ? 5 : 0)
  408. if state.waitForSuggestion {
  409. CustomProgressView(text: progressText.rawValue)
  410. }
  411. }
  412. .scrollContentBackground(.hidden).background(color)
  413. .blur(radius: state.showInfo ? 3 : 0)
  414. .navigationTitle("Treatments")
  415. .navigationBarTitleDisplayMode(.inline)
  416. .toolbar(content: {
  417. ToolbarItem(placement: .topBarLeading) {
  418. Button {
  419. state.hideModal()
  420. } label: {
  421. Text("Close")
  422. }
  423. }
  424. })
  425. .onAppear {
  426. configureView {
  427. state.insulinCalculated = state.calculateInsulin()
  428. }
  429. }
  430. .onDisappear {
  431. state.addButtonPressed = false
  432. }
  433. .sheet(isPresented: $state.showInfo) {
  434. PopupView(state: state)
  435. .presentationDetents(
  436. [.fraction(0.9), .large],
  437. selection: $calculatorDetent
  438. )
  439. }
  440. }
  441. var progressText: ProgressText {
  442. switch (state.amount > 0, state.carbs > 0) {
  443. case (true, true):
  444. return .updatingIOBandCOB
  445. case (false, true):
  446. return .updatingCOB
  447. case (true, false):
  448. return .updatingIOB
  449. default:
  450. return .updatingTreatments
  451. }
  452. }
  453. var stickyButton: some View {
  454. ZStack {
  455. Rectangle()
  456. .frame(width: UIScreen.main.bounds.width, height: 120).offset(y: 40)
  457. .shadow(
  458. color: colorScheme == .dark ? Color(red: 0.02745098039, green: 0.1098039216, blue: 0.1411764706) :
  459. Color.black.opacity(0.33),
  460. radius: 3
  461. )
  462. .foregroundStyle(Color.chart)
  463. Button {
  464. state.invokeTreatmentsTask()
  465. } label: {
  466. taskButtonLabel
  467. .font(.headline)
  468. .foregroundStyle(Color.white)
  469. .frame(maxWidth: .infinity, alignment: .center)
  470. .frame(minHeight: 50)
  471. }
  472. .disabled(disableTaskButton)
  473. .background(
  474. (state.externalInsulin ? externalBolusLimit : pumpBolusLimit) ? Color(.systemRed) :
  475. Color(.systemBlue)
  476. )
  477. .shadow(radius: 3)
  478. .clipShape(RoundedRectangle(cornerRadius: 8))
  479. .padding()
  480. .offset(y: 20)
  481. }
  482. }
  483. private var taskButtonLabel: some View {
  484. let hasInsulin = state.amount > 0
  485. let hasCarbs = state.carbs > 0
  486. let hasFatOrProtein = state.fat > 0 || state.protein > 0
  487. switch (hasInsulin, hasCarbs, hasFatOrProtein) {
  488. case (true, true, true):
  489. return Text(
  490. state
  491. .externalInsulin ? (
  492. externalBolusLimit ? "Manual bolus exceeds max bolus!" : "Log meal and external insulin"
  493. ) :
  494. (pumpBolusLimit ? "Pump bolus exceeds max bolus!" : "Log meal and enact bolus")
  495. )
  496. case (true, true, false):
  497. return Text(
  498. state
  499. .externalInsulin ?
  500. (externalBolusLimit ? "Manual bolus exceeds max bolus!" : "Log carbs and external insulin") :
  501. (pumpBolusLimit ? "Pump bolus exceeds max bolus!" : "Log carbs and enact bolus")
  502. )
  503. case (true, false, true):
  504. return Text(
  505. state
  506. .externalInsulin ?
  507. (externalBolusLimit ? "Manual bolus exceeds max bolus!" : "Log FPUs and external insulin") :
  508. (pumpBolusLimit ? "Pump bolus exceeds max bolus!" : "Log FPUs and enact bolus")
  509. )
  510. case (true, false, false):
  511. return Text(
  512. state
  513. .externalInsulin ? (externalBolusLimit ? "Manual bolus exceeds max bolus!" : "Log external insulin") :
  514. (pumpBolusLimit ? "Pump bolus exceeds max bolus!" : "Enact bolus")
  515. )
  516. case (false, true, true):
  517. return Text("Log meal")
  518. case (false, true, false):
  519. return Text("Log carbs")
  520. case (false, false, true):
  521. return Text("Log FPUs")
  522. default:
  523. return Text("Continue without treatment")
  524. }
  525. }
  526. private var pumpBolusLimit: Bool {
  527. state.amount > state.maxBolus
  528. }
  529. private var externalBolusLimit: Bool {
  530. state.amount > state.maxBolus * 3
  531. }
  532. private var disableTaskButton: Bool {
  533. state.amount > 0 ? (state.externalInsulin ? externalBolusLimit : pumpBolusLimit) : false
  534. }
  535. }
  536. struct DividerDouble: View {
  537. var body: some View {
  538. VStack(spacing: 2) {
  539. Rectangle()
  540. .frame(height: 1)
  541. .foregroundColor(.gray.opacity(0.65))
  542. Rectangle()
  543. .frame(height: 1)
  544. .foregroundColor(.gray.opacity(0.65))
  545. }
  546. .frame(height: 4)
  547. .padding(.vertical)
  548. }
  549. }
  550. struct DividerCustom: View {
  551. var body: some View {
  552. Rectangle()
  553. .frame(height: 1)
  554. .foregroundColor(.gray.opacity(0.65))
  555. .padding(.vertical)
  556. }
  557. }
  558. }
  559. // fix iOS 15 bug
  560. struct ActivityIndicator: UIViewRepresentable {
  561. @Binding var isAnimating: Bool
  562. let style: UIActivityIndicatorView.Style
  563. func makeUIView(context _: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
  564. UIActivityIndicatorView(style: style)
  565. }
  566. func updateUIView(_ uiView: UIActivityIndicatorView, context _: UIViewRepresentableContext<ActivityIndicator>) {
  567. isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
  568. }
  569. }