PreferencesEditorStateModel.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. import Foundation
  2. import SwiftUI
  3. extension PreferencesEditor {
  4. final class StateModel: BaseStateModel<Provider>, PreferencesSettable { private(set) var preferences = Preferences()
  5. @Published var unitsIndex = 1
  6. @Published var allowAnnouncements = false
  7. @Published var skipBolusScreenAfterCarbs = false
  8. @Published var sections: [FieldSection] = []
  9. @Published var useAlternativeBolusCalc: Bool = false
  10. @Published var units: GlucoseUnits = .mmolL
  11. override func subscribe() {
  12. preferences = provider.preferences
  13. units = settingsManager.settings.units
  14. subscribeSetting(\.units, on: $unitsIndex.map { $0 == 0 ? GlucoseUnits.mgdL : .mmolL }) {
  15. unitsIndex = $0 == .mgdL ? 0 : 1
  16. } didSet: { [weak self] _ in
  17. self?.provider.migrateUnits()
  18. }
  19. let mainFields = [
  20. Field(
  21. displayName: NSLocalizedString("Max IOB", comment: "Max IOB"),
  22. type: .decimal(keypath: \.maxIOB),
  23. infoText: NSLocalizedString(
  24. "Max IOB is the maximum amount of insulin on board from all sources – both basal (or SMB correction) and bolus insulin – that your loop is allowed to accumulate to treat higher-than-target BG. Unlike the other two OpenAPS safety settings (max_daily_safety_multiplier and current_basal_safety_multiplier), max_iob is set as a fixed number of units of insulin. As of now manual boluses are NOT limited by this setting. \n\n To test your basal rates during nighttime, you can modify the Max IOB setting to zero while in Closed Loop. This will enable low glucose suspend mode while testing your basal rates settings\n\n(Tip from https://www.loopandlearn.org/freeaps-x/#open-loop).",
  25. comment: "Max IOB"
  26. ),
  27. settable: self
  28. ),
  29. Field(
  30. displayName: NSLocalizedString("Max COB", comment: "Max COB"),
  31. type: .decimal(keypath: \.maxCOB),
  32. infoText: NSLocalizedString(
  33. "The default of maxCOB is 120. (If someone enters more carbs in one or multiple entries, iAPS will cap COB to maxCOB and keep it at maxCOB until the carbs entered above maxCOB have shown to be absorbed. Essentially, this just limits UAM as a safety cap against weird COB calculations due to fluky data.)",
  34. comment: "Max COB"
  35. ),
  36. settable: self
  37. ),
  38. Field(
  39. displayName: NSLocalizedString("Max Daily Safety Multiplier", comment: "Max Daily Safety Multiplier"),
  40. type: .decimal(keypath: \.maxDailySafetyMultiplier),
  41. infoText: NSLocalizedString(
  42. "This is an important OpenAPS safety limit. The default setting (which is unlikely to need adjusting) is 3. This means that OpenAPS will never be allowed to set a temporary basal rate that is more than 3x the highest hourly basal rate programmed in a user’s pump, or, if enabled, determined by autotune.",
  43. comment: "Max Daily Safety Multiplier"
  44. ),
  45. settable: self
  46. ),
  47. Field(
  48. displayName: NSLocalizedString("Current Basal Safety Multiplier", comment: "Current Basal Safety Multiplier"),
  49. type: .decimal(keypath: \.currentBasalSafetyMultiplier),
  50. infoText: NSLocalizedString(
  51. "This is another important OpenAPS safety limit. The default setting (which is also unlikely to need adjusting) is 4. This means that OpenAPS will never be allowed to set a temporary basal rate that is more than 4x the current hourly basal rate programmed in a user’s pump, or, if enabled, determined by autotune.",
  52. comment: "Current Basal Safety Multiplier"
  53. ),
  54. settable: self
  55. ),
  56. Field(
  57. displayName: NSLocalizedString("Autosens Max", comment: "Autosens Max"),
  58. type: .decimal(keypath: \.autosensMax),
  59. infoText: NSLocalizedString(
  60. "This is a multiplier cap for autosens (and autotune) to set a 20% max limit on how high the autosens ratio can be, which in turn determines how high autosens can adjust basals, how low it can adjust ISF, and how low it can set the BG target.",
  61. comment: "Autosens Max"
  62. ),
  63. settable: self
  64. ),
  65. Field(
  66. displayName: NSLocalizedString("Autosens Min", comment: "Autosens Min"),
  67. type: .decimal(keypath: \.autosensMin),
  68. infoText: NSLocalizedString(
  69. "The other side of the autosens safety limits, putting a cap on how low autosens can adjust basals, and how high it can adjust ISF and BG targets.",
  70. comment: "Autosens Min"
  71. ),
  72. settable: self
  73. )
  74. ]
  75. let smbFields = [
  76. Field(
  77. displayName: NSLocalizedString("Enable SMB Always", comment: "Enable SMB Always"),
  78. type: .boolean(keypath: \.enableSMBAlways),
  79. infoText: NSLocalizedString(
  80. "Defaults to false. When true, always enable supermicrobolus (unless disabled by high temptarget).",
  81. comment: "Enable SMB Always"
  82. ),
  83. settable: self
  84. ),
  85. Field(
  86. displayName: NSLocalizedString("Max Delta-BG Threshold SMB", comment: "Max Delta-BG Threshold"),
  87. type: .decimal(keypath: \.maxDeltaBGthreshold),
  88. infoText: NSLocalizedString(
  89. "Defaults to 0.2 (20%). Maximum positive percentual change of BG level to use SMB, above that will disable SMB. Hardcoded cap of 40%. For UAM fully-closed-loop 30% is advisable. Observe in log and popup (maxDelta 27 > 20% of BG 100 - disabling SMB!).",
  90. comment: "Max Delta-BG Threshold"
  91. ),
  92. settable: self
  93. ),
  94. Field(
  95. displayName: NSLocalizedString("Enable SMB With COB", comment: "Enable SMB With COB"),
  96. type: .boolean(keypath: \.enableSMBWithCOB),
  97. infoText: NSLocalizedString(
  98. "This enables supermicrobolus (SMB) while carbs on board (COB) are positive.",
  99. comment: "Enable SMB With COB"
  100. ),
  101. settable: self
  102. ),
  103. Field(
  104. displayName: NSLocalizedString("Enable SMB With Temptarget", comment: "Enable SMB With Temptarget"),
  105. type: .boolean(keypath: \.enableSMBWithTemptarget),
  106. infoText: NSLocalizedString(
  107. "This enables supermicrobolus (SMB) with eating soon / low temp targets. With this feature enabled, any temporary target below 100mg/dL, such as a temp target of 99 (or 80, the typical eating soon target) will enable SMB.",
  108. comment: "Enable SMB With Temptarget"
  109. ),
  110. settable: self
  111. ),
  112. Field(
  113. displayName: NSLocalizedString("Enable SMB After Carbs", comment: "Enable SMB After Carbs"),
  114. type: .boolean(keypath: \.enableSMBAfterCarbs),
  115. infoText: NSLocalizedString(
  116. "Defaults to false. When true, enables supermicrobolus (SMB) for 6h after carbs, even with 0 carbs on board (COB).",
  117. comment: "Enable SMB After Carbs"
  118. ),
  119. settable: self
  120. ),
  121. Field(
  122. displayName: NSLocalizedString(
  123. "Allow SMB With High Temptarget",
  124. comment: "Allow SMB With High Temptarget"
  125. ),
  126. type: .boolean(keypath: \.allowSMBWithHighTemptarget),
  127. infoText: NSLocalizedString(
  128. "Defaults to false. When true, allows supermicrobolus (if otherwise enabled) even with high temp targets (> 100 mg/dl).",
  129. comment: "Allow SMB With High Temptarget"
  130. ),
  131. settable: self
  132. ),
  133. Field(
  134. displayName: NSLocalizedString("Enable SMB With High BG", comment: "Enable SMB With High BG"),
  135. type: .boolean(keypath: \.enableSMB_high_bg),
  136. infoText: NSLocalizedString(
  137. "Enable SMBs when a high BG is detected, based on the high BG target (adjusted or profile)",
  138. comment: "Enable SMB With High BG"
  139. ),
  140. settable: self
  141. ),
  142. Field(
  143. displayName: NSLocalizedString(
  144. "... When Blood Glucose Is Over (mg/dl):",
  145. comment: "... When Blood Glucose Is Over (mg/dl):"
  146. ),
  147. type: .decimal(keypath: \.enableSMB_high_bg_target),
  148. infoText: NSLocalizedString(
  149. "Set the value enableSMB_high_bg will compare against to enable SMB. If BG > than this value, SMBs should enable.",
  150. comment: "... When Blood Glucose Is Over (mg/dl):"
  151. ),
  152. settable: self
  153. ),
  154. Field(
  155. displayName: NSLocalizedString("Enable UAM", comment: "Enable UAM"),
  156. type: .boolean(keypath: \.enableUAM),
  157. infoText: NSLocalizedString(
  158. "With this option enabled, the SMB algorithm can recognize unannounced meals. This is helpful, if you forget to tell iAPS about your carbs or estimate your carbs wrong and the amount of entered carbs is wrong or if a meal with lots of fat and protein has a longer duration than expected. Without any carb entry, UAM can recognize fast glucose increasments caused by carbs, adrenaline, etc, and tries to adjust it with SMBs. This also works the opposite way: if there is a fast glucose decreasement, it can stop SMBs earlier.",
  159. comment: "Enable UAM"
  160. ),
  161. settable: self
  162. ),
  163. Field(
  164. displayName: NSLocalizedString("Max SMB Basal Minutes", comment: "Max SMB Basal Minutes"),
  165. type: .decimal(keypath: \.maxSMBBasalMinutes),
  166. infoText: NSLocalizedString(
  167. "Defaults to start at 30. This is the maximum minutes of basal that can be delivered as a single SMB with uncovered COB. This gives the ability to make SMB more aggressive if you choose. It is recommended that the value is set to start at 30, in line with the default, and if you choose to increase this value, do so in no more than 15 minute increments, keeping a close eye on the effects of the changes. It is not recommended to set this value higher than 90 mins, as this may affect the ability for the algorithm to safely zero temp. It is also recommended that pushover is used when setting the value to be greater than default, so that alerts are generated for any predicted lows or highs.",
  168. comment: "Max SMB Basal Minutes"
  169. ),
  170. settable: self
  171. ),
  172. Field(
  173. displayName: NSLocalizedString("Max UAM SMB Basal Minutes", comment: "Max UAM SMB Basal Minutes"),
  174. type: .decimal(keypath: \.maxUAMSMBBasalMinutes),
  175. infoText: NSLocalizedString(
  176. "Defaults to start at 30. This is the maximum minutes of basal that can be delivered by UAM as a single SMB when IOB exceeds COB. This gives the ability to make UAM more or less aggressive if you choose. It is recommended that the value is set to start at 30, in line with the default, and if you choose to increase this value, do so in no more than 15 minute increments, keeping a close eye on the effects of the changes. Reducing the value will cause UAM to dose less insulin for each SMB. It is not recommended to set this value higher than 60 mins, as this may affect the ability for the algorithm to safely zero temp. It is also recommended that pushover is used when setting the value to be greater than default, so that alerts are generated for any predicted lows or highs.",
  177. comment: "Max UAM SMB Basal Minutes"
  178. ),
  179. settable: self
  180. ),
  181. Field(
  182. displayName: NSLocalizedString("SMB DeliveryRatio", comment: "SMB DeliveryRatio"),
  183. type: .decimal(keypath: \.smbDeliveryRatio),
  184. infoText: NSLocalizedString(
  185. "Default value: 0.5 This is another key OpenAPS safety cap, and specifies what share of the total insulin required can be delivered as SMB. Increase this experimental value slowly and with caution.",
  186. comment: "SMB DeliveryRatio"
  187. ),
  188. settable: self
  189. ),
  190. Field(
  191. displayName: NSLocalizedString("SMB Interval", comment: "SMB Interval"),
  192. type: .decimal(keypath: \.smbInterval),
  193. infoText: NSLocalizedString(
  194. "Minimum duration in minutes for new SMB since last SMB or manual bolus",
  195. comment: "SMB Interval"
  196. ),
  197. settable: self
  198. ),
  199. Field(
  200. displayName: NSLocalizedString("Bolus Increment", comment: "Bolus Increment"),
  201. type: .decimal(keypath: \.bolusIncrement),
  202. infoText: NSLocalizedString(
  203. "Smallest enacted SMB amount. Minimum amount for Omnipod pumps is 0.05 U, whereas for Medtronic pumps it differs for various models, from 0.025 U to 0.10 U. Please check the minimum bolus amount which can be delivered by your pump. The default value is 0.1.",
  204. comment: "Bolus Increment"
  205. ),
  206. settable: self
  207. )
  208. ]
  209. // MARK: - Targets fields
  210. let targetSettings = [
  211. Field(
  212. displayName: NSLocalizedString(
  213. "High Temptarget Raises Sensitivity",
  214. comment: "High Temptarget Raises Sensitivity"
  215. ),
  216. type: .boolean(keypath: \.highTemptargetRaisesSensitivity),
  217. infoText: NSLocalizedString(
  218. "Defaults to false. When set to true, raises sensitivity (lower sensitivity ratio) for temp targets set to >= 111. Synonym for exercise_mode. The higher your temp target above 110 will result in more sensitive (lower) ratios, e.g., temp target of 120 results in sensitivity ratio of 0.75, while 140 results in 0.6 (with default halfBasalTarget of 160).",
  219. comment: "High Temptarget Raises Sensitivity"
  220. ),
  221. settable: self
  222. ),
  223. Field(
  224. displayName: NSLocalizedString(
  225. "Low Temptarget Lowers Sensitivity",
  226. comment: "Low Temptarget Lowers Sensitivity"
  227. ),
  228. type: .boolean(keypath: \.lowTemptargetLowersSensitivity),
  229. infoText: NSLocalizedString(
  230. "Defaults to false. When set to true, can lower sensitivity (higher sensitivity ratio) for temptargets <= 99. The lower your temp target below 100 will result in less sensitive (higher) ratios, e.g., temp target of 95 results in sensitivity ratio of 1.09, while 85 results in 1.33 (with default halfBasalTarget of 160).",
  231. comment: "Low Temptarget Lowers Sensitivity"
  232. ),
  233. settable: self
  234. ),
  235. Field(
  236. displayName: NSLocalizedString("Sensitivity Raises Target", comment: "Sensitivity Raises Target"),
  237. type: .boolean(keypath: \.sensitivityRaisesTarget),
  238. infoText: NSLocalizedString(
  239. "When true, raises BG target when autosens detects sensitivity",
  240. comment: "Sensitivity Raises Target"
  241. ),
  242. settable: self
  243. ),
  244. Field(
  245. displayName: NSLocalizedString("Resistance Lowers Target", comment: "Resistance Lowers Target"),
  246. type: .boolean(keypath: \.resistanceLowersTarget),
  247. infoText: NSLocalizedString(
  248. "Defaults to false. When true, will lower BG target when autosens detects resistance",
  249. comment: "Resistance Lowers Target"
  250. ),
  251. settable: self
  252. ),
  253. Field(
  254. displayName: NSLocalizedString("Half Basal Exercise Target", comment: "Half Basal Exercise Target"),
  255. type: .decimal(keypath: \.halfBasalExerciseTarget),
  256. infoText: NSLocalizedString(
  257. "Set to a number, e.g. 160, which means when temp target is 160 mg/dL, run 50% basal at this level (120 = 75%; 140 = 60%). This can be adjusted, to give you more control over your exercise modes.",
  258. comment: "Half Basal Exercise Target"
  259. ),
  260. settable: self
  261. )
  262. ]
  263. // MARK: - Other fields
  264. let otherSettings = [
  265. Field(
  266. displayName: NSLocalizedString("Rewind Resets Autosens", comment: "Rewind Resets Autosens"),
  267. type: .boolean(keypath: \.rewindResetsAutosens),
  268. infoText: NSLocalizedString(
  269. "This feature, enabled by default, resets the autosens ratio to neutral when you rewind your pump, on the assumption that this corresponds to a probable site change. Autosens will begin learning sensitivity anew from the time of the rewind, which may take up to 6 hours. If you usually rewind your pump independently of site changes, you may want to consider disabling this feature.",
  270. comment: "Rewind Resets Autosens"
  271. ),
  272. settable: self
  273. ),
  274. Field(
  275. displayName: NSLocalizedString("Use Custom Peak Time", comment: "Use Custom Peak Time"),
  276. type: .boolean(keypath: \.useCustomPeakTime),
  277. infoText: NSLocalizedString(
  278. "Defaults to false. Setting to true allows changing insulinPeakTime", comment: "Use Custom Peak Time"
  279. ),
  280. settable: self
  281. ),
  282. Field(
  283. displayName: NSLocalizedString("Insulin Peak Time", comment: "Insulin Peak Time"),
  284. type: .decimal(keypath: \.insulinPeakTime),
  285. infoText: NSLocalizedString(
  286. "Time of maximum blood glucose lowering effect of insulin, in minutes. Beware: Oref assumes for ultra-rapid (Lyumjev) & rapid-acting (Fiasp) curves minimal (35 & 50 min) and maximal (100 & 120 min) applicable insulinPeakTimes. Using a custom insulinPeakTime outside these bounds will result in issues with iAPS, longer loop calculations and possible red loops.",
  287. comment: "Insulin Peak Time"
  288. ),
  289. settable: self
  290. ),
  291. Field(
  292. displayName: NSLocalizedString("Skip Neutral Temps", comment: "Skip Neutral Temps"),
  293. type: .boolean(keypath: \.skipNeutralTemps),
  294. infoText: NSLocalizedString(
  295. "Defaults to false, so that iAPS will set temps whenever it can, so it will be easier to see if the system is working, even when you are offline. This means iAPS will set a “neutral” temp (same as your default basal) if no adjustments are needed. This is an old setting for OpenAPS to have the options to minimise sounds and notifications from the 'rig', that may wake you up during the night.",
  296. comment: "Skip Neutral Temps"
  297. ),
  298. settable: self
  299. ),
  300. Field(
  301. displayName: NSLocalizedString("Unsuspend If No Temp", comment: "Unsuspend If No Temp"),
  302. type: .boolean(keypath: \.unsuspendIfNoTemp),
  303. infoText: NSLocalizedString(
  304. "Many people occasionally forget to resume / unsuspend their pump after reconnecting it. If you’re one of them, and you are willing to reliably set a zero temp basal whenever suspending and disconnecting your pump, this feature has your back. If enabled, it will automatically resume / unsuspend the pump if you forget to do so before your zero temp expires. As long as the zero temp is still running, it will leave the pump suspended.",
  305. comment: "Unsuspend If No Temp"
  306. ),
  307. settable: self
  308. ),
  309. Field(
  310. displayName: NSLocalizedString("Suspend Zeros IOB", comment: "Suspend Zeros IOB"),
  311. type: .boolean(keypath: \.suspendZerosIOB),
  312. infoText: NSLocalizedString(
  313. "Default is false. Any existing temp basals during times the pump was suspended will be deleted and 0 temp basals to negate the profile basal rates during times pump is suspended will be added.",
  314. comment: "Suspend Zeros IOB"
  315. ),
  316. settable: self
  317. ),
  318. Field(
  319. displayName: NSLocalizedString("Min 5m Carbimpact", comment: "Min 5m Carbimpact"),
  320. type: .decimal(keypath: \.min5mCarbimpact),
  321. infoText: NSLocalizedString(
  322. "This is a setting for default carb absorption impact per 5 minutes. The default is an expected 8 mg/dL/5min. This affects how fast COB is decayed in situations when carb absorption is not visible in BG deviations. The default of 8 mg/dL/5min corresponds to a minimum carb absorption rate of 24g/hr at a CSF of 4 mg/dL/g.",
  323. comment: "Min 5m Carbimpact"
  324. ),
  325. settable: self
  326. ),
  327. Field(
  328. displayName: NSLocalizedString(
  329. "Autotune ISF Adjustment Fraction",
  330. comment: "Autotune ISF Adjustment Fraction"
  331. ),
  332. type: .decimal(keypath: \.autotuneISFAdjustmentFraction),
  333. infoText: NSLocalizedString(
  334. "The default of 0.5 for this value keeps autotune ISF closer to pump ISF via a weighted average of fullNewISF and pumpISF. 1.0 allows full adjustment, 0 is no adjustment from pump ISF.",
  335. comment: "Autotune ISF Adjustment Fraction"
  336. ),
  337. settable: self
  338. ),
  339. Field(
  340. displayName: NSLocalizedString("Remaining Carbs Fraction", comment: "Remaining Carbs Fraction"),
  341. type: .decimal(keypath: \.remainingCarbsFraction),
  342. infoText: NSLocalizedString(
  343. "This is the fraction of carbs we’ll assume will absorb over 4h if we don’t yet see carb absorption.",
  344. comment: "Remaining Carbs Fraction"
  345. ),
  346. settable: self
  347. ),
  348. Field(
  349. displayName: NSLocalizedString("Remaining Carbs Cap", comment: "Remaining Carbs Cap"),
  350. type: .decimal(keypath: \.remainingCarbsCap),
  351. infoText: NSLocalizedString(
  352. "This is the amount of the maximum number of carbs we’ll assume will absorb over 4h if we don’t yet see carb absorption.",
  353. comment: "Remaining Carbs Cap"
  354. ),
  355. settable: self
  356. ),
  357. Field(
  358. displayName: NSLocalizedString("Noisy CGM Target Multiplier", comment: "Noisy CGM Target Multiplier"),
  359. type: .decimal(keypath: \.noisyCGMTargetMultiplier),
  360. infoText: NSLocalizedString(
  361. "Defaults to 1.3. Increase target by this amount when looping off raw/noisy CGM data",
  362. comment: "Noisy CGM Target Multiplier"
  363. ),
  364. settable: self
  365. )
  366. ]
  367. sections = [
  368. FieldSection(
  369. displayName: NSLocalizedString("OpenAPS main settings", comment: "OpenAPS main settings"), fields: mainFields
  370. ),
  371. FieldSection(
  372. displayName: NSLocalizedString("OpenAPS SMB settings", comment: "OpenAPS SMB settings"),
  373. fields: smbFields
  374. ),
  375. FieldSection(
  376. displayName: NSLocalizedString("OpenAPS targets settings", comment: "OpenAPS targets settings"),
  377. fields: targetSettings
  378. ),
  379. FieldSection(
  380. displayName: NSLocalizedString("OpenAPS other settings", comment: "OpenAPS other settings"),
  381. fields: otherSettings
  382. )
  383. ]
  384. }
  385. func set<T>(_ keypath: WritableKeyPath<Preferences, T>, value: T) {
  386. preferences[keyPath: keypath] = value
  387. save()
  388. }
  389. func get<T>(_ keypath: WritableKeyPath<Preferences, T>) -> T {
  390. preferences[keyPath: keypath]
  391. }
  392. func save() {
  393. provider.savePreferences(preferences)
  394. }
  395. }
  396. }