AutosensSettingsRootView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import SwiftUI
  2. import Swinject
  3. extension AutosensSettings {
  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. private var rateFormatter: NumberFormatter {
  17. let formatter = NumberFormatter()
  18. formatter.numberStyle = .decimal
  19. formatter.maximumFractionDigits = 2
  20. return formatter
  21. }
  22. var autosensVerboseHint: some View {
  23. VStack(alignment: .leading, spacing: 15) {
  24. Text(
  25. "Autosens automatically adjusts insulin delivery based on how sensitive or resistant you are to insulin at the time of the current loop cycle by analyzing past data to keep blood sugar levels stable."
  26. )
  27. VStack(alignment: .leading, spacing: 5) {
  28. Text("How it Works").bold()
  29. Text(
  30. "It looks at the last 8-24 hours of data, excluding meal-related changes, and adjusts insulin settings like basal rates and targets when needed to match your sensitivity or resistance to insulin."
  31. )
  32. }
  33. VStack(alignment: .leading, spacing: 5) {
  34. Text("What it Adjusts").bold()
  35. Text(
  36. "Autosens modifies Insulin Sensitivity Factor (ISF), basal rates, and target blood sugar levels. It doesn’t account for carbs but adjusts for insulin effectiveness based on patterns in your glucose data."
  37. )
  38. }
  39. VStack(alignment: .leading, spacing: 5) {
  40. Text("Key Limitations").bold()
  41. Text(
  42. "Autosens has safety limits determined by your Autosens Max and Autosens Min settings. These settings prevent over-adjusting."
  43. )
  44. }
  45. Text(
  46. "Autosens functions alongside certain settings, like Super Micro Bolus (SMB). Other settings, like Dynamic ISF, alter portions of the Autosens formula. Please review the in-app hints for the Algorithm Settings prior to enabling them to understand how they may influence it."
  47. )
  48. }
  49. }
  50. var AutosensView: some View {
  51. Section(
  52. header: !state.settingsManager.preferences
  53. .useNewFormula ? Text("Autosens") : Text("Dynamic Sensitivity")
  54. ) {
  55. VStack {
  56. let dynamicRatio = state.determinationsFromPersistence.first?.sensitivityRatio
  57. let dynamicISF = state.determinationsFromPersistence.first?.insulinSensitivity
  58. let newISF = state.autosensISF
  59. HStack {
  60. Text("Sensitivity Ratio")
  61. Spacer()
  62. Text(
  63. rateFormatter
  64. .string(from: (
  65. (
  66. !state.settingsManager.preferences.useNewFormula ? state
  67. .autosensRatio as NSDecimalNumber : dynamicRatio
  68. ) ?? 1
  69. ) as NSNumber) ?? "1"
  70. )
  71. }.padding(.vertical)
  72. HStack {
  73. Text("Calculated Sensitivity")
  74. Spacer()
  75. if state.units == .mgdL {
  76. Text(
  77. !state.settingsManager.preferences
  78. .useNewFormula ? newISF!.description : (dynamicISF ?? 0).description
  79. )
  80. } else {
  81. Text((
  82. !state.settingsManager.preferences
  83. .useNewFormula ? newISF!.formattedAsMmolL : dynamicISF?.decimalValue.formattedAsMmolL
  84. ) ?? "0")
  85. }
  86. Text(state.units.rawValue + "/U").foregroundColor(.secondary)
  87. }
  88. HStack(alignment: .top) {
  89. Text(
  90. "This adjusted ISF is temporary, will change with the next loop cycle, and should not be directly used as your profile ISF value."
  91. )
  92. .font(.footnote)
  93. .foregroundColor(.secondary)
  94. .lineLimit(nil)
  95. Spacer()
  96. Button(
  97. action: {
  98. hintLabel = "Autosens"
  99. selectedVerboseHint = AnyView(autosensVerboseHint)
  100. shouldDisplayHint.toggle()
  101. },
  102. label: {
  103. HStack {
  104. Image(systemName: "questionmark.circle")
  105. }
  106. }
  107. ).buttonStyle(BorderlessButtonStyle())
  108. }.padding(.top)
  109. }.padding(.bottom)
  110. }.listRowBackground(Color.chart)
  111. }
  112. var body: some View {
  113. List {
  114. if state.autosensISF != nil {
  115. AutosensView
  116. }
  117. SettingInputSection(
  118. decimalValue: $state.autosensMax,
  119. booleanValue: $booleanPlaceholder,
  120. shouldDisplayHint: $shouldDisplayHint,
  121. selectedVerboseHint: Binding(
  122. get: { selectedVerboseHint },
  123. set: {
  124. selectedVerboseHint = $0.map { AnyView($0) }
  125. hintLabel = NSLocalizedString("Autosens Max", comment: "Autosens Max")
  126. }
  127. ),
  128. units: state.units,
  129. type: .decimal("autosensMax"),
  130. label: NSLocalizedString("Autosens Max", comment: "Autosens Max"),
  131. miniHint: "Upper limit of the Autosens Ratio.",
  132. verboseHint:
  133. VStack(alignment: .leading, spacing: 10) {
  134. Text("Default: 120%").bold()
  135. Text(
  136. "Autosens Max sets the maximum Autosens Ratio used by Autosens, Dynamic ISF, and Sigmoid Formula."
  137. )
  138. Text(
  139. "The Autosens Ratio is used to calculate the amount of adjustment needed to basal rates, ISF, and CR."
  140. )
  141. Text(
  142. "Tip: Increasing this value allows automatic adjustments of basal rates to be higher, ISF to be lower, and CR to be lower."
  143. )
  144. },
  145. headerText: "Glucose Deviations Algorithm"
  146. )
  147. SettingInputSection(
  148. decimalValue: $state.autosensMin,
  149. booleanValue: $booleanPlaceholder,
  150. shouldDisplayHint: $shouldDisplayHint,
  151. selectedVerboseHint: Binding(
  152. get: { selectedVerboseHint },
  153. set: {
  154. selectedVerboseHint = $0.map { AnyView($0) }
  155. hintLabel = NSLocalizedString("Autosens Min", comment: "Autosens Min")
  156. }
  157. ),
  158. units: state.units,
  159. type: .decimal("autosensMin"),
  160. label: NSLocalizedString("Autosens Min", comment: "Autosens Min"),
  161. miniHint: "Lower limit of the Autosens Ratio.",
  162. verboseHint:
  163. VStack(alignment: .leading, spacing: 10) {
  164. Text("Default: 80%").bold()
  165. Text(
  166. "Autosens Min sets the minimum Autosens Ratio used by Autosens, Dynamic ISF, and Sigmoid Formula."
  167. )
  168. Text(
  169. "The Autosens Ratio is used to calculate the amount of adjustment needed to basal rates, ISF, and CR."
  170. )
  171. Text(
  172. "Tip: Decreasing this value allows automatic adjustments of basal rates to be lower, ISF to be higher, and CR to be higher."
  173. )
  174. }
  175. )
  176. SettingInputSection(
  177. decimalValue: $decimalPlaceholder,
  178. booleanValue: $state.rewindResetsAutosens,
  179. shouldDisplayHint: $shouldDisplayHint,
  180. selectedVerboseHint: Binding(
  181. get: { selectedVerboseHint },
  182. set: {
  183. selectedVerboseHint = $0.map { AnyView($0) }
  184. hintLabel = NSLocalizedString("Rewind Resets Autosens", comment: "Rewind Resets Autosens")
  185. }
  186. ),
  187. units: state.units,
  188. type: .boolean,
  189. label: NSLocalizedString("Rewind Resets Autosens", comment: "Rewind Resets Autosens"),
  190. miniHint: "Pump rewind initiates a reset in Autosens Ratio.",
  191. verboseHint: VStack(alignment: .leading, spacing: 5) {
  192. Text("Default: ON").bold()
  193. Text("Medtronic Users Only").bold()
  194. VStack(alignment: .leading, spacing: 10) {
  195. Text(
  196. "This feature resets the Autosens Ratio to neutral when you rewind your pump on the assumption that this corresponds to a site change."
  197. )
  198. Text(
  199. "Autosens will begin learning sensitivity anew from the time of the rewind, which may take up to 6 hours."
  200. )
  201. Text(
  202. "Tip: If you usually rewind your pump independently of site changes, you may want to consider disabling this feature."
  203. )
  204. }
  205. }
  206. )
  207. }
  208. .listSectionSpacing(sectionSpacing)
  209. .sheet(isPresented: $shouldDisplayHint) {
  210. SettingInputHintView(
  211. hintDetent: $hintDetent,
  212. shouldDisplayHint: $shouldDisplayHint,
  213. hintLabel: hintLabel ?? "",
  214. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  215. sheetTitle: "Help"
  216. )
  217. }
  218. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  219. .onAppear(perform: configureView)
  220. .navigationTitle("Autosens")
  221. .navigationBarTitleDisplayMode(.automatic)
  222. }
  223. }
  224. }