DefaultBolusCalcRootView.swift 18 KB

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