AutosensSettingsRootView.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import SwiftUI
  2. import Swinject
  3. extension AutosensSettings {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @State var state = StateModel()
  7. @State private var shouldDisplayHint: Bool = false
  8. @State var hintDetent = PresentationDetent.large
  9. @State var selectedVerboseHint: String?
  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. private var color: LinearGradient {
  16. colorScheme == .dark ? LinearGradient(
  17. gradient: Gradient(colors: [
  18. Color.bgDarkBlue,
  19. Color.bgDarkerDarkBlue
  20. ]),
  21. startPoint: .top,
  22. endPoint: .bottom
  23. )
  24. :
  25. LinearGradient(
  26. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  27. startPoint: .top,
  28. endPoint: .bottom
  29. )
  30. }
  31. private var rateFormatter: NumberFormatter {
  32. let formatter = NumberFormatter()
  33. formatter.numberStyle = .decimal
  34. formatter.maximumFractionDigits = 2
  35. return formatter
  36. }
  37. var body: some View {
  38. List {
  39. if let newISF = state.autosensISF {
  40. Section(
  41. header: !state.settingsManager.preferences
  42. .useNewFormula ? Text("Autosens") : Text("Dynamic Sensitivity")
  43. ) {
  44. let dynamicRatio = state.determinationsFromPersistence.first?.sensitivityRatio
  45. let dynamicISF = state.determinationsFromPersistence.first?.insulinSensitivity
  46. HStack {
  47. Text("Sensitivity Ratio")
  48. Spacer()
  49. Text(
  50. rateFormatter
  51. .string(from: (
  52. (
  53. !state.settingsManager.preferences.useNewFormula ? state
  54. .autosensRatio as NSDecimalNumber : dynamicRatio
  55. ) ?? 1
  56. ) as NSNumber) ?? "1"
  57. )
  58. }
  59. HStack {
  60. Text("Calculated Sensitivity")
  61. Spacer()
  62. if state.units == .mgdL {
  63. Text(
  64. !state.settingsManager.preferences
  65. .useNewFormula ? newISF.description : (dynamicISF ?? 0).description
  66. )
  67. } else {
  68. Text((
  69. !state.settingsManager.preferences
  70. .useNewFormula ? newISF.formattedAsMmolL : dynamicISF?.decimalValue.formattedAsMmolL
  71. ) ?? "0")
  72. }
  73. Text(state.units.rawValue + "/U").foregroundColor(.secondary)
  74. }
  75. HStack {
  76. Text("This is a snapshot in time and should not be used as your ISF setting.")
  77. .font(.footnote)
  78. .foregroundColor(.secondary)
  79. .lineLimit(nil)
  80. }
  81. }.listRowBackground(Color.chart)
  82. }
  83. SettingInputSection(
  84. decimalValue: $state.autosensMax,
  85. booleanValue: $booleanPlaceholder,
  86. shouldDisplayHint: $shouldDisplayHint,
  87. selectedVerboseHint: Binding(
  88. get: { selectedVerboseHint },
  89. set: {
  90. selectedVerboseHint = $0
  91. hintLabel = NSLocalizedString("Autosens Max", comment: "Autosens Max")
  92. }
  93. ),
  94. units: state.units,
  95. type: .decimal("autosensMax"),
  96. label: NSLocalizedString("Autosens Max", comment: "Autosens Max"),
  97. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  98. verboseHint: NSLocalizedString(
  99. "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.",
  100. comment: "Autosens Max"
  101. ),
  102. headerText: "Glucose Deviations Algorithm"
  103. )
  104. SettingInputSection(
  105. decimalValue: $state.autosensMin,
  106. booleanValue: $booleanPlaceholder,
  107. shouldDisplayHint: $shouldDisplayHint,
  108. selectedVerboseHint: Binding(
  109. get: { selectedVerboseHint },
  110. set: {
  111. selectedVerboseHint = $0
  112. hintLabel = NSLocalizedString("Autosens Min", comment: "Autosens Min")
  113. }
  114. ),
  115. units: state.units,
  116. type: .decimal("autosensMin"),
  117. label: NSLocalizedString("Autosens Min", comment: "Autosens Min"),
  118. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  119. verboseHint: NSLocalizedString(
  120. "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.",
  121. comment: "Autosens Min"
  122. )
  123. )
  124. SettingInputSection(
  125. decimalValue: $decimalPlaceholder,
  126. booleanValue: $state.rewindResetsAutosens,
  127. shouldDisplayHint: $shouldDisplayHint,
  128. selectedVerboseHint: Binding(
  129. get: { selectedVerboseHint },
  130. set: {
  131. selectedVerboseHint = $0
  132. hintLabel = NSLocalizedString("Rewind Resets Autosens", comment: "Rewind Resets Autosens")
  133. }
  134. ),
  135. units: state.units,
  136. type: .boolean,
  137. label: NSLocalizedString("Rewind Resets Autosens", comment: "Rewind Resets Autosens"),
  138. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  139. verboseHint: NSLocalizedString(
  140. "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.",
  141. comment: "Rewind Resets Autosens"
  142. )
  143. )
  144. }
  145. .sheet(isPresented: $shouldDisplayHint) {
  146. SettingInputHintView(
  147. hintDetent: $hintDetent,
  148. shouldDisplayHint: $shouldDisplayHint,
  149. hintLabel: hintLabel ?? "",
  150. hintText: selectedVerboseHint ?? "",
  151. sheetTitle: "Help"
  152. )
  153. }
  154. .scrollContentBackground(.hidden).background(color)
  155. .onAppear(perform: configureView)
  156. .navigationTitle("Autosens")
  157. .navigationBarTitleDisplayMode(.automatic)
  158. .onDisappear {
  159. state.saveIfChanged()
  160. }
  161. }
  162. }
  163. }