|
| 1 | +import UIKit |
| 2 | +import Yosemite |
| 3 | +import Combine |
| 4 | + |
| 5 | +final class PriceInputViewController: UIViewController { |
| 6 | + |
| 7 | + let tableView: UITableView = UITableView(frame: .zero, style: .grouped) |
| 8 | + |
| 9 | + private var viewModel: PriceInputViewModel |
| 10 | + private var subscriptions = Set<AnyCancellable>() |
| 11 | + |
| 12 | + private lazy var noticePresenter: NoticePresenter = { |
| 13 | + let noticePresenter = DefaultNoticePresenter() |
| 14 | + noticePresenter.presentingViewController = self |
| 15 | + return noticePresenter |
| 16 | + }() |
| 17 | + |
| 18 | + init(viewModel: PriceInputViewModel, noticePresenter: NoticePresenter? = nil) { |
| 19 | + self.viewModel = viewModel |
| 20 | + super.init(nibName: nil, bundle: nil) |
| 21 | + |
| 22 | + if let noticePresenter { |
| 23 | + self.noticePresenter = noticePresenter |
| 24 | + } |
| 25 | + } |
| 26 | + required init?(coder: NSCoder) { |
| 27 | + fatalError("init(coder:) has not been implemented") |
| 28 | + } |
| 29 | + |
| 30 | + override func viewDidLoad() { |
| 31 | + super.viewDidLoad() |
| 32 | + |
| 33 | + configureTitleAndBackground() |
| 34 | + configureTableView() |
| 35 | + configureViewModel() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +private extension PriceInputViewController { |
| 40 | + func configureTitleAndBackground() { |
| 41 | + title = viewModel.screenTitle() |
| 42 | + view.backgroundColor = .listBackground |
| 43 | + |
| 44 | + navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, |
| 45 | + target: self, |
| 46 | + action: #selector(cancelButtonTapped)) |
| 47 | + navigationItem.rightBarButtonItem = UIBarButtonItem(title: Localization.bulkEditingApply, |
| 48 | + style: .plain, |
| 49 | + target: self, |
| 50 | + action: #selector(applyButtonTapped)) |
| 51 | + } |
| 52 | + |
| 53 | + func configureViewModel() { |
| 54 | + viewModel.$applyButtonEnabled |
| 55 | + .sink { [weak self] enabled in |
| 56 | + self?.navigationItem.rightBarButtonItem?.isEnabled = enabled |
| 57 | + }.store(in: &subscriptions) |
| 58 | + |
| 59 | + viewModel.$inputValidationError.sink { [weak self] error in |
| 60 | + guard let error = error else { |
| 61 | + return |
| 62 | + } |
| 63 | + self?.displayNoticeForError(error) |
| 64 | + }.store(in: &subscriptions) |
| 65 | + } |
| 66 | + |
| 67 | + func configureTableView() { |
| 68 | + tableView.translatesAutoresizingMaskIntoConstraints = false |
| 69 | + view.addSubview(tableView) |
| 70 | + view.pinSubviewToAllEdges(tableView) |
| 71 | + |
| 72 | + tableView.rowHeight = UITableView.automaticDimension |
| 73 | + tableView.backgroundColor = .listBackground |
| 74 | + |
| 75 | + tableView.registerNib(for: UnitInputTableViewCell.self) |
| 76 | + |
| 77 | + tableView.dataSource = self |
| 78 | + } |
| 79 | + |
| 80 | + /// Called when the cancel button is tapped |
| 81 | + /// |
| 82 | + @objc func cancelButtonTapped() { |
| 83 | + viewModel.cancelButtonTapped() |
| 84 | + } |
| 85 | + |
| 86 | + /// Called when the save button is tapped to update the price for all products |
| 87 | + /// |
| 88 | + @objc func applyButtonTapped() { |
| 89 | + // Dismiss the keyboard before triggering the update |
| 90 | + view.endEditing(true) |
| 91 | + viewModel.applyButtonTapped() |
| 92 | + } |
| 93 | + |
| 94 | + func displayNoticeForError(_ productPriceSettingsError: ProductPriceSettingsError) { |
| 95 | + switch productPriceSettingsError { |
| 96 | + case .salePriceWithoutRegularPrice: |
| 97 | + displayNotice(for: Localization.salePriceWithoutRegularPriceError) |
| 98 | + case .salePriceHigherThanRegularPrice: |
| 99 | + displayNotice(for: Localization.displaySalePriceError) |
| 100 | + case .newSaleWithEmptySalePrice: |
| 101 | + displayNotice(for: Localization.displayMissingSalePriceError) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// Displays a Notice onscreen for a given message |
| 106 | + /// |
| 107 | + func displayNotice(for message: String) { |
| 108 | + view.endEditing(true) |
| 109 | + let notice = Notice(title: message, feedbackType: .error) |
| 110 | + noticePresenter.enqueue(notice: notice) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// MARK: - UITableViewDataSource Conformance |
| 115 | +// |
| 116 | +extension PriceInputViewController: UITableViewDataSource { |
| 117 | + |
| 118 | + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 119 | + return 1 |
| 120 | + } |
| 121 | + |
| 122 | + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
| 123 | + let cell = tableView.dequeueReusableCell(withIdentifier: UnitInputTableViewCell.self.reuseIdentifier, for: indexPath) |
| 124 | + configure(cell, at: indexPath) |
| 125 | + |
| 126 | + return cell |
| 127 | + } |
| 128 | + |
| 129 | + func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { |
| 130 | + return UITableView.automaticDimension |
| 131 | + } |
| 132 | + |
| 133 | + func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { |
| 134 | + return viewModel.footerText |
| 135 | + } |
| 136 | + |
| 137 | + private func configure(_ cell: UITableViewCell, at indexPath: IndexPath) { |
| 138 | + switch cell { |
| 139 | + case let cell as UnitInputTableViewCell: |
| 140 | + let cellViewModel = UnitInputViewModel.createBulkPriceViewModel(using: ServiceLocator.currencySettings) { [weak self] value in |
| 141 | + self?.viewModel.handlePriceChange(value) |
| 142 | + } |
| 143 | + cell.selectionStyle = .none |
| 144 | + cell.configure(viewModel: cellViewModel) |
| 145 | + default: |
| 146 | + fatalError("Unidentified bulk update row type") |
| 147 | + break |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +private extension PriceInputViewController { |
| 153 | + enum Localization { |
| 154 | + static let bulkEditingApply = NSLocalizedString("Apply", comment: "Title for the button to apply bulk editing changes to selected products.") |
| 155 | + |
| 156 | + static let salePriceWithoutRegularPriceError = NSLocalizedString("The sale price can't be added without the regular price.", |
| 157 | + comment: "Bulk price update error message, when the sale price is added but the" |
| 158 | + + " regular price is not") |
| 159 | + static let displaySalePriceError = NSLocalizedString("The sale price should be lower than the regular price.", |
| 160 | + comment: "Bulk price update error, when the sale price is higher than the regular" |
| 161 | + + " price") |
| 162 | + static let displayMissingSalePriceError = NSLocalizedString("Please enter a sale price for the scheduled sale", |
| 163 | + comment: "Bulk price update error, when the sale price is empty") |
| 164 | + } |
| 165 | +} |
0 commit comments