AlgorithmAdvancedSettingsRootView.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import SwiftUI
  2. import Swinject
  3. extension AlgorithmAdvancedSettings {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @State var state = StateModel()
  7. @State private var shouldDisplayHint: Bool = false
  8. @State var hintDetent = PresentationDetent.large
  9. @State var selectedVerboseHint: AnyView?
  10. @State var hintLabel: String?
  11. @State private var decimalPlaceholder: Decimal = 0.0
  12. @State private var booleanPlaceholder: Bool = false
  13. @Environment(\.colorScheme) var colorScheme
  14. @EnvironmentObject var appIcons: Icons
  15. private var color: LinearGradient {
  16. colorScheme == .dark ? LinearGradient(
  17. gradient: Gradient(colors: [
  18. Color.bgDarkBlue,
  19. Color.bgDarkerDarkBlue
  20. ]),
  21. startPoint: .top,
  22. endPoint: .bottom
  23. )
  24. :
  25. LinearGradient(
  26. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  27. startPoint: .top,
  28. endPoint: .bottom
  29. )
  30. }
  31. var body: some View {
  32. List {
  33. Section(
  34. header: Text("DISCLAIMER"),
  35. content: {
  36. VStack(alignment: .leading) {
  37. Text(
  38. "The settings in this section typically do not require ANY modifications. Do not alter them without a solid understanding of what you are changing and the full impact it will have on the algorithm."
  39. ).bold()
  40. }
  41. }
  42. ).listRowBackground(Color.tabBar)
  43. SettingInputSection(
  44. decimalValue: $state.maxDailySafetyMultiplier,
  45. booleanValue: $booleanPlaceholder,
  46. shouldDisplayHint: $shouldDisplayHint,
  47. selectedVerboseHint: Binding(
  48. get: { selectedVerboseHint },
  49. set: {
  50. selectedVerboseHint = $0.map { AnyView($0) }
  51. hintLabel = NSLocalizedString("Max Daily Safety Multiplier", comment: "Max Daily Safety Multiplier")
  52. }
  53. ),
  54. units: state.units,
  55. type: .decimal("maxDailySafetyMultiplier"),
  56. label: NSLocalizedString("Max Daily Safety Multiplier", comment: "Max Daily Safety Multiplier"),
  57. miniHint: "Limits temp basals to this % of your largest basal rate \nDefault:300%",
  58. verboseHint: VStack(spacing: 10) {
  59. Text("Default: 300%").bold()
  60. VStack(alignment: .leading, spacing: 10) {
  61. Text(
  62. "This setting restricts the maximum temporary basal rate Trio can set. At the default of 300%, it caps it at 3 times your highest programmed basal rate."
  63. )
  64. Text("It serves as a safety limit, ensuring no temporary basal rates exceed safe levels.")
  65. Text("Warning: Increasing this setting is not advised.").bold()
  66. }
  67. }
  68. )
  69. SettingInputSection(
  70. decimalValue: $state.currentBasalSafetyMultiplier,
  71. booleanValue: $booleanPlaceholder,
  72. shouldDisplayHint: $shouldDisplayHint,
  73. selectedVerboseHint: Binding(
  74. get: { selectedVerboseHint },
  75. set: {
  76. selectedVerboseHint = $0.map { AnyView($0) }
  77. hintLabel = NSLocalizedString(
  78. "Current Basal Safety Multiplier",
  79. comment: "Current Basal Safety Multiplier"
  80. )
  81. }
  82. ),
  83. units: state.units,
  84. type: .decimal("currentBasalSafetyMultiplier"),
  85. label: NSLocalizedString("Current Basal Safety Multiplier", comment: "Current Basal Safety Multiplier"),
  86. miniHint: "Limits temp basals to this % of the current basal rate \nDefault: 400%",
  87. verboseHint: VStack(spacing: 10) {
  88. Text("Default: 400%").bold()
  89. VStack(alignment: .leading, spacing: 10) {
  90. Text(
  91. "This limits the automatic adjustment of the temporary basal rate to this percentage of the current hourly profile basal rate at the time of the loop cycle."
  92. )
  93. Text(
  94. "This prevents excessive dosing, especially during times of variable insulin sensitivity, enhancing safety."
  95. )
  96. Text("Warning: Increasing this setting is not advised.").bold()
  97. }
  98. }
  99. )
  100. SettingInputSection(
  101. decimalValue: $state.insulinActionCurve,
  102. booleanValue: $booleanPlaceholder,
  103. shouldDisplayHint: $shouldDisplayHint,
  104. selectedVerboseHint: Binding(
  105. get: { selectedVerboseHint },
  106. set: {
  107. selectedVerboseHint = $0.map { AnyView($0) }
  108. hintLabel = "Duration of Insulin Action"
  109. }
  110. ),
  111. units: state.units,
  112. type: .decimal("dia"),
  113. label: "Duration of Insulin Action",
  114. miniHint: "Number of hours insulin is active in your body \nDefault: 6 hours",
  115. verboseHint: VStack(spacing: 10) {
  116. Text("Default: 6 hours").bold()
  117. VStack(alignment: .leading, spacing: 10) {
  118. Text(
  119. "The Duration of Insulin Action (DIA) defines how long your insulin continues to lower blood glucose after a dose."
  120. )
  121. Text(
  122. "This helps the system accurately track Insulin on Board (IOB), avoiding over- or under-corrections by considering the tail end of insulin's effect"
  123. )
  124. Text(
  125. "Tip: It is better to use Custom Peak Time rather than adjust your Duration of Insulin Action (DIA)"
  126. )
  127. Text("Warning: Decreasing this setting is not advised.").bold()
  128. }
  129. }
  130. )
  131. SettingInputSection(
  132. decimalValue: $state.insulinPeakTime,
  133. booleanValue: $state.useCustomPeakTime,
  134. shouldDisplayHint: $shouldDisplayHint,
  135. selectedVerboseHint: Binding(
  136. get: { selectedVerboseHint },
  137. set: {
  138. selectedVerboseHint = $0.map { AnyView($0) }
  139. hintLabel = NSLocalizedString("Use Custom Peak Time", comment: "Use Custom Peak Time")
  140. }
  141. ),
  142. units: state.units,
  143. type: .conditionalDecimal("insulinPeakTime"),
  144. label: NSLocalizedString("Use Custom Peak Time", comment: "Use Custom Peak Time"),
  145. conditionalLabel: NSLocalizedString("Insulin Peak Time", comment: "Insulin Peak Time"),
  146. miniHint: "Sets time of insulin’s peak effect \nDefault: (Set by Insulin Type)",
  147. verboseHint: VStack(spacing: 10) {
  148. Text("Default: Set by Insulin Type").bold()
  149. VStack(alignment: .leading, spacing: 10) {
  150. Text(
  151. "Insulin Peak Time defines when insulin is most effective in lowering blood glucose, set in minutes after dosing."
  152. )
  153. Text(
  154. "This peak informs the system when to expect the most potent glucose-lowering effect, helping it predict glucose trends more accurately."
  155. )
  156. Text("System-Determined Defaults:").bold()
  157. Text("Ultra-Rapid: 55 minutes (permitted range 35-100 minutes)")
  158. Text("Rapid-Acting: 75 minutes (permitted range 50-120 minutes)")
  159. }
  160. }
  161. )
  162. SettingInputSection(
  163. decimalValue: $decimalPlaceholder,
  164. booleanValue: $state.skipNeutralTemps,
  165. shouldDisplayHint: $shouldDisplayHint,
  166. selectedVerboseHint: Binding(
  167. get: { selectedVerboseHint },
  168. set: {
  169. selectedVerboseHint = $0.map { AnyView($0) }
  170. hintLabel = NSLocalizedString("Skip Neutral Temps", comment: "Skip Neutral Temps")
  171. }
  172. ),
  173. units: state.units,
  174. type: .boolean,
  175. label: NSLocalizedString("Skip Neutral Temps", comment: "Skip Neutral Temps"),
  176. miniHint: "Skip neutral temp basals to reduce pump alerts \nDefault: OFF",
  177. verboseHint: VStack {
  178. Text("Default: OFF").bold()
  179. VStack(alignment: .leading, spacing: 10) {
  180. Text(
  181. "When Skip Neutral Temps is enabled, Trio will not set neutral basal rates, minimizing hourly pump alerts. This can help light sleepers avoid alerts but may delay basal adjustments if the pump loses connection."
  182. )
  183. Text("For most users, leaving this OFF is recommended to ensure consistent basal delivery.")
  184. }
  185. }
  186. )
  187. SettingInputSection(
  188. decimalValue: $decimalPlaceholder,
  189. booleanValue: $state.unsuspendIfNoTemp,
  190. shouldDisplayHint: $shouldDisplayHint,
  191. selectedVerboseHint: Binding(
  192. get: { selectedVerboseHint },
  193. set: {
  194. selectedVerboseHint = $0.map { AnyView($0) }
  195. hintLabel = NSLocalizedString("Unsuspend If No Temp", comment: "Unsuspend If No Temp")
  196. }
  197. ),
  198. units: state.units,
  199. type: .boolean,
  200. label: NSLocalizedString("Unsuspend If No Temp", comment: "Unsuspend If No Temp"),
  201. miniHint: "Automatically resumes pump after suspension \nDefault: OFF",
  202. verboseHint: VStack(spacing: 10) {
  203. Text("Default: OFF").bold()
  204. VStack(alignment: .leading, spacing: 10) {
  205. Text(
  206. "Enabling Unsuspend If No Temp allows Trio to resume your pump if you forget, as long as a zero temp basal was set first. This feature ensures insulin delivery restarts if you forget to manually unsuspend, adding a safeguard for pump reconnections."
  207. )
  208. Text("Note: Applies only to pumps with on-pump suspend options")
  209. }
  210. }
  211. )
  212. SettingInputSection(
  213. decimalValue: $decimalPlaceholder,
  214. booleanValue: $state.suspendZerosIOB,
  215. shouldDisplayHint: $shouldDisplayHint,
  216. selectedVerboseHint: Binding(
  217. get: { selectedVerboseHint },
  218. set: {
  219. selectedVerboseHint = $0.map { AnyView($0) }
  220. hintLabel = NSLocalizedString("Suspend Zeros IOB", comment: "Suspend Zeros IOB")
  221. }
  222. ),
  223. units: state.units,
  224. type: .boolean,
  225. label: NSLocalizedString("Suspend Zeros IOB", comment: "Suspend Zeros IOB"),
  226. miniHint: "Clears temp basals and resets IOB when suspended \nDefault: OFF",
  227. verboseHint: VStack(spacing: 10) {
  228. Text("Default: OFF").bold()
  229. VStack(alignment: .leading, spacing: 10) {
  230. Text(
  231. "When Suspend Zeros IOB is enabled, any active temp basals during a pump suspension are reset, with new zero temp basals added to counteract the basal rates during suspension."
  232. )
  233. Text(
  234. "This prevents lingering insulin effects when your pump is suspended, ensuring safer management of insulin on board."
  235. )
  236. Text("Note: Applies to only to pumps with on-pump suspend options")
  237. }
  238. }
  239. )
  240. SettingInputSection(
  241. decimalValue: $state.autotuneISFAdjustmentFraction,
  242. booleanValue: $booleanPlaceholder,
  243. shouldDisplayHint: $shouldDisplayHint,
  244. selectedVerboseHint: Binding(
  245. get: { selectedVerboseHint },
  246. set: {
  247. selectedVerboseHint = $0.map { AnyView($0) }
  248. hintLabel = NSLocalizedString(
  249. "Autotune ISF Adjustment Percent",
  250. comment: "Autotune ISF Adjustment Percent"
  251. )
  252. }
  253. ),
  254. units: state.units,
  255. type: .decimal("autotuneISFAdjustmentFraction"),
  256. label: NSLocalizedString("Autotune ISF Adjustment Percent", comment: "Autotune ISF Adjustment Percent"),
  257. miniHint: "Using Autotune is not advised \nDefault: 50%",
  258. verboseHint: Text(
  259. NSLocalizedString(
  260. "The default of 50% for this value keeps autotune ISF closer to pump ISF via a weighted average of fullNewISF and pumpISF. 100% allows full adjustment, 0% is no adjustment from pump ISF.",
  261. comment: "Autotune ISF Adjustment Percent"
  262. )
  263. )
  264. )
  265. SettingInputSection(
  266. decimalValue: $state.min5mCarbimpact,
  267. booleanValue: $booleanPlaceholder,
  268. shouldDisplayHint: $shouldDisplayHint,
  269. selectedVerboseHint: Binding(
  270. get: { selectedVerboseHint },
  271. set: {
  272. selectedVerboseHint = $0.map { AnyView($0) }
  273. hintLabel = NSLocalizedString("Min 5m Carb Impact", comment: "Min 5m Carb Impact")
  274. }
  275. ),
  276. units: state.units,
  277. type: .decimal("min5mCarbimpact"),
  278. label: NSLocalizedString("Min 5m Carb Impact", comment: "Min 5m Carb Impact"),
  279. miniHint: "Estimates carb absorption rate per 5 minutes \nDefault: 8 mg/dL/5min",
  280. verboseHint: VStack(spacing: 10) {
  281. Text("mg/dL Default: 8 mg/dL/5min").bold()
  282. Text("mmol/L Default: 0.4 mmol/L/5min").bold()
  283. VStack(alignment: .leading, spacing: 10) {
  284. Text(
  285. "Min 5m Carb Impact sets the expected glucose rise from carbs over 5 minutes when absorption isn't obvious from glucose data."
  286. )
  287. Text(
  288. "The default value of 8 mg/dL per 5 minutes corresponds to an absorption rate of 24g of carbs per hour."
  289. )
  290. Text(
  291. "This setting helps the system estimate how much glucose your body is absorbing, even when it's not immediately visible in your glucose data, ensuring more accurate insulin dosing during carb absorption."
  292. )
  293. }
  294. }
  295. )
  296. SettingInputSection(
  297. decimalValue: $state.remainingCarbsFraction,
  298. booleanValue: $booleanPlaceholder,
  299. shouldDisplayHint: $shouldDisplayHint,
  300. selectedVerboseHint: Binding(
  301. get: { selectedVerboseHint },
  302. set: {
  303. selectedVerboseHint = $0.map { AnyView($0) }
  304. hintLabel = NSLocalizedString("Remaining Carbs Percentage", comment: "Remaining Carbs Percentage")
  305. }
  306. ),
  307. units: state.units,
  308. type: .decimal("remainingCarbsFraction"),
  309. label: NSLocalizedString("Remaining Carbs Percentage", comment: "Remaining Carbs Percentage"),
  310. miniHint: "% of carbs still available if no absorption is detected \nDefault: 100%",
  311. verboseHint: VStack(spacing: 10) {
  312. Text("Default: 100%").bold()
  313. VStack(alignment: .leading, spacing: 10) {
  314. Text(
  315. "Remaining Carbs Percentage estimates carbs still absorbing over 4 hours if glucose data doesn't show clear absorption."
  316. )
  317. Text(
  318. "This fallback setting prevents under-dosing by spreading a portion of the entered carbs over time, balancing insulin needs with undetected carb impact."
  319. )
  320. }
  321. }
  322. )
  323. SettingInputSection(
  324. decimalValue: $state.remainingCarbsCap,
  325. booleanValue: $booleanPlaceholder,
  326. shouldDisplayHint: $shouldDisplayHint,
  327. selectedVerboseHint: Binding(
  328. get: { selectedVerboseHint },
  329. set: {
  330. selectedVerboseHint = $0.map { AnyView($0) }
  331. hintLabel = NSLocalizedString("Remaining Carbs Cap", comment: "Remaining Carbs Cap")
  332. }
  333. ),
  334. units: state.units,
  335. type: .decimal("remainingCarbsCap"),
  336. label: NSLocalizedString("Remaining Carbs Cap", comment: "Remaining Carbs Cap"),
  337. miniHint: "Maximum Carbs(g) still available if no absorption is detected \nDefault: 90g",
  338. verboseHint: VStack(spacing: 10) {
  339. Text("Default: 90g").bold()
  340. VStack(alignment: .leading, spacing: 10) {
  341. Text(
  342. "The Remaining Carbs Cap defines the upper limit for how many carbs the system will assume are absorbing over 4 hours, even when there's no clear sign of absorption from your glucose readings."
  343. )
  344. Text(
  345. "This cap prevents the system from overestimating how much insulin is needed when carb absorption isn't visible, offering a safeguard for accurate dosing."
  346. )
  347. }
  348. }
  349. )
  350. SettingInputSection(
  351. decimalValue: $state.noisyCGMTargetMultiplier,
  352. booleanValue: $booleanPlaceholder,
  353. shouldDisplayHint: $shouldDisplayHint,
  354. selectedVerboseHint: Binding(
  355. get: { selectedVerboseHint },
  356. set: {
  357. selectedVerboseHint = $0.map { AnyView($0) }
  358. hintLabel = NSLocalizedString("Noisy CGM Target Multiplier", comment: "Noisy CGM Target Multiplier")
  359. }
  360. ),
  361. units: state.units,
  362. type: .decimal("noisyCGMTargetMultiplier"),
  363. label: NSLocalizedString("Noisy CGM Target Increase", comment: "Noisy CGM Target Increase"),
  364. miniHint: "Increase glucose target when noisy CGM data detected \nDefault: 130%",
  365. verboseHint: VStack(spacing: 10) {
  366. Text("Default: 130%").bold()
  367. VStack(alignment: .leading, spacing: 10) {
  368. Text(
  369. "The Noisy CGM Target Multiplier increases your glucose target when the system detects noisy or raw CGM data. By default, the target is increased by 130% to account for the less reliable glucose readings."
  370. )
  371. Text(
  372. "This helps reduce the risk of incorrect insulin dosing based on inaccurate sensor data, ensuring safer insulin adjustments during periods of poor CGM accuracy."
  373. )
  374. }
  375. }
  376. )
  377. }
  378. .sheet(isPresented: $shouldDisplayHint) {
  379. SettingInputHintView(
  380. hintDetent: $hintDetent,
  381. shouldDisplayHint: $shouldDisplayHint,
  382. hintLabel: hintLabel ?? "",
  383. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  384. sheetTitle: "Help"
  385. )
  386. }
  387. .scrollContentBackground(.hidden).background(color)
  388. .onAppear(perform: configureView)
  389. .navigationTitle("Additionals")
  390. .navigationBarTitleDisplayMode(.automatic)
  391. .onDisappear {
  392. state.saveIfChanged()
  393. }
  394. }
  395. }
  396. }