TreatmentsRootView.swift 20 KB

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