DurationButton.swift 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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"))
  27. .font(.caption2)
  28. .foregroundColor(.secondary)
  29. }
  30. .buttonBorderShape(.automatic)
  31. .controlSize(.mini)
  32. .buttonStyle(.bordered)
  33. // .padding([.trailing], 15)
  34. // .frame(maxWidth: .infinity, alignment: .trailing)
  35. }
  36. }