Skip to content

Commit 0929304

Browse files
committed
Observe locally and remotely created orders to reset the last sync timestamp for each time range tab.
1 parent e042f78 commit 0929304

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Stats v4/StoreStatsAndTopPerformersViewController.swift

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,18 @@ final class StoreStatsAndTopPerformersViewController: ButtonBarPagerTabStripView
3535
private let usageTracksEventEmitter = StoreStatsUsageTracksEventEmitter()
3636
private let dashboardViewModel: DashboardViewModel
3737

38+
private let pushNotificationsManager: PushNotesManager
39+
private var localOrdersSubscription: AnyCancellable?
40+
private var remoteOrdersSubscription: AnyCancellable?
41+
3842
// MARK: - View Lifecycle
3943

40-
init(siteID: Int64, dashboardViewModel: DashboardViewModel) {
44+
init(siteID: Int64,
45+
dashboardViewModel: DashboardViewModel,
46+
pushNotificationsManager: PushNotesManager = ServiceLocator.pushNotesManager) {
4147
self.siteID = siteID
4248
self.dashboardViewModel = dashboardViewModel
49+
self.pushNotificationsManager = pushNotificationsManager
4350
super.init(nibName: nil, bundle: nil)
4451
}
4552

@@ -54,6 +61,8 @@ final class StoreStatsAndTopPerformersViewController: ButtonBarPagerTabStripView
5461

5562
super.viewDidLoad()
5663
configureView()
64+
observeRemotelyCreatedOrdersToResetLastSyncTimestamp()
65+
observeLocallyCreatedOrdersToResetLastSyncTimestamp()
5766
}
5867

5968
override func viewWillAppear(_ animated: Bool) {
@@ -255,6 +264,35 @@ private extension StoreStatsAndTopPerformersViewController {
255264
periodViewController.refreshControl.endRefreshing()
256265
}
257266
}
267+
268+
func observeRemotelyCreatedOrdersToResetLastSyncTimestamp() {
269+
let siteID = self.siteID
270+
remoteOrdersSubscription = Publishers
271+
.Merge(pushNotificationsManager.inactiveNotifications, pushNotificationsManager.foregroundNotificationsToView)
272+
.filter { $0.kind == .storeOrder && $0.siteID == siteID }
273+
.sink { [weak self] _ in
274+
self?.resetLastSyncTimestamp()
275+
}
276+
}
277+
278+
func observeLocallyCreatedOrdersToResetLastSyncTimestamp() {
279+
let action = OrderAction.observeInsertedOrders(siteID: siteID) { [weak self] observableInsertedOrders in
280+
guard let self = self else { return }
281+
self.localOrdersSubscription = observableInsertedOrders
282+
.filter { $0.isNotEmpty }
283+
.sink { [weak self] _ in
284+
guard let self = self else { return }
285+
self.resetLastSyncTimestamp()
286+
}
287+
}
288+
ServiceLocator.stores.dispatch(action)
289+
}
290+
291+
func resetLastSyncTimestamp() {
292+
periodVCs.forEach { periodVC in
293+
periodVC.lastFullSyncTimestamp = nil
294+
}
295+
}
258296
}
259297

260298
// MARK: - Placeholders

0 commit comments

Comments
 (0)