From 36efad7f0656a8a5518b12522d225740b6c25824 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Tue, 2 Apr 2024 16:49:30 +0300 Subject: [PATCH 1/4] Show error when the site is created but cannot be fetched by the local db --- .../ui/sitecreation/previews/SitePreviewViewModel.kt | 6 ++++-- .../ui/sitecreation/previews/SitePreviewViewModelTest.kt | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModel.kt index 8383d1625288..1b004d97d29c 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModel.kt @@ -136,9 +136,11 @@ class SitePreviewViewModel @Inject constructor( private suspend fun fetchNewlyCreatedSiteModel(remoteSiteId: Long): SiteModel? { val onSiteFetched = fetchWpComSiteUseCase.fetchSiteWithRetry(remoteSiteId) return if (!onSiteFetched.isError) { - return requireNotNull(siteStore.getSiteBySiteId(remoteSiteId)) { - "Site successfully fetched but has not been found in the local db." + val site = siteStore.getSiteBySiteId(remoteSiteId) + if (site == null) { + updateUiState(SiteNotCreatedErrorUiState) } + site } else { null } diff --git a/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModelTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModelTest.kt index e5a0051b219f..ae971555335c 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModelTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModelTest.kt @@ -100,6 +100,13 @@ class SitePreviewViewModelTest : BaseUnitTest() { verify(fetchWpComSiteUseCase).fetchSiteWithRetry(SITE_REMOTE_ID) } + @Test + fun `on start if retrying to fetch the site succeeds but retrieving from fb fails show error`() = testWith(FETCH_SUCCESS) { + whenever(siteStore.getSiteBySiteId(SITE_REMOTE_ID)).thenReturn(null) + startViewModel(SITE_CREATION_STATE.copy(result = RESULT_NOT_IN_LOCAL_DB)) + assertThat(viewModel.uiState.value).isInstanceOf(SiteNotCreatedErrorUiState::class.java) + } + @Test fun `on start does not show preview when fetching fails`() = testWith(FETCH_ERROR) { startViewModel() From 1894ed7789dbf5a296c30100eba450260d398e19 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Tue, 2 Apr 2024 16:54:38 +0300 Subject: [PATCH 2/4] Adds a separate state for the error --- .../ui/sitecreation/previews/SitePreviewViewModel.kt | 11 ++++++++++- .../sitecreation/previews/SitePreviewViewModelTest.kt | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModel.kt index 1b004d97d29c..0bdd80f02a86 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModel.kt @@ -24,6 +24,7 @@ import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SiteP import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SitePreviewLoadingShimmerState import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SitePreviewWebErrorUiState import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SiteNotCreatedErrorUiState +import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SiteNotFoundInDbUiState import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.UrlData import org.wordpress.android.ui.sitecreation.services.FetchWpComSiteUseCase import org.wordpress.android.ui.sitecreation.usecases.isWordPressComSubDomain @@ -138,7 +139,7 @@ class SitePreviewViewModel @Inject constructor( return if (!onSiteFetched.isError) { val site = siteStore.getSiteBySiteId(remoteSiteId) if (site == null) { - updateUiState(SiteNotCreatedErrorUiState) + updateUiState(SiteNotFoundInDbUiState) } site } else { @@ -226,6 +227,14 @@ class SitePreviewViewModel @Inject constructor( errorTitle = UiStringRes(R.string.error), ) + data object SiteNotFoundInDbUiState : SitePreviewUiState( + urlData = UrlData("", "", 0 to 0, 0 to 0), + webViewVisibility = false, + webViewErrorVisibility = true, + subtitle = UiStringRes(R.string.site_creation_error_generic_title), + caption = UiStringRes(R.string.site_creation_error_generic_subtitle), + ) + data class SitePreviewLoadingShimmerState( val isFree: Boolean, override val urlData: UrlData, diff --git a/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModelTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModelTest.kt index ae971555335c..e7975e4713b4 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModelTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModelTest.kt @@ -40,6 +40,7 @@ import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SiteP import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SitePreviewContentUiState import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SitePreviewWebErrorUiState import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SiteNotCreatedErrorUiState +import org.wordpress.android.ui.sitecreation.previews.SitePreviewViewModel.SitePreviewUiState.SiteNotFoundInDbUiState import org.wordpress.android.ui.sitecreation.progress.LOADING_STATE_TEXT_ANIMATION_DELAY import org.wordpress.android.ui.sitecreation.services.FetchWpComSiteUseCase import org.wordpress.android.util.UrlUtilsWrapper @@ -104,7 +105,7 @@ class SitePreviewViewModelTest : BaseUnitTest() { fun `on start if retrying to fetch the site succeeds but retrieving from fb fails show error`() = testWith(FETCH_SUCCESS) { whenever(siteStore.getSiteBySiteId(SITE_REMOTE_ID)).thenReturn(null) startViewModel(SITE_CREATION_STATE.copy(result = RESULT_NOT_IN_LOCAL_DB)) - assertThat(viewModel.uiState.value).isInstanceOf(SiteNotCreatedErrorUiState::class.java) + assertThat(viewModel.uiState.value).isInstanceOf(SiteNotFoundInDbUiState::class.java) } @Test From 56c8a865d89fe43b144185c99151cbc68d2fb1c9 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Tue, 2 Apr 2024 17:13:42 +0300 Subject: [PATCH 3/4] Fixed detekt issue in test --- .../ui/sitecreation/previews/SitePreviewViewModelTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModelTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModelTest.kt index e7975e4713b4..b1ed67de828e 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModelTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModelTest.kt @@ -102,7 +102,7 @@ class SitePreviewViewModelTest : BaseUnitTest() { } @Test - fun `on start if retrying to fetch the site succeeds but retrieving from fb fails show error`() = testWith(FETCH_SUCCESS) { + fun `on start if site is created but cannot be retrieved from fb fails show error`() = testWith(FETCH_SUCCESS) { whenever(siteStore.getSiteBySiteId(SITE_REMOTE_ID)).thenReturn(null) startViewModel(SITE_CREATION_STATE.copy(result = RESULT_NOT_IN_LOCAL_DB)) assertThat(viewModel.uiState.value).isInstanceOf(SiteNotFoundInDbUiState::class.java) From ade4a33fcc5ea92fa42d726ecc0b7295ac830792 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Tue, 2 Apr 2024 17:24:35 +0300 Subject: [PATCH 4/4] Update state on the UI thread --- .../android/ui/sitecreation/previews/SitePreviewViewModel.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModel.kt index 0bdd80f02a86..84f96ab53e6c 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/sitecreation/previews/SitePreviewViewModel.kt @@ -139,7 +139,9 @@ class SitePreviewViewModel @Inject constructor( return if (!onSiteFetched.isError) { val site = siteStore.getSiteBySiteId(remoteSiteId) if (site == null) { - updateUiState(SiteNotFoundInDbUiState) + withContext(mainDispatcher) { + updateUiState(SiteNotFoundInDbUiState) + } } site } else {