|
| 1 | +import Combine |
| 2 | +import UIKit |
| 3 | +import Yosemite |
| 4 | + |
| 5 | +/// Displays a paginated list of products where the user can select. |
| 6 | +/// This uses the old design for the product list selector, |
| 7 | +/// and should be removed once updates for `ProductListSelectorViewController` are complete. |
| 8 | +final class LegacyProductListSelectorViewController: UIViewController { |
| 9 | + private let excludedProductIDs: [Int64] |
| 10 | + private var productIDs: [Int64] = [] { |
| 11 | + didSet { |
| 12 | + if productIDs != oldValue { |
| 13 | + updateNavigationTitle(productIDs: productIDs) |
| 14 | + updateNavigationRightBarButtonItem(productIDs: productIDs) |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + private let siteID: Int64 |
| 20 | + private var selectedProductIDsSubscription: AnyCancellable? |
| 21 | + |
| 22 | + private lazy var dataSource = ProductListMultiSelectorDataSource(siteID: siteID, excludedProductIDs: excludedProductIDs) |
| 23 | + |
| 24 | + private lazy var paginatedListSelector: PaginatedListSelectorViewController |
| 25 | + <ProductListMultiSelectorDataSource, Product, StorageProduct, ProductsTabProductTableViewCell> = { |
| 26 | + let viewProperties = PaginatedListSelectorViewProperties(navigationBarTitle: nil, |
| 27 | + noResultsPlaceholderText: Localization.noResultsPlaceholder, |
| 28 | + noResultsPlaceholderImage: .emptyProductsImage, |
| 29 | + noResultsPlaceholderImageTintColor: .primary, |
| 30 | + tableViewStyle: .plain, |
| 31 | + separatorStyle: .none) |
| 32 | + return PaginatedListSelectorViewController(viewProperties: viewProperties, dataSource: dataSource, onDismiss: { _ in }) |
| 33 | + }() |
| 34 | + |
| 35 | + // Completion callback |
| 36 | + // |
| 37 | + typealias Completion = (_ selectedProductIDs: [Int64]) -> Void |
| 38 | + private let onCompletion: Completion |
| 39 | + |
| 40 | + init(excludedProductIDs: [Int64], siteID: Int64, onCompletion: @escaping Completion) { |
| 41 | + self.excludedProductIDs = excludedProductIDs |
| 42 | + self.siteID = siteID |
| 43 | + self.onCompletion = onCompletion |
| 44 | + super.init(nibName: nil, bundle: nil) |
| 45 | + } |
| 46 | + |
| 47 | + required init?(coder: NSCoder) { |
| 48 | + fatalError("init(coder:) has not been implemented") |
| 49 | + } |
| 50 | + |
| 51 | + override func viewDidLoad() { |
| 52 | + super.viewDidLoad() |
| 53 | + |
| 54 | + configureMainView() |
| 55 | + configureNavigation() |
| 56 | + configurePaginatedProductListSelectorChildViewController() |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// MARK: - Actions |
| 61 | +private extension LegacyProductListSelectorViewController { |
| 62 | + @objc func doneButtonTapped() { |
| 63 | + completeUpdating() |
| 64 | + } |
| 65 | + |
| 66 | + @objc func searchButtonTapped() { |
| 67 | + let productIDsToExclude = (excludedProductIDs + productIDs).removingDuplicates() |
| 68 | + let searchProductsCommand = ProductListMultiSelectorSearchUICommand(siteID: siteID, |
| 69 | + excludedProductIDs: productIDsToExclude) { [weak self] productIDs in |
| 70 | + self?.didSelectProductsFromSearch(ids: productIDs) |
| 71 | + } |
| 72 | + let searchViewController = SearchViewController(storeID: siteID, |
| 73 | + command: searchProductsCommand, |
| 74 | + cellType: ProductsTabProductTableViewCell.self, |
| 75 | + cellSeparator: .none) |
| 76 | + let navigationController = WooNavigationController(rootViewController: searchViewController) |
| 77 | + searchProductsCommand.configurePresentingViewControllerForDiscardChangesAlert(presentingViewController: navigationController) |
| 78 | + present(navigationController, animated: true, completion: nil) |
| 79 | + } |
| 80 | + |
| 81 | + func didSelectProductsFromSearch(ids: [Int64]) { |
| 82 | + dataSource.addProducts(ids) |
| 83 | + paginatedListSelector.reloadData() |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// MARK: - Navigation actions handling |
| 88 | +// |
| 89 | +extension LegacyProductListSelectorViewController { |
| 90 | + override func shouldPopOnBackButton() -> Bool { |
| 91 | + if hasUnsavedChanges() { |
| 92 | + presentBackNavigationActionSheet() |
| 93 | + return false |
| 94 | + } |
| 95 | + return true |
| 96 | + } |
| 97 | + |
| 98 | + override func shouldPopOnSwipeBack() -> Bool { |
| 99 | + return shouldPopOnBackButton() |
| 100 | + } |
| 101 | + |
| 102 | + private func completeUpdating() { |
| 103 | + onCompletion(productIDs) |
| 104 | + } |
| 105 | + |
| 106 | + private func hasUnsavedChanges() -> Bool { |
| 107 | + return productIDs.isNotEmpty |
| 108 | + } |
| 109 | + |
| 110 | + private func presentBackNavigationActionSheet() { |
| 111 | + UIAlertController.presentDiscardChangesActionSheet(viewController: self, onDiscard: { [weak self] in |
| 112 | + self?.navigationController?.popViewController(animated: true) |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// MARK: - UI updates |
| 118 | +private extension LegacyProductListSelectorViewController { |
| 119 | + func updateNavigationRightBarButtonItem(productIDs: [Int64]) { |
| 120 | + if productIDs.isEmpty { |
| 121 | + navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .search, target: self, action: #selector(searchButtonTapped)) |
| 122 | + } else { |
| 123 | + navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonTapped)) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + func updateNavigationTitle(productIDs: [Int64]) { |
| 128 | + let title: String |
| 129 | + switch productIDs.count { |
| 130 | + case 0: |
| 131 | + title = Localization.titleWithoutSelectedProducts |
| 132 | + case 1: |
| 133 | + title = Localization.titleWithOneSelectedProduct |
| 134 | + default: |
| 135 | + title = String.localizedStringWithFormat(Localization.titleWithMultipleSelectedProductsFormat, productIDs.count) |
| 136 | + } |
| 137 | + navigationItem.title = title |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// MARK: - UI configurations |
| 142 | +// |
| 143 | +private extension LegacyProductListSelectorViewController { |
| 144 | + func configureMainView() { |
| 145 | + view.backgroundColor = .basicBackground |
| 146 | + } |
| 147 | + |
| 148 | + func configureNavigation() { |
| 149 | + updateNavigationTitle(productIDs: productIDs) |
| 150 | + updateNavigationRightBarButtonItem(productIDs: productIDs) |
| 151 | + } |
| 152 | + |
| 153 | + func configurePaginatedProductListSelectorChildViewController() { |
| 154 | + observeSelectedProductIDs(observableProductIDs: dataSource.productIDs) |
| 155 | + |
| 156 | + addChild(paginatedListSelector) |
| 157 | + paginatedListSelector.view.translatesAutoresizingMaskIntoConstraints = false |
| 158 | + view.addSubview(paginatedListSelector.view) |
| 159 | + paginatedListSelector.didMove(toParent: self) |
| 160 | + |
| 161 | + view.pinSubviewToAllEdges(paginatedListSelector.view) |
| 162 | + } |
| 163 | + |
| 164 | + func observeSelectedProductIDs(observableProductIDs: AnyPublisher<[Int64], Never>) { |
| 165 | + selectedProductIDsSubscription = observableProductIDs.sink { [weak self] selectedProductIDs in |
| 166 | + self?.productIDs = selectedProductIDs |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// MARK: - Constants |
| 172 | +// |
| 173 | +private extension LegacyProductListSelectorViewController { |
| 174 | + enum Localization { |
| 175 | + static let noResultsPlaceholder = NSLocalizedString("No products yet", |
| 176 | + comment: "Placeholder text when there are no products on the product list selector") |
| 177 | + static let titleWithoutSelectedProducts = NSLocalizedString("Add Products", comment: "Navigation bar title for selecting multiple products.") |
| 178 | + static let titleWithOneSelectedProduct = |
| 179 | + NSLocalizedString("1 Product Selected", |
| 180 | + comment: "Navigation bar title for selecting multiple products when one product has been selected.") |
| 181 | + static let titleWithMultipleSelectedProductsFormat = |
| 182 | + NSLocalizedString("%ld Products Selected", |
| 183 | + comment: "Navigation bar title for selecting multiple products when more multiple products have been selected.") |
| 184 | + } |
| 185 | +} |
0 commit comments