Skip to content

Commit 3f240a2

Browse files
committed
Move coupon sync to service
1 parent 0f236e7 commit 3f240a2

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

WooCommerce/Classes/POS/Controllers/PointOfSaleCouponsController.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,8 @@ private extension PointOfSaleCouponsController {
4848
itemsViewState = ItemsViewState(containerState: .content,
4949
itemsStack: .init(root: .loaded(coupons, hasMoreItems: false),
5050
itemStates: [:]))
51-
52-
await syncCoupons()
5351
} catch {
5452
debugPrint(error)
5553
}
5654
}
57-
58-
@MainActor
59-
func syncCoupons() async {
60-
guard let siteID = ServiceLocator.stores.sessionManager.defaultStoreID else {
61-
return
62-
}
63-
let action = CouponAction.synchronizeCoupons(siteID: siteID,
64-
pageNumber: 1,
65-
pageSize: 25,
66-
onCompletion: { _ in })
67-
ServiceLocator.stores.dispatch(action)
68-
}
6955
}

WooCommerce/Classes/ViewRelated/Hub Menu/HubMenuViewModel.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ final class HubMenuViewModel: ObservableObject {
108108
private(set) lazy var posCouponProvider: PointOfSaleItemServiceProtocol = {
109109
let storage = ServiceLocator.storageManager
110110
let currencySettings = ServiceLocator.currencySettings
111+
let stores = ServiceLocator.stores
111112

112113
return PointOfSaleCouponService(siteID: siteID,
113114
currencySettings: currencySettings,
114115
credentials: credentials,
116+
stores: stores,
115117
storage: storage)
116118
}()
117119

Yosemite/Yosemite/PointOfSale/PointOfSaleCouponService.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,30 @@ public final class PointOfSaleCouponService: PointOfSaleItemServiceProtocol {
1010
private var siteID: Int64
1111
private let currencyFormatter: CurrencyFormatter
1212
private let couponsRemote: CouponsRemote
13+
private let stores: StoresManager?
1314
private let storage: StorageManagerType?
1415

1516
public init(siteID: Int64,
1617
currencySettings: CurrencySettings,
1718
network: Network,
19+
stores: StoresManager? = nil,
1820
storage: StorageManagerType? = nil) {
1921
self.siteID = siteID
2022
self.currencyFormatter = CurrencyFormatter(currencySettings: currencySettings)
2123
self.couponsRemote = CouponsRemote(network: network)
24+
self.stores = stores
2225
self.storage = storage
2326
}
2427

2528
public convenience init(siteID: Int64,
2629
currencySettings: CurrencySettings,
2730
credentials: Credentials?,
31+
stores: StoresManager,
2832
storage: StorageManagerType) {
2933
self.init(siteID: siteID,
3034
currencySettings: currencySettings,
3135
network: AlamofireNetwork(credentials: credentials),
36+
stores: stores,
3237
storage: storage)
3338
}
3439

@@ -72,6 +77,20 @@ public final class PointOfSaleCouponService: PointOfSaleItemServiceProtocol {
7277
@MainActor
7378
public func providePointOfSaleItems(pageNumber: Int) async throws -> PagedItems<POSItem> {
7479
let coupons = await providePointOfSaleCoupons()
80+
81+
syncCouponsFromRemote(pageNumber: pageNumber)
82+
7583
return .init(items: coupons, hasMorePages: false)
7684
}
85+
86+
private func syncCouponsFromRemote(pageNumber: Int) {
87+
guard let stores = stores else {
88+
return
89+
}
90+
let action = CouponAction.synchronizeCoupons(siteID: siteID,
91+
pageNumber: pageNumber,
92+
pageSize: 25,
93+
onCompletion: { _ in })
94+
stores.dispatch(action)
95+
}
7796
}

0 commit comments

Comments
 (0)