DynamicSettingsRootView.swift 9.7 KB

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