PreferencesEditorStateModel.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. import Foundation
  2. import SwiftUI
  3. extension PreferencesEditor {
  4. final class StateModel: BaseStateModel<Provider>, PreferencesSettable {
  5. @Injected() var settingsManager: SettingsManager!
  6. private(set) var preferences = Preferences()
  7. @Published var unitsIndex = 1
  8. @Published var allowAnnouncements = false
  9. @Published var insulinReqFraction: Decimal = 0.7
  10. @Published var skipBolusScreenAfterCarbs = false
  11. @Published var sections: [FieldSection] = []
  12. override func subscribe() {
  13. preferences = provider.preferences
  14. unitsIndex = settingsManager.settings.units == .mgdL ? 0 : 1
  15. allowAnnouncements = settingsManager.settings.allowAnnouncements
  16. insulinReqFraction = settingsManager.settings.insulinReqFraction ?? 0.7
  17. skipBolusScreenAfterCarbs = settingsManager.settings.skipBolusScreenAfterCarbs ?? false
  18. $unitsIndex
  19. .removeDuplicates()
  20. .sink { [weak self] index in
  21. self?.settingsManager.settings.units = index == 0 ? .mgdL : .mmolL
  22. self?.provider.migrateUnits()
  23. }
  24. .store(in: &lifetime)
  25. $allowAnnouncements
  26. .removeDuplicates()
  27. .sink { [weak self] allow in
  28. self?.settingsManager.settings.allowAnnouncements = allow
  29. }
  30. .store(in: &lifetime)
  31. $insulinReqFraction
  32. .removeDuplicates()
  33. .sink { [weak self] fraction in
  34. self?.settingsManager.settings.insulinReqFraction = fraction
  35. }
  36. .store(in: &lifetime)
  37. $skipBolusScreenAfterCarbs
  38. .removeDuplicates()
  39. .sink { [weak self] skip in
  40. self?.settingsManager.settings.skipBolusScreenAfterCarbs = skip
  41. }
  42. .store(in: &lifetime)
  43. // MARK: - Main fields
  44. let mainFields = [
  45. Field(
  46. displayName: "Insulin curve",
  47. type: .insulinCurve(keypath: \.curve),
  48. infoText: "Insulin curve info",
  49. settable: self
  50. ),
  51. Field(
  52. displayName: "Max IOB",
  53. type: .decimal(keypath: \.maxIOB),
  54. infoText: NSLocalizedString(
  55. "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).",
  56. comment: "Max IOB"
  57. ),
  58. settable: self
  59. ),
  60. Field(
  61. displayName: "Max COB",
  62. type: .decimal(keypath: \.maxCOB),
  63. infoText: NSLocalizedString(
  64. "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.)",
  65. comment: "Max COB"
  66. ),
  67. settable: self
  68. ),
  69. Field(
  70. displayName: "Max Daily Safety Multiplier",
  71. type: .decimal(keypath: \.maxDailySafetyMultiplier),
  72. infoText: NSLocalizedString(
  73. "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.",
  74. comment: "Max Daily Safety Multiplier"
  75. ),
  76. settable: self
  77. ),
  78. Field(
  79. displayName: "Current Basal Safety Multiplier",
  80. type: .decimal(keypath: \.currentBasalSafetyMultiplier),
  81. infoText: NSLocalizedString(
  82. "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.",
  83. comment: "Current Basal Safety Multiplier"
  84. ),
  85. settable: self
  86. ),
  87. Field(
  88. displayName: "Autosens Max",
  89. type: .decimal(keypath: \.autosensMax),
  90. infoText: NSLocalizedString(
  91. "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.",
  92. comment: "Autosens Max"
  93. ),
  94. settable: self
  95. ),
  96. Field(
  97. displayName: "Autosens Min",
  98. type: .decimal(keypath: \.autosensMin),
  99. infoText: NSLocalizedString(
  100. "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.",
  101. comment: "Autosens Min"
  102. ),
  103. settable: self
  104. )
  105. ]
  106. // MARK: - SMB fields
  107. let smbFields = [
  108. Field(
  109. displayName: "Enable SMB Always",
  110. type: .boolean(keypath: \.enableSMBAlways),
  111. infoText: NSLocalizedString(
  112. "Defaults to false. When true, always enable supermicrobolus (unless disabled by high temptarget).",
  113. comment: "Enable SMB Always"
  114. ),
  115. settable: self
  116. ),
  117. Field(
  118. displayName: "Enable SMB With COB",
  119. type: .boolean(keypath: \.enableSMBWithCOB),
  120. infoText: NSLocalizedString(
  121. "This enables supermicrobolus (SMB) while carbs on board (COB) are positive.",
  122. comment: "Enable SMB With COB"
  123. ),
  124. settable: self
  125. ),
  126. Field(
  127. displayName: "Enable SMB With Temptarget",
  128. type: .boolean(keypath: \.enableSMBWithTemptarget),
  129. infoText: NSLocalizedString(
  130. "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.",
  131. comment: "Enable SMB With Temptarget"
  132. ),
  133. settable: self
  134. ),
  135. Field(
  136. displayName: "Enable SMB After Carbs",
  137. type: .boolean(keypath: \.enableSMBAfterCarbs),
  138. infoText: NSLocalizedString(
  139. "Defaults to false. When true, enables supermicrobolus (SMB) for 6h after carbs, even with 0 carbs on board (COB).",
  140. comment: "Enable SMB After Carbs"
  141. ),
  142. settable: self
  143. ),
  144. Field(
  145. displayName: "Allow SMB With High Temptarget",
  146. type: .boolean(keypath: \.allowSMBWithHighTemptarget),
  147. infoText: NSLocalizedString(
  148. "Defaults to false. When true, allows supermicrobolus (if otherwise enabled) even with high temp targets.",
  149. comment: "Allow SMB With High Temptarget"
  150. ),
  151. settable: self
  152. ),
  153. Field(
  154. displayName: "Enable UAM",
  155. type: .boolean(keypath: \.enableUAM),
  156. infoText: NSLocalizedString(
  157. "With this option enabled, the SMB algorithm can recognize unannounced meals. This is helpful, if you forget to tell AndroidAPS 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.",
  158. comment: "Enable UAM"
  159. ),
  160. settable: self
  161. ),
  162. Field(
  163. displayName: "Max SMB Basal Minutes",
  164. type: .decimal(keypath: \.maxSMBBasalMinutes),
  165. infoText: NSLocalizedString(
  166. "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.",
  167. comment: "Max SMB Basal Minutes"
  168. ),
  169. settable: self
  170. ),
  171. Field(
  172. displayName: "Max UAM SMB Basal Minutes",
  173. type: .decimal(keypath: \.maxUAMSMBBasalMinutes),
  174. infoText: NSLocalizedString(
  175. "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.",
  176. comment: "Max UAM SMB Basal Minutes"
  177. ),
  178. settable: self
  179. ),
  180. Field(
  181. displayName: "SMB Interval",
  182. type: .decimal(keypath: \.smbInterval),
  183. infoText: NSLocalizedString("Minimum duration in minutes between two enacted SMBs", comment: "SMB Interval"),
  184. settable: self
  185. ),
  186. Field(
  187. displayName: "Bolus Increment",
  188. type: .decimal(keypath: \.bolusIncrement),
  189. infoText: NSLocalizedString("Smallest possible bolus amount", comment: "Bolus Increment"),
  190. settable: self
  191. )
  192. ]
  193. // MARK: - Targets fields
  194. let targetSettings = [
  195. Field(
  196. displayName: "High Temptarget Raises Sensitivity",
  197. type: .boolean(keypath: \.highTemptargetRaisesSensitivity),
  198. infoText: NSLocalizedString(
  199. "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).",
  200. comment: "High Temptarget Raises Sensitivity"
  201. ),
  202. settable: self
  203. ),
  204. Field(
  205. displayName: "Low Temptarget Lowers Sensitivity",
  206. type: .boolean(keypath: \.lowTemptargetLowersSensitivity),
  207. infoText: NSLocalizedString(
  208. "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).",
  209. comment: "Low Temptarget Lowers Sensitivity"
  210. ),
  211. settable: self
  212. ),
  213. Field(
  214. displayName: "Sensitivity Raises Target",
  215. type: .boolean(keypath: \.sensitivityRaisesTarget),
  216. infoText: NSLocalizedString(
  217. "When true, raises BG target when autosens detects sensitivity",
  218. comment: "Sensitivity Raises Target"
  219. ),
  220. settable: self
  221. ),
  222. Field(
  223. displayName: "Resistance Lowers Target",
  224. type: .boolean(keypath: \.resistanceLowersTarget),
  225. infoText: NSLocalizedString(
  226. "Defaults to false. When true, will lower BG target when autosens detects resistance",
  227. comment: "Resistance Lowers Target"
  228. ),
  229. settable: self
  230. ),
  231. Field(
  232. displayName: "Advanced Target Adjustments",
  233. type: .boolean(keypath: \.advTargetAdjustments),
  234. infoText: NSLocalizedString(
  235. "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.",
  236. comment: "Advanced Target Adjustments"
  237. ),
  238. settable: self
  239. ),
  240. Field(
  241. displayName: "Exercise Mode",
  242. type: .boolean(keypath: \.exerciseMode),
  243. infoText: NSLocalizedString(
  244. "Defaults to false. When true, > 105 mg/dL high temp target adjusts sensitivityRatio for exercise_mode. Synonym for high_temptarget_raises_sensitivity",
  245. comment: "Exercise Mode"
  246. ),
  247. settable: self
  248. ),
  249. Field(
  250. displayName: "Half Basal Exercise Target",
  251. type: .decimal(keypath: \.halfBasalExerciseTarget),
  252. infoText: NSLocalizedString(
  253. "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.",
  254. comment: "Half Basal Exercise Target"
  255. ),
  256. settable: self
  257. ),
  258. Field(
  259. displayName: "Wide BG Target Range",
  260. type: .boolean(keypath: \.wideBGTargetRange),
  261. infoText: NSLocalizedString(
  262. "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.",
  263. comment: "Wide BG Target Range"
  264. ),
  265. settable: self
  266. )
  267. ]
  268. // MARK: - Other fields
  269. let otherSettings = [
  270. Field(
  271. displayName: "Rewind Resets Autosens",
  272. type: .boolean(keypath: \.rewindResetsAutosens),
  273. infoText: NSLocalizedString(
  274. "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.",
  275. comment: "Rewind Resets Autosens"
  276. ),
  277. settable: self
  278. ),
  279. Field(
  280. displayName: "Use Custom Peak Time",
  281. type: .boolean(keypath: \.useCustomPeakTime),
  282. infoText: NSLocalizedString(
  283. "Defaults to false. Setting to true allows changing insulinPeakTime", comment: "Use Custom Peak Time"
  284. ),
  285. settable: self
  286. ),
  287. Field(
  288. displayName: "Insulin Peak Time",
  289. type: .decimal(keypath: \.insulinPeakTime),
  290. infoText: NSLocalizedString(
  291. "Time of maximum blood glucose lowering effect of insulin, in minutes",
  292. comment: "Insulin Peak Time"
  293. ),
  294. settable: self
  295. ),
  296. // Field(
  297. // displayName: "Carbs Req Threshold",
  298. // type: .decimal(keypath: \.carbsReqThreshold),
  299. // infoText: NSLocalizedString(
  300. // "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.",
  301. // comment: "Carbs Req Threshold"
  302. // ),
  303. // settable: self
  304. // ),
  305. Field(
  306. displayName: "Skip Neutral Temps",
  307. type: .boolean(keypath: \.skipNeutralTemps),
  308. infoText: NSLocalizedString(
  309. "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.",
  310. comment: "Skip Neutral Temps"
  311. ),
  312. settable: self
  313. ),
  314. Field(
  315. displayName: "Unsuspend If No Temp",
  316. type: .boolean(keypath: \.unsuspendIfNoTemp),
  317. infoText: NSLocalizedString(
  318. "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.",
  319. comment: "Unsuspend If No Temp"
  320. ),
  321. settable: self
  322. ),
  323. Field(
  324. displayName: "Suspend Zeros IOB",
  325. type: .boolean(keypath: \.suspendZerosIOB),
  326. infoText: NSLocalizedString(
  327. "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.",
  328. comment: "Suspend Zeros IOB"
  329. ),
  330. settable: self
  331. ),
  332. Field(
  333. displayName: "Bolus Snooze DIA Divisor",
  334. type: .decimal(keypath: \.bolusSnoozeDIADivisor),
  335. infoText: NSLocalizedString(
  336. "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).",
  337. comment: "Bolus Snooze DIA Divisor"
  338. ),
  339. settable: self
  340. ),
  341. Field(
  342. displayName: "Min 5m Carbimpact",
  343. type: .decimal(keypath: \.min5mCarbimpact),
  344. infoText: NSLocalizedString(
  345. "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.",
  346. comment: "Min 5m Carbimpact"
  347. ),
  348. settable: self
  349. ),
  350. Field(
  351. displayName: "Autotune ISF Adjustment Fraction",
  352. type: .decimal(keypath: \.autotuneISFAdjustmentFraction),
  353. infoText: NSLocalizedString(
  354. "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.",
  355. comment: "Autotune ISF Adjustment Fraction"
  356. ),
  357. settable: self
  358. ),
  359. Field(
  360. displayName: "Remaining Carbs Fraction",
  361. type: .decimal(keypath: \.remainingCarbsFraction),
  362. infoText: NSLocalizedString(
  363. "This is the fraction of carbs we’ll assume will absorb over 4h if we don’t yet see carb absorption.",
  364. comment: "Remaining Carbs Fraction"
  365. ),
  366. settable: self
  367. ),
  368. Field(
  369. displayName: "Remaining Carbs Cap",
  370. type: .decimal(keypath: \.remainingCarbsCap),
  371. infoText: NSLocalizedString(
  372. "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.",
  373. comment: "Remaining Carbs Cap"
  374. ),
  375. settable: self
  376. ),
  377. Field(
  378. displayName: "Noisy CGM Target Multiplier",
  379. type: .decimal(keypath: \.noisyCGMTargetMultiplier),
  380. infoText: NSLocalizedString(
  381. "Defaults to 1.3. Increase target by this amount when looping off raw/noisy CGM data",
  382. comment: "Noisy CGM Target Multiplier"
  383. ),
  384. settable: self
  385. )
  386. ]
  387. sections = [
  388. FieldSection(
  389. displayName: NSLocalizedString("OpenAPS main settings", comment: "OpenAPS main settings"), fields: mainFields
  390. ),
  391. FieldSection(
  392. displayName: NSLocalizedString("OpenAPS SMB settings", comment: "OpenAPS SMB settings"), fields: smbFields
  393. ),
  394. FieldSection(
  395. displayName: NSLocalizedString("OpenAPS targets settings", comment: "OpenAPS targets settings"),
  396. fields: targetSettings
  397. ),
  398. FieldSection(
  399. displayName: NSLocalizedString("OpenAPS other settings", comment: "OpenAPS other settings"),
  400. fields: otherSettings
  401. )
  402. ]
  403. }
  404. func set<T>(_ keypath: WritableKeyPath<Preferences, T>, value: T) {
  405. preferences[keyPath: keypath] = value
  406. save()
  407. }
  408. func get<T>(_ keypath: WritableKeyPath<Preferences, T>) -> T {
  409. preferences[keyPath: keypath]
  410. }
  411. func save() {
  412. provider.savePreferences(preferences)
  413. }
  414. }
  415. }