DurationButton.swift 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import SwiftUI
  2. protocol DurationButton: CaseIterable {
  3. var title: String { get }
  4. }
  5. extension DurationButton where Self: RawRepresentable, RawValue == String {
  6. var title: String {
  7. rawValue
  8. }
  9. }
  10. enum durationState: String, DurationButton {
  11. case day = "Past 24 Hours "
  12. case week = "Past Week "
  13. case month = "Past Month "
  14. case ninetyDays = "Past 90 Days "
  15. case total = "All Past Days of Data "
  16. }
  17. struct durationButton<T: DurationButton>: View {
  18. let states: [T]
  19. @State var currentIndex = 0
  20. @Binding var selectedState: T
  21. var body: some View {
  22. Button {
  23. currentIndex = currentIndex < states.count - 1 ? currentIndex + 1 : 0
  24. selectedState = states[currentIndex]
  25. } label: {
  26. Text(NSLocalizedString(states[currentIndex].title, comment: "Duration displayed in statPanel")).font(.caption2)
  27. .foregroundColor(.secondary)
  28. }
  29. .buttonBorderShape(.automatic)
  30. .controlSize(.mini)
  31. .buttonStyle(.bordered)
  32. }
  33. }