DefaultBolusCalcRootView.swift 18 KB

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