TargetBehavoirRootView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import SwiftUI
  2. import Swinject
  3. extension TargetBehavoir {
  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. 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. var body: some View {
  32. List {
  33. SettingInputSection(
  34. decimalValue: $decimalPlaceholder,
  35. booleanValue: $state.highTemptargetRaisesSensitivity,
  36. shouldDisplayHint: $shouldDisplayHint,
  37. selectedVerboseHint: Binding(
  38. get: { selectedVerboseHint },
  39. set: {
  40. selectedVerboseHint = $0.map { AnyView($0) }
  41. hintLabel = NSLocalizedString(
  42. "High Temp Target Raises Sensitivity",
  43. comment: "High Temp Target Raises Sensitivity"
  44. )
  45. }
  46. ),
  47. units: state.units,
  48. type: .boolean,
  49. label: NSLocalizedString(
  50. "High Temp Target Raises Sensitivity",
  51. comment: "High Temp Target Raises Sensitivity"
  52. ),
  53. miniHint: "A Temp Target > \(state.units == .mgdL ? "110" : 110.formattedAsMmol ?? "110") \(state.units.rawValue) increases sensitivity when glucose is above target.",
  54. verboseHint:
  55. VStack(alignment: .leading, spacing: 10) {
  56. Text("Default: OFF").bold()
  57. Text(
  58. "When this feature is enabled, setting a temporary target above \(state.units == .mgdL ? "110" : 110.formattedAsMmol ?? "110") \(state.units.rawValue) will decrease the Autosens Ratio used for ISF and basal adjustments, resulting in less insulin delivered overall. This scales with the temporary target set; the higher the temp target, the lower the Autosens Ratio used."
  59. )
  60. Text(
  61. "If Half Basal Exercise Target is set to \(state.units == .mgdL ? "160" : 160.formattedAsMmol ?? "160") \(state.units.rawValue), a temp target of \(state.units == .mgdL ? "120" : 120.formattedAsMmol ?? "120") \(state.units.rawValue) uses an Autosens Ratio of 0.75. A temp target of \(state.units == .mgdL ? "140" : 140.formattedAsMmol ?? "140") \(state.units.rawValue) uses an Autosens Ratio of 0.6."
  62. )
  63. Text("Note: The effect of this can be adjusted with the Half Basal Exercise Target")
  64. },
  65. headerText: "Algorithmic Target Settings"
  66. )
  67. SettingInputSection(
  68. decimalValue: $decimalPlaceholder,
  69. booleanValue: $state.lowTemptargetLowersSensitivity,
  70. shouldDisplayHint: $shouldDisplayHint,
  71. selectedVerboseHint: Binding(
  72. get: { selectedVerboseHint },
  73. set: {
  74. selectedVerboseHint = $0.map { AnyView($0) }
  75. hintLabel = NSLocalizedString(
  76. "Low Temp Target Lowers Sensitivity",
  77. comment: "Low Temp Target Lowers Sensitivity"
  78. )
  79. }
  80. ),
  81. units: state.units,
  82. type: .boolean,
  83. label: NSLocalizedString(
  84. "Low Temp Target Lowers Sensitivity",
  85. comment: "Low Temp Target Lowers Sensitivity"
  86. ),
  87. miniHint: "Temp Target < \(state.units == .mgdL ? "100" : 100.formattedAsMmol ?? "100") \(state.units.rawValue) decreases sensitivity when glucose is below target.",
  88. verboseHint:
  89. VStack(alignment: .leading, spacing: 10) {
  90. Text("Default: OFF").bold()
  91. Text(
  92. "When this feature is enabled, setting a temporary target below \(state.units == .mgdL ? "100" : 100.formattedAsMmol ?? "100") \(state.units.rawValue) will increase the Autosens Ratio used for ISF and basal adjustments, resulting in more insulin delivered overall. This scales with the temporary target set; the lower the Temp Target, the higher the Autosens Ratio used."
  93. )
  94. Text(
  95. "If Half Basal Exercise Target is \(state.units == .mgdL ? "160" : 160.formattedAsMmol ?? "160") \(state.units.rawValue), a Temp Target of \(state.units == .mgdL ? "95" : 95.formattedAsMmol ?? "95") \(state.units.rawValue) uses an Autosens Ratio of 1.09. A Temp Target of \(state.units == .mgdL ? "85" : 85.formattedAsMmol ?? "85") \(state.units.rawValue) uses an Autosens Ratio of 1.33."
  96. )
  97. Text("Note: The effect of this can be adjusted with the Half Basal Exercise Target")
  98. }
  99. )
  100. SettingInputSection(
  101. decimalValue: $decimalPlaceholder,
  102. booleanValue: $state.sensitivityRaisesTarget,
  103. shouldDisplayHint: $shouldDisplayHint,
  104. selectedVerboseHint: Binding(
  105. get: { selectedVerboseHint },
  106. set: {
  107. selectedVerboseHint = $0.map { AnyView($0) }
  108. hintLabel = NSLocalizedString("Sensitivity Raises Target", comment: "Sensitivity Raises Target")
  109. }
  110. ),
  111. units: state.units,
  112. type: .boolean,
  113. label: NSLocalizedString("Sensitivity Raises Target", comment: "Sensitivity Raises Target"),
  114. miniHint: "Automatically raise target glucose if sensitivity is detected.",
  115. verboseHint: VStack(spacing: 10) {
  116. Text("Default: OFF").bold()
  117. Text("Automatically increase target glucose if it detects an increase in sensitivity.")
  118. }
  119. )
  120. SettingInputSection(
  121. decimalValue: $decimalPlaceholder,
  122. booleanValue: $state.resistanceLowersTarget,
  123. shouldDisplayHint: $shouldDisplayHint,
  124. selectedVerboseHint: Binding(
  125. get: { selectedVerboseHint },
  126. set: {
  127. selectedVerboseHint = $0.map { AnyView($0) }
  128. hintLabel = NSLocalizedString("Resistance Lowers Target", comment: "Resistance Lowers Target")
  129. }
  130. ),
  131. units: state.units,
  132. type: .boolean,
  133. label: NSLocalizedString("Resistance Lowers Target", comment: "Resistance Lowers Target"),
  134. miniHint: "Automatically lower target glucose if resistance is detected.",
  135. verboseHint: VStack(spacing: 10) {
  136. Text("Default: OFF").bold()
  137. Text(
  138. "Enabling this feature causes Trio to automatically reduce the targeted glucose if it detects a decrease in sensitivity (resistance)."
  139. )
  140. }
  141. )
  142. SettingInputSection(
  143. decimalValue: $state.halfBasalExerciseTarget,
  144. booleanValue: $booleanPlaceholder,
  145. shouldDisplayHint: $shouldDisplayHint,
  146. selectedVerboseHint: Binding(
  147. get: { selectedVerboseHint },
  148. set: {
  149. selectedVerboseHint = $0.map { AnyView($0) }
  150. hintLabel = NSLocalizedString("Half Basal Exercise Target", comment: "Half Basal Exercise Target")
  151. }
  152. ),
  153. units: state.units,
  154. type: .decimal("halfBasalExerciseTarget"),
  155. label: NSLocalizedString("Half Basal Exercise Target", comment: "Half Basal Exercise Target"),
  156. miniHint: "Scales down your basal rate to 50% at this value.",
  157. verboseHint:
  158. VStack(alignment: .leading, spacing: 10) {
  159. Text(
  160. "Default: \(state.units == .mgdL ? "160" : 160.formattedAsMmol ?? "160") \(state.units.rawValue)"
  161. )
  162. .bold()
  163. Text(
  164. "The Half Basal Exercise Target allows you to scale down your basal insulin during exercise or scale up your basal insulin when eating soon when a temporary glucose target is set."
  165. )
  166. Text(
  167. "For example, at a temp target of \(state.units == .mgdL ? "160" : 160.formattedAsMmol ?? "160") \(state.units.rawValue), your basal is reduced to 50%, but this scales depending on the target (e.g., 75% at \(state.units == .mgdL ? "120" : 120.formattedAsMmol ?? "120") \(state.units.rawValue), 60% at \(state.units == .mgdL ? "140" : 140.formattedAsMmol ?? "140") \(state.units.rawValue))."
  168. )
  169. Text(
  170. "Note: This setting is only utilized if the settings \"Low Temp Target Lowers Sensitivity\" OR \"High Temp Target Raises Sensitivity\" are enabled."
  171. )
  172. }
  173. )
  174. }
  175. .listSectionSpacing(sectionSpacing)
  176. .sheet(isPresented: $shouldDisplayHint) {
  177. SettingInputHintView(
  178. hintDetent: $hintDetent,
  179. shouldDisplayHint: $shouldDisplayHint,
  180. hintLabel: hintLabel ?? "",
  181. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  182. sheetTitle: "Help"
  183. )
  184. }
  185. .scrollContentBackground(.hidden).background(color)
  186. .onAppear(perform: configureView)
  187. .navigationTitle("Target Behavior")
  188. .navigationBarTitleDisplayMode(.automatic)
  189. .onDisappear {
  190. state.saveIfChanged()
  191. }
  192. }
  193. }
  194. }