@@ -5,11 +5,10 @@ class OrdersViewController: UIViewController {
55
66 @IBOutlet weak var tableView : UITableView !
77 var orders = [ Order] ( )
8- var searchResults = [ Order] ( )
9- let searchController = UISearchController ( searchResultsController: nil )
8+ var filterResults = [ Order] ( )
109 private var isUsingFilterAction = false
1110 var displaysNoResults : Bool {
12- return searchResults . isEmpty && isUsingFilterAction
11+ return filterResults . isEmpty && isUsingFilterAction
1312 }
1413
1514 func loadSampleOrders( ) -> [ Order ] {
@@ -67,13 +66,12 @@ class OrdersViewController: UIViewController {
6766 super. viewDidLoad ( )
6867 configureNavigation ( )
6968 configureTableView ( )
70- configureSearch ( )
7169 orders = loadSampleOrders ( )
7270 }
7371
7472 func configureNavigation( ) {
7573 title = NSLocalizedString ( " Orders " , comment: " Orders title " )
76- let rightBarButton = UIBarButtonItem ( image: Gridicon . iconOfType ( . listUnordered ) ,
74+ let rightBarButton = UIBarButtonItem ( image: Gridicon . iconOfType ( . menus ) ,
7775 style: . plain,
7876 target: self ,
7977 action: #selector( rightButtonTapped) )
@@ -96,24 +94,6 @@ class OrdersViewController: UIViewController {
9694 tableView. register ( nib, forCellReuseIdentifier: NoResultsTableViewCell . reuseIdentifier)
9795 }
9896
99- func configureSearch( ) {
100- searchController. searchResultsUpdater = self
101- searchController. obscuresBackgroundDuringPresentation = false
102- searchController. hidesNavigationBarDuringPresentation = false
103- searchController. searchBar. delegate = self
104- searchController. searchBar. placeholder = NSLocalizedString ( " Search all orders " , comment: " Search placeholder text " )
105- searchController. searchBar. sizeToFit ( )
106-
107- // This may need set app-wide in the future. Not yet.
108- searchController. searchBar. barTintColor = tableView. backgroundColor
109- searchController. searchBar. layer. borderWidth = 1
110- searchController. searchBar. layer. borderColor = tableView. backgroundColor? . cgColor
111-
112- // add it to the table header, which leaves us room for pull-to-refresh control
113- tableView. tableHeaderView = searchController. searchBar
114- tableView. contentOffset = CGPoint ( x: 0.0 , y: searchController. searchBar. frame. size. height)
115- }
116-
11797 @objc func rightButtonTapped( ) {
11898 let actionSheet = UIAlertController ( title: nil , message: nil , preferredStyle: . actionSheet)
11999 actionSheet. view. tintColor = StyleManager . wooCommerceBrandColor
@@ -141,11 +121,11 @@ class OrdersViewController: UIViewController {
141121 return
142122 }
143123
144- searchResults = orders. filter { order in
124+ filterResults = orders. filter { order in
145125 return order. status. description. contains ( status. description)
146126 }
147127
148- isUsingFilterAction = searchResults . count != orders. count
128+ isUsingFilterAction = filterResults . count != orders. count
149129 tableView. reloadData ( )
150130 }
151131
@@ -156,21 +136,10 @@ class OrdersViewController: UIViewController {
156136 customOrders. append ( order)
157137 }
158138 }
159- searchResults = customOrders
160- isUsingFilterAction = searchResults . count != orders. count
139+ filterResults = customOrders
140+ isUsingFilterAction = filterResults . count != orders. count
161141 tableView. reloadData ( )
162142 }
163-
164- // MARK: Search bar
165- func isFiltering( ) -> Bool {
166- let usingSearch = searchController. isActive && !searchBarIsEmpty( )
167- let usingFilter = isUsingFilterAction
168- return usingSearch || usingFilter
169- }
170-
171- func searchBarIsEmpty( ) -> Bool {
172- return searchController. searchBar. text? . isEmpty ?? true
173- }
174143}
175144
176145// MARK: UITableViewDataSource
@@ -181,19 +150,19 @@ extension OrdersViewController: UITableViewDataSource {
181150 }
182151
183152 func tableView( _ tableView: UITableView , numberOfRowsInSection section: Int ) -> Int {
184- if isFiltering ( ) {
185- if searchResults . isEmpty {
186- return Constants . searchResultsNotFoundRowCount
153+ if isUsingFilterAction == true {
154+ if filterResults . isEmpty {
155+ return Constants . filterResultsNotFoundRowCount
187156 }
188- return searchResults . count
157+ return filterResults . count
189158 }
190159 return orders. count
191160 }
192161
193162 func tableView( _ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
194163 guard !displaysNoResults else {
195164 let cell = tableView. dequeueReusableCell ( withIdentifier: NoResultsTableViewCell . reuseIdentifier, for: indexPath) as! NoResultsTableViewCell
196- cell. configure ( text: NSLocalizedString ( " No results found. Clear the filter or search bar to try again. " , comment: " Displays message to user when no filter or search results were found. " ) )
165+ cell. configure ( text: NSLocalizedString ( " No results found. Clear the filter to try again. " , comment: " Displays message to user when no filter results were found. " ) )
197166 return cell
198167 }
199168 let order = orderAtIndexPath ( indexPath)
@@ -208,7 +177,7 @@ extension OrdersViewController: UITableViewDataSource {
208177 }
209178
210179 func orderAtIndexPath( _ indexPath: IndexPath ) -> Order {
211- return isFiltering ( ) ? searchResults [ indexPath. row] : orders [ indexPath. row]
180+ return isUsingFilterAction ? filterResults [ indexPath. row] : orders [ indexPath. row]
212181 }
213182}
214183
@@ -229,10 +198,6 @@ extension OrdersViewController: UITableViewDelegate {
229198 return
230199 }
231200
232- if searchController. isActive {
233- searchController. dismiss ( animated: true , completion: nil )
234- }
235-
236201 if segue. identifier == Constants . orderDetailsSegue {
237202 if let singleOrderViewController = segue. destination as? OrderDetailsViewController {
238203 let indexPath = sender as! IndexPath
@@ -245,40 +210,12 @@ extension OrdersViewController: UITableViewDelegate {
245210 }
246211}
247212
248- // MARK: UISearchResultsUpdating
249- //
250- extension OrdersViewController : UISearchResultsUpdating {
251- func updateSearchResults( for searchController: UISearchController ) {
252- guard let searchString = searchController. searchBar. text else {
253- return
254- }
255- // TODO: filter search results properly
256- searchResults = orders. filter { order in
257- return order. shippingAddress. firstName. lowercased ( ) . contains ( searchString. lowercased ( ) )
258- }
259- tableView. reloadData ( )
260- }
261- }
262-
263- // MARK: UISearchBarDelegate
264- //
265- extension OrdersViewController : UISearchBarDelegate {
266- func searchBarTextDidEndEditing( _ searchBar: UISearchBar ) {
267- tableView. reloadData ( )
268- }
269-
270- func searchBarCancelButtonClicked( _ searchBar: UISearchBar ) {
271- searchController. searchBar. resignFirstResponder ( )
272- tableView. reloadData ( )
273- }
274- }
275-
276213// MARK: Constants
277214//
278215extension OrdersViewController {
279216 struct Constants {
280217 static let rowHeight = CGFloat ( 86 )
281218 static let orderDetailsSegue = " ShowOrderDetailsViewController "
282- static let searchResultsNotFoundRowCount = 1
219+ static let filterResultsNotFoundRowCount = 1
283220 }
284221}
0 commit comments