DeliveryLimitsStepView.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import SwiftUI
  2. struct DeliveryLimitsStepView: View {
  3. @Bindable var state: Onboarding.StateModel
  4. let substep: DeliveryLimitSubstep
  5. @State private var shouldDisplayPicker: Bool = false
  6. private let settingsProvider = PickerSettingsProvider.shared
  7. var body: some View {
  8. VStack(alignment: .leading, spacing: 16) {
  9. Text(substep.hint)
  10. .padding(.horizontal)
  11. .font(.headline)
  12. switch substep {
  13. case .maxIOB:
  14. deliveryLimitInputSection(
  15. label: substep.title,
  16. displayPicker: $shouldDisplayPicker,
  17. setting: settingsProvider.settings.maxIOB,
  18. decimalValue: $state.maxIOB
  19. )
  20. case .maxBolus:
  21. deliveryLimitInputSection(
  22. label: substep.title,
  23. displayPicker: $shouldDisplayPicker,
  24. setting: settingsProvider.settings.maxBolus,
  25. decimalValue: $state.maxBolus
  26. )
  27. case .maxBasal:
  28. deliveryLimitInputSection(
  29. label: substep.title,
  30. displayPicker: $shouldDisplayPicker,
  31. setting: settingsProvider.settings.maxBasal,
  32. decimalValue: $state.maxBasal
  33. )
  34. case .maxCOB:
  35. deliveryLimitInputSection(
  36. label: substep.title,
  37. displayPicker: $shouldDisplayPicker,
  38. setting: settingsProvider.settings.maxCOB,
  39. decimalValue: $state.maxCOB
  40. )
  41. case .minimumSafetyThreshold:
  42. deliveryLimitInputSection(
  43. label: substep.title,
  44. displayPicker: $shouldDisplayPicker,
  45. setting: settingsProvider.settings.threshold_setting,
  46. decimalValue: $state.minimumSafetyThreshold
  47. )
  48. }
  49. AnyView(substep.description(units: state.units))
  50. .font(.footnote)
  51. .foregroundStyle(.secondary)
  52. .padding(.horizontal)
  53. .multilineTextAlignment(.leading)
  54. }
  55. }
  56. @ViewBuilder private func deliveryLimitInputSection(
  57. label: String,
  58. displayPicker: Binding<Bool>,
  59. setting: PickerSetting,
  60. decimalValue: Binding<Decimal>
  61. ) -> some View {
  62. VStack {
  63. HStack {
  64. Text(label)
  65. Spacer()
  66. displayText(for: substep, decimalValue: decimalValue.wrappedValue)
  67. .foregroundColor(!displayPicker.wrappedValue ? .primary : .accentColor)
  68. .onTapGesture {
  69. displayPicker.wrappedValue.toggle()
  70. }
  71. }
  72. if displayPicker.wrappedValue {
  73. Picker(selection: decimalValue, label: Text(label)) {
  74. ForEach(settingsProvider.generatePickerValues(from: setting, units: state.units), id: \.self) { value in
  75. displayText(for: substep, decimalValue: value).tag(value)
  76. }
  77. }
  78. .pickerStyle(WheelPickerStyle())
  79. .frame(maxWidth: .infinity)
  80. }
  81. }
  82. .padding()
  83. .background(Color.chart.opacity(0.65))
  84. .cornerRadius(10)
  85. }
  86. private func displayText(for substep: DeliveryLimitSubstep, decimalValue: Decimal) -> Text {
  87. switch substep {
  88. case .maxBasal,
  89. .maxBolus,
  90. .maxIOB:
  91. return Text("\(decimalValue) \(String(localized: "U", comment: "Insulin unit abbreviation"))")
  92. case .maxCOB:
  93. return Text("\(decimalValue) \(String(localized: "g", comment: "Gram abbreviation"))")
  94. case .minimumSafetyThreshold:
  95. let optionallyParsedValue = state.units == .mgdL ? decimalValue : decimalValue.asMmolL
  96. return Text("\(optionallyParsedValue) \(state.units.rawValue)")
  97. }
  98. }
  99. }