|
| 1 | +import Foundation |
| 2 | +import SwiftUI |
| 3 | + |
| 4 | +/// View to select a custom date range. |
| 5 | +/// Consists of two date pickers laid out vertically. |
| 6 | +/// |
| 7 | +struct RangedDatePicker: View { |
| 8 | + |
| 9 | + @Environment(\.presentationMode) var presentation |
| 10 | + |
| 11 | + /// Closure invoked when the custom date range has been confirmed. |
| 12 | + /// |
| 13 | + var datesSelected: ((_ start: Date, _ end: Date) -> Void)? |
| 14 | + |
| 15 | + /// Start date binding variable |
| 16 | + /// |
| 17 | + @State private var startDate = Date() |
| 18 | + |
| 19 | + /// End date binding variable |
| 20 | + /// |
| 21 | + @State private var endDate = Date() |
| 22 | + |
| 23 | + var body: some View { |
| 24 | + NavigationView { |
| 25 | + ScrollView { |
| 26 | + VStack(alignment: .leading) { |
| 27 | + |
| 28 | + // Start Picker |
| 29 | + Text(Localization.startDate) |
| 30 | + .foregroundColor(Color(.accent)) |
| 31 | + .headlineStyle() |
| 32 | + |
| 33 | + Divider() |
| 34 | + |
| 35 | + DatePicker("", selection: $startDate, in: ...Date(), displayedComponents: [.date]) |
| 36 | + .datePickerStyle(.graphical) |
| 37 | + .accentColor(Color(.brand)) |
| 38 | + |
| 39 | + // End Picker |
| 40 | + Text(Localization.endDate) |
| 41 | + .foregroundColor(Color(.accent)) |
| 42 | + .headlineStyle() |
| 43 | + |
| 44 | + Divider() |
| 45 | + |
| 46 | + DatePicker("", selection: $endDate, in: ...Date(), displayedComponents: [.date]) |
| 47 | + .datePickerStyle(.graphical) |
| 48 | + .accentColor(Color(.brand)) |
| 49 | + } |
| 50 | + .padding() |
| 51 | + } |
| 52 | + .navigationBarTitleDisplayMode(.inline) |
| 53 | + .toolbar(content: { |
| 54 | + ToolbarItem(placement: .principal) { |
| 55 | + // Navigation Bar title |
| 56 | + VStack(spacing: Layout.titleSpacing) { |
| 57 | + Text(Localization.title) |
| 58 | + .headlineStyle() |
| 59 | + |
| 60 | + // TODO: Properly format date ranges outside the view |
| 61 | + Text("\(DateFormatter.monthAndDayFormatter.string(from: startDate)) - \(DateFormatter.monthAndDayFormatter.string(from: endDate))") |
| 62 | + .captionStyle() |
| 63 | + } |
| 64 | + } |
| 65 | + ToolbarItem(placement: .confirmationAction) { |
| 66 | + Button(action: { |
| 67 | + presentation.wrappedValue.dismiss() |
| 68 | + datesSelected?(startDate, endDate) |
| 69 | + }, label: { |
| 70 | + Text(Localization.apply) |
| 71 | + }) |
| 72 | + } |
| 73 | + ToolbarItem(placement: .cancellationAction) { |
| 74 | + Button(action: { |
| 75 | + presentation.wrappedValue.dismiss() |
| 76 | + }, label: { |
| 77 | + Image(uiImage: .closeButton) |
| 78 | + }) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | + .navigationViewStyle(.stack) |
| 83 | + .wooNavigationBarStyle() |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// MARK: Constant |
| 88 | + |
| 89 | +private extension RangedDatePicker { |
| 90 | + enum Localization { |
| 91 | + static let title = NSLocalizedString("Custom Date Range", comment: "Title in custom range date picker") |
| 92 | + static let apply = NSLocalizedString("Apply", comment: "Apply navigation button in custom range date picker") |
| 93 | + static let startDate = NSLocalizedString("Start Date", comment: "Start Date label in custom range date picker") |
| 94 | + static let endDate = NSLocalizedString("End Date", comment: "End Date label in custom range date picker") |
| 95 | + } |
| 96 | + enum Layout { |
| 97 | + static let titleSpacing: CGFloat = 4.0 |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// MARK: Previews |
| 102 | + |
| 103 | +struct RangedDatePickerPreview: PreviewProvider { |
| 104 | + static var previews: some View { |
| 105 | + RangedDatePicker() |
| 106 | + } |
| 107 | +} |
0 commit comments