BolusRootView.swift 25 KB

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