Skip to content

Commit f858d2d

Browse files
committed
Make sure the order status is updated in the view model
1 parent 9cc69b6 commit f858d2d

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

WooCommerce/Classes/ViewRelated/Orders/OrderDetailsViewController.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ class OrderDetailsViewController: UIViewController {
3636
return EntityListener(storageManager: AppDelegate.shared.storageManager, readOnlyEntity: viewModel.order)
3737
}()
3838

39+
/// ResultsController: Surrounds us. Binds the galaxy together. And also, keeps the UITableView <> (Stored) OrderStatuses in sync.
40+
///
41+
private lazy var statusResultsController: ResultsController<StorageOrderStatus> = {
42+
let storageManager = AppDelegate.shared.storageManager
43+
let predicate = NSPredicate(format: "siteID == %lld", StoresManager.shared.sessionManager.defaultStoreID ?? Int.min)
44+
let descriptor = NSSortDescriptor(key: "slug", ascending: true)
45+
46+
return ResultsController<StorageOrderStatus>(storageManager: storageManager, matching: predicate, sortedBy: [descriptor])
47+
}()
48+
3949
/// Sections to be rendered
4050
///
4151
private var sections = [Section]()
@@ -70,6 +80,7 @@ class OrderDetailsViewController: UIViewController {
7080
configureNavigation()
7181
configureTableView()
7282
configureEntityListener()
83+
configureResultsController()
7384
registerTableViewCells()
7485
registerTableViewHeaderFooters()
7586
}
@@ -114,7 +125,8 @@ private extension OrderDetailsViewController {
114125
return
115126
}
116127

117-
self.viewModel = OrderDetailsViewModel(order: order)
128+
let orderStatus = self.lookUpOrderStatus(for: order)
129+
self.viewModel = OrderDetailsViewModel(order: order, orderStatus: orderStatus)
118130
}
119131

120132
entityListener.onDelete = { [weak self] in
@@ -127,6 +139,12 @@ private extension OrderDetailsViewController {
127139
}
128140
}
129141

142+
/// Setup: Results Controller
143+
///
144+
private func configureResultsController() {
145+
try? statusResultsController.performFetch()
146+
}
147+
130148
/// Reloads the tableView, granted that the view has been effectively loaded.
131149
///
132150
func reloadTableViewIfPossible() {
@@ -533,6 +551,15 @@ private extension OrderDetailsViewController {
533551

534552
StoresManager.shared.dispatch(action)
535553
}
554+
555+
func lookUpOrderStatus(for order: Order) -> OrderStatus? {
556+
let listAll = statusResultsController.fetchedObjects
557+
for orderStatus in listAll where orderStatus.slug == order.statusKey {
558+
return orderStatus
559+
}
560+
561+
return nil
562+
}
536563
}
537564

538565

0 commit comments

Comments
 (0)