EditOverrideForm.swift 28 KB

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