EditOverrideForm.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. import Foundation
  2. import SwiftUI
  3. struct EditOverrideForm: View {
  4. @ObservedObject var override: OverrideStored
  5. @Environment(\.presentationMode) var presentationMode
  6. @Environment(\.colorScheme) var colorScheme
  7. @Bindable var state: OverrideConfig.StateModel
  8. @State private var name: String
  9. @State private var percentage: Double
  10. @State private var indefinite: Bool
  11. @State private var duration: Decimal
  12. @State private var target: Decimal?
  13. @State private var advancedSettings: Bool
  14. @State private var smbIsOff: Bool
  15. @State private var smbIsScheduledOff: Bool
  16. @State private var start: Decimal?
  17. @State private var end: Decimal?
  18. @State private var isfAndCr: Bool
  19. @State private var isf: Bool
  20. @State private var cr: Bool
  21. @State private var smbMinutes: Decimal?
  22. @State private var uamMinutes: Decimal?
  23. @State private var selectedIsfCrOption: IsfAndOrCrOptions
  24. @State private var selectedDisableSmbOption: DisableSmbOptions
  25. @State private var hasChanges = false
  26. @State private var isEditing = false
  27. @State private var target_override = false
  28. @State private var percentageStep: Int = 5
  29. @State private var displayPickerPercentage: Bool = false
  30. @State private var displayPickerDuration: Bool = false
  31. @State private var targetStep: Decimal = 5
  32. @State private var displayPickerTarget: Bool = false
  33. @State private var displayPickerDisableSmbSchedule: Bool = false
  34. @State private var displayPickerSmbMinutes: Bool = false
  35. init(overrideToEdit: OverrideStored, state: OverrideConfig.StateModel) {
  36. override = overrideToEdit
  37. _state = Bindable(wrappedValue: state)
  38. _name = State(initialValue: overrideToEdit.name ?? "")
  39. _percentage = State(initialValue: overrideToEdit.percentage)
  40. _indefinite = State(initialValue: overrideToEdit.indefinite)
  41. _duration = State(initialValue: overrideToEdit.duration?.decimalValue ?? 0)
  42. _target = State(initialValue: overrideToEdit.target?.decimalValue)
  43. _target_override = State(initialValue: overrideToEdit.target?.decimalValue != 0)
  44. _advancedSettings = State(initialValue: overrideToEdit.advancedSettings)
  45. _smbIsOff = State(initialValue: overrideToEdit.smbIsOff)
  46. _smbIsScheduledOff = State(initialValue: overrideToEdit.smbIsScheduledOff)
  47. _start = State(initialValue: overrideToEdit.start?.decimalValue)
  48. _end = State(initialValue: overrideToEdit.end?.decimalValue)
  49. _isfAndCr = State(initialValue: overrideToEdit.isfAndCr)
  50. _isf = State(initialValue: overrideToEdit.isf)
  51. _cr = State(initialValue: overrideToEdit.cr)
  52. _selectedIsfCrOption = State(
  53. initialValue: overrideToEdit.isfAndCr ? .isfAndCr
  54. : (overrideToEdit.isf ? .isf : (overrideToEdit.cr ? .cr : .nothing))
  55. )
  56. _selectedDisableSmbOption = State(
  57. initialValue: overrideToEdit.smbIsScheduledOff ? .disableOnSchedule
  58. : (overrideToEdit.smbIsOff ? .disable : .dontDisable)
  59. )
  60. _smbMinutes = State(initialValue: overrideToEdit.smbMinutes?.decimalValue)
  61. _uamMinutes = State(initialValue: overrideToEdit.uamMinutes?.decimalValue)
  62. }
  63. var color: LinearGradient {
  64. colorScheme == .dark ? LinearGradient(
  65. gradient: Gradient(colors: [
  66. Color.bgDarkBlue,
  67. Color.bgDarkerDarkBlue
  68. ]),
  69. startPoint: .top,
  70. endPoint: .bottom
  71. ) :
  72. LinearGradient(
  73. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  74. startPoint: .top,
  75. endPoint: .bottom
  76. )
  77. }
  78. private var formatter: NumberFormatter {
  79. let formatter = NumberFormatter()
  80. formatter.numberStyle = .decimal
  81. formatter.maximumFractionDigits = 0
  82. return formatter
  83. }
  84. private var percentageSelection: Binding<Double> {
  85. Binding<Double>(
  86. get: {
  87. let value = floor(percentage / Double(percentageStep)) * Double(percentageStep)
  88. return max(10, min(value, 200))
  89. },
  90. set: {
  91. percentage = $0
  92. hasChanges = true
  93. }
  94. )
  95. }
  96. var body: some View {
  97. NavigationView {
  98. List {
  99. editOverride()
  100. saveButton
  101. }
  102. .listSectionSpacing(10)
  103. .padding(.top, 30)
  104. .ignoresSafeArea(edges: .top)
  105. .scrollContentBackground(.hidden).background(color)
  106. .navigationTitle("Edit Override")
  107. .navigationBarTitleDisplayMode(.inline)
  108. .toolbar {
  109. ToolbarItem(placement: .topBarLeading) {
  110. Button(action: {
  111. presentationMode.wrappedValue.dismiss()
  112. }, label: {
  113. Text("Cancel")
  114. })
  115. }
  116. ToolbarItem(placement: .topBarTrailing) {
  117. Button(
  118. action: {
  119. state.isHelpSheetPresented.toggle()
  120. },
  121. label: {
  122. Image(systemName: "questionmark.circle")
  123. }
  124. )
  125. }
  126. }
  127. .onAppear { targetStep = state.units == .mgdL ? 5 : 9 }
  128. .onDisappear {
  129. if !hasChanges {
  130. // Reset UI changes
  131. resetValues()
  132. }
  133. }
  134. .sheet(isPresented: $state.isHelpSheetPresented) {
  135. NavigationStack {
  136. List {
  137. Text("Lorem Ipsum Dolor Sit Amet")
  138. }
  139. .padding(.trailing, 10)
  140. .navigationBarTitle("Help", displayMode: .inline)
  141. Button { state.isHelpSheetPresented.toggle() }
  142. label: { Text("Got it!").frame(maxWidth: .infinity, alignment: .center) }
  143. .buttonStyle(.bordered)
  144. .padding(.top)
  145. }
  146. .padding()
  147. .presentationDetents(
  148. [.fraction(0.9), .large],
  149. selection: $state.helpSheetDetent
  150. )
  151. }
  152. }
  153. }
  154. @ViewBuilder private func editOverride() -> some View {
  155. Group {
  156. if override.name != nil {
  157. Section {
  158. HStack {
  159. Text("Name")
  160. Spacer()
  161. TextField("Name", text: $name)
  162. .onChange(of: name) { hasChanges = true }
  163. .multilineTextAlignment(.trailing)
  164. }
  165. }
  166. .listRowBackground(Color.chart)
  167. }
  168. Section {
  169. Toggle(isOn: $indefinite) { Text("Enable Indefinitely") }
  170. .onChange(of: indefinite) { hasChanges = true }
  171. if !indefinite {
  172. HStack {
  173. Text("Duration")
  174. Spacer()
  175. Text(formatHrMin(Int(truncating: duration as NSNumber)))
  176. .foregroundColor(!displayPickerDuration ? .primary : .accentColor)
  177. }
  178. .onTapGesture {
  179. displayPickerDuration = toggleScrollWheel(displayPickerDuration)
  180. }
  181. if displayPickerDuration {
  182. HStack {
  183. Picker(
  184. selection: Binding(
  185. get: {
  186. Int(truncating: duration as NSNumber) / 60
  187. },
  188. set: {
  189. let minutes = Int(truncating: duration as NSNumber) % 60
  190. let totalMinutes = $0 * 60 + minutes
  191. duration = Decimal(totalMinutes)
  192. hasChanges = true
  193. }
  194. ),
  195. label: Text("")
  196. ) {
  197. ForEach(0 ..< 24) { hour in
  198. Text("\(hour) hr").tag(hour)
  199. }
  200. }
  201. .pickerStyle(WheelPickerStyle())
  202. .frame(maxWidth: .infinity)
  203. Picker(
  204. selection: Binding(
  205. get: {
  206. Int(truncating: duration as NSNumber) %
  207. 60 // Convert Decimal to Int for modulus operation
  208. },
  209. set: {
  210. duration = Decimal((Int(truncating: duration as NSNumber) / 60) * 60 + $0)
  211. hasChanges = true
  212. }
  213. ),
  214. label: Text("")
  215. ) {
  216. ForEach(Array(stride(from: 0, through: 55, by: 5)), id: \.self) { minute in
  217. Text("\(minute) min").tag(minute)
  218. }
  219. }
  220. .pickerStyle(WheelPickerStyle())
  221. .frame(maxWidth: .infinity)
  222. }
  223. .listRowSeparator(.hidden, edges: .top)
  224. }
  225. }
  226. }
  227. .listRowBackground(Color.chart)
  228. // Percentage Picker
  229. Section(footer: percentageDescription(percentage)) {
  230. HStack {
  231. Text("Change Basal Rate by")
  232. Spacer()
  233. Text("\(percentage.formatted(.number)) %")
  234. .foregroundColor(!displayPickerPercentage ? .primary : .accentColor)
  235. }
  236. .onTapGesture {
  237. displayPickerPercentage = toggleScrollWheel(displayPickerPercentage)
  238. }
  239. if displayPickerPercentage {
  240. HStack {
  241. // Radio buttons and text on the left side
  242. VStack(alignment: .leading) {
  243. // Radio buttons for step iteration
  244. ForEach([1, 5], id: \.self) { step in
  245. RadioButton(isSelected: percentageStep == step, label: "\(step) %") {
  246. percentageStep = step
  247. percentage = OverrideConfig.StateModel.roundOverridePercentageToStep(percentage, step)
  248. }
  249. .padding(.top, 10)
  250. }
  251. }
  252. .frame(maxWidth: .infinity)
  253. Spacer()
  254. // Picker on the right side
  255. Picker(
  256. selection: percentageSelection,
  257. label: Text("")
  258. ) {
  259. ForEach(
  260. Array(stride(from: 40.0, through: 150.0, by: Double(percentageStep))),
  261. id: \.self
  262. ) { percent in
  263. Text("\(Int(percent)) %").tag(percent)
  264. }
  265. }
  266. .pickerStyle(WheelPickerStyle())
  267. .frame(maxWidth: .infinity)
  268. }
  269. .listRowSeparator(.hidden, edges: .top)
  270. }
  271. // Picker for ISF/CR settings
  272. Picker("Also Change", selection: $selectedIsfCrOption) {
  273. ForEach(IsfAndOrCrOptions.allCases, id: \.self) { option in
  274. Text(option.rawValue).tag(option)
  275. }
  276. }
  277. .pickerStyle(MenuPickerStyle())
  278. .onChange(of: selectedIsfCrOption) { _, newValue in
  279. switch newValue {
  280. case .isfAndCr:
  281. isfAndCr = true
  282. isf = false
  283. cr = false
  284. case .isf:
  285. isfAndCr = false
  286. isf = true
  287. cr = false
  288. case .cr:
  289. isfAndCr = false
  290. isf = false
  291. cr = true
  292. case .nothing:
  293. isfAndCr = false
  294. isf = false
  295. cr = false
  296. }
  297. hasChanges = true
  298. }
  299. }
  300. .listRowBackground(Color.chart)
  301. Section {
  302. Toggle(isOn: $target_override) {
  303. Text("Override Target")
  304. }
  305. .onChange(of: target_override) {
  306. hasChanges = true
  307. }
  308. // Target Glucose Picker
  309. if target_override {
  310. let settingsProvider = PickerSettingsProvider.shared
  311. let glucoseSetting = PickerSetting(value: 0, step: targetStep, min: 72, max: 270, type: .glucose)
  312. TargetPicker(
  313. label: "Target Glucose",
  314. selection: Binding(
  315. get: { target ?? 100 },
  316. set: { target = $0 }
  317. ),
  318. options: settingsProvider.generatePickerValues(from: glucoseSetting, units: state.units, roundMinToStep: true),
  319. units: state.units,
  320. hasChanges: $hasChanges,
  321. targetStep: $targetStep,
  322. displayPickerTarget: $displayPickerTarget,
  323. toggleScrollWheel: toggleScrollWheel
  324. )
  325. }
  326. }
  327. .listRowBackground(Color.chart)
  328. Section {
  329. // Picker for Disable SMB settings
  330. Picker("Disable SMBs", selection: $selectedDisableSmbOption) {
  331. ForEach(DisableSmbOptions.allCases, id: \.self) { option in
  332. Text(option.rawValue).tag(option)
  333. }
  334. }
  335. .pickerStyle(MenuPickerStyle())
  336. .onChange(of: selectedDisableSmbOption) { _, newValue in
  337. switch newValue {
  338. case .dontDisable:
  339. smbIsOff = false
  340. smbIsScheduledOff = false
  341. case .disable:
  342. smbIsOff = true
  343. smbIsScheduledOff = false
  344. case .disableOnSchedule:
  345. smbIsOff = false
  346. smbIsScheduledOff = true
  347. }
  348. hasChanges = true
  349. }
  350. if smbIsScheduledOff {
  351. // First Hour SMBs Are Disabled
  352. HStack {
  353. Text("From")
  354. Spacer()
  355. Text(
  356. is24HourFormat() ? format24Hour(Int(truncating: start! as NSNumber)) + ":00" :
  357. convertTo12HourFormat(Int(truncating: start! as NSNumber))
  358. )
  359. .foregroundColor(!displayPickerDisableSmbSchedule ? .primary : .accentColor)
  360. Spacer()
  361. Divider().frame(width: 1, height: 20)
  362. Spacer()
  363. Text("To")
  364. Spacer()
  365. Text(
  366. is24HourFormat() ? format24Hour(Int(truncating: end! as NSNumber)) + ":00" :
  367. convertTo12HourFormat(Int(truncating: end! as NSNumber))
  368. )
  369. .foregroundColor(!displayPickerDisableSmbSchedule ? .primary : .accentColor)
  370. }
  371. .onTapGesture {
  372. displayPickerDisableSmbSchedule = toggleScrollWheel(displayPickerDisableSmbSchedule)
  373. }
  374. if displayPickerDisableSmbSchedule {
  375. HStack {
  376. Picker(selection: Binding(
  377. get: { Int(truncating: start! as NSNumber) },
  378. set: {
  379. start = Decimal($0)
  380. hasChanges = true
  381. }
  382. ), label: Text("")) {
  383. if is24HourFormat() {
  384. ForEach(0 ..< 24, id: \.self) { hour in
  385. Text(format24Hour(hour) + ":00").tag(hour)
  386. }
  387. } else {
  388. ForEach(0 ..< 24, id: \.self) { hour in
  389. Text(convertTo12HourFormat(hour)).tag(hour)
  390. }
  391. }
  392. }
  393. .pickerStyle(WheelPickerStyle())
  394. .frame(maxWidth: .infinity)
  395. Picker(selection: Binding(
  396. get: { Int(truncating: end! as NSNumber) },
  397. set: {
  398. end = Decimal($0)
  399. hasChanges = true
  400. }
  401. ), label: Text("")) {
  402. if is24HourFormat() {
  403. ForEach(0 ..< 24, id: \.self) { hour in
  404. Text(format24Hour(hour) + ":00").tag(hour)
  405. }
  406. } else {
  407. ForEach(0 ..< 24, id: \.self) { hour in
  408. Text(convertTo12HourFormat(hour)).tag(hour)
  409. }
  410. }
  411. }
  412. .pickerStyle(WheelPickerStyle())
  413. .frame(maxWidth: .infinity)
  414. }
  415. .listRowSeparator(.hidden, edges: .top)
  416. }
  417. }
  418. }
  419. .listRowBackground(Color.chart)
  420. if !smbIsOff {
  421. Section {
  422. Toggle(isOn: $advancedSettings) {
  423. Text("Change Max SMB Minutes")
  424. }
  425. .onChange(of: advancedSettings) { hasChanges = true }
  426. if advancedSettings {
  427. // SMB Minutes Picker
  428. HStack {
  429. Text("SMB")
  430. Spacer()
  431. Text("\(smbMinutes?.formatted(.number) ?? "\(state.defaultSmbMinutes)") min")
  432. .foregroundColor(!displayPickerSmbMinutes ? .primary : .accentColor)
  433. Spacer()
  434. Divider().frame(width: 1, height: 20)
  435. Spacer()
  436. Text("UAM")
  437. Spacer()
  438. Text("\(uamMinutes?.formatted(.number) ?? "\(state.defaultUamMinutes)") min")
  439. .foregroundColor(!displayPickerSmbMinutes ? .primary : .accentColor)
  440. }
  441. .onTapGesture {
  442. displayPickerSmbMinutes = toggleScrollWheel(displayPickerSmbMinutes)
  443. }
  444. if displayPickerSmbMinutes {
  445. HStack {
  446. Picker(
  447. selection: Binding(
  448. get: { smbMinutes ?? state.defaultSmbMinutes },
  449. set: {
  450. smbMinutes = $0
  451. hasChanges = true
  452. }
  453. ),
  454. label: Text("")
  455. ) {
  456. ForEach(Array(stride(from: 0, through: 180, by: 5)), id: \.self) { minute in
  457. Text("\(minute) min").tag(Decimal(minute))
  458. }
  459. }
  460. .pickerStyle(WheelPickerStyle())
  461. .frame(maxWidth: .infinity)
  462. Picker(
  463. selection: Binding(
  464. get: { uamMinutes ?? state.defaultUamMinutes },
  465. set: {
  466. uamMinutes = $0
  467. hasChanges = true
  468. }
  469. ),
  470. label: Text("")
  471. ) {
  472. ForEach(Array(stride(from: 0, through: 180, by: 5)), id: \.self) { minute in
  473. Text("\(minute) min").tag(Decimal(minute))
  474. }
  475. }
  476. .pickerStyle(WheelPickerStyle())
  477. .frame(maxWidth: .infinity)
  478. }
  479. .listRowSeparator(.hidden, edges: .top)
  480. }
  481. }
  482. }
  483. .listRowBackground(Color.chart)
  484. }
  485. }
  486. }
  487. private var saveButton: some View {
  488. let (isInvalid, errorMessage) = isOverrideInvalid()
  489. return Section(
  490. header:
  491. HStack {
  492. Spacer()
  493. Text(errorMessage ?? "").textCase(nil)
  494. .foregroundColor(colorScheme == .dark ? .orange : .accentColor)
  495. Spacer()
  496. },
  497. content: {
  498. Button(action: {
  499. saveChanges()
  500. do {
  501. guard let moc = override.managedObjectContext else { return }
  502. guard moc.hasChanges else { return }
  503. try moc.save()
  504. if let currentActiveOverride = state.currentActiveOverride {
  505. Task {
  506. await state.disableAllActiveOverrides(
  507. except: currentActiveOverride.objectID,
  508. createOverrideRunEntry: false
  509. )
  510. // Update View
  511. state.updateLatestOverrideConfiguration()
  512. }
  513. }
  514. hasChanges = false
  515. presentationMode.wrappedValue.dismiss()
  516. } catch {
  517. debugPrint("\(DebuggingIdentifiers.failed) \(#file) \(#function) Failed to edit Override")
  518. }
  519. }, label: {
  520. Text("Save Override")
  521. })
  522. .disabled(isInvalid) // Disable button if changes are invalid
  523. .frame(maxWidth: .infinity, alignment: .center)
  524. .tint(.white)
  525. }
  526. )
  527. .listRowBackground(isInvalid ? Color(.systemGray4) : Color(.systemBlue))
  528. }
  529. private func isOverrideInvalid() -> (Bool, String?) {
  530. let noDurationSpecified = !indefinite && duration == 0
  531. let targetZeroWithOverride = target_override && (target ?? 0 < 72 || target ?? 0 > 270)
  532. let allSettingsDefault = percentage == 100 && !target_override && !advancedSettings &&
  533. !smbIsOff && !smbIsScheduledOff
  534. if noDurationSpecified {
  535. return (true, "Enable indefinitely or set a duration.")
  536. }
  537. if targetZeroWithOverride {
  538. return (true, "Target glucose is out of range (\(state.units == .mgdL ? "72-270" : "4-14")).")
  539. }
  540. if allSettingsDefault {
  541. return (true, "All settings are at default values.")
  542. }
  543. if !hasChanges {
  544. return (true, nil)
  545. }
  546. return (false, nil)
  547. }
  548. private func saveChanges() {
  549. if !override.isPreset, hasChanges, name == (override.name ?? "") {
  550. override.name = "Custom Override"
  551. } else {
  552. override.name = name
  553. }
  554. override.percentage = percentage
  555. override.indefinite = indefinite
  556. override.duration = NSDecimalNumber(decimal: duration)
  557. override.target = NSDecimalNumber(decimal: target ?? 100)
  558. override.advancedSettings = advancedSettings
  559. override.smbIsOff = smbIsOff
  560. override.smbIsScheduledOff = smbIsScheduledOff
  561. override.start = start.map { NSDecimalNumber(decimal: $0) }
  562. override.end = end.map { NSDecimalNumber(decimal: $0) }
  563. override.isfAndCr = isfAndCr
  564. override.isf = isf
  565. override.cr = cr
  566. override.smbMinutes = smbMinutes.map { NSDecimalNumber(decimal: $0) }
  567. override.uamMinutes = uamMinutes.map { NSDecimalNumber(decimal: $0) }
  568. override.isUploadedToNS = false
  569. }
  570. private func resetValues() {
  571. name = override.name ?? ""
  572. percentage = override.percentage
  573. indefinite = override.indefinite
  574. duration = override.duration?.decimalValue ?? 0
  575. target = override.target?.decimalValue
  576. advancedSettings = override.advancedSettings
  577. smbIsOff = override.smbIsOff
  578. smbIsScheduledOff = override.smbIsScheduledOff
  579. start = override.start?.decimalValue
  580. end = override.end?.decimalValue
  581. isfAndCr = override.isfAndCr
  582. isf = override.isf
  583. cr = override.cr
  584. smbMinutes = override.smbMinutes?.decimalValue ?? state.defaultSmbMinutes
  585. uamMinutes = override.uamMinutes?.decimalValue ?? state.defaultUamMinutes
  586. }
  587. private func toggleScrollWheel(_ toggle: Bool) -> Bool {
  588. displayPickerDuration = false
  589. displayPickerPercentage = false
  590. displayPickerTarget = false
  591. displayPickerDisableSmbSchedule = false
  592. displayPickerSmbMinutes = false
  593. return !toggle
  594. }
  595. }
  596. struct TargetPicker: View {
  597. let label: String
  598. @Binding var selection: Decimal
  599. let options: [Decimal]
  600. let units: GlucoseUnits
  601. @Binding var hasChanges: Bool
  602. @Binding var targetStep: Decimal
  603. @Binding var displayPickerTarget: Bool
  604. var toggleScrollWheel: (_ picker: Bool) -> Bool
  605. var body: some View {
  606. HStack {
  607. Text(label)
  608. Spacer()
  609. Text(
  610. (units == .mgdL ? selection.description : selection.formattedAsMmolL) + " " + units.rawValue
  611. )
  612. .foregroundColor(!displayPickerTarget ? .primary : .accentColor)
  613. }
  614. .onTapGesture {
  615. displayPickerTarget = toggleScrollWheel(displayPickerTarget)
  616. }
  617. if displayPickerTarget {
  618. HStack {
  619. // Radio buttons and text on the left side
  620. VStack(alignment: .leading) {
  621. // Radio buttons for step iteration
  622. let stepChoices: [Decimal] = units == .mgdL ? [1, 5] : [1, 9]
  623. ForEach(stepChoices, id: \.self) { step in
  624. let label = (units == .mgdL ? step.description : step.formattedAsMmolL) + " " +
  625. units.rawValue
  626. RadioButton(
  627. isSelected: targetStep == step,
  628. label: label
  629. ) {
  630. targetStep = step
  631. selection = OverrideConfig.StateModel.roundTargetToStep(selection, step)
  632. }
  633. .padding(.top, 10)
  634. }
  635. }
  636. .frame(maxWidth: .infinity)
  637. Spacer()
  638. // Picker on the right side
  639. Picker(selection: Binding(
  640. get: { OverrideConfig.StateModel.roundTargetToStep(selection, targetStep) },
  641. set: {
  642. selection = $0
  643. hasChanges = true
  644. }
  645. ), label: Text("")) {
  646. ForEach(options, id: \.self) { option in
  647. Text((units == .mgdL ? option.description : option.formattedAsMmolL) + " " + units.rawValue)
  648. .tag(option)
  649. }
  650. }
  651. .pickerStyle(WheelPickerStyle())
  652. .frame(maxWidth: .infinity)
  653. }
  654. .listRowSeparator(.hidden, edges: .top)
  655. }
  656. }
  657. }