DefaultBolusCalcRootView.swift 17 KB

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