EditOverrideForm.swift 30 KB

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