Skip to content

Commit 4b2accc

Browse files
authored
Merge pull request #8012 from woocommerce/issue/7916-refresh-jitms
[Just In Time Messages] Refresh JITMs
2 parents 7ef4a7c + 3eb1f86 commit 4b2accc

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/DashboardViewController.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,13 @@ private extension DashboardViewController {
293293
}
294294

295295
private func openWebView(viewModel: WebViewSheetViewModel) {
296-
let cardReaderWebview = WebViewSheet(viewModel: viewModel) { [weak self] in
297-
self?.dismiss(animated: true)
296+
let webViewSheet = WebViewSheet(viewModel: viewModel) { [weak self] in
297+
guard let self = self else { return }
298+
self.dismiss(animated: true)
299+
self.viewModel.syncAnnouncements(for: self.siteID)
298300
}
299-
let hostingController = UIHostingController(rootView: cardReaderWebview)
301+
let hostingController = UIHostingController(rootView: webViewSheet)
302+
hostingController.presentationController?.delegate = self
300303
present(hostingController, animated: true, completion: nil)
301304
}
302305

@@ -383,12 +386,21 @@ private extension DashboardViewController {
383386
}
384387
}
385388

389+
// MARK: - Delegate conformance
386390
extension DashboardViewController: DashboardUIScrollDelegate {
387391
func dashboardUIScrollViewDidScroll(_ scrollView: UIScrollView) {
388392
hiddenScrollView.updateFromScrollViewDidScrollEventForLargeTitleWorkaround(scrollView)
389393
}
390394
}
391395

396+
extension DashboardViewController: UIAdaptivePresentationControllerDelegate {
397+
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
398+
if presentationController.presentedViewController is UIHostingController<WebViewSheet> {
399+
viewModel.syncAnnouncements(for: siteID)
400+
}
401+
}
402+
}
403+
392404
// MARK: - Updates
393405
//
394406
private extension DashboardViewController {
@@ -497,6 +509,7 @@ private extension DashboardViewController {
497509

498510
func pullToRefresh() async {
499511
ServiceLocator.analytics.track(.dashboardPulledToRefresh)
512+
viewModel.syncAnnouncements(for: siteID)
500513
await reloadDashboardUIStatsVersion(forced: true)
501514
}
502515
}

WooCommerce/Classes/ViewRelated/Dashboard/DashboardViewModel.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ final class DashboardViewModel {
111111
///
112112
func syncAnnouncements(for siteID: Int64) {
113113
syncProductsOnboarding(for: siteID) { [weak self] in
114-
// For now, products onboarding takes precedence over Just In Time Messages, so we can stop if there is an onboarding announcement to display.
115-
// This should be revisited when either onboarding or JITMs are expanded. See: pe5pgL-11B-p2
116-
guard let self, self.announcementViewModel == nil else { return }
117-
118-
self.syncJustInTimeMessages(for: siteID)
114+
self?.syncJustInTimeMessages(for: siteID)
119115
}
120116
}
121117

@@ -134,6 +130,11 @@ final class DashboardViewModel {
134130
MainTabBarController.presentAddProductFlow()
135131
})
136132
self?.announcementViewModel = viewModel
133+
// For now, products onboarding takes precedence over Just In Time Messages,
134+
// so we can stop if there is an onboarding announcement to display.
135+
// This should be revisited when either onboarding or JITMs are expanded. See:
136+
// pe5pgL-11B-p2
137+
return
137138
}
138139
}
139140
onCompletion()
@@ -160,6 +161,7 @@ final class DashboardViewModel {
160161
switch result {
161162
case let .success(messages):
162163
guard let message = messages.first else {
164+
self.announcementViewModel = nil
163165
return
164166
}
165167
self.analytics.track(event:

WooCommerce/WooCommerceTests/ViewRelated/Dashboard/DashboardViewModelTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,21 @@ final class DashboardViewModelTests: XCTestCase {
235235
assertEqual("Networking.DotcomError", properties["error_domain"] as? String)
236236
assertEqual("Dotcom Invalid REST Route", properties["error_description"] as? String)
237237
}
238+
239+
func test_when_no_messages_are_received_existing_messages_are_removed() {
240+
// Given
241+
prepareStoresToShowJustInTimeMessage(.success([]))
242+
243+
let viewModel = DashboardViewModel(stores: stores, analytics: analytics)
244+
viewModel.announcementViewModel = JustInTimeMessageAnnouncementCardViewModel(
245+
justInTimeMessage: .fake(),
246+
screenName: "my_store",
247+
siteID: sampleSiteID)
248+
249+
// When
250+
viewModel.syncAnnouncements(for: sampleSiteID)
251+
252+
// Then
253+
XCTAssertNil(viewModel.announcementViewModel)
254+
}
238255
}

0 commit comments

Comments
 (0)