DurationButton.swift 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 total = "All Past Days of Data "
  15. }
  16. struct durationButton<T: DurationButton>: View {
  17. let states: [T]
  18. @State var currentIndex = 0
  19. @Binding var selectedState: T
  20. var body: some View {
  21. Button {
  22. currentIndex = currentIndex < states.count - 1 ? currentIndex + 1 : 0
  23. selectedState = states[currentIndex]
  24. } label: {
  25. Text(NSLocalizedString(states[currentIndex].title, comment: "Duration displayed in statPanel"))
  26. .font(.caption2)
  27. .foregroundColor(.secondary)
  28. }
  29. .buttonBorderShape(.automatic)
  30. .controlSize(.mini)
  31. .buttonStyle(.bordered)
  32. // .padding([.trailing], 15)
  33. // .frame(maxWidth: .infinity, alignment: .trailing)
  34. }
  35. }