EditOverrideForm.swift 30 KB

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