BolusRootView.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. case bolus
  13. }
  14. @FocusState private var focusedField: FocusedField?
  15. let resolver: Resolver
  16. @StateObject var state = StateModel()
  17. @State private var showPresetSheet = false
  18. @State private var autofocus: Bool = true
  19. @State private var calculatorDetent = PresentationDetent.medium
  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. private var formatter: NumberFormatter {
  28. let formatter = NumberFormatter()
  29. formatter.numberStyle = .decimal
  30. formatter.maximumFractionDigits = 2
  31. return formatter
  32. }
  33. private var mealFormatter: NumberFormatter {
  34. let formatter = NumberFormatter()
  35. formatter.numberStyle = .decimal
  36. formatter.maximumFractionDigits = 1
  37. return formatter
  38. }
  39. private var gluoseFormatter: NumberFormatter {
  40. let formatter = NumberFormatter()
  41. formatter.numberStyle = .decimal
  42. if state.units == .mmolL {
  43. formatter.maximumFractionDigits = 1
  44. } else { formatter.maximumFractionDigits = 0 }
  45. return formatter
  46. }
  47. private var fractionDigits: Int {
  48. if state.units == .mmolL {
  49. return 1
  50. } else { return 0 }
  51. }
  52. private var color: LinearGradient {
  53. colorScheme == .dark ? LinearGradient(
  54. gradient: Gradient(colors: [
  55. Color.bgDarkBlue,
  56. Color.bgDarkerDarkBlue
  57. ]),
  58. startPoint: .top,
  59. endPoint: .bottom
  60. )
  61. :
  62. LinearGradient(
  63. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  64. startPoint: .top,
  65. endPoint: .bottom
  66. )
  67. }
  68. /// Handles macro input (carb, fat, protein) in a debounced fashion.
  69. func handleDebouncedInput() {
  70. debounce?.cancel()
  71. debounce = DispatchWorkItem { [self] in
  72. state.insulinCalculated = state.calculateInsulin()
  73. Task {
  74. await state.updateForecasts()
  75. }
  76. }
  77. if let debounce = debounce {
  78. DispatchQueue.main.asyncAfter(deadline: .now() + 0.35, execute: debounce)
  79. }
  80. }
  81. @ViewBuilder private func proteinAndFat() -> some View {
  82. HStack {
  83. Text("Fat").foregroundColor(.orange)
  84. Spacer()
  85. TextFieldWithToolBar(
  86. text: $state.fat,
  87. placeholder: "0",
  88. keyboardType: .numberPad,
  89. numberFormatter: mealFormatter,
  90. previousTextField: { focusOnPreviousTextField(index: 2) },
  91. nextTextField: { focusOnNextTextField(index: 2) }
  92. ).focused($focusedField, equals: .fat)
  93. Text("g").foregroundColor(.secondary)
  94. }
  95. HStack {
  96. Text("Protein").foregroundColor(.red)
  97. Spacer()
  98. TextFieldWithToolBar(
  99. text: $state.protein,
  100. placeholder: "0",
  101. keyboardType: .numberPad,
  102. numberFormatter: mealFormatter,
  103. previousTextField: { focusOnPreviousTextField(index: 3) },
  104. nextTextField: { focusOnNextTextField(index: 3) }
  105. ).focused($focusedField, equals: .protein)
  106. Text("g").foregroundColor(.secondary)
  107. }
  108. }
  109. @ViewBuilder private func carbsTextField() -> some View {
  110. HStack {
  111. Text("Carbs").fontWeight(.semibold)
  112. Spacer()
  113. TextFieldWithToolBar(
  114. text: $state.carbs,
  115. placeholder: "0",
  116. keyboardType: .numberPad,
  117. numberFormatter: mealFormatter,
  118. previousTextField: { focusOnPreviousTextField(index: 1) },
  119. nextTextField: { focusOnNextTextField(index: 1) }
  120. ).focused($focusedField, equals: .carbs)
  121. .onChange(of: state.carbs) { _ in
  122. handleDebouncedInput()
  123. }
  124. Text("g").foregroundColor(.secondary)
  125. }
  126. }
  127. func focusOnPreviousTextField(index: Int) {
  128. switch index {
  129. case 2:
  130. focusedField = .carbs
  131. case 3:
  132. focusedField = .fat
  133. case 4:
  134. focusedField = .protein
  135. default:
  136. break
  137. }
  138. }
  139. func focusOnNextTextField(index: Int) {
  140. switch index {
  141. case 1:
  142. focusedField = .fat
  143. case 2:
  144. focusedField = .protein
  145. case 3:
  146. focusedField = .bolus
  147. default:
  148. break
  149. }
  150. }
  151. var body: some View {
  152. ZStack(alignment: .center) {
  153. VStack {
  154. Form {
  155. Section {
  156. carbsTextField()
  157. if state.useFPUconversion {
  158. proteinAndFat()
  159. }
  160. // Time
  161. HStack {
  162. Text("Time").foregroundStyle(Color.secondary)
  163. Spacer()
  164. if !pushed {
  165. Button {
  166. pushed = true
  167. } label: { Text("Now") }.buttonStyle(.borderless).foregroundColor(.secondary)
  168. .padding(.trailing, 5)
  169. } else {
  170. Button { state.date = state.date.addingTimeInterval(-15.minutes.timeInterval) }
  171. label: { Image(systemName: "minus.circle") }.tint(.blue).buttonStyle(.borderless)
  172. DatePicker(
  173. "Time",
  174. selection: $state.date,
  175. displayedComponents: [.hourAndMinute]
  176. ).controlSize(.mini)
  177. .labelsHidden()
  178. Button {
  179. state.date = state.date.addingTimeInterval(15.minutes.timeInterval)
  180. }
  181. label: { Image(systemName: "plus.circle") }.tint(.blue).buttonStyle(.borderless)
  182. }
  183. }
  184. HStack {
  185. Image(systemName: "square.and.pencil").foregroundColor(.secondary)
  186. TextFieldWithToolBarString(text: $state.note, placeholder: "", maxLength: 25)
  187. }
  188. }.listRowBackground(Color.chart)
  189. Section {
  190. HStack {
  191. Button(action: {
  192. state.showInfo.toggle()
  193. }, label: {
  194. Image(systemName: "info.circle")
  195. Text("Calculations")
  196. })
  197. .foregroundStyle(.blue)
  198. .font(.footnote)
  199. .buttonStyle(PlainButtonStyle())
  200. .frame(maxWidth: .infinity, alignment: .leading)
  201. if state.fattyMeals {
  202. Spacer()
  203. Toggle(isOn: $state.useFattyMealCorrectionFactor) {
  204. Text("Fatty Meal")
  205. }
  206. .toggleStyle(CheckboxToggleStyle())
  207. .font(.footnote)
  208. .onChange(of: state.useFattyMealCorrectionFactor) { _ in
  209. state.insulinCalculated = state.calculateInsulin()
  210. if state.useFattyMealCorrectionFactor {
  211. state.useSuperBolus = false
  212. }
  213. }
  214. }
  215. if state.sweetMeals {
  216. Spacer()
  217. Toggle(isOn: $state.useSuperBolus) {
  218. Text("Super Bolus")
  219. }
  220. .toggleStyle(CheckboxToggleStyle())
  221. .font(.footnote)
  222. .onChange(of: state.useSuperBolus) { _ in
  223. state.insulinCalculated = state.calculateInsulin()
  224. if state.useSuperBolus {
  225. state.useFattyMealCorrectionFactor = false
  226. }
  227. }
  228. }
  229. }
  230. HStack {
  231. Text("Recommended Bolus")
  232. Spacer()
  233. Text(
  234. formatter
  235. .string(from: Double(state.insulinCalculated) as NSNumber) ?? ""
  236. )
  237. Text(
  238. NSLocalizedString(
  239. " U",
  240. comment: "Unit in number of units delivered (keep the space character!)"
  241. )
  242. ).foregroundColor(.secondary)
  243. }.contentShape(Rectangle())
  244. .onTapGesture { state.amount = state.insulinCalculated }
  245. HStack {
  246. Text("Bolus")
  247. Spacer()
  248. TextFieldWithToolBar(
  249. text: $state.amount,
  250. placeholder: "0",
  251. textColor: colorScheme == .dark ? .white : .blue,
  252. maxLength: 5,
  253. numberFormatter: formatter,
  254. previousTextField: { focusOnPreviousTextField(index: 4) },
  255. nextTextField: { focusOnNextTextField(index: 4) }
  256. ).focused($focusedField, equals: .bolus)
  257. .onChange(of: state.amount) { _ in
  258. Task {
  259. await state.updateForecasts()
  260. }
  261. }
  262. Text(" U").foregroundColor(.secondary)
  263. }
  264. HStack {
  265. Text("External insulin")
  266. Spacer()
  267. Toggle("", isOn: $state.externalInsulin).toggleStyle(Checkbox())
  268. }
  269. }.listRowBackground(Color.chart)
  270. Section {
  271. ForeCastChart(state: state, units: $state.units)
  272. .padding(.vertical)
  273. }.listRowBackground(Color.chart)
  274. }
  275. }
  276. .safeAreaInset(edge: .bottom, spacing: 0) {
  277. stickyButton
  278. }.blur(radius: state.waitForSuggestion ? 5 : 0)
  279. if state.waitForSuggestion {
  280. CustomProgressView(text: progressText.rawValue)
  281. }
  282. }
  283. .scrollContentBackground(.hidden).background(color)
  284. .blur(radius: state.showInfo ? 3 : 0)
  285. .navigationTitle("Treatments")
  286. .navigationBarTitleDisplayMode(.inline)
  287. .toolbar(content: {
  288. ToolbarItem(placement: .topBarLeading) {
  289. Button {
  290. state.hideModal()
  291. } label: {
  292. Text("Close")
  293. }
  294. }
  295. ToolbarItem(placement: .topBarTrailing) {
  296. Button(action: {
  297. showPresetSheet = true
  298. }, label: {
  299. HStack {
  300. Text("Presets")
  301. Image(systemName: "plus")
  302. }
  303. })
  304. }
  305. })
  306. .onAppear {
  307. configureView {
  308. state.insulinCalculated = state.calculateInsulin()
  309. }
  310. }
  311. .onDisappear {
  312. state.addButtonPressed = false
  313. }
  314. .sheet(isPresented: $state.showInfo) {
  315. PopupView(state: state)
  316. .presentationDetents(
  317. [.fraction(0.9), .large],
  318. selection: $calculatorDetent
  319. )
  320. }
  321. .sheet(isPresented: $showPresetSheet, onDismiss: {
  322. showPresetSheet = false
  323. }) {
  324. MealPresetView(state: state)
  325. }
  326. }
  327. var progressText: ProgressText {
  328. switch (state.amount > 0, state.carbs > 0) {
  329. case (true, true):
  330. return .updatingIOBandCOB
  331. case (false, true):
  332. return .updatingCOB
  333. case (true, false):
  334. return .updatingIOB
  335. default:
  336. return .updatingTreatments
  337. }
  338. }
  339. var stickyButton: some View {
  340. ZStack {
  341. Rectangle()
  342. .frame(width: UIScreen.main.bounds.width, height: 120).offset(y: 40)
  343. .shadow(
  344. color: colorScheme == .dark ? Color(red: 0.02745098039, green: 0.1098039216, blue: 0.1411764706) :
  345. Color.black.opacity(0.33),
  346. radius: 3
  347. )
  348. .foregroundStyle(Color.chart)
  349. Button {
  350. state.invokeTreatmentsTask()
  351. } label: {
  352. taskButtonLabel
  353. .font(.headline)
  354. .foregroundStyle(Color.white)
  355. .frame(maxWidth: .infinity, alignment: .center)
  356. .frame(minHeight: 50)
  357. }
  358. .disabled(disableTaskButton)
  359. .background(limitExceeded ? Color(.systemRed) : Color(.systemBlue))
  360. .shadow(radius: 3)
  361. .clipShape(RoundedRectangle(cornerRadius: 8))
  362. .padding()
  363. .offset(y: 20)
  364. }
  365. }
  366. private var taskButtonLabel: some View {
  367. if pumpBolusLimitExceeded {
  368. return Text("Max Bolus of \(state.maxBolus) U Exceeded")
  369. } else if externalBolusLimitExceeded {
  370. return Text("Max External Bolus of \(state.maxExternal) U Exceeded")
  371. } else if carbLimitExceeded {
  372. return Text("Max Carbs of \(state.maxCarbs) g Exceeded")
  373. } else if fatLimitExceeded {
  374. return Text("Max Fat of \(state.maxFat) g Exceeded")
  375. } else if proteinLimitExceeded {
  376. return Text("Max Protein of \(state.maxProtein) g Exceeded")
  377. }
  378. let hasInsulin = state.amount > 0
  379. let hasCarbs = state.carbs > 0
  380. let hasFatOrProtein = state.fat > 0 || state.protein > 0
  381. let bolusString = state.externalInsulin ? "External Insulin" : "Enact Bolus"
  382. switch (hasInsulin, hasCarbs, hasFatOrProtein) {
  383. case (true, true, true):
  384. return Text("Log Meal and \(bolusString)")
  385. case (true, true, false):
  386. return Text("Log Carbs and \(bolusString)")
  387. case (true, false, true):
  388. return Text("Log FPU and \(bolusString)")
  389. case (true, false, false):
  390. return Text(state.externalInsulin ? "Log External Insulin" : "Enact Bolus")
  391. case (false, true, true):
  392. return Text("Log Meal")
  393. case (false, true, false):
  394. return Text("Log Carbs")
  395. case (false, false, true):
  396. return Text("Log FPU")
  397. default:
  398. return Text("Continue Without Treatment")
  399. }
  400. }
  401. private var pumpBolusLimitExceeded: Bool {
  402. !state.externalInsulin && state.amount > state.maxBolus
  403. }
  404. private var externalBolusLimitExceeded: Bool {
  405. state.externalInsulin && state.amount > state.maxExternal
  406. }
  407. private var carbLimitExceeded: Bool {
  408. state.carbs > state.maxCarbs
  409. }
  410. private var fatLimitExceeded: Bool {
  411. state.fat > state.maxFat
  412. }
  413. private var proteinLimitExceeded: Bool {
  414. state.protein > state.maxProtein
  415. }
  416. private var limitExceeded: Bool {
  417. pumpBolusLimitExceeded || externalBolusLimitExceeded || carbLimitExceeded || fatLimitExceeded || proteinLimitExceeded
  418. }
  419. private var disableTaskButton: Bool {
  420. state.addButtonPressed || limitExceeded
  421. }
  422. }
  423. struct DividerDouble: View {
  424. var body: some View {
  425. VStack(spacing: 2) {
  426. Rectangle()
  427. .frame(height: 1)
  428. .foregroundColor(.gray.opacity(0.65))
  429. Rectangle()
  430. .frame(height: 1)
  431. .foregroundColor(.gray.opacity(0.65))
  432. }
  433. .frame(height: 4)
  434. .padding(.vertical)
  435. }
  436. }
  437. struct DividerCustom: View {
  438. var body: some View {
  439. Rectangle()
  440. .frame(height: 1)
  441. .foregroundColor(.gray.opacity(0.65))
  442. .padding(.vertical)
  443. }
  444. }
  445. }
  446. // fix iOS 15 bug
  447. struct ActivityIndicator: UIViewRepresentable {
  448. @Binding var isAnimating: Bool
  449. let style: UIActivityIndicatorView.Style
  450. func makeUIView(context _: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
  451. UIActivityIndicatorView(style: style)
  452. }
  453. func updateUIView(_ uiView: UIActivityIndicatorView, context _: UIViewRepresentableContext<ActivityIndicator>) {
  454. isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
  455. }
  456. }