EditOverrideForm.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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(
  319. from: glucoseSetting,
  320. units: state.units,
  321. roundMinToStep: true
  322. ),
  323. units: state.units,
  324. hasChanges: $hasChanges,
  325. targetStep: $targetStep,
  326. displayPickerTarget: $displayPickerTarget,
  327. toggleScrollWheel: toggleScrollWheel
  328. )
  329. }
  330. }
  331. .listRowBackground(Color.chart)
  332. Section {
  333. // Picker for Disable SMB settings
  334. Picker("Disable SMBs", selection: $selectedDisableSmbOption) {
  335. ForEach(DisableSmbOptions.allCases, id: \.self) { option in
  336. Text(option.rawValue).tag(option)
  337. }
  338. }
  339. .pickerStyle(MenuPickerStyle())
  340. .onChange(of: selectedDisableSmbOption) { _, newValue in
  341. switch newValue {
  342. case .dontDisable:
  343. smbIsOff = false
  344. smbIsScheduledOff = false
  345. case .disable:
  346. smbIsOff = true
  347. smbIsScheduledOff = false
  348. case .disableOnSchedule:
  349. smbIsOff = false
  350. smbIsScheduledOff = true
  351. }
  352. hasChanges = true
  353. }
  354. if smbIsScheduledOff {
  355. // First Hour SMBs Are Disabled
  356. HStack {
  357. Text("From")
  358. Spacer()
  359. Text(
  360. is24HourFormat() ? format24Hour(Int(truncating: start! as NSNumber)) + ":00" :
  361. convertTo12HourFormat(Int(truncating: start! as NSNumber))
  362. )
  363. .foregroundColor(!displayPickerDisableSmbSchedule ? .primary : .accentColor)
  364. Spacer()
  365. Divider().frame(width: 1, height: 20)
  366. Spacer()
  367. Text("To")
  368. Spacer()
  369. Text(
  370. is24HourFormat() ? format24Hour(Int(truncating: end! as NSNumber)) + ":00" :
  371. convertTo12HourFormat(Int(truncating: end! as NSNumber))
  372. )
  373. .foregroundColor(!displayPickerDisableSmbSchedule ? .primary : .accentColor)
  374. }
  375. .onTapGesture {
  376. displayPickerDisableSmbSchedule = toggleScrollWheel(displayPickerDisableSmbSchedule)
  377. }
  378. if displayPickerDisableSmbSchedule {
  379. HStack {
  380. Picker(selection: Binding(
  381. get: { Int(truncating: start! as NSNumber) },
  382. set: {
  383. start = Decimal($0)
  384. hasChanges = true
  385. }
  386. ), label: Text("")) {
  387. if is24HourFormat() {
  388. ForEach(0 ..< 24, id: \.self) { hour in
  389. Text(format24Hour(hour) + ":00").tag(hour)
  390. }
  391. } else {
  392. ForEach(0 ..< 24, id: \.self) { hour in
  393. Text(convertTo12HourFormat(hour)).tag(hour)
  394. }
  395. }
  396. }
  397. .pickerStyle(WheelPickerStyle())
  398. .frame(maxWidth: .infinity)
  399. Picker(selection: Binding(
  400. get: { Int(truncating: end! as NSNumber) },
  401. set: {
  402. end = Decimal($0)
  403. hasChanges = true
  404. }
  405. ), label: Text("")) {
  406. if is24HourFormat() {
  407. ForEach(0 ..< 24, id: \.self) { hour in
  408. Text(format24Hour(hour) + ":00").tag(hour)
  409. }
  410. } else {
  411. ForEach(0 ..< 24, id: \.self) { hour in
  412. Text(convertTo12HourFormat(hour)).tag(hour)
  413. }
  414. }
  415. }
  416. .pickerStyle(WheelPickerStyle())
  417. .frame(maxWidth: .infinity)
  418. }
  419. .listRowSeparator(.hidden, edges: .top)
  420. }
  421. }
  422. }
  423. .listRowBackground(Color.chart)
  424. if !smbIsOff {
  425. Section {
  426. Toggle(isOn: $advancedSettings) {
  427. Text("Change Max SMB Minutes")
  428. }
  429. .onChange(of: advancedSettings) { hasChanges = true }
  430. if advancedSettings {
  431. // SMB Minutes Picker
  432. HStack {
  433. Text("SMB")
  434. Spacer()
  435. Text("\(smbMinutes?.formatted(.number) ?? "\(state.defaultSmbMinutes)") min")
  436. .foregroundColor(!displayPickerSmbMinutes ? .primary : .accentColor)
  437. Spacer()
  438. Divider().frame(width: 1, height: 20)
  439. Spacer()
  440. Text("UAM")
  441. Spacer()
  442. Text("\(uamMinutes?.formatted(.number) ?? "\(state.defaultUamMinutes)") min")
  443. .foregroundColor(!displayPickerSmbMinutes ? .primary : .accentColor)
  444. }
  445. .onTapGesture {
  446. displayPickerSmbMinutes = toggleScrollWheel(displayPickerSmbMinutes)
  447. }
  448. if displayPickerSmbMinutes {
  449. HStack {
  450. Picker(
  451. selection: Binding(
  452. get: { smbMinutes ?? state.defaultSmbMinutes },
  453. set: {
  454. smbMinutes = $0
  455. hasChanges = true
  456. }
  457. ),
  458. label: Text("")
  459. ) {
  460. ForEach(Array(stride(from: 0, through: 180, by: 5)), id: \.self) { minute in
  461. Text("\(minute) min").tag(Decimal(minute))
  462. }
  463. }
  464. .pickerStyle(WheelPickerStyle())
  465. .frame(maxWidth: .infinity)
  466. Picker(
  467. selection: Binding(
  468. get: { uamMinutes ?? state.defaultUamMinutes },
  469. set: {
  470. uamMinutes = $0
  471. hasChanges = true
  472. }
  473. ),
  474. label: Text("")
  475. ) {
  476. ForEach(Array(stride(from: 0, through: 180, by: 5)), id: \.self) { minute in
  477. Text("\(minute) min").tag(Decimal(minute))
  478. }
  479. }
  480. .pickerStyle(WheelPickerStyle())
  481. .frame(maxWidth: .infinity)
  482. }
  483. .listRowSeparator(.hidden, edges: .top)
  484. }
  485. }
  486. }
  487. .listRowBackground(Color.chart)
  488. }
  489. }
  490. }
  491. private var saveButton: some View {
  492. let (isInvalid, errorMessage) = isOverrideInvalid()
  493. return Section(
  494. header:
  495. HStack {
  496. Spacer()
  497. Text(errorMessage ?? "").textCase(nil)
  498. .foregroundColor(colorScheme == .dark ? .orange : .accentColor)
  499. Spacer()
  500. },
  501. content: {
  502. Button(action: {
  503. saveChanges()
  504. do {
  505. guard let moc = override.managedObjectContext else { return }
  506. guard moc.hasChanges else { return }
  507. try moc.save()
  508. if let currentActiveOverride = state.currentActiveOverride {
  509. Task {
  510. await state.disableAllActiveOverrides(
  511. except: currentActiveOverride.objectID,
  512. createOverrideRunEntry: false
  513. )
  514. // Update View
  515. state.updateLatestOverrideConfiguration()
  516. }
  517. }
  518. hasChanges = false
  519. presentationMode.wrappedValue.dismiss()
  520. } catch {
  521. debugPrint("\(DebuggingIdentifiers.failed) \(#file) \(#function) Failed to edit Override")
  522. }
  523. }, label: {
  524. Text("Save Override")
  525. })
  526. .disabled(isInvalid) // Disable button if changes are invalid
  527. .frame(maxWidth: .infinity, alignment: .center)
  528. .tint(.white)
  529. }
  530. )
  531. .listRowBackground(isInvalid ? Color(.systemGray4) : Color(.systemBlue))
  532. }
  533. private func isOverrideInvalid() -> (Bool, String?) {
  534. let noDurationSpecified = !indefinite && duration == 0
  535. let targetZeroWithOverride = target_override && (target ?? 0 < 72 || target ?? 0 > 270)
  536. let allSettingsDefault = percentage == 100 && !target_override && !advancedSettings &&
  537. !smbIsOff && !smbIsScheduledOff
  538. if noDurationSpecified {
  539. return (true, "Enable indefinitely or set a duration.")
  540. }
  541. if targetZeroWithOverride {
  542. return (true, "Target glucose is out of range (\(state.units == .mgdL ? "72-270" : "4-14")).")
  543. }
  544. if allSettingsDefault {
  545. return (true, "All settings are at default values.")
  546. }
  547. if !hasChanges {
  548. return (true, nil)
  549. }
  550. return (false, nil)
  551. }
  552. private func saveChanges() {
  553. if !override.isPreset, hasChanges, name == (override.name ?? "") {
  554. override.name = "Custom Override"
  555. } else {
  556. override.name = name
  557. }
  558. override.percentage = percentage
  559. override.indefinite = indefinite
  560. override.duration = NSDecimalNumber(decimal: duration)
  561. override.target = NSDecimalNumber(decimal: target ?? 100)
  562. override.advancedSettings = advancedSettings
  563. override.smbIsOff = smbIsOff
  564. override.smbIsScheduledOff = smbIsScheduledOff
  565. override.start = start.map { NSDecimalNumber(decimal: $0) }
  566. override.end = end.map { NSDecimalNumber(decimal: $0) }
  567. override.isfAndCr = isfAndCr
  568. override.isf = isf
  569. override.cr = cr
  570. override.smbMinutes = smbMinutes.map { NSDecimalNumber(decimal: $0) }
  571. override.uamMinutes = uamMinutes.map { NSDecimalNumber(decimal: $0) }
  572. override.isUploadedToNS = false
  573. }
  574. private func resetValues() {
  575. name = override.name ?? ""
  576. percentage = override.percentage
  577. indefinite = override.indefinite
  578. duration = override.duration?.decimalValue ?? 0
  579. target = override.target?.decimalValue
  580. advancedSettings = override.advancedSettings
  581. smbIsOff = override.smbIsOff
  582. smbIsScheduledOff = override.smbIsScheduledOff
  583. start = override.start?.decimalValue
  584. end = override.end?.decimalValue
  585. isfAndCr = override.isfAndCr
  586. isf = override.isf
  587. cr = override.cr
  588. smbMinutes = override.smbMinutes?.decimalValue ?? state.defaultSmbMinutes
  589. uamMinutes = override.uamMinutes?.decimalValue ?? state.defaultUamMinutes
  590. }
  591. private func toggleScrollWheel(_ toggle: Bool) -> Bool {
  592. displayPickerDuration = false
  593. displayPickerPercentage = false
  594. displayPickerTarget = false
  595. displayPickerDisableSmbSchedule = false
  596. displayPickerSmbMinutes = false
  597. return !toggle
  598. }
  599. }
  600. struct TargetPicker: View {
  601. let label: String
  602. @Binding var selection: Decimal
  603. let options: [Decimal]
  604. let units: GlucoseUnits
  605. @Binding var hasChanges: Bool
  606. @Binding var targetStep: Decimal
  607. @Binding var displayPickerTarget: Bool
  608. var toggleScrollWheel: (_ picker: Bool) -> Bool
  609. var body: some View {
  610. HStack {
  611. Text(label)
  612. Spacer()
  613. Text(
  614. (units == .mgdL ? selection.description : selection.formattedAsMmolL) + " " + units.rawValue
  615. )
  616. .foregroundColor(!displayPickerTarget ? .primary : .accentColor)
  617. }
  618. .onTapGesture {
  619. displayPickerTarget = toggleScrollWheel(displayPickerTarget)
  620. }
  621. if displayPickerTarget {
  622. HStack {
  623. // Radio buttons and text on the left side
  624. VStack(alignment: .leading) {
  625. // Radio buttons for step iteration
  626. let stepChoices: [Decimal] = units == .mgdL ? [1, 5] : [1, 9]
  627. ForEach(stepChoices, id: \.self) { step in
  628. let label = (units == .mgdL ? step.description : step.formattedAsMmolL) + " " +
  629. units.rawValue
  630. RadioButton(
  631. isSelected: targetStep == step,
  632. label: label
  633. ) {
  634. targetStep = step
  635. selection = OverrideConfig.StateModel.roundTargetToStep(selection, step)
  636. }
  637. .padding(.top, 10)
  638. }
  639. }
  640. .frame(maxWidth: .infinity)
  641. Spacer()
  642. // Picker on the right side
  643. Picker(selection: Binding(
  644. get: { OverrideConfig.StateModel.roundTargetToStep(selection, targetStep) },
  645. set: {
  646. selection = $0
  647. hasChanges = true
  648. }
  649. ), label: Text("")) {
  650. ForEach(options, id: \.self) { option in
  651. Text((units == .mgdL ? option.description : option.formattedAsMmolL) + " " + units.rawValue)
  652. .tag(option)
  653. }
  654. }
  655. .pickerStyle(WheelPickerStyle())
  656. .frame(maxWidth: .infinity)
  657. }
  658. .listRowSeparator(.hidden, edges: .top)
  659. }
  660. }
  661. }