DefaultBolusCalcRootView.swift 16 KB

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