OnboardingView+AlgorithmUtil.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. //
  2. // OnboardingView+AlgorithmUtil.swift
  3. // Trio
  4. //
  5. // Created by Cengiz Deniz on 15.04.25.
  6. //
  7. import SwiftUI
  8. protocol AlgorithmSubstepProtocol: Identifiable, RawRepresentable {
  9. var title: String { get }
  10. func hint(units: GlucoseUnits) -> String
  11. func description(units: GlucoseUnits) -> any View
  12. }
  13. extension AlgorithmSubstepProtocol {
  14. var title: String {
  15. AlgorithmSettingMeta(rawValue: String(describing: self))?.title ?? ""
  16. }
  17. func hint(units: GlucoseUnits) -> String {
  18. AlgorithmSettingMeta(rawValue: String(describing: self))?.hint(units: units) ?? ""
  19. }
  20. func description(units: GlucoseUnits) -> any View {
  21. AlgorithmSettingMeta(rawValue: String(describing: self))?.description(units: units) ?? AnyView(EmptyView())
  22. }
  23. }
  24. extension AlgorithmSubstepProtocol where Self: RawRepresentable, Self.RawValue == Int {
  25. func toAlgorithmSubstep() -> AlgorithmSettingsSubstep? {
  26. switch self {
  27. case let step as AutosensSettingsSubstep:
  28. return [
  29. .autosensMin,
  30. .autosensMax,
  31. .rewindResetsAutosens
  32. ][step.rawValue]
  33. case let step as SMBSettingsSubstep:
  34. return [
  35. .enableSMBAlways,
  36. .enableSMBWithCOB,
  37. .enableSMBWithTempTarget,
  38. .enableSMBAfterCarbs,
  39. .enableSMBWithHighGlucoseTarget,
  40. .allowSMBWithHighTempTarget,
  41. .enableUAM,
  42. .maxSMBMinutes,
  43. .maxUAMMinutes,
  44. .maxDeltaGlucoseThreshold
  45. ][step.rawValue]
  46. case let step as TargetBehaviorSubstep:
  47. return [
  48. .highTempTargetRaisesSensitivity,
  49. .lowTempTargetLowersSensitivity,
  50. .sensitivityRaisesTarget,
  51. .resistanceLowersTarget,
  52. .halfBasalTarget
  53. ][step.rawValue]
  54. default:
  55. return nil
  56. }
  57. }
  58. }
  59. extension View {
  60. func eraseToAnyView() -> AnyView {
  61. AnyView(self)
  62. }
  63. }
  64. enum AlgorithmSettingsSubstep: Int, CaseIterable, Identifiable {
  65. case autosensMin
  66. case autosensMax
  67. case rewindResetsAutosens
  68. case enableSMBAlways
  69. case enableSMBWithCOB
  70. case enableSMBWithTempTarget
  71. case enableSMBAfterCarbs
  72. case enableSMBWithHighGlucoseTarget
  73. case allowSMBWithHighTempTarget
  74. case enableUAM
  75. case maxSMBMinutes
  76. case maxUAMMinutes
  77. case maxDeltaGlucoseThreshold
  78. case highTempTargetRaisesSensitivity
  79. case lowTempTargetLowersSensitivity
  80. case sensitivityRaisesTarget
  81. case resistanceLowersTarget
  82. case halfBasalTarget
  83. var id: Int { rawValue }
  84. var title: String {
  85. switch self {
  86. case .autosensMin: return String(localized: "Autosens Min", comment: "Autosens Min")
  87. case .autosensMax: return String(localized: "Autosens Max", comment: "Autosens Max")
  88. case .rewindResetsAutosens: return String(localized: "Rewind Resets Autosens", comment: "Rewind Resets Autosens")
  89. case .enableSMBAlways: return String(localized: "Enable SMB Always", comment: "Enable SMB Always")
  90. case .enableSMBWithCOB: return String(localized: "Enable SMB With COB", comment: "Enable SMB With COB")
  91. case .enableSMBWithTempTarget: return String(
  92. localized: "Enable SMB With Temptarget",
  93. comment: "Enable SMB With Temptarget"
  94. )
  95. case .enableSMBAfterCarbs: return String(localized: "Enable SMB After Carbs", comment: "Enable SMB After Carbs")
  96. case .enableSMBWithHighGlucoseTarget: return String(
  97. localized: "Enable SMB With High BG",
  98. comment: "Enable SMB With High BG"
  99. )
  100. case .allowSMBWithHighTempTarget: return String(
  101. localized: "Allow SMB With High Temptarget",
  102. comment: "Allow SMB With High Temptarget"
  103. )
  104. case .enableUAM: return String(localized: "Enable UAM", comment: "Enable UAM")
  105. case .maxSMBMinutes: return String(localized: "Max SMB Basal Minutes", comment: "Max SMB Basal Minutes")
  106. case .maxUAMMinutes: return String(localized: "Max UAM Basal Minutes", comment: "Max UAM Basal Minutes")
  107. case .maxDeltaGlucoseThreshold: return String(localized: "Max Delta-BG Threshold SMB", comment: "Max Delta-BG Threshold")
  108. case .highTempTargetRaisesSensitivity: return String(
  109. localized: "High Temp Target Raises Sensitivity",
  110. comment: "High Temp Target Raises Sensitivity"
  111. )
  112. case .lowTempTargetLowersSensitivity: return String(
  113. localized: "High Temp Target Raises Sensitivity",
  114. comment: "High Temp Target Raises Sensitivity"
  115. )
  116. case .sensitivityRaisesTarget: return String(localized: "Sensitivity Raises Target", comment: "Sensitivity Raises Target")
  117. case .resistanceLowersTarget: return String(localized: "Resistance Lowers Target", comment: "Resistance Lowers Target")
  118. case .halfBasalTarget: return String(localized: "Half Basal Exercise Target", comment: "Half Basal Exercise Target")
  119. }
  120. }
  121. func hint(units: GlucoseUnits) -> String {
  122. switch self {
  123. case .autosensMin: return String(localized: "Lower limit of the Autosens Ratio.")
  124. case .autosensMax: return String(localized: "Upper limit of the Autosens Ratio.")
  125. case .rewindResetsAutosens: return String(localized: "Pump rewind initiates a reset in Autosens Ratio.")
  126. case .enableSMBAlways: return String(localized: "Allow SMBs at all times except when a high Temp Target is set.")
  127. case .enableSMBWithCOB: return String(localized: "Allow SMB when carbs are on board.")
  128. case .enableSMBWithTempTarget: return String(
  129. localized: "Allow SMB when a manual Temporary Target is set under \(units == .mgdL ? "100" : 100.formattedAsMmolL) \(units.rawValue)."
  130. )
  131. case .enableSMBAfterCarbs: return String(localized: "Allow SMB for 6 hrs after a carb entry.")
  132. case .enableSMBWithHighGlucoseTarget: return String(localized: "Allow SMB when glucose is above the High BG Target value.")
  133. case .allowSMBWithHighTempTarget: return String(
  134. localized: "Allow SMB when a manual Temporary Target is set greater than \(units == .mgdL ? "100" : 100.formattedAsMmolL) \(units.rawValue)."
  135. )
  136. case .enableUAM: return String(localized: "Enable Unannounced Meals SMB.")
  137. case .maxSMBMinutes: return String(localized: "Limits the size of a single Super Micro Bolus (SMB) dose.")
  138. case .maxUAMMinutes: return String(localized: "Limits the size of a single Unannounced Meal (UAM) SMB dose.")
  139. case .maxDeltaGlucoseThreshold: return String(localized: "Disables SMBs if last two glucose values differ by more than this percent.")
  140. case .highTempTargetRaisesSensitivity: return String(
  141. localized: "Increase sensitivity when glucose is above target if a manual Temp Target > \(units == .mgdL ? "100" : 100.formattedAsMmolL) \(units.rawValue) is set."
  142. )
  143. case .lowTempTargetLowersSensitivity: return String(
  144. localized: "Decrease sensitivity when glucose is below target if a manual Temp Target < \(units == .mgdL ? "100" : 100.formattedAsMmolL) \(units.rawValue) is set."
  145. )
  146. case .sensitivityRaisesTarget: return String(localized: "Raise target glucose if when Autosens Ratio is >1.")
  147. case .resistanceLowersTarget: return String(localized: "Lower target glucose when Autosens Ratio is <1.")
  148. case .halfBasalTarget: return String(localized: "Scales down your basal rate to 50% at this value.")
  149. }
  150. }
  151. func description(units: GlucoseUnits) -> any View {
  152. switch self {
  153. case .autosensMin:
  154. return VStack(alignment: .leading, spacing: 8) {
  155. Text("Default: 70%").bold()
  156. Text(
  157. "Autosens Min sets the minimum Autosens Ratio used by Autosens, Dynamic ISF, and Sigmoid Formula."
  158. )
  159. Text(
  160. "The Autosens Ratio is used to calculate the amount of adjustment needed to basal rates, ISF, and CR."
  161. )
  162. Text(
  163. "Tip: Decreasing this value allows automatic adjustments of basal rates to be lower, ISF to be higher, and CR to be higher."
  164. )
  165. }
  166. case .autosensMax:
  167. return VStack(alignment: .leading, spacing: 8) {
  168. Text("Default: 120%").bold()
  169. Text(
  170. "Autosens Max sets the maximum Autosens Ratio used by Autosens, Dynamic ISF, and Sigmoid Formula."
  171. )
  172. Text(
  173. "The Autosens Ratio is used to calculate the amount of adjustment needed to basal rates, ISF, and CR."
  174. )
  175. Text(
  176. "Tip: Increasing this value allows automatic adjustments of basal rates to be higher, ISF to be lower, and CR to be lower."
  177. )
  178. }
  179. case .rewindResetsAutosens:
  180. return VStack(alignment: .leading, spacing: 8) {
  181. Text(
  182. "This feature resets the Autosens Ratio to neutral when you rewind your pump on the assumption that this corresponds to a site change."
  183. )
  184. Text(
  185. "Autosens will begin learning sensitivity anew from the time of the rewind, which may take up to 6 hours."
  186. )
  187. Text(
  188. "Tip: If you usually rewind your pump independently of site changes, you may want to consider disabling this feature."
  189. )
  190. }
  191. case .enableSMBAlways:
  192. return VStack(alignment: .leading, spacing: 8) {
  193. Text("Default: OFF").bold()
  194. Text(
  195. "When enabled, Super Micro Boluses (SMBs) will always be allowed if dosing calculations determine insulin is needed via the SMB delivery method, except when a high Temp Target is set. Enabling SMB Always will remove redundant \"Enable SMB\" options when this setting is enacted."
  196. )
  197. Text(
  198. "Note: If you would like to allow SMBs when a high Temp Target is set, enable the \"Allow SMBs with High Temptarget\" setting."
  199. )
  200. }
  201. case .enableSMBWithCOB:
  202. return VStack(alignment: .leading, spacing: 8) {
  203. Text("Default: OFF").bold()
  204. Text(
  205. "When the carb on board (COB) forecast line is active, enabling this feature allows Trio to use Super Micro Boluses (SMB) to deliver the insulin required."
  206. )
  207. Text(
  208. "Note: If this is enabled and the criteria are met, SMBs could be utilized regardless of other SMB settings being enabled or not."
  209. )
  210. }
  211. case .enableSMBWithTempTarget:
  212. return VStack(alignment: .leading, spacing: 8) {
  213. Text("Default: OFF").bold()
  214. Text(
  215. "Enabling this feature allows Trio to deliver insulin required using Super Micro Boluses (SMB) at times when a manual Temporary Target under \(units == .mgdL ? "100" : 100.formattedAsMmolL) \(units.rawValue) is set."
  216. )
  217. Text(
  218. "Note: If this is enabled and the criteria are met, SMBs could be utilized regardless of other SMB settings being enabled or not."
  219. )
  220. }
  221. case .enableSMBAfterCarbs:
  222. return VStack(alignment: .leading, spacing: 8) {
  223. Text("Default: OFF").bold()
  224. Text(
  225. "Enabling this feature allows Trio to deliver insulin required using Super Micro Boluses (SMB) for 6 hours after a carb entry, regardless of whether there are active carbs on board (COB)."
  226. )
  227. Text(
  228. "Note: If this is enabled and the criteria are met, SMBs could be utilized regardless of other SMB settings being enabled or not."
  229. )
  230. }
  231. case .enableSMBWithHighGlucoseTarget:
  232. return VStack(alignment: .leading, spacing: 8) {
  233. Text("Default: OFF").bold()
  234. Text(
  235. "Enabling this feature allows Trio to deliver insulin required using Super Micro Boluses (SMB) when glucose reading is above the value set as High BG Target."
  236. )
  237. Text(
  238. "Note: If this is enabled and the criteria are met, SMBs could be utilized regardless of other SMB settings being enabled or not."
  239. )
  240. }
  241. case .allowSMBWithHighTempTarget:
  242. return VStack(alignment: .leading, spacing: 8) {
  243. Text("Default: OFF").bold()
  244. Text(
  245. "Enabling this feature allows Trio to deliver insulin required using Super Micro Boluses (SMB) when a manual Temporary Target above \(units == .mgdL ? "100" : 100.formattedAsMmolL) \(units.rawValue) is set."
  246. )
  247. Text(
  248. "Note: If this is enabled and the criteria are met, SMBs could be utilized regardless of other SMB settings being enabled or not."
  249. )
  250. Text(
  251. "Warning: High Temp Targets are often set when recovering from lows. If you use High Temp Targets for that purpose, this feature should remain disabled."
  252. ).bold()
  253. }
  254. case .enableUAM:
  255. return VStack(alignment: .leading, spacing: 8) {
  256. Text("Default: OFF").bold()
  257. Text(
  258. "Enabling the UAM (Unannounced Meals) feature allows the system to detect and respond to unexpected rises in glucose readings caused by unannounced or miscalculated carbs, meals high in fat or protein, or other factors like adrenaline."
  259. )
  260. Text(
  261. "It uses the SMB (Super Micro Bolus) algorithm to deliver insulin in small amounts to correct glucose spikes. UAM also works in reverse, reducing or stopping SMBs if glucose levels drop unexpectedly."
  262. )
  263. Text(
  264. "This feature ensures more accurate insulin adjustments when carb entries are missing or incorrect."
  265. )
  266. }
  267. case .maxSMBMinutes:
  268. return VStack(spacing: 8) {
  269. VStack(alignment: .leading, spacing: 8) {
  270. VStack(alignment: .leading, spacing: 1) {
  271. Text("Default: 30 minutes").bold()
  272. Text("(50% current basal rate)").bold()
  273. }
  274. VStack(alignment: .leading, spacing: 8) {
  275. Text(
  276. "This is a limit on the size of a single SMB. One SMB can only be as large as this many minutes of your current profile basal rate."
  277. )
  278. Text(
  279. "To calculate the maximum SMB allowed based on this setting, use the following formula:"
  280. )
  281. }
  282. }
  283. VStack(alignment: .center, spacing: 5) {
  284. Text(
  285. "𝒳 = Max SMB Basal Minutes"
  286. )
  287. Text("(𝒳 / 60) × current basal rate")
  288. }
  289. VStack(alignment: .leading, spacing: 8) {
  290. Text(
  291. "Warning: Increasing this value above 90 minutes may impact Trio's ability to effectively zero temp and prevent lows."
  292. ).bold()
  293. Text("Note: SMBs must be enabled to use this limit.")
  294. }
  295. }
  296. case .maxUAMMinutes:
  297. return VStack(spacing: 8) {
  298. VStack(alignment: .leading, spacing: 8) {
  299. VStack(alignment: .leading, spacing: 1) {
  300. Text("Default: 30 minutes").bold()
  301. Text("(50% current basal rate)").bold()
  302. }
  303. VStack(alignment: .leading, spacing: 8) {
  304. Text(
  305. "This is a limit on the size of a single UAM SMB. One UAM SMB can only be as large as this many minutes of your current profile basal rate."
  306. )
  307. Text(
  308. "To calculate the maximum UAM SMB allowed based on this setting, use the following formula:"
  309. )
  310. }
  311. }
  312. VStack(alignment: .center, spacing: 5) {
  313. Text(
  314. "𝒳 = Max UAM SMB Basal Minutes"
  315. )
  316. Text("(𝒳 / 60) × current basal rate")
  317. }
  318. VStack(alignment: .leading, spacing: 8) {
  319. Text(
  320. "Warning: Increasing this value above 60 minutes may impact Trio's ability to effectively zero temp and prevent lows."
  321. ).bold()
  322. Text("Note: UAM SMBs must be enabled to use this limit.")
  323. }
  324. }
  325. case .maxDeltaGlucoseThreshold:
  326. return VStack(alignment: .leading, spacing: 8) {
  327. Text("Default: 20% increase").bold()
  328. Text(
  329. "Maximum allowed positive percent change in glucose level to permit SMBs. If the difference in glucose is greater than this, Trio will disable SMBs."
  330. )
  331. Text("Note: This setting has a hard-coded cap of 40%")
  332. }
  333. case .highTempTargetRaisesSensitivity:
  334. return VStack(alignment: .leading, spacing: 8) {
  335. Text("Default: OFF").bold()
  336. Text(
  337. "When this feature is enabled, manually setting a temporary target above \(units == .mgdL ? "100" : 100.formattedAsMmolL) \(units.rawValue) will decrease the Autosens Ratio used for ISF and basal adjustments, resulting in less insulin delivered overall. This scales with the temporary target set; the higher the temp target, the lower the Autosens Ratio used."
  338. )
  339. Text(
  340. "If Half Basal Exercise Target is set to \(units == .mgdL ? "160" : 160.formattedAsMmolL) \(units.rawValue), a temp target of \(units == .mgdL ? "120" : 120.formattedAsMmolL) \(units.rawValue) uses an Autosens Ratio of 0.75. A temp target of \(units == .mgdL ? "140" : 140.formattedAsMmolL) \(units.rawValue) uses an Autosens Ratio of 0.6."
  341. )
  342. Text("Note: The effect of this can be adjusted with the Half Basal Exercise Target")
  343. }
  344. case .lowTempTargetLowersSensitivity:
  345. return VStack(alignment: .leading, spacing: 8) {
  346. Text("Default: OFF").bold()
  347. Text(
  348. "When this feature is enabled, setting a temporary target below \(units == .mgdL ? "100" : 100.formattedAsMmolL) \(units.rawValue) will increase the Autosens Ratio used for ISF and basal adjustments, resulting in more insulin delivered overall. This scales with the temporary target set; the lower the Temp Target, the higher the Autosens Ratio used. It requires Algorithm Settings > Autosens > Autosens Max to be set to > 100% to work."
  349. )
  350. Text(
  351. "If Half Basal Exercise Target is \(units == .mgdL ? "160" : 160.formattedAsMmolL) \(units.rawValue), a Temp Target of \(units == .mgdL ? "95" : 95.formattedAsMmolL) \(units.rawValue) uses an Autosens Ratio of 1.09. A Temp Target of \(units == .mgdL ? "85" : 85.formattedAsMmolL) \(units.rawValue) uses an Autosens Ratio of 1.33."
  352. )
  353. Text("Note: The effect of this can be adjusted with the Half Basal Exercise Target")
  354. }
  355. case .sensitivityRaisesTarget:
  356. return VStack(alignment: .leading, spacing: 8) {
  357. Text("Default: OFF").bold()
  358. Text(
  359. "Enabling this feature causes Trio to automatically raise the targeted glucose if it detects an increase in insulin sensitivity from your baseline."
  360. )
  361. }
  362. case .resistanceLowersTarget:
  363. return VStack(alignment: .leading, spacing: 8) {
  364. Text("Default: OFF").bold()
  365. Text(
  366. "Enabling this feature causes Trio to automatically reduce the targeted glucose if it detects a decrease in sensitivity (resistance) from your baseline."
  367. )
  368. }
  369. case .halfBasalTarget:
  370. return VStack(alignment: .leading, spacing: 8) {
  371. Text(
  372. "Default: \(units == .mgdL ? "160" : 160.formattedAsMmolL) \(units.rawValue)"
  373. )
  374. .bold()
  375. Text(
  376. "The Half Basal Exercise Target allows you to scale down your basal insulin during exercise or scale up your basal insulin when eating soon when a temporary glucose target is set."
  377. )
  378. Text(
  379. "For example, at a temp target of \(units == .mgdL ? "160" : 160.formattedAsMmolL) \(units.rawValue), your basal is reduced to 50%, but this scales depending on the target (e.g., 75% at \(units == .mgdL ? "120" : 120.formattedAsMmolL) \(units.rawValue), 60% at \(units == .mgdL ? "140" : 140.formattedAsMmolL) \(units.rawValue))."
  380. )
  381. Text(
  382. "Note: This setting is only utilized if the settings \"Low Temp Target Lowers Sensitivity\" OR \"High Temp Target Raises Sensitivity\" are enabled."
  383. )
  384. }
  385. }
  386. }
  387. init?(rawValue: String) {
  388. self.init(rawValue: AlgorithmSettingsSubstep.allCases.first { rawValue == "\($0)" }?.rawValue ?? -1)
  389. }
  390. }
  391. // MARK: - Algorithm Settings Substep Groups
  392. enum AutosensSettingsSubstep: Int, CaseIterable, Identifiable {
  393. case autosensMin
  394. case autosensMax
  395. case rewindResetsAutosens
  396. var id: Int { rawValue }
  397. }
  398. enum SMBSettingsSubstep: Int, CaseIterable, Identifiable {
  399. case enableSMBAlways
  400. case enableSMBWithCOB
  401. case enableSMBWithTempTarget
  402. case enableSMBAfterCarbs
  403. case enableSMBWithHighGlucoseTarget
  404. case allowSMBWithHighTempTarget
  405. case enableUAM
  406. case maxSMBMinutes
  407. case maxUAMMinutes
  408. case maxDeltaGlucoseThreshold
  409. var id: Int { rawValue }
  410. }
  411. enum TargetBehaviorSubstep: Int, CaseIterable, Identifiable {
  412. case highTempTargetRaisesSensitivity
  413. case lowTempTargetLowersSensitivity
  414. case sensitivityRaisesTarget
  415. case resistanceLowersTarget
  416. case halfBasalTarget
  417. var id: Int { rawValue }
  418. }
  419. extension AutosensSettingsSubstep: AlgorithmSubstepProtocol {}
  420. extension SMBSettingsSubstep: AlgorithmSubstepProtocol {}
  421. extension TargetBehaviorSubstep: AlgorithmSubstepProtocol {}
  422. // MARK: - Shared Metadata Helper
  423. enum AlgorithmSettingMeta: String {
  424. case autosensMin
  425. case autosensMax
  426. case rewindResetsAutosens
  427. case enableSMBAlways
  428. case enableSMBWithCOB
  429. case enableSMBWithTempTarget
  430. case enableSMBAfterCarbs
  431. case enableSMBWithHighGlucoseTarget
  432. case allowSMBWithHighTempTarget
  433. case enableUAM
  434. case maxSMBMinutes
  435. case maxUAMMinutes
  436. case maxDeltaGlucoseThreshold
  437. case highTempTargetRaisesSensitivity
  438. case lowTempTargetLowersSensitivity
  439. case sensitivityRaisesTarget
  440. case resistanceLowersTarget
  441. case halfBasalTarget
  442. var title: String {
  443. AlgorithmSettingsSubstep(rawValue: rawValue)?.title ?? ""
  444. }
  445. func hint(units: GlucoseUnits) -> String {
  446. AlgorithmSettingsSubstep(rawValue: rawValue)?.hint(units: units) ?? ""
  447. }
  448. func description(units: GlucoseUnits) -> any View {
  449. AlgorithmSettingsSubstep(rawValue: rawValue)?.description(units: units) ?? AnyView(EmptyView())
  450. }
  451. }