Skip to content

Commit 2539df0

Browse files
committed
Observe mocked item instead of storage item
1 parent 90c82cd commit 2539df0

File tree

2 files changed

+111
-9
lines changed

2 files changed

+111
-9
lines changed

Modules/Sources/Networking/Model/Stats/TopEarnerStatsItem.swift

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,91 @@ extension TopEarnerStatsItem: Identifiable {
7373
productID
7474
}
7575
}
76+
77+
// MARK: - Helper to init Product
78+
//
79+
public extension Product {
80+
init(siteID: Int64,
81+
productID: Int64,
82+
name: String,
83+
images: [ProductImage]) {
84+
self.init(siteID: siteID,
85+
productID: productID,
86+
name: name,
87+
slug: "",
88+
permalink: "",
89+
date: Date(),
90+
dateCreated: Date(),
91+
dateModified: nil,
92+
dateOnSaleStart: nil,
93+
dateOnSaleEnd: nil,
94+
productTypeKey: ProductType.simple.rawValue,
95+
statusKey: ProductStatus.draft.rawValue,
96+
featured: false,
97+
catalogVisibilityKey: ProductCatalogVisibility.visible.rawValue,
98+
fullDescription: "",
99+
shortDescription: "",
100+
sku: "",
101+
globalUniqueID: "",
102+
price: "",
103+
regularPrice: "",
104+
salePrice: "",
105+
onSale: false,
106+
purchasable: false,
107+
totalSales: 0,
108+
virtual: false,
109+
downloadable: false,
110+
downloads: [],
111+
downloadLimit: -1,
112+
downloadExpiry: -1,
113+
buttonText: "",
114+
externalURL: "",
115+
taxStatusKey: ProductTaxStatus.taxable.rawValue,
116+
taxClass: "",
117+
manageStock: false,
118+
stockQuantity: nil,
119+
stockStatusKey: ProductStockStatus.inStock.rawValue,
120+
backordersKey: ProductBackordersSetting.notAllowed.rawValue,
121+
backordersAllowed: false,
122+
backordered: false,
123+
soldIndividually: false,
124+
weight: "",
125+
dimensions: ProductDimensions(length: "", width: "", height: ""),
126+
shippingRequired: true,
127+
shippingTaxable: true,
128+
shippingClass: "",
129+
shippingClassID: 0,
130+
productShippingClass: nil,
131+
reviewsAllowed: true,
132+
averageRating: "",
133+
ratingCount: 0,
134+
relatedIDs: [],
135+
upsellIDs: [],
136+
crossSellIDs: [],
137+
parentID: 0,
138+
purchaseNote: "",
139+
categories: [],
140+
tags: [],
141+
images: images,
142+
attributes: [],
143+
defaultAttributes: [],
144+
variations: [],
145+
groupedProducts: [],
146+
menuOrder: 0,
147+
addOns: [],
148+
isSampleItem: false,
149+
bundleStockStatus: nil,
150+
bundleStockQuantity: nil,
151+
bundleMinSize: nil,
152+
bundleMaxSize: nil,
153+
bundledItems: [],
154+
password: nil,
155+
compositeComponents: [],
156+
subscription: nil,
157+
minAllowedQuantity: nil,
158+
maxAllowedQuantity: nil,
159+
groupOfQuantity: nil,
160+
combineVariationQuantities: nil,
161+
customFields: [])
162+
}
163+
}

WooCommerce/Classes/ViewRelated/Dashboard/TopPerformers/TopPerformersDashboardViewModel.swift

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,32 @@ private extension TopPerformersDashboardViewModel {
232232
.store(in: &subscriptions)
233233
}
234234

235-
func observeProducts(ids: [Int64]) {
235+
func observeProducts(for items: [TopEarnerStatsItem]) {
236+
let ids = items.map { $0.productID }
236237
entityListeners = entityListeners.filter { ids.contains($0.key) }
237-
for id in ids {
238-
if entityListeners[id] == nil, let listener = createProductEntityListener(id: id) {
239-
entityListeners[id] = listener
238+
for item in items {
239+
if entityListeners[item.productID] == nil {
240+
let listener = createProductEntityListener(for: item)
241+
entityListeners[item.productID] = listener
240242
}
241243
}
242244
}
243245

244-
func createProductEntityListener(id: Int64) -> EntityListener<Product>? {
245-
guard var product = storageManager.viewStorage.loadProduct(siteID: siteID, productID: id)?.toReadOnly() else {
246-
return nil
247-
}
246+
func createProductEntityListener(for item: TopEarnerStatsItem) -> EntityListener<Product> {
247+
/// Mock product item out of info from top start item
248+
var product = Product(
249+
siteID: siteID,
250+
productID: item.productID,
251+
name: item.productName ?? "",
252+
images: [ProductImage(
253+
imageID: -1,
254+
dateCreated: Date(),
255+
dateModified: nil,
256+
src: item.imageUrl ?? "",
257+
name: nil,
258+
alt: nil)
259+
]
260+
)
248261
let entityListener = EntityListener(storageManager: ServiceLocator.storageManager, readOnlyEntity: product)
249262
entityListener.onUpsert = { [weak self] updatedProduct in
250263
// reload stats if there are changes to product
@@ -306,10 +319,11 @@ private extension TopPerformersDashboardViewModel {
306319
return
307320
}
308321
guard let items = topEarnerStats?.items?.sorted(by: >), items.isNotEmpty else {
322+
entityListeners = [:]
309323
return periodViewModel.update(state: .loaded(rows: []))
310324
}
311325
periodViewModel.update(state: .loaded(rows: items))
312-
observeProducts(ids: items.map { $0.productID })
326+
observeProducts(for: items)
313327
}
314328

315329
func createResultsController(timeRange: StatsTimeRangeV4) -> ResultsController<StorageTopEarnerStats> {

0 commit comments

Comments
 (0)