DefaultBolusCalcRootView.swift 17 KB

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