BolusRootView.swift 25 KB

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