AlternativeBolusCalcRootView.swift 21 KB

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