AlgorithmSettingsSubstepView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // AlgorithmSettingsSubstepView.swift
  3. // Trio
  4. //
  5. // Created by Cengiz Deniz on 15.04.25.
  6. //
  7. import SwiftUI
  8. struct AlgorithmSettingsSubstepView<Substep: AlgorithmSubstepProtocol & RawRepresentable>: View where Substep.RawValue == Int {
  9. @Bindable var state: Onboarding.StateModel
  10. let substep: Substep
  11. @State private var shouldDisplayPicker: Bool = false
  12. @State private var decimalPlaceholder: Decimal = 0.0
  13. @State private var booleanPlaceholder: Bool = false
  14. private let settingsProvider = PickerSettingsProvider.shared
  15. var body: some View {
  16. VStack(alignment: .leading, spacing: 16) {
  17. Text(substep.title)
  18. .padding(.horizontal)
  19. .font(.title3)
  20. .bold()
  21. Text(substep.hint(units: state.units))
  22. .font(.subheadline)
  23. .multilineTextAlignment(.leading)
  24. .padding(.horizontal)
  25. .foregroundStyle(Color.secondary)
  26. if let step = substep.toAlgorithmSubstep() {
  27. switch step {
  28. case .autosensMin:
  29. algorithmSettingsInput(
  30. label: step.title,
  31. displayPicker: $shouldDisplayPicker,
  32. setting: settingsProvider.settings.autosensMin,
  33. decimalValue: $state.autosensMin,
  34. booleanValue: $booleanPlaceholder,
  35. type: OnboardingInputSectionType.decimal
  36. )
  37. case .autosensMax:
  38. algorithmSettingsInput(
  39. label: substep.title,
  40. displayPicker: $shouldDisplayPicker,
  41. setting: settingsProvider.settings.autosensMax,
  42. decimalValue: $state.autosensMax,
  43. booleanValue: $booleanPlaceholder,
  44. type: OnboardingInputSectionType.decimal
  45. )
  46. case .rewindResetsAutosens:
  47. algorithmSettingsInput(
  48. label: substep.title,
  49. displayPicker: $shouldDisplayPicker,
  50. setting: nil,
  51. decimalValue: $decimalPlaceholder,
  52. booleanValue: $state.rewindResetsAutosens,
  53. type: OnboardingInputSectionType.boolean
  54. )
  55. case .enableSMBAlways:
  56. algorithmSettingsInput(
  57. label: substep.title,
  58. displayPicker: $shouldDisplayPicker,
  59. setting: nil,
  60. decimalValue: $decimalPlaceholder,
  61. booleanValue: $state.enableSMBAlways,
  62. type: OnboardingInputSectionType.boolean
  63. )
  64. case .enableSMBWithCOB:
  65. algorithmSettingsInput(
  66. label: substep.title,
  67. displayPicker: $shouldDisplayPicker,
  68. setting: nil,
  69. decimalValue: $decimalPlaceholder,
  70. booleanValue: $state.enableSMBWithCOB,
  71. type: OnboardingInputSectionType.boolean,
  72. disabled: state.enableSMBAlways
  73. )
  74. case .enableSMBWithTempTarget:
  75. algorithmSettingsInput(
  76. label: substep.title,
  77. displayPicker: $shouldDisplayPicker,
  78. setting: nil,
  79. decimalValue: $decimalPlaceholder,
  80. booleanValue: $state.enableSMBWithTempTarget,
  81. type: OnboardingInputSectionType.boolean,
  82. disabled: state.enableSMBAlways
  83. )
  84. case .enableSMBAfterCarbs:
  85. algorithmSettingsInput(
  86. label: substep.title,
  87. displayPicker: $shouldDisplayPicker,
  88. setting: nil,
  89. decimalValue: $decimalPlaceholder,
  90. booleanValue: $state.enableSMBAfterCarbs,
  91. type: OnboardingInputSectionType.boolean,
  92. disabled: state.enableSMBAlways
  93. )
  94. case .enableSMBWithHighGlucoseTarget:
  95. algorithmSettingsInput(
  96. label: substep.title,
  97. displayPicker: $shouldDisplayPicker,
  98. setting: nil,
  99. decimalValue: $decimalPlaceholder,
  100. booleanValue: $state.enableSMBWithHighGlucoseTarget,
  101. type: OnboardingInputSectionType.boolean,
  102. disabled: state.enableSMBAlways
  103. )
  104. if state.enableSMBWithHighGlucoseTarget {
  105. algorithmSettingsInput(
  106. label: String(localized: "High Glucose Target"),
  107. displayPicker: $shouldDisplayPicker,
  108. setting: settingsProvider.settings.enableSMB_high_bg_target,
  109. decimalValue: $state.highGlucoseTarget,
  110. booleanValue: $booleanPlaceholder,
  111. type: OnboardingInputSectionType.decimal,
  112. disabled: state.enableSMBAlways
  113. )
  114. }
  115. case .allowSMBWithHighTempTarget:
  116. algorithmSettingsInput(
  117. label: substep.title,
  118. displayPicker: $shouldDisplayPicker,
  119. setting: nil,
  120. decimalValue: $decimalPlaceholder,
  121. booleanValue: $state.allowSMBWithHighTempTarget,
  122. type: OnboardingInputSectionType.boolean
  123. )
  124. case .enableUAM:
  125. algorithmSettingsInput(
  126. label: substep.title,
  127. displayPicker: $shouldDisplayPicker,
  128. setting: nil,
  129. decimalValue: $decimalPlaceholder,
  130. booleanValue: $state.enableUAM,
  131. type: OnboardingInputSectionType.boolean
  132. )
  133. case .maxSMBMinutes:
  134. algorithmSettingsInput(
  135. label: substep.title,
  136. displayPicker: $shouldDisplayPicker,
  137. setting: settingsProvider.settings.maxSMBBasalMinutes,
  138. decimalValue: $state.maxSMBMinutes,
  139. booleanValue: $booleanPlaceholder,
  140. type: OnboardingInputSectionType.decimal
  141. )
  142. case .maxUAMMinutes:
  143. algorithmSettingsInput(
  144. label: substep.title,
  145. displayPicker: $shouldDisplayPicker,
  146. setting: settingsProvider.settings.maxUAMSMBBasalMinutes,
  147. decimalValue: $state.maxUAMMinutes,
  148. booleanValue: $booleanPlaceholder,
  149. type: OnboardingInputSectionType.decimal
  150. )
  151. case .maxDeltaGlucoseThreshold:
  152. algorithmSettingsInput(
  153. label: substep.title,
  154. displayPicker: $shouldDisplayPicker,
  155. setting: settingsProvider.settings.maxDeltaBGthreshold,
  156. decimalValue: $state.maxDeltaGlucoseThreshold,
  157. booleanValue: $booleanPlaceholder,
  158. type: OnboardingInputSectionType.decimal
  159. )
  160. case .highTempTargetRaisesSensitivity:
  161. algorithmSettingsInput(
  162. label: substep.title,
  163. displayPicker: $shouldDisplayPicker,
  164. setting: nil,
  165. decimalValue: $decimalPlaceholder,
  166. booleanValue: $state.highTempTargetRaisesSensitivity,
  167. type: OnboardingInputSectionType.boolean
  168. )
  169. case .lowTempTargetLowersSensitivity:
  170. algorithmSettingsInput(
  171. label: substep.title,
  172. displayPicker: $shouldDisplayPicker,
  173. setting: nil,
  174. decimalValue: $decimalPlaceholder,
  175. booleanValue: $state.lowTempTargetLowersSensitivity,
  176. type: OnboardingInputSectionType.boolean
  177. )
  178. case .sensitivityRaisesTarget:
  179. algorithmSettingsInput(
  180. label: substep.title,
  181. displayPicker: $shouldDisplayPicker,
  182. setting: nil,
  183. decimalValue: $decimalPlaceholder,
  184. booleanValue: $state.sensitivityRaisesTarget,
  185. type: OnboardingInputSectionType.boolean
  186. )
  187. case .resistanceLowersTarget:
  188. algorithmSettingsInput(
  189. label: substep.title,
  190. displayPicker: $shouldDisplayPicker,
  191. setting: nil,
  192. decimalValue: $decimalPlaceholder,
  193. booleanValue: $state.resistanceLowersTarget,
  194. type: OnboardingInputSectionType.boolean
  195. )
  196. case .halfBasalTarget:
  197. algorithmSettingsInput(
  198. label: substep.title,
  199. displayPicker: $shouldDisplayPicker,
  200. setting: settingsProvider.settings.halfBasalExerciseTarget,
  201. decimalValue: $state.halfBasalTarget,
  202. booleanValue: $booleanPlaceholder,
  203. type: OnboardingInputSectionType.decimal
  204. )
  205. }
  206. }
  207. substep.description(units: state.units).eraseToAnyView()
  208. .font(.footnote)
  209. .foregroundStyle(.secondary)
  210. .padding(.horizontal)
  211. .multilineTextAlignment(.leading)
  212. }
  213. .onAppear {
  214. // Ensure picker view is closed, when switching between setting steps
  215. shouldDisplayPicker = false
  216. }
  217. }
  218. @ViewBuilder private func algorithmSettingsInput(
  219. label: String,
  220. displayPicker: Binding<Bool>,
  221. setting: PickerSetting?,
  222. decimalValue: Binding<Decimal>,
  223. booleanValue: Binding<Bool>,
  224. type: OnboardingInputSectionType,
  225. disabled: Bool = false /// parameter only relevant for `Enable SMB Always` dependent settings
  226. ) -> some View {
  227. VStack {
  228. VStack {
  229. switch type {
  230. case .boolean:
  231. Toggle(isOn: booleanValue) {
  232. Text(label)
  233. }.tint(Color.accentColor)
  234. .disabled(disabled)
  235. case .decimal:
  236. Group {
  237. HStack {
  238. Text(label)
  239. Spacer()
  240. displayText(for: substep, decimalValue: decimalValue.wrappedValue, units: state.units)
  241. .foregroundColor(!displayPicker.wrappedValue ? .primary : .accentColor)
  242. .onTapGesture {
  243. displayPicker.wrappedValue.toggle()
  244. }
  245. }.disabled(disabled)
  246. if displayPicker.wrappedValue {
  247. Picker(selection: decimalValue, label: Text(label)) {
  248. if let setting = setting {
  249. ForEach(
  250. settingsProvider.generatePickerValues(from: setting, units: state.units),
  251. id: \.self
  252. ) { value in
  253. displayText(for: substep, decimalValue: value, units: state.units).tag(value)
  254. }
  255. }
  256. }
  257. .disabled(disabled)
  258. .pickerStyle(WheelPickerStyle())
  259. .frame(maxWidth: .infinity)
  260. }
  261. }
  262. }
  263. }
  264. .padding()
  265. .background(Color.chart.opacity(0.65))
  266. .cornerRadius(10)
  267. }
  268. }
  269. private func displayText(for substep: Substep, decimalValue: Decimal, units: GlucoseUnits) -> Text {
  270. guard let step = substep.toAlgorithmSubstep() else {
  271. return Text(decimalValue.description)
  272. }
  273. switch step {
  274. case .autosensMax,
  275. .autosensMin,
  276. .maxDeltaGlucoseThreshold:
  277. return Text("\(decimalValue * 100) \(String(localized: "%"))")
  278. case .enableSMBWithHighGlucoseTarget,
  279. .halfBasalTarget:
  280. let displayValue = units == .mmolL ? decimalValue.asMmolL : decimalValue
  281. return Text("\(displayValue.description) \(units.rawValue)")
  282. case .maxSMBMinutes,
  283. .maxUAMMinutes:
  284. return Text("\(decimalValue) \(String(localized: "min"))")
  285. default:
  286. return Text("") // not needed, because input type is boolean
  287. }
  288. }
  289. }