SMBSettingsRootView.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. import SwiftUI
  2. import Swinject
  3. extension SMBSettings {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject 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. @Environment(AppState.self) var appState
  16. var body: some View {
  17. List {
  18. SettingInputSection(
  19. decimalValue: $decimalPlaceholder,
  20. booleanValue: $state.enableSMBAlways,
  21. shouldDisplayHint: $shouldDisplayHint,
  22. selectedVerboseHint: Binding(
  23. get: { selectedVerboseHint },
  24. set: {
  25. selectedVerboseHint = $0.map { AnyView($0) }
  26. hintLabel = NSLocalizedString("Enable SMB Always", comment: "Enable SMB Always")
  27. }
  28. ),
  29. units: state.units,
  30. type: .boolean,
  31. label: NSLocalizedString("Enable SMB Always", comment: "Enable SMB Always"),
  32. miniHint: "Allow SMBs at all times except when a high Temp Target is set.",
  33. verboseHint: VStack(alignment: .leading, spacing: 10) {
  34. Text("Default: OFF").bold()
  35. Text(
  36. "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."
  37. )
  38. Text(
  39. "Note: If you would like to allow SMBs when a high Temp Target is set, enable the \"Allow SMBs with High Temptarget\" setting."
  40. )
  41. },
  42. headerText: "Super-Micro-Bolus"
  43. )
  44. if !state.enableSMBAlways {
  45. SettingInputSection(
  46. decimalValue: $decimalPlaceholder,
  47. booleanValue: $state.enableSMBWithCOB,
  48. shouldDisplayHint: $shouldDisplayHint,
  49. selectedVerboseHint: Binding(
  50. get: { selectedVerboseHint },
  51. set: {
  52. selectedVerboseHint = $0.map { AnyView($0) }
  53. hintLabel = NSLocalizedString("Enable SMB With COB", comment: "Enable SMB With COB")
  54. }
  55. ),
  56. units: state.units,
  57. type: .boolean,
  58. label: NSLocalizedString("Enable SMB With COB", comment: "Enable SMB With COB"),
  59. miniHint: "Allow SMB when carbs are on board.",
  60. verboseHint:
  61. VStack(alignment: .leading, spacing: 10) {
  62. Text("Default: OFF").bold()
  63. Text(
  64. "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."
  65. )
  66. Text(
  67. "Note: If this is enabled and the criteria are met, SMBs could be utilized regardless of other SMB settings being enabled or not."
  68. )
  69. }
  70. )
  71. SettingInputSection(
  72. decimalValue: $decimalPlaceholder,
  73. booleanValue: $state.enableSMBWithTemptarget,
  74. shouldDisplayHint: $shouldDisplayHint,
  75. selectedVerboseHint: Binding(
  76. get: { selectedVerboseHint },
  77. set: {
  78. selectedVerboseHint = $0.map { AnyView($0) }
  79. hintLabel = NSLocalizedString("Enable SMB With Temptarget", comment: "Enable SMB With Temptarget")
  80. }
  81. ),
  82. units: state.units,
  83. type: .boolean,
  84. label: NSLocalizedString("Enable SMB With Temptarget", comment: "Enable SMB With Temptarget"),
  85. miniHint: "Allow SMB when a manual Temporary Target is set under \(state.units == .mgdL ? "100" : 100.formattedAsMmolL) \(state.units.rawValue).",
  86. verboseHint:
  87. VStack(alignment: .leading, spacing: 10) {
  88. Text("Default: OFF").bold()
  89. Text(
  90. "Enabling this feature allows Trio to deliver insulin required using Super Micro Boluses (SMB) at times when a manual Temporary Target under \(state.units == .mgdL ? "100" : 100.formattedAsMmolL) \(state.units.rawValue) is set."
  91. )
  92. Text(
  93. "Note: If this is enabled and the criteria are met, SMBs could be utilized regardless of other SMB settings being enabled or not."
  94. )
  95. }
  96. )
  97. SettingInputSection(
  98. decimalValue: $decimalPlaceholder,
  99. booleanValue: $state.enableSMBAfterCarbs,
  100. shouldDisplayHint: $shouldDisplayHint,
  101. selectedVerboseHint: Binding(
  102. get: { selectedVerboseHint },
  103. set: {
  104. selectedVerboseHint = $0.map { AnyView($0) }
  105. hintLabel = NSLocalizedString("Enable SMB After Carbs", comment: "Enable SMB After Carbs")
  106. }
  107. ),
  108. units: state.units,
  109. type: .boolean,
  110. label: NSLocalizedString("Enable SMB After Carbs", comment: "Enable SMB After Carbs"),
  111. miniHint: "Allow SMB for 6 hrs after a carb entry.",
  112. verboseHint:
  113. VStack(alignment: .leading, spacing: 10) {
  114. Text("Default: OFF").bold()
  115. Text(
  116. "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)."
  117. )
  118. Text(
  119. "Note: If this is enabled and the criteria are met, SMBs could be utilized regardless of other SMB settings being enabled or not."
  120. )
  121. }
  122. )
  123. SettingInputSection(
  124. decimalValue: $state.enableSMB_high_bg_target,
  125. booleanValue: $state.enableSMB_high_bg,
  126. shouldDisplayHint: $shouldDisplayHint,
  127. selectedVerboseHint: Binding(
  128. get: { selectedVerboseHint },
  129. set: {
  130. selectedVerboseHint = $0.map { AnyView($0) }
  131. hintLabel = NSLocalizedString("Enable SMB With High BG", comment: "Enable SMB With High BG")
  132. }
  133. ),
  134. units: state.units,
  135. type: .conditionalDecimal("enableSMB_high_bg_target"),
  136. label: NSLocalizedString("Enable SMB With High BG", comment: "Enable SMB With High BG"),
  137. conditionalLabel: "High BG Target",
  138. miniHint: "Allow SMB when glucose is above the High BG Target value.",
  139. verboseHint:
  140. VStack(alignment: .leading, spacing: 10) {
  141. Text("Default: OFF").bold()
  142. Text(
  143. "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."
  144. )
  145. Text(
  146. "Note: If this is enabled and the criteria are met, SMBs could be utilized regardless of other SMB settings being enabled or not."
  147. )
  148. }
  149. )
  150. }
  151. SettingInputSection(
  152. decimalValue: $decimalPlaceholder,
  153. booleanValue: $state.allowSMBWithHighTemptarget,
  154. shouldDisplayHint: $shouldDisplayHint,
  155. selectedVerboseHint: Binding(
  156. get: { selectedVerboseHint },
  157. set: {
  158. selectedVerboseHint = $0.map { AnyView($0) }
  159. hintLabel = NSLocalizedString(
  160. "Allow SMB With High Temptarget",
  161. comment: "Allow SMB With High Temptarget"
  162. )
  163. }
  164. ),
  165. units: state.units,
  166. type: .boolean,
  167. label: NSLocalizedString(
  168. "Allow SMB With High Temptarget",
  169. comment: "Allow SMB With High Temptarget"
  170. ),
  171. miniHint: "Allow SMB when a manual Temporary Target is set greater than \(state.units == .mgdL ? "100" : 100.formattedAsMmolL) \(state.units.rawValue).",
  172. verboseHint:
  173. VStack(alignment: .leading, spacing: 10) {
  174. Text("Default: OFF").bold()
  175. Text(
  176. "Enabling this feature allows Trio to deliver insulin required using Super Micro Boluses (SMB) when a manual Temporary Target above \(state.units == .mgdL ? "100" : 100.formattedAsMmolL) \(state.units.rawValue) is set."
  177. )
  178. Text(
  179. "Note: If this is enabled and the criteria are met, SMBs could be utilized regardless of other SMB settings being enabled or not."
  180. )
  181. Text(
  182. "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."
  183. ).bold()
  184. }
  185. )
  186. SettingInputSection(
  187. decimalValue: $decimalPlaceholder,
  188. booleanValue: $state.enableUAM,
  189. shouldDisplayHint: $shouldDisplayHint,
  190. selectedVerboseHint: Binding(
  191. get: { selectedVerboseHint },
  192. set: {
  193. selectedVerboseHint = $0.map { AnyView($0) }
  194. hintLabel = NSLocalizedString("Enable UAM", comment: "Enable UAM")
  195. }
  196. ),
  197. units: state.units,
  198. type: .boolean,
  199. label: NSLocalizedString("Enable UAM", comment: "Enable UAM"),
  200. miniHint: "Enable Unannounced Meals SMB.",
  201. verboseHint:
  202. VStack(alignment: .leading, spacing: 10) {
  203. Text("Default: OFF").bold()
  204. Text(
  205. "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."
  206. )
  207. Text(
  208. "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."
  209. )
  210. Text(
  211. "This feature ensures more accurate insulin adjustments when carb entries are missing or incorrect."
  212. )
  213. }
  214. )
  215. SettingInputSection(
  216. decimalValue: $state.maxSMBBasalMinutes,
  217. booleanValue: $booleanPlaceholder,
  218. shouldDisplayHint: $shouldDisplayHint,
  219. selectedVerboseHint: Binding(
  220. get: { selectedVerboseHint },
  221. set: {
  222. selectedVerboseHint = $0.map { AnyView($0) }
  223. hintLabel = NSLocalizedString("Max SMB Basal Minutes", comment: "Max SMB Basal Minutes")
  224. }
  225. ),
  226. units: state.units,
  227. type: .decimal("maxSMBBasalMinutes"),
  228. label: NSLocalizedString("Max SMB Basal Minutes", comment: "Max SMB Basal Minutes"),
  229. miniHint: "Limits the size of a single Super Micro Bolus (SMB) dose.",
  230. verboseHint: VStack(spacing: 10) {
  231. VStack(alignment: .leading, spacing: 10) {
  232. VStack(alignment: .leading, spacing: 1) {
  233. Text("Default: 30 minutes").bold()
  234. Text("(50% current basal rate)").bold()
  235. }
  236. VStack(alignment: .leading, spacing: 10) {
  237. Text(
  238. "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."
  239. )
  240. Text(
  241. "To calculate the maximum SMB allowed based on this setting, use the following formula:"
  242. )
  243. }
  244. }
  245. VStack(alignment: .center, spacing: 5) {
  246. Text(
  247. "𝒳 = Max SMB Basal Minutes"
  248. )
  249. Text("(𝒳 ÷ 60) × current basal rate")
  250. }
  251. VStack(alignment: .leading, spacing: 10) {
  252. Text(
  253. "Warning: Increasing this value above 90 minutes may impact Trio's ability to effectively zero temp and prevent lows."
  254. ).bold()
  255. Text("Note: SMBs must be enabled to use this limit.")
  256. }
  257. }
  258. )
  259. SettingInputSection(
  260. decimalValue: $state.maxUAMSMBBasalMinutes,
  261. booleanValue: $booleanPlaceholder,
  262. shouldDisplayHint: $shouldDisplayHint,
  263. selectedVerboseHint: Binding(
  264. get: { selectedVerboseHint },
  265. set: {
  266. selectedVerboseHint = $0.map { AnyView($0) }
  267. hintLabel = NSLocalizedString("Max UAM Basal Minutes", comment: "Max UAM Basal Minutes")
  268. }
  269. ),
  270. units: state.units,
  271. type: .decimal("maxUAMSMBBasalMinutes"),
  272. label: NSLocalizedString("Max UAM Basal Minutes", comment: "Max UAM Basal Minutes"),
  273. miniHint: "Limits the size of a single Unannounced Meal (UAM) SMB dose.",
  274. verboseHint: VStack(spacing: 10) {
  275. VStack(alignment: .leading, spacing: 10) {
  276. VStack(alignment: .leading, spacing: 1) {
  277. Text("Default: 30 minutes").bold()
  278. Text("(50% current basal rate)").bold()
  279. }
  280. VStack(alignment: .leading, spacing: 10) {
  281. Text(
  282. "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."
  283. )
  284. Text(
  285. "To calculate the maximum UAM SMB allowed based on this setting, use the following formula:"
  286. )
  287. }
  288. }
  289. VStack(alignment: .center, spacing: 5) {
  290. Text(
  291. "𝒳 = Max UAM SMB Basal Minutes"
  292. )
  293. Text("(𝒳 ÷ 60) × current basal rate")
  294. }
  295. VStack(alignment: .leading, spacing: 10) {
  296. Text(
  297. "Warning: Increasing this value above 90 minutes may impact Trio's ability to effectively zero temp and prevent lows."
  298. ).bold()
  299. Text("Note: UAM SMBs must be enabled to use this limit.")
  300. }
  301. }
  302. )
  303. SettingInputSection(
  304. decimalValue: $state.maxDeltaBGthreshold,
  305. booleanValue: $booleanPlaceholder,
  306. shouldDisplayHint: $shouldDisplayHint,
  307. selectedVerboseHint: Binding(
  308. get: { selectedVerboseHint },
  309. set: {
  310. selectedVerboseHint = $0.map { AnyView($0) }
  311. hintLabel = NSLocalizedString("Max Delta-BG Threshold SMB", comment: "Max Delta-BG Threshold")
  312. }
  313. ),
  314. units: state.units,
  315. type: .decimal("maxDeltaBGthreshold"),
  316. label: NSLocalizedString("Max Delta-BG Threshold SMB", comment: "Max Delta-BG Threshold"),
  317. miniHint: "Disables SMBs if last two glucose values differ by more than this percent.",
  318. verboseHint:
  319. VStack(alignment: .leading, spacing: 10) {
  320. Text("Default: 20% increase").bold()
  321. Text(
  322. "Maximum allowed positive percentual change in glucose level to permit SMBs. If the difference in glucose is greater than this, Trio will disable SMBs."
  323. )
  324. Text("Note: This setting has a hard-coded cap of 40%")
  325. }
  326. )
  327. SettingInputSection(
  328. decimalValue: $state.smbDeliveryRatio,
  329. booleanValue: $booleanPlaceholder,
  330. shouldDisplayHint: $shouldDisplayHint,
  331. selectedVerboseHint: Binding(
  332. get: { selectedVerboseHint },
  333. set: {
  334. selectedVerboseHint = $0.map { AnyView($0) }
  335. hintLabel = NSLocalizedString("SMB Delivery Ratio", comment: "SMB Delivery Ratio")
  336. }
  337. ),
  338. units: state.units,
  339. type: .decimal("smbDeliveryRatio"),
  340. label: NSLocalizedString("SMB Delivery Ratio", comment: "SMB Delivery Ratio"),
  341. miniHint: "Percentage of calculated insulin required that is given as SMB.",
  342. verboseHint:
  343. VStack(alignment: .leading, spacing: 10) {
  344. Text("Default: 50%").bold()
  345. Text(
  346. "Once the total insulin required is calculated, this safety limit specifies what percentage of the insulin required can be delivered as an SMB."
  347. )
  348. Text(
  349. "Due to SMBs potentially occurring every 5 minutes with each loop cycle, it is important to set this value to a reasonable level that allows Trio to safely zero temp should dosing needs suddenly change. Increase this value with caution."
  350. )
  351. Text("Note: Allowed range is 30 - 70%")
  352. }
  353. )
  354. SettingInputSection(
  355. decimalValue: $state.smbInterval,
  356. booleanValue: $booleanPlaceholder,
  357. shouldDisplayHint: $shouldDisplayHint,
  358. selectedVerboseHint: Binding(
  359. get: { selectedVerboseHint },
  360. set: {
  361. selectedVerboseHint = $0.map { AnyView($0) }
  362. hintLabel = NSLocalizedString("SMB Interval", comment: "SMB Interval")
  363. }
  364. ),
  365. units: state.units,
  366. type: .decimal("smbInterval"),
  367. label: NSLocalizedString("SMB Interval", comment: "SMB Interval"),
  368. miniHint: "Minimum minutes since the last SMB or manual bolus to allow an automated SMB.",
  369. verboseHint:
  370. VStack(alignment: .leading, spacing: 10) {
  371. Text("Default: 3 min").bold()
  372. Text(
  373. "This is the minimum number of minutes since the last SMB or manual bolus before Trio will permit an automated SMB."
  374. )
  375. }
  376. )
  377. }
  378. .listSectionSpacing(sectionSpacing)
  379. .sheet(isPresented: $shouldDisplayHint) {
  380. SettingInputHintView(
  381. hintDetent: $hintDetent,
  382. shouldDisplayHint: $shouldDisplayHint,
  383. hintLabel: hintLabel ?? "",
  384. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  385. sheetTitle: "Help"
  386. )
  387. }
  388. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  389. .onAppear(perform: configureView)
  390. .navigationTitle("SMB Settings")
  391. .navigationBarTitleDisplayMode(.automatic)
  392. }
  393. }
  394. }