| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- import SwiftUI
- import Swinject
- extension AutosensSettings {
- struct RootView: BaseView {
- let resolver: Resolver
- @StateObject var state = StateModel()
- @State private var shouldDisplayHint: Bool = false
- @State var hintDetent = PresentationDetent.large
- @State var selectedVerboseHint: AnyView?
- @State var hintLabel: String?
- @State private var decimalPlaceholder: Decimal = 0.0
- @State private var booleanPlaceholder: Bool = false
- @Environment(\.colorScheme) var colorScheme
- @EnvironmentObject var appIcons: Icons
- @Environment(AppState.self) var appState
- private var rateFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 2
- return formatter
- }
- var autosensVerboseHint: some View {
- VStack(alignment: .leading, spacing: 15) {
- Text(
- "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."
- )
- VStack(alignment: .leading, spacing: 5) {
- Text("How it Works").bold()
- Text(
- "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."
- )
- }
- VStack(alignment: .leading, spacing: 5) {
- Text("What it Adjusts").bold()
- Text(
- "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."
- )
- }
- VStack(alignment: .leading, spacing: 5) {
- Text("Key Limitations").bold()
- Text(
- "Autosens has safety limits determined by your Autosens Max and Autosens Min settings. These settings prevent over-adjusting."
- )
- }
- Text(
- "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."
- )
- }
- }
- var AutosensView: some View {
- Section(
- header: !state.settingsManager.preferences
- .useNewFormula ? Text("Autosens") : Text("Dynamic Sensitivity")
- ) {
- VStack {
- let dynamicRatio = state.determinationsFromPersistence.first?.sensitivityRatio
- let dynamicISF = state.determinationsFromPersistence.first?.insulinSensitivity
- let newISF = state.autosensISF
- HStack {
- Text("Sensitivity Ratio")
- Spacer()
- Text(
- rateFormatter
- .string(from: (
- (
- !state.settingsManager.preferences.useNewFormula ? state
- .autosensRatio as NSDecimalNumber : dynamicRatio
- ) ?? 1
- ) as NSNumber) ?? "1"
- )
- }.padding(.vertical)
- HStack {
- Text("Calculated Sensitivity")
- Spacer()
- if state.units == .mgdL {
- Text(
- !state.settingsManager.preferences
- .useNewFormula ? newISF!.description : (dynamicISF ?? 0).description
- )
- } else {
- Text((
- !state.settingsManager.preferences
- .useNewFormula ? newISF!.formattedAsMmolL : dynamicISF?.decimalValue.formattedAsMmolL
- ) ?? "0")
- }
- Text(state.units.rawValue + "/U").foregroundColor(.secondary)
- }
- HStack(alignment: .top) {
- Text(
- "This adjusted ISF is temporary, will change with the next loop cycle, and should not be directly used as your profile ISF value."
- )
- .font(.footnote)
- .foregroundColor(.secondary)
- .lineLimit(nil)
- Spacer()
- Button(
- action: {
- hintLabel = "Autosens"
- selectedVerboseHint = AnyView(autosensVerboseHint)
- shouldDisplayHint.toggle()
- },
- label: {
- HStack {
- Image(systemName: "questionmark.circle")
- }
- }
- ).buttonStyle(BorderlessButtonStyle())
- }.padding(.top)
- }.padding(.bottom)
- }.listRowBackground(Color.chart)
- }
- var body: some View {
- List {
- if state.autosensISF != nil {
- AutosensView
- }
- SettingInputSection(
- decimalValue: $state.autosensMax,
- booleanValue: $booleanPlaceholder,
- shouldDisplayHint: $shouldDisplayHint,
- selectedVerboseHint: Binding(
- get: { selectedVerboseHint },
- set: {
- selectedVerboseHint = $0.map { AnyView($0) }
- hintLabel = NSLocalizedString("Autosens Max", comment: "Autosens Max")
- }
- ),
- units: state.units,
- type: .decimal("autosensMax"),
- label: NSLocalizedString("Autosens Max", comment: "Autosens Max"),
- miniHint: "Upper limit of the Autosens Ratio.",
- verboseHint:
- VStack(alignment: .leading, spacing: 10) {
- Text("Default: 120%").bold()
- Text(
- "Autosens Max sets the maximum Autosens Ratio used by Autosens, Dynamic ISF, and Sigmoid Formula."
- )
- Text(
- "The Autosens Ratio is used to calculate the amount of adjustment needed to basal rates, ISF, and CR."
- )
- Text(
- "Tip: Increasing this value allows automatic adjustments of basal rates to be higher, ISF to be lower, and CR to be lower."
- )
- },
- headerText: "Glucose Deviations Algorithm"
- )
- SettingInputSection(
- decimalValue: $state.autosensMin,
- booleanValue: $booleanPlaceholder,
- shouldDisplayHint: $shouldDisplayHint,
- selectedVerboseHint: Binding(
- get: { selectedVerboseHint },
- set: {
- selectedVerboseHint = $0.map { AnyView($0) }
- hintLabel = NSLocalizedString("Autosens Min", comment: "Autosens Min")
- }
- ),
- units: state.units,
- type: .decimal("autosensMin"),
- label: NSLocalizedString("Autosens Min", comment: "Autosens Min"),
- miniHint: "Lower limit of the Autosens Ratio.",
- verboseHint:
- VStack(alignment: .leading, spacing: 10) {
- Text("Default: 80%").bold()
- Text(
- "Autosens Min sets the minimum Autosens Ratio used by Autosens, Dynamic ISF, and Sigmoid Formula."
- )
- Text(
- "The Autosens Ratio is used to calculate the amount of adjustment needed to basal rates, ISF, and CR."
- )
- Text(
- "Tip: Decreasing this value allows automatic adjustments of basal rates to be lower, ISF to be higher, and CR to be higher."
- )
- }
- )
- SettingInputSection(
- decimalValue: $decimalPlaceholder,
- booleanValue: $state.rewindResetsAutosens,
- shouldDisplayHint: $shouldDisplayHint,
- selectedVerboseHint: Binding(
- get: { selectedVerboseHint },
- set: {
- selectedVerboseHint = $0.map { AnyView($0) }
- hintLabel = NSLocalizedString("Rewind Resets Autosens", comment: "Rewind Resets Autosens")
- }
- ),
- units: state.units,
- type: .boolean,
- label: NSLocalizedString("Rewind Resets Autosens", comment: "Rewind Resets Autosens"),
- miniHint: "Pump rewind initiates a reset in Autosens Ratio.",
- verboseHint: VStack(alignment: .leading, spacing: 5) {
- Text("Default: ON").bold()
- Text("Medtronic Users Only").bold()
- VStack(alignment: .leading, spacing: 10) {
- Text(
- "This feature resets the Autosens Ratio to neutral when you rewind your pump on the assumption that this corresponds to a site change."
- )
- Text(
- "Autosens will begin learning sensitivity anew from the time of the rewind, which may take up to 6 hours."
- )
- Text(
- "Tip: If you usually rewind your pump independently of site changes, you may want to consider disabling this feature."
- )
- }
- }
- )
- }
- .listSectionSpacing(sectionSpacing)
- .sheet(isPresented: $shouldDisplayHint) {
- SettingInputHintView(
- hintDetent: $hintDetent,
- shouldDisplayHint: $shouldDisplayHint,
- hintLabel: hintLabel ?? "",
- hintText: selectedVerboseHint ?? AnyView(EmptyView()),
- sheetTitle: "Help"
- )
- }
- .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
- .onAppear(perform: configureView)
- .navigationTitle("Autosens")
- .navigationBarTitleDisplayMode(.automatic)
- }
- }
- }
|