DefaultBolusCalcRootView.swift 17 KB

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