Skip to content

Commit cd318eb

Browse files
Merge pull request #323 from woocommerce/issue/306-pending-orders-filter
New Orders: Filtering Processing
2 parents cb258ca + 06a03b6 commit cd318eb

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/MyStore/NewOrdersViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private extension NewOrdersViewController {
9898

9999
@IBAction func buttonTouchUpInside(_ sender: UIButton) {
100100
sender.fadeOutSelectedBackground {
101-
MainTabBarController.switchToOrdersTab()
101+
MainTabBarController.switchToOrdersTab(filter: .processing)
102102
}
103103
}
104104

WooCommerce/Classes/ViewRelated/MainTabBarController.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import UIKit
22
import Gridicons
3+
import Yosemite
4+
35

46
/// Enum representing the individual tabs
57
///
@@ -78,8 +80,14 @@ extension MainTabBarController {
7880

7981
/// Switches to the Orders tab and pops to the root view controller
8082
///
81-
static func switchToOrdersTab() {
83+
static func switchToOrdersTab(filter: OrderStatus? = nil) {
8284
navigateTo(.orders)
85+
86+
guard let ordersViewController: OrdersViewController = childViewController() else {
87+
return
88+
}
89+
90+
ordersViewController.statusFilter = filter
8391
}
8492

8593
/// Switches to the Notifications tab and pops to the root view controller
@@ -88,6 +96,8 @@ extension MainTabBarController {
8896
navigateTo(.notifications)
8997
}
9098

99+
/// Switches the TabBarcController to the specified Tab
100+
///
91101
private static func navigateTo(_ tab: WooTab) {
92102
guard let tabBar = AppDelegate.shared.tabBarController else {
93103
return
@@ -98,4 +108,15 @@ extension MainTabBarController {
98108
navController.popToRootViewController(animated: false)
99109
}
100110
}
111+
112+
/// Returns the "Top Visible Child" of the specified type
113+
///
114+
private static func childViewController<T: UIViewController>() -> T? {
115+
let selectedViewController = AppDelegate.shared.tabBarController?.selectedViewController
116+
guard let navController = selectedViewController as? UINavigationController else {
117+
return selectedViewController as? T
118+
}
119+
120+
return navController.topViewController as? T
121+
}
101122
}

WooCommerce/Classes/ViewRelated/Orders/OrdersViewController.swift

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ class OrdersViewController: UIViewController {
4040

4141
/// OrderStatus that must be matched by retrieved orders.
4242
///
43-
private var statusFilter: OrderStatus? {
43+
var statusFilter: OrderStatus? {
4444
didSet {
45+
guard isViewLoaded else {
46+
return
47+
}
48+
4549
guard oldValue?.rawValue != statusFilter?.rawValue else {
4650
return
4751
}
@@ -93,8 +97,11 @@ class OrdersViewController: UIViewController {
9397
override func viewDidLoad() {
9498
super.viewDidLoad()
9599

100+
refreshTitle()
101+
refreshResultsPredicate()
96102
configureSyncingCoordinator()
97103
configureNavigation()
104+
configureTabBarItem()
98105
configureTableView()
99106
configureResultsController()
100107
}
@@ -111,8 +118,22 @@ class OrdersViewController: UIViewController {
111118
//
112119
private extension OrdersViewController {
113120

121+
func refreshTitle() {
122+
guard let filter = statusFilter?.rawValue.capitalized else {
123+
navigationItem.title = NSLocalizedString("Orders", comment: "Orders Title")
124+
return
125+
}
126+
127+
navigationItem.title = NSLocalizedString("Orders: \(filter)", comment: "Orders Title")
128+
}
129+
130+
func refreshResultsPredicate() {
131+
resultsController.predicate = statusFilter.map { NSPredicate(format: "status = %@", $0.rawValue) }
132+
tableView.setContentOffset(.zero, animated: false)
133+
tableView.reloadData()
134+
}
135+
114136
func configureNavigation() {
115-
title = NSLocalizedString("Orders", comment: "Orders title")
116137
let rightBarButton = UIBarButtonItem(image: Gridicon.iconOfType(.menus),
117138
style: .plain,
118139
target: self,
@@ -127,6 +148,10 @@ private extension OrdersViewController {
127148
navigationItem.backBarButtonItem = UIBarButtonItem(title: String(), style: .plain, target: nil, action: nil)
128149
}
129150

151+
func configureTabBarItem() {
152+
tabBarItem.title = NSLocalizedString("Orders", comment: "Orders title")
153+
}
154+
130155
func configureTableView() {
131156
view.backgroundColor = StyleManager.tableViewBackgroundColor
132157
tableView.backgroundColor = StyleManager.tableViewBackgroundColor
@@ -181,11 +206,12 @@ extension OrdersViewController {
181206
//
182207
private extension OrdersViewController {
183208

184-
private func didChangeFilter(newFilter: OrderStatus?) {
209+
func didChangeFilter(newFilter: OrderStatus?) {
210+
// Display the Filter in the Title
211+
refreshTitle()
212+
185213
// Filter right away the cached orders
186-
resultsController.predicate = newFilter.map { NSPredicate(format: "status = %@", $0.rawValue) }
187-
tableView.setContentOffset(.zero, animated: false)
188-
tableView.reloadData()
214+
refreshResultsPredicate()
189215

190216
// Drop Cache (If Needed) + Re-Sync First Page
191217
ensureStoredOrdersAreReset { [weak self] in

0 commit comments

Comments
 (0)