|
| 1 | +import UIKit |
| 2 | + |
| 3 | +// MARK: - DateRangeFilterViewController |
| 4 | +// Allow to choose a range of dates (eg. used in Order Filters Date Range) |
| 5 | +// |
| 6 | +final class DateRangeFilterViewController: UIViewController { |
| 7 | + |
| 8 | + @IBOutlet private weak var tableView: UITableView! |
| 9 | + |
| 10 | + // Completion callback |
| 11 | + // |
| 12 | + typealias Completion = (_ startDate: Date?, _ endDate: Date?) -> Void |
| 13 | + private let onCompletion: Completion |
| 14 | + |
| 15 | + private var startDate: Date? |
| 16 | + private var endDate: Date? |
| 17 | + |
| 18 | + // Indicate when the start or the end date picker is expanded |
| 19 | + // |
| 20 | + private var startDateExpanded: Bool = false |
| 21 | + private var endDateExpanded: Bool = false |
| 22 | + |
| 23 | + private var rows: [Row] = [] |
| 24 | + |
| 25 | + /// Init |
| 26 | + /// |
| 27 | + init(startDate: Date?, |
| 28 | + endDate: Date?, |
| 29 | + completion: @escaping Completion) { |
| 30 | + self.startDate = startDate |
| 31 | + self.endDate = endDate |
| 32 | + onCompletion = completion |
| 33 | + super.init(nibName: nil, bundle: nil) |
| 34 | + } |
| 35 | + |
| 36 | + required init?(coder: NSCoder) { |
| 37 | + fatalError("init(coder:) has not been implemented") |
| 38 | + } |
| 39 | + |
| 40 | + override func viewDidLoad() { |
| 41 | + super.viewDidLoad() |
| 42 | + configureNavigationBar() |
| 43 | + configureMainView() |
| 44 | + updateRows() |
| 45 | + configureTableView() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// MARK: - View Configuration |
| 50 | +// |
| 51 | +private extension DateRangeFilterViewController { |
| 52 | + |
| 53 | + func configureNavigationBar() { |
| 54 | + title = Localization.navigationBarTitle |
| 55 | + } |
| 56 | + |
| 57 | + func configureMainView() { |
| 58 | + view.backgroundColor = .listBackground |
| 59 | + } |
| 60 | + |
| 61 | + func configureTableView() { |
| 62 | + tableView.dataSource = self |
| 63 | + tableView.delegate = self |
| 64 | + |
| 65 | + /// Registers all of the available TableViewCells |
| 66 | + /// |
| 67 | + for row in Row.allCases { |
| 68 | + tableView.registerNib(for: row.type) |
| 69 | + } |
| 70 | + |
| 71 | + tableView.backgroundColor = .listBackground |
| 72 | + tableView.removeLastCellSeparator() |
| 73 | + } |
| 74 | + |
| 75 | + func updateRows() { |
| 76 | + var tempRows: [Row] = [.startDateTitle] |
| 77 | + if startDateExpanded { |
| 78 | + tempRows.append(.startDatePicker) |
| 79 | + } |
| 80 | + tempRows.append(.endDateTitle) |
| 81 | + if endDateExpanded { |
| 82 | + tempRows.append(.endDatePicker) |
| 83 | + } |
| 84 | + rows = tempRows |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// MARK: - UITableViewDataSource Conformance |
| 89 | +// |
| 90 | +extension DateRangeFilterViewController: UITableViewDataSource { |
| 91 | + |
| 92 | + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 93 | + return rows.count |
| 94 | + } |
| 95 | + |
| 96 | + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
| 97 | + let row = rows[indexPath.row] |
| 98 | + let cell = tableView.dequeueReusableCell(withIdentifier: row.reuseIdentifier, for: indexPath) |
| 99 | + configure(cell, for: row, at: indexPath) |
| 100 | + return cell |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// MARK: - UITableViewDelegate Conformance |
| 105 | +// |
| 106 | +extension DateRangeFilterViewController: UITableViewDelegate { |
| 107 | + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { |
| 108 | + tableView.deselectRow(at: indexPath, animated: true) |
| 109 | + |
| 110 | + switch rows[indexPath.row] { |
| 111 | + case .startDateTitle: |
| 112 | + startDateExpanded.toggle() |
| 113 | + updateRows() |
| 114 | + tableView.reloadData() |
| 115 | + case .endDateTitle: |
| 116 | + endDateExpanded.toggle() |
| 117 | + updateRows() |
| 118 | + tableView.reloadData() |
| 119 | + default: |
| 120 | + return |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { |
| 125 | + return UITableView.automaticDimension |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// MARK: - Cell configuration |
| 130 | +// |
| 131 | +private extension DateRangeFilterViewController { |
| 132 | + /// Cells currently configured in the order they appear on screen |
| 133 | + /// |
| 134 | + func configure(_ cell: UITableViewCell, for row: Row, at indexPath: IndexPath) { |
| 135 | + switch cell { |
| 136 | + case let cell as TitleAndValueTableViewCell where row == .startDateTitle: |
| 137 | + configureStartDateTitle(cell: cell) |
| 138 | + break |
| 139 | + case let cell as DatePickerTableViewCell where row == .startDatePicker: |
| 140 | + configureStartDatePicker(cell: cell) |
| 141 | + break |
| 142 | + case let cell as TitleAndValueTableViewCell where row == .endDateTitle: |
| 143 | + configureEndDateTitle(cell: cell) |
| 144 | + break |
| 145 | + case let cell as DatePickerTableViewCell where row == .endDatePicker: |
| 146 | + configureEndDatePicker(cell: cell) |
| 147 | + break |
| 148 | + default: |
| 149 | + fatalError() |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + func configureStartDateTitle(cell: TitleAndValueTableViewCell) { |
| 154 | + cell.updateUI(title: Localization.startDateTitle, value: startDate?.toString(dateStyle: .medium, timeStyle: .none)) |
| 155 | + cell.selectionStyle = .none |
| 156 | + } |
| 157 | + |
| 158 | + func configureStartDatePicker(cell: DatePickerTableViewCell) { |
| 159 | + if let startDate = startDate { |
| 160 | + cell.getPicker().setDate(startDate, animated: false) |
| 161 | + } |
| 162 | + cell.getPicker().maximumDate = endDate |
| 163 | + cell.getPicker().preferredDatePickerStyle = .inline |
| 164 | + cell.onDateSelected = { [weak self] date in |
| 165 | + guard let self = self else { |
| 166 | + return |
| 167 | + } |
| 168 | + self.startDate = date |
| 169 | + self.tableView.reloadData() |
| 170 | + self.onCompletion(self.startDate, self.endDate) |
| 171 | + } |
| 172 | + cell.selectionStyle = .none |
| 173 | + } |
| 174 | + |
| 175 | + func configureEndDateTitle(cell: TitleAndValueTableViewCell) { |
| 176 | + cell.updateUI(title: Localization.endDateTitle, value: endDate?.toString(dateStyle: .medium, timeStyle: .none)) |
| 177 | + cell.selectionStyle = .none |
| 178 | + } |
| 179 | + |
| 180 | + func configureEndDatePicker(cell: DatePickerTableViewCell) { |
| 181 | + if let endDate = endDate { |
| 182 | + cell.getPicker().setDate(endDate, animated: false) |
| 183 | + } |
| 184 | + cell.getPicker().minimumDate = startDate |
| 185 | + cell.getPicker().preferredDatePickerStyle = .inline |
| 186 | + cell.onDateSelected = { [weak self] date in |
| 187 | + guard let self = self else { |
| 188 | + return |
| 189 | + } |
| 190 | + self.endDate = date |
| 191 | + self.tableView.reloadData() |
| 192 | + self.onCompletion(self.startDate, self.endDate) |
| 193 | + } |
| 194 | + cell.selectionStyle = .none |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +// MARK: - Private Types |
| 199 | +// |
| 200 | +private extension DateRangeFilterViewController { |
| 201 | + |
| 202 | + enum Row: CaseIterable { |
| 203 | + case startDateTitle |
| 204 | + case startDatePicker |
| 205 | + case endDateTitle |
| 206 | + case endDatePicker |
| 207 | + |
| 208 | + fileprivate var type: UITableViewCell.Type { |
| 209 | + switch self { |
| 210 | + case .startDateTitle, .endDateTitle: |
| 211 | + return TitleAndValueTableViewCell.self |
| 212 | + case .startDatePicker, .endDatePicker: |
| 213 | + return DatePickerTableViewCell.self |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + fileprivate var reuseIdentifier: String { |
| 218 | + return type.reuseIdentifier |
| 219 | + } |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +private extension DateRangeFilterViewController { |
| 224 | + enum Localization { |
| 225 | + static let navigationBarTitle = NSLocalizedString("Custom Range", |
| 226 | + comment: "Navigation title of the orders filter selector screen for custom date range") |
| 227 | + static let startDateTitle = NSLocalizedString("Start Date", comment: "Label for one of the filters in order custom date range") |
| 228 | + static let endDateTitle = NSLocalizedString("End Date", comment: "Label for one of the filters in order custom date range") |
| 229 | + } |
| 230 | +} |
0 commit comments