AlternativeBolusCalcRootView.swift 24 KB

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