Skip to content

Commit 42981e7

Browse files
authored
Merge pull request #2156 from woocommerce/issue/2054-product-sort-options-bottom-sheet
Bottom Sheet: Products tab sort options
2 parents 84aa9ef + f2c4258 commit 42981e7

File tree

8 files changed

+146
-69
lines changed

8 files changed

+146
-69
lines changed

WooCommerce/Classes/ViewRelated/BottomSheet/ListSelector/BottomSheetListSelectorCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ protocol BottomSheetListSelectorCommand {
1313
var selected: Model? { get }
1414

1515
/// Called when a different model is selected.
16-
mutating func handleSelectedChange(selected: Model)
16+
func handleSelectedChange(selected: Model)
1717

1818
/// Configures the cell with the given model.
1919
func configureCell(cell: Cell, model: Model)

WooCommerce/Classes/ViewRelated/BottomSheet/ListSelector/BottomSheetListSelectorViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import UIKit
55
final class BottomSheetListSelectorViewController<Command: BottomSheetListSelectorCommand, Model, Cell>:
66
UIViewController, UITableViewDataSource, UITableViewDelegate where Command.Model == Model, Command.Cell == Cell {
77
private let viewProperties: BottomSheetListSelectorViewProperties
8-
private var command: Command
8+
private let command: Command
99
private let onDismiss: (_ selected: Model?) -> Void
1010

1111
private let rowType = Cell.self
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Yosemite
2+
3+
private extension ProductsSortOrder {
4+
/// Title shown on the action sheet.
5+
///
6+
var actionSheetTitle: String {
7+
switch self {
8+
case .dateAscending:
9+
return NSLocalizedString("Date: Oldest to Newest", comment: "Action sheet option to sort products from the oldest to the newest")
10+
case .dateDescending:
11+
return NSLocalizedString("Date: Newest to Oldest", comment: "Action sheet option to sort products from the newest to the oldest")
12+
case .nameAscending:
13+
return NSLocalizedString("Title: A to Z", comment: "Action sheet option to sort products by ascending product name")
14+
case .nameDescending:
15+
return NSLocalizedString("Title: Z to A", comment: "Action sheet option to sort products by descending product name")
16+
}
17+
}
18+
}
19+
20+
/// `BottomSheetListSelectorCommand` for selecting a sort order for the Products tab.
21+
///
22+
final class ProductsSortOrderBottomSheetListSelectorCommand: BottomSheetListSelectorCommand {
23+
typealias Model = ProductsSortOrder
24+
typealias Cell = BasicTableViewCell
25+
26+
let data: [ProductsSortOrder] = [
27+
.dateDescending,
28+
.dateAscending,
29+
.nameDescending,
30+
.nameAscending
31+
]
32+
33+
var selected: ProductsSortOrder?
34+
35+
init(selected: ProductsSortOrder?) {
36+
self.selected = selected
37+
}
38+
39+
func configureCell(cell: BasicTableViewCell, model: ProductsSortOrder) {
40+
cell.selectionStyle = .default
41+
cell.textLabel?.text = model.actionSheetTitle
42+
cell.accessoryType = isSelected(model: model) ? .checkmark: .none
43+
}
44+
45+
func handleSelectedChange(selected: ProductsSortOrder) {
46+
self.selected = selected
47+
}
48+
49+
func isSelected(model: ProductsSortOrder) -> Bool {
50+
return model == selected
51+
}
52+
}

WooCommerce/Classes/ViewRelated/Products/ProductsViewController.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private extension ProductsViewController {
260260
let sortTitle = NSLocalizedString("Sort by", comment: "Title of the toolbar button to sort products in different ways.")
261261
let sortButton = UIButton(frame: .zero)
262262
sortButton.setTitle(sortTitle, for: .normal)
263-
sortButton.addTarget(self, action: #selector(sortButtonTapped), for: .touchUpInside)
263+
sortButton.addTarget(self, action: #selector(sortButtonTapped(sender:)), for: .touchUpInside)
264264

265265
let filterTitle = NSLocalizedString("Filter", comment: "Title of the toolbar button to filter products by different attributes.")
266266
let filterButton = UIButton(frame: .zero)
@@ -434,12 +434,25 @@ private extension ProductsViewController {
434434
}
435435
}
436436

437-
@objc func sortButtonTapped() {
438-
UIAlertController.presentSortProductsActionSheet(viewController: self, onSelect: { [weak self] sortOrder in
439-
self?.sortOrder = sortOrder
440-
}, onCancel: { [weak self] in
441-
self?.dismiss(animated: true, completion: nil)
442-
})
437+
@objc func sortButtonTapped(sender: UIButton) {
438+
let title = NSLocalizedString("Sort by",
439+
comment: "Message title for sort products action bottom sheet")
440+
let viewProperties = BottomSheetListSelectorViewProperties(title: title)
441+
let command = ProductsSortOrderBottomSheetListSelectorCommand(selected: sortOrder)
442+
let sortOrderListViewController = BottomSheetListSelectorViewController(viewProperties: viewProperties,
443+
command: command) { [weak self] selectedSortOrder in
444+
defer {
445+
self?.dismiss(animated: true, completion: nil)
446+
}
447+
448+
guard let selectedSortOrder = selectedSortOrder else {
449+
return
450+
}
451+
self?.sortOrder = selectedSortOrder
452+
}
453+
454+
let bottomSheet = BottomSheetViewController(childViewController: sortOrderListViewController)
455+
bottomSheet.show(from: self, sourceView: sender, arrowDirections: .up)
443456
}
444457

445458
@objc func filterButtonTapped() {

WooCommerce/Classes/ViewRelated/Products/UIAlertController+SortProducts.swift

Lines changed: 0 additions & 55 deletions
This file was deleted.

WooCommerce/Vendors/WPiOS-BottomSheet/BottomSheetViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class BottomSheetViewController: UIViewController {
3434
popoverPresentationController?.permittedArrowDirections = arrowDirections
3535
popoverPresentationController?.sourceView = sourceView ?? UIView()
3636
popoverPresentationController?.sourceRect = sourceView?.bounds ?? .zero
37+
popoverPresentationController?.backgroundColor = view.backgroundColor
3738
} else {
3839
transitioningDelegate = self
3940
modalPresentationStyle = .custom
@@ -67,8 +68,8 @@ class BottomSheetViewController: UIViewController {
6768
view.clipsToBounds = true
6869
view.layer.cornerRadius = Constants.cornerRadius
6970
view.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
70-
view.backgroundColor = .basicBackground
7171

72+
view.backgroundColor = childViewController?.view.backgroundColor
7273
NSLayoutConstraint.activate([
7374
gripButton.heightAnchor.constraint(equalToConstant: Constants.gripHeight)
7475
])

0 commit comments

Comments
 (0)