TargetPicker.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import SwiftUI
  2. struct TargetPicker: View {
  3. let label: String
  4. @Binding var selection: Decimal
  5. let options: [Decimal]
  6. let units: GlucoseUnits
  7. var hasChanges: Binding<Bool>?
  8. @Binding var targetStep: Decimal
  9. @Binding var displayPickerTarget: Bool
  10. var toggleScrollWheel: (_ picker: Bool) -> Bool
  11. var body: some View {
  12. HStack {
  13. Text(label)
  14. Spacer()
  15. Text(
  16. (units == .mgdL ? selection.description : selection.formattedAsMmolL) + " " + units.rawValue
  17. )
  18. .foregroundColor(!displayPickerTarget ? .primary : .accentColor)
  19. .onTapGesture {
  20. displayPickerTarget = toggleScrollWheel(displayPickerTarget)
  21. }
  22. }
  23. if displayPickerTarget {
  24. HStack {
  25. // Radio buttons and text on the left side
  26. VStack(alignment: .leading) {
  27. // Radio buttons for step iteration
  28. let stepChoices: [Decimal] = units == .mgdL ? [1, 5] : [1, 9]
  29. ForEach(stepChoices, id: \.self) { step in
  30. let label = (units == .mgdL ? step.description : step.formattedAsMmolL) + " " +
  31. units.rawValue
  32. RadioButton(
  33. isSelected: targetStep == step,
  34. label: label
  35. ) {
  36. targetStep = step
  37. selection = Adjustments.StateModel.roundTargetToStep(selection, step)
  38. }
  39. .padding(.top, 10)
  40. }
  41. }
  42. .frame(maxWidth: .infinity)
  43. Spacer()
  44. // Picker on the right side
  45. Picker(selection: Binding(
  46. get: { Adjustments.StateModel.roundTargetToStep(selection, targetStep) },
  47. set: {
  48. selection = $0
  49. hasChanges?.wrappedValue = true // This safely updates if hasChanges is provided
  50. }
  51. ), label: Text("")) {
  52. ForEach(options, id: \.self) { option in
  53. Text((units == .mgdL ? option.description : option.formattedAsMmolL) + " " + units.rawValue)
  54. .tag(option)
  55. }
  56. }
  57. .pickerStyle(WheelPickerStyle())
  58. .frame(maxWidth: .infinity)
  59. }
  60. .listRowSeparator(.hidden, edges: .top)
  61. }
  62. }
  63. }