AlarmGramsSection.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import SwiftUI
  2. /// Mirror of `AlarmBGSection` but for gram-valued thresholds (carbsRequired).
  3. /// Single fixed unit, no mg/dL ↔︎ mmol/L conversion.
  4. struct AlarmGramsSection: View {
  5. let header: String
  6. let footer: String?
  7. let title: String
  8. let range: ClosedRange<Int>
  9. let step: Int
  10. @Binding var valueGrams: Decimal
  11. @State private var showPicker = false
  12. var body: some View {
  13. Section(
  14. header: Text(header),
  15. footer: footer.map { Text($0) }
  16. ) {
  17. VStack(spacing: 0) {
  18. HStack {
  19. Text(title)
  20. Spacer()
  21. Text("\(Int(NSDecimalNumber(decimal: valueGrams).intValue))")
  22. .foregroundColor(showPicker ? .accentColor : .primary)
  23. Text(String(localized: "g", comment: "Abbreviation for grams"))
  24. .foregroundColor(.secondary)
  25. }
  26. .contentShape(Rectangle())
  27. .onTapGesture { showPicker.toggle() }
  28. if showPicker {
  29. Picker(title, selection: Binding(
  30. get: { Int(NSDecimalNumber(decimal: valueGrams).intValue) },
  31. set: { valueGrams = Decimal($0) }
  32. )) {
  33. ForEach(Array(stride(from: range.lowerBound, through: range.upperBound, by: step)), id: \.self) { v in
  34. Text("\(v)").tag(v)
  35. }
  36. }
  37. .pickerStyle(.wheel)
  38. .frame(maxWidth: .infinity)
  39. }
  40. }
  41. }.listRowBackground(Color.chart)
  42. }
  43. }