DefaultBolusCalcRootView.swift 17 KB

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