PopupView.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. import SwiftUI
  2. struct PopupView: View {
  3. var state: Bolus.StateModel
  4. @Environment(\.colorScheme) var colorScheme
  5. private var fractionDigits: Int {
  6. if state.units == .mmolL {
  7. return 1
  8. } else { return 0 }
  9. }
  10. var body: some View {
  11. NavigationStack {
  12. ScrollView {
  13. Grid(alignment: .topLeading, horizontalSpacing: 3, verticalSpacing: 0) {
  14. GridRow {
  15. Text("Calculations").fontWeight(.bold).gridCellColumns(3).gridCellAnchor(.center).padding(.vertical)
  16. }
  17. calcSettingsFirstRow
  18. calcSettingsSecondRow
  19. DividerCustom()
  20. // meal entries as grid rows
  21. if state.carbs > 0 {
  22. GridRow {
  23. Text("Carbs").foregroundColor(.secondary)
  24. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  25. HStack {
  26. Text(state.carbs.formatted())
  27. Text("g").foregroundColor(.secondary)
  28. }.gridCellAnchor(.trailing)
  29. }
  30. }
  31. if state.fat > 0 {
  32. GridRow {
  33. Text("Fat").foregroundColor(.secondary)
  34. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  35. HStack {
  36. Text(state.fat.formatted())
  37. Text("g").foregroundColor(.secondary)
  38. }.gridCellAnchor(.trailing)
  39. }
  40. }
  41. if state.protein > 0 {
  42. GridRow {
  43. Text("Protein").foregroundColor(.secondary)
  44. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  45. HStack {
  46. Text(state.protein.formatted())
  47. Text("g").foregroundColor(.secondary)
  48. }.gridCellAnchor(.trailing)
  49. }
  50. }
  51. if state.carbs > 0 || state.protein > 0 || state.fat > 0 {
  52. DividerCustom()
  53. }
  54. GridRow {
  55. Text("Detailed Calculation Steps").gridCellColumns(3).gridCellAnchor(.center)
  56. .padding(.bottom, 10)
  57. }
  58. calcGlucoseFirstRow
  59. calcGlucoseSecondRow.padding(.bottom, 5)
  60. calcGlucoseFormulaRow
  61. DividerCustom()
  62. calcIOBRow
  63. DividerCustom()
  64. calcCOBRow.padding(.bottom, 5)
  65. calcCOBFormulaRow
  66. DividerCustom()
  67. calcDeltaRow
  68. calcDeltaFormulaRow
  69. DividerCustom()
  70. calcFullBolusRow
  71. if state.useSuperBolus {
  72. DividerCustom()
  73. calcSuperBolusRow
  74. }
  75. DividerDouble()
  76. calcResultRow
  77. calcResultFormulaRow
  78. }
  79. Spacer()
  80. Button { state.showInfo = false }
  81. label: { Text("Got it!").frame(maxWidth: .infinity, alignment: .center) }
  82. .buttonStyle(.bordered)
  83. .padding(.top)
  84. }
  85. .padding([.horizontal, .bottom])
  86. .font(.subheadline)
  87. }
  88. }
  89. var calcSettingsFirstRow: some View {
  90. GridRow {
  91. Group {
  92. Text("Carb Ratio:")
  93. .foregroundColor(.secondary)
  94. }.gridCellAnchor(.leading)
  95. Group {
  96. Text("ISF:")
  97. .foregroundColor(.secondary)
  98. }.gridCellAnchor(.leading)
  99. VStack {
  100. Text("Target:")
  101. .foregroundColor(.secondary)
  102. }.gridCellAnchor(.leading)
  103. }
  104. }
  105. var calcSettingsSecondRow: some View {
  106. GridRow {
  107. Text(state.carbRatio.formatted() + " " + NSLocalizedString("g/U", comment: " grams per Unit"))
  108. .gridCellAnchor(.leading)
  109. let isf = state.units == .mmolL ? state.isf.formattedAsMmolL : state.isf.description
  110. Text(
  111. isf + " " + state.units
  112. .rawValue + NSLocalizedString("/U", comment: "/Insulin unit")
  113. ).gridCellAnchor(.leading)
  114. let target = state.units == .mmolL ? state.target.formattedAsMmolL : state.target.description
  115. Text(
  116. target +
  117. " " + state.units.rawValue
  118. ).gridCellAnchor(.leading)
  119. }
  120. }
  121. var calcGlucoseFirstRow: some View {
  122. GridRow(alignment: .center) {
  123. let currentBG = state.units == .mmolL ? state.currentBG.formattedAsMmolL : state.currentBG.description
  124. let target = state.units == .mmolL ? state.target.formattedAsMmolL : state.target.description
  125. Text("Glucose:").foregroundColor(.secondary)
  126. let targetDifference = state.units == .mmolL ? state.targetDifference.formattedAsMmolL : state.targetDifference
  127. .description
  128. let firstRow = currentBG
  129. + " - " +
  130. target
  131. + " = " +
  132. targetDifference
  133. Text(firstRow).frame(minWidth: 0, alignment: .leading).foregroundColor(.secondary)
  134. .gridColumnAlignment(.leading)
  135. HStack {
  136. Text(
  137. self.insulinFormatter(state.targetDifferenceInsulin)
  138. )
  139. Text("U").foregroundColor(.secondary)
  140. }.fontWeight(.bold)
  141. .gridColumnAlignment(.trailing)
  142. }
  143. }
  144. var calcGlucoseSecondRow: some View {
  145. GridRow(alignment: .center) {
  146. let currentBG = state.units == .mmolL ? state.currentBG.formattedAsMmolL : state.currentBG.description
  147. Text(
  148. currentBG
  149. + " " +
  150. state.units.rawValue
  151. )
  152. let targetDifference = state.units == .mmolL ? state.targetDifference.formattedAsMmolL : state.targetDifference
  153. .description
  154. let secondRow = targetDifference + " / " +
  155. (state.units == .mmolL ? state.isf.formattedAsMmolL : state.isf.description)
  156. .description + " ≈ " + self.insulinFormatter(state.targetDifferenceInsulin)
  157. Text(secondRow).foregroundColor(.secondary).gridColumnAlignment(.leading)
  158. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  159. }
  160. }
  161. var calcGlucoseFormulaRow: some View {
  162. GridRow(alignment: .top) {
  163. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  164. Text("(Current - Target) / ISF").foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  165. .gridColumnAlignment(.leading)
  166. .gridCellColumns(2)
  167. }
  168. .font(.caption)
  169. }
  170. var calcIOBRow: some View {
  171. GridRow(alignment: .center) {
  172. HStack {
  173. Text("IOB:").foregroundColor(.secondary)
  174. Text(
  175. self.insulinFormatter(state.iob)
  176. )
  177. }
  178. Text("Subtract IOB").foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8)).font(.footnote)
  179. let iobFormatted = self.insulinFormatter(state.iob)
  180. HStack {
  181. Text((state.iob >= 0 ? "-" : "") + (state.iob >= 0 ? iobFormatted : "(" + iobFormatted + ")"))
  182. Text("U").foregroundColor(.secondary)
  183. }.fontWeight(.bold)
  184. .gridColumnAlignment(.trailing)
  185. }
  186. }
  187. var calcCOBRow: some View {
  188. GridRow(alignment: .center) {
  189. HStack {
  190. Text("COB:").foregroundColor(.secondary)
  191. Text(
  192. state.wholeCob
  193. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
  194. NSLocalizedString(" g", comment: "grams")
  195. )
  196. }
  197. Text(
  198. state.wholeCob
  199. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
  200. + " / " +
  201. state.carbRatio.formatted()
  202. + " ≈ " +
  203. self.insulinFormatter(state.wholeCobInsulin)
  204. )
  205. .foregroundColor(.secondary)
  206. .gridColumnAlignment(.leading)
  207. HStack {
  208. Text(
  209. self.insulinFormatter(state.wholeCobInsulin)
  210. )
  211. Text("U").foregroundColor(.secondary)
  212. }.fontWeight(.bold)
  213. .gridColumnAlignment(.trailing)
  214. }
  215. }
  216. var calcCOBFormulaRow: some View {
  217. GridRow(alignment: .center) {
  218. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  219. Text("COB / Carb Ratio").foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  220. .gridColumnAlignment(.leading)
  221. .gridCellColumns(2)
  222. }
  223. .font(.caption)
  224. }
  225. var calcDeltaRow: some View {
  226. GridRow(alignment: .center) {
  227. Text("Delta:").foregroundColor(.secondary)
  228. let deltaBG = state.units == .mmolL ? state.deltaBG.formattedAsMmolL : state.deltaBG.description
  229. let isf = state.units == .mmolL ? state.isf.formattedAsMmolL : state.isf.description
  230. let fifteenMinInsulinFormatted = self.insulinFormatter(state.fifteenMinInsulin)
  231. Text(
  232. deltaBG + " / " + isf + " ≈ " + fifteenMinInsulinFormatted
  233. )
  234. .foregroundColor(.secondary)
  235. .gridColumnAlignment(.leading)
  236. HStack {
  237. Text(fifteenMinInsulinFormatted)
  238. Text("U").foregroundColor(.secondary)
  239. }.fontWeight(.bold)
  240. .gridColumnAlignment(.trailing)
  241. }
  242. }
  243. var calcDeltaFormulaRow: some View {
  244. GridRow(alignment: .center) {
  245. let deltaBG = state.units == .mmolL ? state.deltaBG.formattedAsMmolL : state.deltaBG.description
  246. Text(
  247. deltaBG
  248. + " " +
  249. state.units.rawValue
  250. )
  251. Text("15min Delta / ISF").font(.caption).foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  252. .gridColumnAlignment(.leading)
  253. .gridCellColumns(2).padding(.top, 5)
  254. }
  255. }
  256. var calcFullBolusRow: some View {
  257. GridRow(alignment: .center) {
  258. Text("Full Bolus")
  259. .foregroundColor(.secondary)
  260. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  261. HStack {
  262. Text(self.insulinFormatter(state.wholeCalc))
  263. .foregroundStyle(state.wholeCalc < 0 ? Color.loopRed : Color.primary)
  264. Text("U").foregroundColor(.secondary)
  265. }.gridColumnAlignment(.trailing)
  266. .fontWeight(.bold)
  267. }
  268. }
  269. var calcSuperBolusRow: some View {
  270. GridRow(alignment: .center) {
  271. Text("Super Bolus")
  272. .foregroundColor(.secondary)
  273. Text("Added to Result").foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8)).font(.footnote)
  274. HStack {
  275. Text("+" + self.insulinFormatter(state.superBolusInsulin))
  276. .foregroundStyle(Color.loopRed)
  277. Text("U").foregroundColor(.secondary)
  278. }.gridColumnAlignment(.trailing)
  279. .fontWeight(.bold)
  280. }
  281. }
  282. var calcResultRow: some View {
  283. GridRow(alignment: .center) {
  284. Text("Result").fontWeight(.bold)
  285. HStack {
  286. Text(state.useSuperBolus ? "(" : "")
  287. .foregroundColor(.loopRed)
  288. + Text(state.fraction.formatted())
  289. + Text(" x ")
  290. .foregroundColor(.secondary)
  291. // if fatty meal is chosen
  292. + Text(state.useFattyMealCorrectionFactor ? state.fattyMealFactor.formatted() : "")
  293. .foregroundColor(.orange)
  294. + Text(state.useFattyMealCorrectionFactor ? " x " : "")
  295. .foregroundColor(.secondary)
  296. // endif fatty meal is chosen
  297. + Text(self.insulinFormatter(state.wholeCalc))
  298. .foregroundColor(state.wholeCalc < 0 ? Color.loopRed : Color.primary)
  299. // if superbolus is chosen
  300. + Text(state.useSuperBolus ? ")" : "")
  301. .foregroundColor(.loopRed)
  302. + Text(state.useSuperBolus ? " + " : "")
  303. .foregroundColor(.secondary)
  304. + Text(state.useSuperBolus ? self.insulinFormatter(state.superBolusInsulin) : "")
  305. .foregroundColor(.loopRed)
  306. // endif superbolus is chosen
  307. + Text(" ≈ ")
  308. .foregroundColor(.secondary)
  309. }
  310. .gridColumnAlignment(.leading)
  311. HStack {
  312. Text(self.insulinFormatter(state.insulinCalculated))
  313. .fontWeight(.bold)
  314. .foregroundColor(state.wholeCalc >= state.maxBolus ? Color.loopRed : Color.blue)
  315. Text("U").foregroundColor(.secondary)
  316. }
  317. .gridColumnAlignment(.trailing)
  318. .fontWeight(.bold)
  319. }
  320. }
  321. var calcResultFormulaRow: some View {
  322. GridRow(alignment: .bottom) {
  323. if state.useFattyMealCorrectionFactor {
  324. Group {
  325. Text("Factor x Fatty Meal Factor x Full Bolus")
  326. .foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  327. +
  328. Text(state.wholeCalc > state.maxBolus ? " ≈ Max Bolus" : "").foregroundColor(Color.loopRed)
  329. }
  330. .font(.caption)
  331. .gridCellAnchor(.center)
  332. .gridCellColumns(3)
  333. } else if state.useSuperBolus {
  334. Group {
  335. Text("(Factor x Full Bolus) + Super Bolus")
  336. .foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  337. +
  338. Text(state.wholeCalc > state.maxBolus ? " ≈ Max Bolus" : "").foregroundColor(Color.loopRed)
  339. }
  340. .font(.caption)
  341. .gridCellAnchor(.center)
  342. .gridCellColumns(3)
  343. } else {
  344. Color.clear.gridCellUnsizedAxes([.horizontal, .vertical])
  345. Group {
  346. Text("Factor x Full Bolus")
  347. .foregroundColor(.secondary.opacity(colorScheme == .dark ? 0.65 : 0.8))
  348. +
  349. Text(state.wholeCalc > state.maxBolus ? " ≈ Max Bolus" : "").foregroundColor(Color.loopRed)
  350. }
  351. .font(.caption)
  352. .padding(.top, 5)
  353. .gridCellAnchor(.leading)
  354. .gridCellColumns(2)
  355. }
  356. }
  357. }
  358. private func insulinFormatter(_ value: Decimal) -> String {
  359. let toRound = NSDecimalNumber(decimal: value).doubleValue
  360. let roundedValue = Decimal(floor(100 * toRound) / 100)
  361. let formatter = NumberFormatter()
  362. formatter.numberStyle = .decimal
  363. formatter.minimumFractionDigits = 2
  364. formatter.maximumFractionDigits = 2
  365. formatter.locale = Locale.current // Uses the user's locale
  366. return formatter.string(from: roundedValue as NSNumber) ?? String(format: "%.2f", toRound)
  367. }
  368. struct DividerDouble: View {
  369. var body: some View {
  370. VStack(spacing: 2) {
  371. Rectangle()
  372. .frame(height: 1)
  373. .foregroundColor(.gray.opacity(0.65))
  374. Rectangle()
  375. .frame(height: 1)
  376. .foregroundColor(.gray.opacity(0.65))
  377. }
  378. .frame(height: 4)
  379. .padding(.vertical)
  380. }
  381. }
  382. struct DividerCustom: View {
  383. var body: some View {
  384. Rectangle()
  385. .frame(height: 1)
  386. .foregroundColor(.gray.opacity(0.65))
  387. .padding(.vertical)
  388. }
  389. }
  390. }