PreferencesEditorStateModel.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 insulinReqFraction: Decimal = 0.7
  8. @Published var skipBolusScreenAfterCarbs = false
  9. @Published var sections: [FieldSection] = []
  10. override func subscribe() {
  11. preferences = provider.preferences
  12. unitsIndex = settingsManager.settings.units == .mgdL ? 0 : 1
  13. allowAnnouncements = settingsManager.settings.allowAnnouncements
  14. insulinReqFraction = settingsManager.settings.insulinReqFraction
  15. skipBolusScreenAfterCarbs = settingsManager.settings.skipBolusScreenAfterCarbs
  16. subscribeSetting(
  17. \.units,
  18. on: $unitsIndex
  19. .map { [weak self] index in
  20. self?.provider.migrateUnits()
  21. return index == 0 ? GlucoseUnits.mgdL : .mmolL
  22. }
  23. )
  24. subscribeSetting(\.allowAnnouncements, on: $allowAnnouncements)
  25. subscribeSetting(\.insulinReqFraction, on: $insulinReqFraction)
  26. subscribeSetting(\.skipBolusScreenAfterCarbs, on: $skipBolusScreenAfterCarbs)
  27. // MARK: - Main fields
  28. let mainFields = [
  29. Field(
  30. displayName: "Insulin curve",
  31. type: .insulinCurve(keypath: \.curve),
  32. infoText: "Insulin curve info",
  33. settable: self
  34. ),
  35. Field(
  36. displayName: "Max IOB",
  37. type: .decimal(keypath: \.maxIOB),
  38. infoText: NSLocalizedString(
  39. "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).",
  40. comment: "Max IOB"
  41. ),
  42. settable: self
  43. ),
  44. Field(
  45. displayName: "Max COB",
  46. type: .decimal(keypath: \.maxCOB),
  47. infoText: NSLocalizedString(
  48. "This defaults maxCOB to 120 because that’s the most a typical body can absorb over 4 hours. (If someone enters more carbs or stacks more; OpenAPS will just truncate dosing based on 120. Essentially, this just limits AMA as a safety cap against weird COB calculations due to fluky data.)",
  49. comment: "Max COB"
  50. ),
  51. settable: self
  52. ),
  53. Field(
  54. displayName: "Max Daily Safety Multiplier",
  55. type: .decimal(keypath: \.maxDailySafetyMultiplier),
  56. infoText: NSLocalizedString(
  57. "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.",
  58. comment: "Max Daily Safety Multiplier"
  59. ),
  60. settable: self
  61. ),
  62. Field(
  63. displayName: "Current Basal Safety Multiplier",
  64. type: .decimal(keypath: \.currentBasalSafetyMultiplier),
  65. infoText: NSLocalizedString(
  66. "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.",
  67. comment: "Current Basal Safety Multiplier"
  68. ),
  69. settable: self
  70. ),
  71. Field(
  72. displayName: "Autosens Max",
  73. type: .decimal(keypath: \.autosensMax),
  74. infoText: NSLocalizedString(
  75. "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.",
  76. comment: "Autosens Max"
  77. ),
  78. settable: self
  79. ),
  80. Field(
  81. displayName: "Autosens Min",
  82. type: .decimal(keypath: \.autosensMin),
  83. infoText: NSLocalizedString(
  84. "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.",
  85. comment: "Autosens Min"
  86. ),
  87. settable: self
  88. )
  89. ]
  90. // MARK: - SMB fields
  91. let smbFields = [
  92. Field(
  93. displayName: "Enable SMB Always",
  94. type: .boolean(keypath: \.enableSMBAlways),
  95. infoText: NSLocalizedString(
  96. "Defaults to false. When true, always enable supermicrobolus (unless disabled by high temptarget).",
  97. comment: "Enable SMB Always"
  98. ),
  99. settable: self
  100. ),
  101. Field(
  102. displayName: "Enable SMB With COB",
  103. type: .boolean(keypath: \.enableSMBWithCOB),
  104. infoText: NSLocalizedString(
  105. "This enables supermicrobolus (SMB) while carbs on board (COB) are positive.",
  106. comment: "Enable SMB With COB"
  107. ),
  108. settable: self
  109. ),
  110. Field(
  111. displayName: "Enable SMB With Temptarget",
  112. type: .boolean(keypath: \.enableSMBWithTemptarget),
  113. infoText: NSLocalizedString(
  114. "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.",
  115. comment: "Enable SMB With Temptarget"
  116. ),
  117. settable: self
  118. ),
  119. Field(
  120. displayName: "Enable SMB After Carbs",
  121. type: .boolean(keypath: \.enableSMBAfterCarbs),
  122. infoText: NSLocalizedString(
  123. "Defaults to false. When true, enables supermicrobolus (SMB) for 6h after carbs, even with 0 carbs on board (COB).",
  124. comment: "Enable SMB After Carbs"
  125. ),
  126. settable: self
  127. ),
  128. Field(
  129. displayName: "Allow SMB With High Temptarget",
  130. type: .boolean(keypath: \.allowSMBWithHighTemptarget),
  131. infoText: NSLocalizedString(
  132. "Defaults to false. When true, allows supermicrobolus (if otherwise enabled) even with high temp targets.",
  133. comment: "Allow SMB With High Temptarget"
  134. ),
  135. settable: self
  136. ),
  137. Field(
  138. displayName: "Enable UAM",
  139. type: .boolean(keypath: \.enableUAM),
  140. infoText: NSLocalizedString(
  141. "With this option enabled, the SMB algorithm can recognize unannounced meals. This is helpful, if you forget to tell FreeAPS X 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.",
  142. comment: "Enable UAM"
  143. ),
  144. settable: self
  145. ),
  146. Field(
  147. displayName: "Max SMB Basal Minutes",
  148. type: .decimal(keypath: \.maxSMBBasalMinutes),
  149. infoText: NSLocalizedString(
  150. "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.",
  151. comment: "Max SMB Basal Minutes"
  152. ),
  153. settable: self
  154. ),
  155. Field(
  156. displayName: "Max UAM SMB Basal Minutes",
  157. type: .decimal(keypath: \.maxUAMSMBBasalMinutes),
  158. infoText: NSLocalizedString(
  159. "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.",
  160. comment: "Max UAM SMB Basal Minutes"
  161. ),
  162. settable: self
  163. ),
  164. Field(
  165. displayName: "SMB Interval",
  166. type: .decimal(keypath: \.smbInterval),
  167. infoText: NSLocalizedString("Minimum duration in minutes between two enacted SMBs", comment: "SMB Interval"),
  168. settable: self
  169. ),
  170. Field(
  171. displayName: "Bolus Increment",
  172. type: .decimal(keypath: \.bolusIncrement),
  173. infoText: NSLocalizedString("Smallest possible bolus amount", comment: "Bolus Increment"),
  174. settable: self
  175. )
  176. ]
  177. // MARK: - Targets fields
  178. let targetSettings = [
  179. Field(
  180. displayName: "High Temptarget Raises Sensitivity",
  181. type: .boolean(keypath: \.highTemptargetRaisesSensitivity),
  182. infoText: NSLocalizedString(
  183. "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).",
  184. comment: "High Temptarget Raises Sensitivity"
  185. ),
  186. settable: self
  187. ),
  188. Field(
  189. displayName: "Low Temptarget Lowers Sensitivity",
  190. type: .boolean(keypath: \.lowTemptargetLowersSensitivity),
  191. infoText: NSLocalizedString(
  192. "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).",
  193. comment: "Low Temptarget Lowers Sensitivity"
  194. ),
  195. settable: self
  196. ),
  197. Field(
  198. displayName: "Sensitivity Raises Target",
  199. type: .boolean(keypath: \.sensitivityRaisesTarget),
  200. infoText: NSLocalizedString(
  201. "When true, raises BG target when autosens detects sensitivity",
  202. comment: "Sensitivity Raises Target"
  203. ),
  204. settable: self
  205. ),
  206. Field(
  207. displayName: "Resistance Lowers Target",
  208. type: .boolean(keypath: \.resistanceLowersTarget),
  209. infoText: NSLocalizedString(
  210. "Defaults to false. When true, will lower BG target when autosens detects resistance",
  211. comment: "Resistance Lowers Target"
  212. ),
  213. settable: self
  214. ),
  215. Field(
  216. displayName: "Advanced Target Adjustments",
  217. type: .boolean(keypath: \.advTargetAdjustments),
  218. infoText: NSLocalizedString(
  219. "This feature was previously enabled by default but will now default to false (will NOT be enabled automatically) in oref0 0.6.0 and beyond. (There is no need for this with 0.6.0). This feature lowers oref0’s target BG automatically when current BG and eventualBG are high. This helps prevent and mitigate high BG, but automatically switches to low-temping to ensure that BG comes down smoothly toward your actual target. If you find this behavior too aggressive, you can disable this feature. If you do so, please let us know so we can better understand what settings work best for everyone.",
  220. comment: "Advanced Target Adjustments"
  221. ),
  222. settable: self
  223. ),
  224. Field(
  225. displayName: "Exercise Mode",
  226. type: .boolean(keypath: \.exerciseMode),
  227. infoText: NSLocalizedString(
  228. "Defaults to false. When true, > 105 mg/dL high temp target adjusts sensitivityRatio for exercise_mode. Synonym for high_temptarget_raises_sensitivity",
  229. comment: "Exercise Mode"
  230. ),
  231. settable: self
  232. ),
  233. Field(
  234. displayName: "Half Basal Exercise Target",
  235. type: .decimal(keypath: \.halfBasalExerciseTarget),
  236. infoText: NSLocalizedString(
  237. "Set to a number, e.g. 160, which means when temp target is 160 mg/dL and exercise_mode=true, run 50% basal at this level (120 = 75%; 140 = 60%). This can be adjusted, to give you more control over your exercise modes.",
  238. comment: "Half Basal Exercise Target"
  239. ),
  240. settable: self
  241. ),
  242. Field(
  243. displayName: "Wide BG Target Range",
  244. type: .boolean(keypath: \.wideBGTargetRange),
  245. infoText: NSLocalizedString(
  246. "Defaults to false, which means by default only the low end of the pump’s BG target range is used as OpenAPS target. This is a safety feature to prevent too-wide targets and less-optimal outcomes. Therefore the higher end of the target range is used only for avoiding bolus wizard overcorrections. Use wide_bg_target_range: true to force neutral temps over a wider range of eventualBGs.",
  247. comment: "Wide BG Target Range"
  248. ),
  249. settable: self
  250. )
  251. ]
  252. // MARK: - Other fields
  253. let otherSettings = [
  254. Field(
  255. displayName: "Rewind Resets Autosens",
  256. type: .boolean(keypath: \.rewindResetsAutosens),
  257. infoText: NSLocalizedString(
  258. "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.",
  259. comment: "Rewind Resets Autosens"
  260. ),
  261. settable: self
  262. ),
  263. Field(
  264. displayName: "Use Custom Peak Time",
  265. type: .boolean(keypath: \.useCustomPeakTime),
  266. infoText: NSLocalizedString(
  267. "Defaults to false. Setting to true allows changing insulinPeakTime", comment: "Use Custom Peak Time"
  268. ),
  269. settable: self
  270. ),
  271. Field(
  272. displayName: "Insulin Peak Time",
  273. type: .decimal(keypath: \.insulinPeakTime),
  274. infoText: NSLocalizedString(
  275. "Time of maximum blood glucose lowering effect of insulin, in minutes. Beware: Oref assumes for ultra-papid (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 FreeAPS-X, longer loop calculations and possible red loops.",
  276. comment: "Insulin Peak Time"
  277. ),
  278. settable: self
  279. ),
  280. // Field(
  281. // displayName: "Carbs Req Threshold",
  282. // type: .decimal(keypath: \.carbsReqThreshold),
  283. // infoText: NSLocalizedString(
  284. // "Grams of carbsReq to trigger a pushover. Defaults to 1 (for 1 gram of carbohydrate). Can be increased if you only want to get Pushover for carbsReq at X threshold.",
  285. // comment: "Carbs Req Threshold"
  286. // ),
  287. // settable: self
  288. // ),
  289. Field(
  290. displayName: "Skip Neutral Temps",
  291. type: .boolean(keypath: \.skipNeutralTemps),
  292. infoText: NSLocalizedString(
  293. "Defaults to false, so that FreeAPS X 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 FreeAPS X 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.",
  294. comment: "Skip Neutral Temps"
  295. ),
  296. settable: self
  297. ),
  298. Field(
  299. displayName: "Unsuspend If No Temp",
  300. type: .boolean(keypath: \.unsuspendIfNoTemp),
  301. infoText: NSLocalizedString(
  302. "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.",
  303. comment: "Unsuspend If No Temp"
  304. ),
  305. settable: self
  306. ),
  307. Field(
  308. displayName: "Suspend Zeros IOB",
  309. type: .boolean(keypath: \.suspendZerosIOB),
  310. infoText: NSLocalizedString(
  311. "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.",
  312. comment: "Suspend Zeros IOB"
  313. ),
  314. settable: self
  315. ),
  316. Field(
  317. displayName: "Bolus Snooze DIA Divisor",
  318. type: .decimal(keypath: \.bolusSnoozeDIADivisor),
  319. infoText: NSLocalizedString(
  320. "Bolus snooze is enacted after you do a meal bolus, so the loop won’t counteract with low temps when you’ve just eaten. The example here and default is 2; so a 3 hour DIA means that bolus snooze will be gradually phased out over 1.5 hours (3DIA/2).",
  321. comment: "Bolus Snooze DIA Divisor"
  322. ),
  323. settable: self
  324. ),
  325. Field(
  326. displayName: "Min 5m Carbimpact",
  327. type: .decimal(keypath: \.min5mCarbimpact),
  328. infoText: NSLocalizedString(
  329. "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.",
  330. comment: "Min 5m Carbimpact"
  331. ),
  332. settable: self
  333. ),
  334. Field(
  335. displayName: "Autotune ISF Adjustment Fraction",
  336. type: .decimal(keypath: \.autotuneISFAdjustmentFraction),
  337. infoText: NSLocalizedString(
  338. "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.",
  339. comment: "Autotune ISF Adjustment Fraction"
  340. ),
  341. settable: self
  342. ),
  343. Field(
  344. displayName: "Remaining Carbs Fraction",
  345. type: .decimal(keypath: \.remainingCarbsFraction),
  346. infoText: NSLocalizedString(
  347. "This is the fraction of carbs we’ll assume will absorb over 4h if we don’t yet see carb absorption.",
  348. comment: "Remaining Carbs Fraction"
  349. ),
  350. settable: self
  351. ),
  352. Field(
  353. displayName: "Remaining Carbs Cap",
  354. type: .decimal(keypath: \.remainingCarbsCap),
  355. infoText: NSLocalizedString(
  356. "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.",
  357. comment: "Remaining Carbs Cap"
  358. ),
  359. settable: self
  360. ),
  361. Field(
  362. displayName: "Noisy CGM Target Multiplier",
  363. type: .decimal(keypath: \.noisyCGMTargetMultiplier),
  364. infoText: NSLocalizedString(
  365. "Defaults to 1.3. Increase target by this amount when looping off raw/noisy CGM data",
  366. comment: "Noisy CGM Target Multiplier"
  367. ),
  368. settable: self
  369. )
  370. ]
  371. sections = [
  372. FieldSection(
  373. displayName: NSLocalizedString("OpenAPS main settings", comment: "OpenAPS main settings"), fields: mainFields
  374. ),
  375. FieldSection(
  376. displayName: NSLocalizedString("OpenAPS SMB settings", comment: "OpenAPS SMB settings"), fields: smbFields
  377. ),
  378. FieldSection(
  379. displayName: NSLocalizedString("OpenAPS targets settings", comment: "OpenAPS targets settings"),
  380. fields: targetSettings
  381. ),
  382. FieldSection(
  383. displayName: NSLocalizedString("OpenAPS other settings", comment: "OpenAPS other settings"),
  384. fields: otherSettings
  385. )
  386. ]
  387. }
  388. func set<T>(_ keypath: WritableKeyPath<Preferences, T>, value: T) {
  389. preferences[keyPath: keypath] = value
  390. save()
  391. }
  392. func get<T>(_ keypath: WritableKeyPath<Preferences, T>) -> T {
  393. preferences[keyPath: keypath]
  394. }
  395. func save() {
  396. provider.savePreferences(preferences)
  397. }
  398. }
  399. }