EditOverrideForm.swift 29 KB

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