DefaultBolusCalcRootView.swift 17 KB

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