Skip to content

Commit cfaaff8

Browse files
committed
Avoid using computed variables for fetched objects on order details
1 parent 0f31c1d commit cfaaff8

File tree

1 file changed

+61
-50
lines changed

1 file changed

+61
-50
lines changed

WooCommerce/Classes/ViewModels/Order Details/OrderDetailsResultsControllers.swift

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -88,72 +88,41 @@ final class OrderDetailsResultsControllers {
8888

8989
/// Order shipment tracking list
9090
///
91-
var orderTracking: [ShipmentTracking] {
92-
return trackingResultsController.fetchedObjects
93-
}
91+
private(set) var orderTracking: [ShipmentTracking] = []
9492

9593
/// Order statuses list
9694
///
97-
var currentSiteStatuses: [OrderStatus] {
98-
return statusResultsController.fetchedObjects
99-
}
95+
private(set) var currentSiteStatuses: [OrderStatus] = []
10096

10197
/// Products from an Order
10298
///
103-
var products: [ProductListItem] {
104-
productResultsController.listItemObjects
105-
}
99+
private(set) var products: [ProductListItem] = []
106100

107101
/// ProductVariations from an Order
108102
///
109-
var productVariations: [ProductVariation] {
110-
return productVariationResultsController.fetchedObjects
111-
}
103+
private(set) var productVariations: [ProductVariation] = []
112104

113105
/// Refunds in an Order
114106
///
115-
var refunds: [Refund] {
116-
return refundResultsController.fetchedObjects
117-
}
107+
private(set) var refunds: [Refund] = []
118108

119109
/// Shipping labels for an Order
120110
///
121-
var shippingLabels: [ShippingLabel] {
122-
guard shipments.isEmpty else {
123-
return shipments.compactMap { $0.shippingLabel }
124-
}
125-
return order.shippingLabels.sorted(by: { label1, label2 in
126-
if let shipmentID1 = label1.shipmentID,
127-
let shipmentID2 = label2.shipmentID {
128-
return shipmentID1.localizedStandardCompare(shipmentID2) == .orderedAscending
129-
}
130-
return label1.dateCreated < label2.dateCreated
131-
})
132-
}
111+
private(set) var shippingLabels: [ShippingLabel] = []
133112

134-
var shipments: [WooShippingShipment] {
135-
shipmentResultsController.fetchedObjects
136-
}
113+
private(set) var shipments: [WooShippingShipment] = []
137114

138115
/// Site's add-on groups.
139116
///
140-
var addOnGroups: [AddOnGroup] {
141-
return addOnGroupResultsController.fetchedObjects
142-
}
117+
private(set) var addOnGroups: [AddOnGroup] = []
143118

144-
var sitePlugins: [SitePlugin] {
145-
return sitePluginsResultsController.fetchedObjects
146-
}
119+
private(set) var sitePlugins: [SitePlugin] = []
147120

148-
var feeLines: [OrderFeeLine] {
149-
return order.fees
150-
}
121+
private(set) var feeLines: [OrderFeeLine] = []
151122

152123
/// Shipping methods list
153124
///
154-
var siteShippingMethods: [ShippingMethod] {
155-
return shippingMethodsResultsController.fetchedObjects
156-
}
125+
private(set) var siteShippingMethods: [ShippingMethod] = []
157126

158127
/// Completion handler for when results controllers reload.
159128
///
@@ -164,6 +133,8 @@ final class OrderDetailsResultsControllers {
164133
self.order = order
165134
self.siteID = order.siteID
166135
self.storageManager = storageManager
136+
feeLines = order.fees
137+
updateShippingLabels()
167138
}
168139

169140
func configureResultsControllers(onReload: @escaping () -> Void) {
@@ -181,6 +152,8 @@ final class OrderDetailsResultsControllers {
181152

182153
func update(order: Order) {
183154
self.order = order
155+
feeLines = order.fees
156+
updateShippingLabels()
184157
// Product variation results controller depends on order items to load variations,
185158
// so we need to recreate it whenever receiving an updated order.
186159
self.productVariationResultsController = getProductVariationResultsController()
@@ -202,7 +175,9 @@ private extension OrderDetailsResultsControllers {
202175
}
203176

204177
func configureShipmentResultsController(onReload: @escaping () -> Void) {
205-
shipmentResultsController.onDidChangeContent = {
178+
shipmentResultsController.onDidChangeContent = { [weak self] in
179+
guard let self else { return }
180+
shipments = shipmentResultsController.fetchedObjects
206181
onReload()
207182
}
208183

@@ -216,6 +191,7 @@ private extension OrderDetailsResultsControllers {
216191

217192
do {
218193
try shipmentResultsController.performFetch()
194+
shipments = shipmentResultsController.fetchedObjects
219195
} catch {
220196
DDLogError("⛔️ Unable to fetch shipments: \(error)")
221197
}
@@ -224,13 +200,16 @@ private extension OrderDetailsResultsControllers {
224200
func configureStatusResultsController() {
225201
do {
226202
try statusResultsController.performFetch()
203+
currentSiteStatuses = statusResultsController.fetchedObjects
227204
} catch {
228205
DDLogError("⛔️ Unable to fetch Order Statuses: \(error)")
229206
}
230207
}
231208

232209
private func configureTrackingResultsController(onReload: @escaping () -> Void) {
233-
trackingResultsController.onDidChangeContent = {
210+
trackingResultsController.onDidChangeContent = { [weak self] in
211+
guard let self else { return }
212+
orderTracking = trackingResultsController.fetchedObjects
234213
onReload()
235214
}
236215

@@ -244,13 +223,16 @@ private extension OrderDetailsResultsControllers {
244223

245224
do {
246225
try trackingResultsController.performFetch()
226+
orderTracking = trackingResultsController.fetchedObjects
247227
} catch {
248228
DDLogError("⛔️ Unable to fetch Order \(order.orderID) shipment tracking details: \(error)")
249229
}
250230
}
251231

252232
private func configureProductResultsController(onReload: @escaping () -> Void) {
253-
productResultsController.onDidChangeContent = {
233+
productResultsController.onDidChangeContent = { [weak self] in
234+
guard let self else { return }
235+
products = productResultsController.listItemObjects
254236
onReload()
255237
}
256238

@@ -264,13 +246,16 @@ private extension OrderDetailsResultsControllers {
264246

265247
do {
266248
try productResultsController.performFetch()
249+
products = productResultsController.listItemObjects
267250
} catch {
268251
DDLogError("⛔️ Unable to fetch Products for Site \(siteID): \(error)")
269252
}
270253
}
271254

272255
private func configureProductVariationResultsController(onReload: @escaping () -> Void) {
273-
productVariationResultsController.onDidChangeContent = {
256+
productVariationResultsController.onDidChangeContent = { [weak self] in
257+
guard let self else { return }
258+
productVariations = productVariationResultsController.fetchedObjects
274259
onReload()
275260
}
276261

@@ -284,13 +269,16 @@ private extension OrderDetailsResultsControllers {
284269

285270
do {
286271
try productVariationResultsController.performFetch()
272+
productVariations = productVariationResultsController.fetchedObjects
287273
} catch {
288274
DDLogError("⛔️ Error fetching ProductVariations for Order \(order.orderID): \(error)")
289275
}
290276
}
291277

292278
private func configureRefundResultsController(onReload: @escaping () -> Void) {
293-
refundResultsController.onDidChangeContent = {
279+
refundResultsController.onDidChangeContent = { [weak self] in
280+
guard let self else { return }
281+
refunds = refundResultsController.fetchedObjects
294282
onReload()
295283
}
296284

@@ -304,13 +292,16 @@ private extension OrderDetailsResultsControllers {
304292

305293
do {
306294
try refundResultsController.performFetch()
295+
refunds = refundResultsController.fetchedObjects
307296
} catch {
308297
DDLogError("⛔️ Unable to fetch Refunds for Site \(siteID) and Order \(order.orderID): \(error)")
309298
}
310299
}
311300

312301
private func configureAddOnGroupResultsController(onReload: @escaping () -> Void) {
313-
addOnGroupResultsController.onDidChangeContent = {
302+
addOnGroupResultsController.onDidChangeContent = { [weak self] in
303+
guard let self else { return }
304+
addOnGroups = addOnGroupResultsController.fetchedObjects
314305
onReload()
315306
}
316307

@@ -322,13 +313,16 @@ private extension OrderDetailsResultsControllers {
322313

323314
do {
324315
try addOnGroupResultsController.performFetch()
316+
addOnGroups = addOnGroupResultsController.fetchedObjects
325317
} catch {
326318
DDLogError("⛔️ Unable to fetch AddOnGroups for Site \(siteID): \(error)")
327319
}
328320
}
329321

330322
private func configureSitePluginsResultsController(onReload: @escaping () -> Void) {
331-
sitePluginsResultsController.onDidChangeContent = {
323+
sitePluginsResultsController.onDidChangeContent = { [weak self] in
324+
guard let self else { return }
325+
sitePlugins = sitePluginsResultsController.fetchedObjects
332326
onReload()
333327
}
334328

@@ -340,13 +334,16 @@ private extension OrderDetailsResultsControllers {
340334

341335
do {
342336
try sitePluginsResultsController.performFetch()
337+
sitePlugins = sitePluginsResultsController.fetchedObjects
343338
} catch {
344339
DDLogError("⛔️ Unable to fetch Site Plugins for Site \(siteID): \(error)")
345340
}
346341
}
347342

348343
private func configureShippingMethodsResultsController(onReload: @escaping () -> Void) {
349-
shippingMethodsResultsController.onDidChangeContent = {
344+
shippingMethodsResultsController.onDidChangeContent = { [weak self] in
345+
guard let self else { return }
346+
siteShippingMethods = shippingMethodsResultsController.fetchedObjects
350347
onReload()
351348
}
352349

@@ -358,6 +355,7 @@ private extension OrderDetailsResultsControllers {
358355

359356
do {
360357
try shippingMethodsResultsController.performFetch()
358+
siteShippingMethods = shippingMethodsResultsController.fetchedObjects
361359
} catch {
362360
DDLogError("⛔️ Unable to fetch Shipping Methods for Site \(siteID): \(error)")
363361
}
@@ -375,4 +373,17 @@ private extension OrderDetailsResultsControllers {
375373
try? sitePluginsResultsController.performFetch()
376374
try? shippingMethodsResultsController.performFetch()
377375
}
376+
377+
guard shipments.isEmpty else {
378+
shippingLabels = shipments.compactMap { $0.shippingLabel }
379+
return
380+
}
381+
shippingLabels = order.shippingLabels.sorted(by: { label1, label2 in
382+
if let shipmentID1 = label1.shipmentID,
383+
let shipmentID2 = label2.shipmentID {
384+
return shipmentID1.localizedStandardCompare(shipmentID2) == .orderedAscending
385+
}
386+
return label1.dateCreated < label2.dateCreated
387+
})
388+
}
378389
}

0 commit comments

Comments
 (0)