DynamicSettingsRootView.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import SwiftUI
  2. import Swinject
  3. extension DynamicSettings {
  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: String?
  10. @State var hintLabel: String?
  11. @State private var decimalPlaceholder: Decimal = 0.0
  12. @State private var booleanPlaceholder: Bool = false
  13. private var conversionFormatter: NumberFormatter {
  14. let formatter = NumberFormatter()
  15. formatter.numberStyle = .decimal
  16. formatter.maximumFractionDigits = 1
  17. return formatter
  18. }
  19. @Environment(\.colorScheme) var colorScheme
  20. @Environment(AppState.self) var appState
  21. private var formatter: NumberFormatter {
  22. let formatter = NumberFormatter()
  23. formatter.numberStyle = .decimal
  24. return formatter
  25. }
  26. private var glucoseFormatter: NumberFormatter {
  27. let formatter = NumberFormatter()
  28. formatter.numberStyle = .decimal
  29. if state.units == .mmolL {
  30. formatter.maximumFractionDigits = 1
  31. } else { formatter.maximumFractionDigits = 0 }
  32. formatter.roundingMode = .halfUp
  33. return formatter
  34. }
  35. var body: some View {
  36. List {
  37. SettingInputSection(
  38. decimalValue: $decimalPlaceholder,
  39. booleanValue: $state.useNewFormula,
  40. shouldDisplayHint: $shouldDisplayHint,
  41. selectedVerboseHint: Binding(
  42. get: { selectedVerboseHint },
  43. set: {
  44. selectedVerboseHint = $0
  45. hintLabel = "Activate Dynamic Sensitivity (ISF)"
  46. }
  47. ),
  48. units: state.units,
  49. type: .boolean,
  50. label: "Activate Dynamic Sensitivity (ISF)",
  51. miniHint: "Trio calculates insulin sensitivity (ISF) each loop cycle based on current blood sugar, daily insulin use, and an adjustment factor, within set limits.",
  52. verboseHint: "DynamicISF",
  53. headerText: "Dynamic Insulin Sensitivity"
  54. )
  55. if state.useNewFormula {
  56. SettingInputSection(
  57. decimalValue: $decimalPlaceholder,
  58. booleanValue: $state.enableDynamicCR,
  59. shouldDisplayHint: $shouldDisplayHint,
  60. selectedVerboseHint: Binding(
  61. get: { selectedVerboseHint },
  62. set: {
  63. selectedVerboseHint = $0
  64. hintLabel = "Activate Dynamic Carb Ratio (CR)"
  65. }
  66. ),
  67. units: state.units,
  68. type: .boolean,
  69. label: "Activate Dynamic Carb Ratio (CR)",
  70. miniHint: "Similar to Dynamic Sensitivity, Trio calculates a dynamic carb ratio every loop cycle.",
  71. verboseHint: "Logarithmic Dynamic Insulin Sensitivity"
  72. )
  73. SettingInputSection(
  74. decimalValue: $decimalPlaceholder,
  75. booleanValue: $state.sigmoid,
  76. shouldDisplayHint: $shouldDisplayHint,
  77. selectedVerboseHint: Binding(
  78. get: { selectedVerboseHint },
  79. set: {
  80. selectedVerboseHint = $0
  81. hintLabel = "Use Sigmoid Formula"
  82. }
  83. ),
  84. units: state.units,
  85. type: .boolean,
  86. label: "Use Sigmoid Formula",
  87. miniHint: "Alternative formula for dynamic ISF, that alters ISF based on distance from target BG",
  88. verboseHint: "Sigmoid Dynamic Insulin Sensitivity"
  89. )
  90. if !state.sigmoid {
  91. SettingInputSection(
  92. decimalValue: $state.adjustmentFactor,
  93. booleanValue: $booleanPlaceholder,
  94. shouldDisplayHint: $shouldDisplayHint,
  95. selectedVerboseHint: Binding(
  96. get: { selectedVerboseHint },
  97. set: {
  98. selectedVerboseHint = $0
  99. hintLabel = "Adjustment Factor"
  100. }
  101. ),
  102. units: state.units,
  103. type: .decimal("adjustmentFactor"),
  104. label: "Adjustment Factor",
  105. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  106. verboseHint: "Adjustment Factor for logarithmic dynamic sensitvity... bla bla bla"
  107. )
  108. } else {
  109. SettingInputSection(
  110. decimalValue: $state.adjustmentFactorSigmoid,
  111. booleanValue: $booleanPlaceholder,
  112. shouldDisplayHint: $shouldDisplayHint,
  113. selectedVerboseHint: Binding(
  114. get: { selectedVerboseHint },
  115. set: {
  116. selectedVerboseHint = $0
  117. hintLabel = "Sigmoid Adjustment Factor"
  118. }
  119. ),
  120. units: state.units,
  121. type: .decimal("adjustmentFactorSigmoid"),
  122. label: "Sigmoid Adjustment Factor",
  123. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  124. verboseHint: "Sigmoid Adjustment Factor… should be 0.5… bla bla ba"
  125. )
  126. }
  127. SettingInputSection(
  128. decimalValue: $state.weightPercentage,
  129. booleanValue: $booleanPlaceholder,
  130. shouldDisplayHint: $shouldDisplayHint,
  131. selectedVerboseHint: Binding(
  132. get: { selectedVerboseHint },
  133. set: {
  134. selectedVerboseHint = $0
  135. hintLabel = "Weighted Average of TDD"
  136. }
  137. ),
  138. units: state.units,
  139. type: .decimal("weightPercentage"),
  140. label: "Weighted Average of TDD",
  141. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  142. verboseHint: "Weight of past 24 hours"
  143. )
  144. SettingInputSection(
  145. decimalValue: $decimalPlaceholder,
  146. booleanValue: $state.tddAdjBasal,
  147. shouldDisplayHint: $shouldDisplayHint,
  148. selectedVerboseHint: Binding(
  149. get: { selectedVerboseHint },
  150. set: {
  151. selectedVerboseHint = $0
  152. hintLabel = "Adjust Basal"
  153. }
  154. ),
  155. units: state.units,
  156. type: .boolean,
  157. label: "Adjust Basal",
  158. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  159. verboseHint: "Adjust basal dynamically… bla bla"
  160. )
  161. SettingInputSection(
  162. decimalValue: $state.threshold_setting,
  163. booleanValue: $booleanPlaceholder,
  164. shouldDisplayHint: $shouldDisplayHint,
  165. selectedVerboseHint: Binding(
  166. get: { selectedVerboseHint },
  167. set: {
  168. selectedVerboseHint = $0
  169. hintLabel = "Minimum Safety Threshold"
  170. }
  171. ),
  172. units: state.units,
  173. type: .decimal("threshold_setting"),
  174. label: "Minimum Safety Threshold",
  175. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  176. verboseHint: "Minimum Safety Threshold… bla bla bla"
  177. )
  178. }
  179. }
  180. .sheet(isPresented: $shouldDisplayHint) {
  181. SettingInputHintView(
  182. hintDetent: $hintDetent,
  183. shouldDisplayHint: $shouldDisplayHint,
  184. hintLabel: hintLabel ?? "",
  185. hintText: selectedVerboseHint ?? "",
  186. sheetTitle: "Help"
  187. )
  188. }
  189. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  190. .onAppear(perform: configureView)
  191. .navigationBarTitle("Dynamic Settings")
  192. .navigationBarTitleDisplayMode(.automatic)
  193. }
  194. }
  195. }