BolusRootView.swift 18 KB

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