AlternativeBolusCalcRootView.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. import Charts
  2. import CoreData
  3. import SwiftUI
  4. import Swinject
  5. extension Bolus {
  6. struct AlternativeBolusCalcRootView: BaseView {
  7. let resolver: Resolver
  8. let waitForSuggestion: Bool
  9. let fetch: Bool
  10. @StateObject var state: StateModel
  11. @State private var showInfo = false
  12. @State private var exceededMaxBolus = false
  13. @State private var keepForNextWiew: Bool = false
  14. private enum Config {
  15. static let dividerHeight: CGFloat = 2
  16. static let overlayColour: Color = .white // Currently commented out
  17. static let spacing: CGFloat = 3
  18. }
  19. @Environment(\.colorScheme) var colorScheme
  20. @FetchRequest(
  21. entity: Meals.entity(),
  22. sortDescriptors: [NSSortDescriptor(key: "createdAt", ascending: false)]
  23. ) var meal: FetchedResults<Meals>
  24. private var formatter: NumberFormatter {
  25. let formatter = NumberFormatter()
  26. formatter.numberStyle = .decimal
  27. formatter.maximumFractionDigits = 2
  28. return formatter
  29. }
  30. private var mealFormatter: NumberFormatter {
  31. let formatter = NumberFormatter()
  32. formatter.numberStyle = .decimal
  33. formatter.maximumFractionDigits = 1
  34. return formatter
  35. }
  36. private var gluoseFormatter: NumberFormatter {
  37. let formatter = NumberFormatter()
  38. formatter.numberStyle = .decimal
  39. if state.units == .mmolL {
  40. formatter.maximumFractionDigits = 1
  41. } else { formatter.maximumFractionDigits = 0 }
  42. return formatter
  43. }
  44. private var fractionDigits: Int {
  45. if state.units == .mmolL {
  46. return 1
  47. } else { return 0 }
  48. }
  49. var body: some View {
  50. Form {
  51. if fetch {
  52. Section {
  53. mealEntries
  54. } header: { Text("Meal Summary") }
  55. }
  56. Section {
  57. Button {
  58. let id_ = meal.first?.id ?? ""
  59. if fetch {
  60. keepForNextWiew = true
  61. state.backToCarbsView(complexEntry: fetch, id_)
  62. } else {
  63. state.showModal(for: .addCarbs(editMode: false))
  64. }
  65. }
  66. label: { Text(fetch ? "Edit Meal" : "Add Meal") }.frame(maxWidth: .infinity, alignment: .center)
  67. } header: { Text(!fetch ? "Meal Summary" : "") }
  68. Section {
  69. HStack {
  70. Button(action: {
  71. showInfo.toggle()
  72. }, label: {
  73. Image(systemName: "info.circle")
  74. Text("Calculations")
  75. })
  76. .foregroundStyle(.blue)
  77. .font(.footnote)
  78. .buttonStyle(PlainButtonStyle())
  79. .frame(maxWidth: .infinity, alignment: .leading)
  80. if state.fattyMeals {
  81. Spacer()
  82. Toggle(isOn: $state.useFattyMealCorrectionFactor) {
  83. Text("Fatty Meal")
  84. }
  85. .toggleStyle(CheckboxToggleStyle())
  86. .font(.footnote)
  87. .onChange(of: state.useFattyMealCorrectionFactor) { _ in
  88. state.insulinCalculated = state.calculateInsulin()
  89. }
  90. }
  91. }
  92. if state.waitForSuggestion {
  93. HStack {
  94. Text("Wait please").foregroundColor(.secondary)
  95. Spacer()
  96. ActivityIndicator(isAnimating: .constant(true), style: .medium) // fix iOS 15 bug
  97. }
  98. } else {
  99. HStack {
  100. Text("Recommended Bolus")
  101. Spacer()
  102. Text(
  103. formatter
  104. .string(from: Double(state.insulinCalculated) as NSNumber) ?? ""
  105. )
  106. Text(
  107. NSLocalizedString(" U", comment: "Unit in number of units delivered (keep the space character!)")
  108. ).foregroundColor(.secondary)
  109. }.contentShape(Rectangle())
  110. .onTapGesture { state.amount = state.insulinCalculated }
  111. }
  112. if !state.waitForSuggestion {
  113. HStack {
  114. Text("Bolus")
  115. Spacer()
  116. DecimalTextField(
  117. "0",
  118. value: $state.amount,
  119. formatter: formatter,
  120. autofocus: false,
  121. cleanInput: true
  122. )
  123. Text(exceededMaxBolus ? "😵" : " U").foregroundColor(.secondary)
  124. }
  125. .onChange(of: state.amount) { newValue in
  126. if newValue > state.maxBolus {
  127. exceededMaxBolus = true
  128. } else {
  129. exceededMaxBolus = false
  130. }
  131. }
  132. }
  133. } header: { Text("Bolus Summary") }
  134. if state.amount > 0 {
  135. Section {
  136. Button {
  137. keepForNextWiew = true
  138. state.add()
  139. }
  140. label: { Text(exceededMaxBolus ? "Max Bolus exceeded!" : "Enact bolus") }
  141. .frame(maxWidth: .infinity, alignment: .center)
  142. .foregroundColor(exceededMaxBolus ? .loopRed : .accentColor)
  143. .disabled(
  144. state.amount <= 0 || state.amount > state.maxBolus
  145. )
  146. }
  147. }
  148. Section {
  149. Button {
  150. keepForNextWiew = true
  151. state.showModal(for: nil)
  152. }
  153. label: { Text("Continue without bolus") }.frame(maxWidth: .infinity, alignment: .center)
  154. }
  155. }
  156. .blur(radius: showInfo ? 3 : 0)
  157. .navigationTitle("Enact Bolus")
  158. .navigationBarTitleDisplayMode(.inline)
  159. .navigationBarItems(
  160. leading: Button { state.hideModal() }
  161. label: { Text("Close") }
  162. )
  163. .onAppear {
  164. configureView {
  165. state.waitForSuggestionInitial = waitForSuggestion
  166. state.waitForSuggestion = waitForSuggestion
  167. state.insulinCalculated = state.calculateInsulin()
  168. }
  169. }
  170. .onDisappear {
  171. if fetch, hasFatOrProtein, !keepForNextWiew, state.useCalc {
  172. state.delete(deleteTwice: true, id: meal.first?.id ?? "")
  173. } else if fetch, !keepForNextWiew, state.useCalc {
  174. state.delete(deleteTwice: false, id: meal.first?.id ?? "")
  175. }
  176. }
  177. .popup(isPresented: showInfo) {
  178. bolusInfoAlternativeCalculator
  179. }
  180. }
  181. // Pop-up
  182. var bolusInfoAlternativeCalculator: some View {
  183. VStack {
  184. VStack {
  185. VStack(spacing: Config.spacing) {
  186. HStack {
  187. Text("Calculations")
  188. .font(.title3).frame(maxWidth: .infinity, alignment: .center)
  189. }.padding(10)
  190. if fetch {
  191. mealEntries.padding()
  192. Divider().frame(height: Config.dividerHeight) // .overlay(Config.overlayColour)
  193. }
  194. settings.padding()
  195. }
  196. Divider().frame(height: Config.dividerHeight) // .overlay(Config.overlayColour)
  197. insulinParts.padding()
  198. Divider().frame(height: Config.dividerHeight) // .overlay(Config.overlayColour)
  199. VStack {
  200. HStack {
  201. Text("Full Bolus")
  202. .foregroundColor(.secondary)
  203. Spacer()
  204. let insulin = state.roundedWholeCalc
  205. Text(insulin.formatted()).foregroundStyle(state.roundedWholeCalc < 0 ? Color.loopRed : Color.primary)
  206. Text(" U")
  207. .foregroundColor(.secondary)
  208. }
  209. }.padding(.horizontal)
  210. Divider().frame(height: Config.dividerHeight)
  211. results.padding()
  212. Divider().frame(height: Config.dividerHeight) // .overlay(Config.overlayColour)
  213. if exceededMaxBolus {
  214. HStack {
  215. let maxBolus = state.maxBolus
  216. let maxBolusFormatted = maxBolus.formatted()
  217. Text("Your entered amount was limited by your max Bolus setting of \(maxBolusFormatted)\(" U")")
  218. }
  219. .padding()
  220. .fontWeight(.semibold)
  221. .foregroundStyle(Color.loopRed)
  222. }
  223. }
  224. .padding(.top, 10)
  225. .padding(.bottom, 15)
  226. // Hide pop-up
  227. VStack {
  228. Button { showInfo = false }
  229. label: { Text("OK") }
  230. .frame(maxWidth: .infinity, alignment: .center)
  231. .font(.system(size: 16))
  232. .fontWeight(.semibold)
  233. .foregroundColor(.blue)
  234. }
  235. .padding(.bottom, 20)
  236. }
  237. .font(.footnote)
  238. .background(
  239. RoundedRectangle(cornerRadius: 10, style: .continuous)
  240. .fill(Color(colorScheme == .dark ? UIColor.systemGray4 : UIColor.systemGray4).opacity(0.9))
  241. )
  242. }
  243. var changed: Bool {
  244. ((meal.first?.carbs ?? 0) > 0) || ((meal.first?.fat ?? 0) > 0) || ((meal.first?.protein ?? 0) > 0)
  245. }
  246. var hasFatOrProtein: Bool {
  247. ((meal.first?.fat ?? 0) > 0) || ((meal.first?.protein ?? 0) > 0)
  248. }
  249. var mealEntries: some View {
  250. VStack {
  251. if let carbs = meal.first?.carbs, carbs > 0 {
  252. HStack {
  253. Text("Carbs")
  254. Spacer()
  255. Text(carbs.formatted())
  256. Text("g")
  257. }.foregroundColor(.secondary)
  258. }
  259. if let fat = meal.first?.fat, fat > 0 {
  260. HStack {
  261. Text("Fat")
  262. Spacer()
  263. Text(fat.formatted())
  264. Text("g")
  265. }.foregroundColor(.secondary)
  266. }
  267. if let protein = meal.first?.protein, protein > 0 {
  268. HStack {
  269. Text("Protein")
  270. Spacer()
  271. Text(protein.formatted())
  272. Text("g")
  273. }.foregroundColor(.secondary)
  274. }
  275. if let note = meal.first?.note, note != "" {
  276. HStack {
  277. Text("Note")
  278. Spacer()
  279. Text(note)
  280. }.foregroundColor(.secondary)
  281. }
  282. }
  283. }
  284. var settings: some View {
  285. VStack {
  286. HStack {
  287. Text("Carb Ratio")
  288. .foregroundColor(.secondary)
  289. Spacer()
  290. Text(state.carbRatio.formatted())
  291. Text(NSLocalizedString(" g/U", comment: " grams per Unit"))
  292. .foregroundColor(.secondary)
  293. }
  294. HStack {
  295. Text("ISF")
  296. .foregroundColor(.secondary)
  297. Spacer()
  298. let isf = state.isf
  299. Text(isf.formatted())
  300. Text(state.units.rawValue + NSLocalizedString("/U", comment: "/Insulin unit"))
  301. .foregroundColor(.secondary)
  302. }
  303. HStack {
  304. Text("Target Glucose")
  305. .foregroundColor(.secondary)
  306. Spacer()
  307. let target = state.units == .mmolL ? state.target.asMmolL : state.target
  308. Text(
  309. target
  310. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  311. )
  312. Text(state.units.rawValue)
  313. .foregroundColor(.secondary)
  314. }
  315. HStack {
  316. Text("Basal")
  317. .foregroundColor(.secondary)
  318. Spacer()
  319. let basal = state.basal
  320. Text(basal.formatted())
  321. Text(NSLocalizedString(" U/h", comment: " Units per hour"))
  322. .foregroundColor(.secondary)
  323. }
  324. HStack {
  325. Text("Fraction")
  326. .foregroundColor(.secondary)
  327. Spacer()
  328. let fraction = state.fraction
  329. Text(fraction.formatted())
  330. }
  331. if state.useFattyMealCorrectionFactor {
  332. HStack {
  333. Text("Fatty Meal Factor")
  334. .foregroundColor(.orange)
  335. Spacer()
  336. let fraction = state.fattyMealFactor
  337. Text(fraction.formatted())
  338. .foregroundColor(.orange)
  339. }
  340. }
  341. }
  342. }
  343. var insulinParts: some View {
  344. VStack(spacing: Config.spacing) {
  345. HStack {
  346. Text("Glucose")
  347. .foregroundColor(.secondary)
  348. Spacer()
  349. let glucose = state.units == .mmolL ? state.currentBG.asMmolL : state.currentBG
  350. Text(glucose.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  351. Text(state.units.rawValue)
  352. .foregroundColor(.secondary)
  353. Spacer()
  354. Image(systemName: "arrow.right")
  355. Spacer()
  356. let targetDifferenceInsulin = state.targetDifferenceInsulin
  357. // rounding
  358. let targetDifferenceInsulinAsDouble = NSDecimalNumber(decimal: targetDifferenceInsulin).doubleValue
  359. let roundedTargetDifferenceInsulin = Decimal(round(100 * targetDifferenceInsulinAsDouble) / 100)
  360. Text(roundedTargetDifferenceInsulin.formatted())
  361. Text(" U")
  362. .foregroundColor(.secondary)
  363. }
  364. HStack {
  365. Text("IOB")
  366. .foregroundColor(.secondary)
  367. Spacer()
  368. let iob = state.iob
  369. // rounding
  370. let iobAsDouble = NSDecimalNumber(decimal: iob).doubleValue
  371. let roundedIob = Decimal(round(100 * iobAsDouble) / 100)
  372. Text(roundedIob.formatted())
  373. Text(" U")
  374. .foregroundColor(.secondary)
  375. Spacer()
  376. Image(systemName: "arrow.right")
  377. Spacer()
  378. let iobCalc = state.iobInsulinReduction
  379. // rounding
  380. let iobCalcAsDouble = NSDecimalNumber(decimal: iobCalc).doubleValue
  381. let roundedIobCalc = Decimal(round(100 * iobCalcAsDouble) / 100)
  382. Text(roundedIobCalc.formatted())
  383. Text(" U").foregroundColor(.secondary)
  384. }
  385. HStack {
  386. Text("Trend")
  387. .foregroundColor(.secondary)
  388. Spacer()
  389. let trend = state.units == .mmolL ? state.deltaBG.asMmolL : state.deltaBG
  390. Text(trend.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
  391. Text(state.units.rawValue).foregroundColor(.secondary)
  392. Spacer()
  393. Image(systemName: "arrow.right")
  394. Spacer()
  395. let trendInsulin = state.fifteenMinInsulin
  396. // rounding
  397. let trendInsulinAsDouble = NSDecimalNumber(decimal: trendInsulin).doubleValue
  398. let roundedTrendInsulin = Decimal(round(100 * trendInsulinAsDouble) / 100)
  399. Text(roundedTrendInsulin.formatted())
  400. Text(" U")
  401. .foregroundColor(.secondary)
  402. }
  403. HStack {
  404. Text("COB")
  405. .foregroundColor(.secondary)
  406. Spacer()
  407. let cob = state.cob
  408. Text(cob.formatted())
  409. let unitGrams = NSLocalizedString(" g", comment: "grams")
  410. Text(unitGrams).foregroundColor(.secondary)
  411. Spacer()
  412. Image(systemName: "arrow.right")
  413. Spacer()
  414. let insulinCob = state.wholeCobInsulin
  415. // rounding
  416. let insulinCobAsDouble = NSDecimalNumber(decimal: insulinCob).doubleValue
  417. let roundedInsulinCob = Decimal(round(100 * insulinCobAsDouble) / 100)
  418. Text(roundedInsulinCob.formatted())
  419. Text(" U")
  420. .foregroundColor(.secondary)
  421. }
  422. }
  423. }
  424. var results: some View {
  425. VStack {
  426. HStack {
  427. Text("Result")
  428. .fontWeight(.bold)
  429. Spacer()
  430. let fraction = state.fraction
  431. Text(fraction.formatted())
  432. Text(" x ")
  433. .foregroundColor(.secondary)
  434. // if fatty meal is chosen
  435. if state.useFattyMealCorrectionFactor {
  436. let fattyMealFactor = state.fattyMealFactor
  437. Text(fattyMealFactor.formatted())
  438. .foregroundColor(.orange)
  439. Text(" x ")
  440. .foregroundColor(.secondary)
  441. }
  442. let insulin = state.roundedWholeCalc
  443. Text(insulin.formatted()).foregroundStyle(state.roundedWholeCalc < 0 ? Color.loopRed : Color.primary)
  444. Text(" U")
  445. .foregroundColor(.secondary)
  446. Text(" = ")
  447. .foregroundColor(.secondary)
  448. let result = state.insulinCalculated
  449. // rounding
  450. let resultAsDouble = NSDecimalNumber(decimal: result).doubleValue
  451. let roundedResult = Decimal(round(100 * resultAsDouble) / 100)
  452. Text(roundedResult.formatted())
  453. .fontWeight(.bold)
  454. .font(.system(size: 16))
  455. .foregroundColor(.blue)
  456. Text(" U")
  457. .foregroundColor(.secondary)
  458. }
  459. }
  460. }
  461. }
  462. }