Skip to content

Commit 9590ff7

Browse files
committed
feat: Hub Menu Badges Update when a reviews push notification arrives
1 parent da971f5 commit 9590ff7

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ final class HubMenuViewModel: ObservableObject {
4343

4444
@Published var showingReviewDetail = false
4545

46+
@Published var reviewsNotificationBadgeCount = 0
47+
4648
private var productReviewFromNoteParcel: ProductReviewFromNoteParcel?
4749

4850
private var storePickerCoordinator: StorePickerCoordinator?
@@ -57,6 +59,8 @@ final class HubMenuViewModel: ObservableObject {
5759
menuElements.append(.coupons)
5860
}
5961
observeSiteForUIUpdates()
62+
startListeningToReviewsBadgeUpdates()
63+
loadReviewsNotificationCountAndUpdateBadge()
6064
}
6165

6266
/// Present the `StorePickerViewController` using the `StorePickerCoordinator`, passing the navigation controller from the entry point.
@@ -83,12 +87,38 @@ final class HubMenuViewModel: ObservableObject {
8387
}
8488

8589
private func observeSiteForUIUpdates() {
86-
ServiceLocator.stores.site.sink { site in
87-
// This will be useful in the future for updating some info of the screen depending on the store site info
90+
ServiceLocator.stores.site.sink { [weak self] site in
91+
self?.loadReviewsNotificationCountAndUpdateBadge()
8892
}.store(in: &cancellables)
8993
}
9094
}
9195

96+
// MARK: - Hub Menu Badges Updates
97+
//
98+
private extension HubMenuViewModel {
99+
100+
/// Setup: KVO Hooks.
101+
///
102+
func startListeningToReviewsBadgeUpdates() {
103+
NotificationCenter.default.addObserver(self,
104+
selector: #selector(loadReviewsNotificationCountAndUpdateBadge),
105+
name: .reviewsBadgeReloadRequired,
106+
object: nil)
107+
}
108+
109+
@objc func loadReviewsNotificationCountAndUpdateBadge() {
110+
let stores: StoresManager = ServiceLocator.stores
111+
guard let siteID = stores.sessionManager.defaultStoreID else {
112+
return
113+
}
114+
115+
let action = NotificationCountAction.load(siteID: siteID, type: .kind(.comment)) { [weak self] count in
116+
self?.reviewsNotificationBadgeCount = count
117+
}
118+
stores.dispatch(action)
119+
}
120+
}
121+
92122
extension HubMenuViewModel {
93123
enum Menu: CaseIterable {
94124
case woocommerceAdmin

0 commit comments

Comments
 (0)