DefaultBolusCalcRootView.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. import Charts
  2. import CoreData
  3. import SwiftUI
  4. import Swinject
  5. extension Bolus {
  6. struct DefaultBolusCalcRootView: BaseView {
  7. let resolver: Resolver
  8. let waitForSuggestion: Bool
  9. let fetch: Bool
  10. @StateObject var state = StateModel()
  11. @State private var isAddInsulinAlertPresented = false
  12. @State private var presentInfo = false
  13. @State private var displayError = false
  14. @State private var keepForNextWiew: Bool = false
  15. @ObservedObject var appState: AppState
  16. @Environment(\.colorScheme) var colorScheme
  17. @FetchRequest(
  18. entity: Meals.entity(),
  19. sortDescriptors: [NSSortDescriptor(key: "createdAt", ascending: false)]
  20. ) var meal: FetchedResults<Meals>
  21. private var formatter: NumberFormatter {
  22. let formatter = NumberFormatter()
  23. formatter.numberStyle = .decimal
  24. formatter.maximumFractionDigits = 2
  25. return formatter
  26. }
  27. private var fractionDigits: Int {
  28. if state.units == .mmolL {
  29. return 1
  30. } else { return 0 }
  31. }
  32. private var color: LinearGradient {
  33. colorScheme == .dark ? LinearGradient(
  34. gradient: Gradient(colors: [
  35. Color.bgDarkBlue,
  36. Color.bgDarkerDarkBlue
  37. ]),
  38. startPoint: .top,
  39. endPoint: .bottom
  40. )
  41. :
  42. LinearGradient(
  43. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  44. startPoint: .top,
  45. endPoint: .bottom
  46. )
  47. }
  48. var body: some View {
  49. Form {
  50. Section {
  51. if state.waitForSuggestion {
  52. Text("Please wait")
  53. } else {
  54. predictionChart
  55. }
  56. } header: { Text("Predictions") }.listRowBackground(Color.chart)
  57. if fetch {
  58. Section {
  59. mealEntries
  60. } header: { Text("Meal Summary") }.listRowBackground(Color.chart)
  61. }
  62. Section {
  63. if state.waitForSuggestion {
  64. HStack {
  65. Text("Wait please").foregroundColor(.secondary)
  66. Spacer()
  67. ActivityIndicator(isAnimating: .constant(true), style: .medium) // fix iOS 15 bug
  68. }
  69. } else {
  70. HStack {
  71. Text("Insulin recommended")
  72. Image(systemName: "info.bubble")
  73. .symbolRenderingMode(.palette)
  74. .foregroundStyle(.primary, .blue)
  75. .onTapGesture {
  76. presentInfo.toggle()
  77. }
  78. Spacer()
  79. Text(
  80. formatter
  81. .string(from: state.insulinRecommended as NSNumber)! +
  82. NSLocalizedString(" U", comment: "Insulin unit")
  83. ).foregroundColor((state.error && state.insulinRecommended > 0) ? .red : .secondary)
  84. .onTapGesture {
  85. if state.error, state.insulinRecommended > 0 { displayError = true }
  86. else { state.amount = state.insulinRecommended }
  87. }
  88. }.contentShape(Rectangle())
  89. }
  90. HStack {
  91. Text("Amount")
  92. Spacer()
  93. DecimalTextField(
  94. "0",
  95. value: $state.amount,
  96. formatter: formatter,
  97. autofocus: true,
  98. cleanInput: true
  99. )
  100. Text(!(state.amount > state.maxBolus) ? "U" : "😵").foregroundColor(.secondary)
  101. }
  102. } header: { Text("Bolus") }.listRowBackground(Color.chart)
  103. if state.amount > 0 {
  104. Section {
  105. Button {
  106. keepForNextWiew = true
  107. state.add()
  108. state.hideModal()
  109. // appState.currentTab = .home
  110. }
  111. label: { Text(!(state.amount > state.maxBolus) ? "Enact bolus" : "Max Bolus exceeded!") }
  112. .frame(maxWidth: .infinity, alignment: .center)
  113. .disabled(disabled)
  114. .listRowBackground(!disabled ? Color(.systemBlue) : Color(.systemGray4))
  115. .tint(.white)
  116. }
  117. }
  118. if state.amount <= 0 {
  119. Section {
  120. Button {
  121. keepForNextWiew = true
  122. state.hideModal()
  123. // appState.currentTab = .home
  124. }
  125. label: { Text("Continue without bolus") }.frame(maxWidth: .infinity, alignment: .center)
  126. }.listRowBackground(Color.chart)
  127. }
  128. }.scrollContentBackground(.hidden).background(color)
  129. .alert(isPresented: $displayError) {
  130. Alert(
  131. title: Text("Warning!"),
  132. message: Text("\n" + alertString() + "\n"),
  133. primaryButton: .destructive(
  134. Text("Add"),
  135. action: {
  136. state.amount = state.insulinRecommended
  137. displayError = false
  138. }
  139. ),
  140. secondaryButton: .cancel()
  141. )
  142. }.onAppear {
  143. configureView {
  144. state.waitForSuggestionInitial = waitForSuggestion
  145. state.waitForSuggestion = waitForSuggestion
  146. }
  147. }
  148. .onDisappear {
  149. if fetch, hasFatOrProtein, !keepForNextWiew, !state.useCalc {
  150. state.delete(deleteTwice: true, meal: meal)
  151. } else if fetch, !keepForNextWiew, !state.useCalc {
  152. state.delete(deleteTwice: false, meal: meal)
  153. }
  154. }
  155. .navigationTitle("Enact Bolus")
  156. .navigationBarTitleDisplayMode(.inline)
  157. .toolbar {
  158. ToolbarItem(placement: .topBarLeading) {
  159. if fetch {
  160. Button {
  161. keepForNextWiew = true
  162. state.backToCarbsView(complexEntry: true, meal, override: false)
  163. } label: {
  164. HStack {
  165. Image(systemName: "chevron.backward")
  166. Text("Meal")
  167. }
  168. }
  169. }
  170. else {
  171. Button {
  172. state.hideModal()
  173. } label: {
  174. Text("Close")
  175. }
  176. }
  177. }
  178. }
  179. .popup(isPresented: presentInfo, alignment: .center, direction: .bottom) {
  180. bolusInfo
  181. }
  182. }
  183. var disabled: Bool {
  184. state.amount <= 0 || state.amount > state.maxBolus
  185. }
  186. var predictionChart: some View {
  187. ZStack {
  188. PredictionView(
  189. predictions: $state.predictions, units: $state.units, eventualBG: $state.evBG, target: $state.target,
  190. displayPredictions: $state.displayPredictions
  191. )
  192. }
  193. }
  194. var changed: Bool {
  195. ((meal.first?.carbs ?? 0) > 0) || ((meal.first?.fat ?? 0) > 0) || ((meal.first?.protein ?? 0) > 0)
  196. }
  197. var hasFatOrProtein: Bool {
  198. ((meal.first?.fat ?? 0) > 0) || ((meal.first?.protein ?? 0) > 0)
  199. }
  200. var mealEntries: some View {
  201. VStack {
  202. if let carbs = meal.first?.carbs, carbs > 0 {
  203. HStack {
  204. Text("Carbs")
  205. Spacer()
  206. Text(carbs.formatted())
  207. Text("g")
  208. }.foregroundColor(.secondary)
  209. }
  210. if let fat = meal.first?.fat, fat > 0 {
  211. HStack {
  212. Text("Fat")
  213. Spacer()
  214. Text(fat.formatted())
  215. Text("g")
  216. }.foregroundColor(.secondary)
  217. }
  218. if let protein = meal.first?.protein, protein > 0 {
  219. HStack {
  220. Text("Protein")
  221. Spacer()
  222. Text(protein.formatted())
  223. Text("g")
  224. }.foregroundColor(.secondary)
  225. }
  226. if let note = meal.first?.note, note != "" {
  227. HStack {
  228. Text("Note")
  229. Spacer()
  230. Text(note)
  231. }.foregroundColor(.secondary)
  232. }
  233. }
  234. }
  235. var bolusInfo: some View {
  236. VStack {
  237. // Variables
  238. VStack(spacing: 3) {
  239. HStack {
  240. Text("Eventual Glucose").foregroundColor(.secondary)
  241. let evg = state.units == .mmolL ? Decimal(state.evBG).asMmolL : Decimal(state.evBG)
  242. Text(evg.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  243. Text(state.units.rawValue).foregroundColor(.secondary)
  244. }
  245. HStack {
  246. Text("Target Glucose").foregroundColor(.secondary)
  247. let target = state.units == .mmolL ? state.target.asMmolL : state.target
  248. Text(target.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  249. Text(state.units.rawValue).foregroundColor(.secondary)
  250. }
  251. HStack {
  252. Text("ISF").foregroundColor(.secondary)
  253. let isf = state.isf
  254. Text(isf.formatted())
  255. Text(state.units.rawValue + NSLocalizedString("/U", comment: "/Insulin unit"))
  256. .foregroundColor(.secondary)
  257. }
  258. HStack {
  259. Text("ISF:")
  260. Text("Insulin Sensitivity")
  261. }.foregroundColor(.secondary).italic()
  262. if state.percentage != 100 {
  263. HStack {
  264. Text("Percentage setting").foregroundColor(.secondary)
  265. let percentage = state.percentage
  266. Text(percentage.formatted())
  267. Text("%").foregroundColor(.secondary)
  268. }
  269. }
  270. HStack {
  271. Text("Formula:")
  272. Text("(Eventual Glucose - Target) / ISF")
  273. }.foregroundColor(.secondary).italic().padding(.top, 5)
  274. }
  275. .font(.footnote)
  276. .padding(.top, 10)
  277. Divider()
  278. // Formula
  279. VStack(spacing: 5) {
  280. let unit = NSLocalizedString(
  281. " U",
  282. comment: "Unit in number of units delivered (keep the space character!)"
  283. )
  284. let color: Color = (state.percentage != 100 && state.insulin > 0) ? .secondary : .blue
  285. let fontWeight: Font.Weight = (state.percentage != 100 && state.insulin > 0) ? .regular : .bold
  286. HStack {
  287. Text(NSLocalizedString("Insulin recommended", comment: "") + ":").font(.callout)
  288. Text(state.insulin.formatted() + unit).font(.callout).foregroundColor(color).fontWeight(fontWeight)
  289. }
  290. if state.percentage != 100, state.insulin > 0 {
  291. Divider()
  292. HStack { Text(state.percentage.formatted() + " % ->").font(.callout).foregroundColor(.secondary)
  293. Text(
  294. state.insulinRecommended.formatted() + unit
  295. ).font(.callout).foregroundColor(.blue).bold()
  296. }
  297. }
  298. }
  299. // Warning
  300. if state.error, state.insulinRecommended > 0 {
  301. VStack(spacing: 5) {
  302. Divider()
  303. Text("Warning!").font(.callout).bold().foregroundColor(.orange)
  304. Text(alertString()).font(.footnote)
  305. Divider()
  306. }.padding(.horizontal, 10)
  307. }
  308. // Footer
  309. if !(state.error && state.insulinRecommended > 0) {
  310. VStack {
  311. Text(
  312. "Carbs and previous insulin are included in the glucose prediction, but if the Eventual Glucose is lower than the Target Glucose, a bolus will not be recommended."
  313. ).font(.caption2).foregroundColor(.secondary)
  314. }.padding(20)
  315. }
  316. // Hide button
  317. VStack {
  318. Button { presentInfo = false }
  319. label: { Text("Hide") }.frame(maxWidth: .infinity, alignment: .center).font(.callout)
  320. .foregroundColor(.blue)
  321. }.padding(.bottom, 10)
  322. }
  323. .background(
  324. RoundedRectangle(cornerRadius: 8, style: .continuous)
  325. .fill(Color(colorScheme == .dark ? UIColor.systemGray4 : UIColor.systemGray4))
  326. )
  327. }
  328. // Localize the Oref0 error/warning strings. The default should never be returned
  329. private func alertString() -> String {
  330. switch state.errorString {
  331. case 1,
  332. 2:
  333. return NSLocalizedString(
  334. "Eventual Glucose > Target Glucose, but glucose is predicted to first drop down to ",
  335. comment: "Bolus pop-up / Alert string. Make translations concise!"
  336. ) + state.minGuardBG
  337. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state.units
  338. .rawValue + ", " +
  339. NSLocalizedString(
  340. "which is below your Threshold (",
  341. comment: "Bolus pop-up / Alert string. Make translations concise!"
  342. ) + state
  343. .threshold.formatted() + " " + state.units.rawValue + ")"
  344. case 3:
  345. return NSLocalizedString(
  346. "Eventual Glucose > Target Glucose, but glucose is climbing slower than expected. Expected: ",
  347. comment: "Bolus pop-up / Alert string. Make translations concise!"
  348. ) +
  349. state.expectedDelta
  350. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  351. NSLocalizedString(". Climbing: ", comment: "Bolus pop-up / Alert string. Make translatons concise!") + state
  352. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  353. case 4:
  354. return NSLocalizedString(
  355. "Eventual Glucose > Target Glucose, but glucose is falling faster than expected. Expected: ",
  356. comment: "Bolus pop-up / Alert string. Make translations concise!"
  357. ) +
  358. state.expectedDelta
  359. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  360. NSLocalizedString(". Falling: ", comment: "Bolus pop-up / Alert string. Make translations concise!") + state
  361. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  362. case 5:
  363. return NSLocalizedString(
  364. "Eventual Glucose > Target Glucose, but glucose is changing faster than expected. Expected: ",
  365. comment: "Bolus pop-up / Alert string. Make translations concise!"
  366. ) +
  367. state.expectedDelta
  368. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  369. NSLocalizedString(". Changing: ", comment: "Bolus pop-up / Alert string. Make translations concise!") + state
  370. .minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  371. case 6:
  372. return NSLocalizedString(
  373. "Eventual Glucose > Target Glucose, but glucose is predicted to first drop down to ",
  374. comment: "Bolus pop-up / Alert string. Make translations concise!"
  375. ) + state
  376. .minPredBG
  377. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state
  378. .units
  379. .rawValue
  380. default:
  381. return "Ignore Warning..."
  382. }
  383. }
  384. }
  385. }