From 5cde933aed09b75445c3a0d09c10c92561766afe Mon Sep 17 00:00:00 2001 From: Matthew Kevins Date: Wed, 23 Nov 2022 16:02:13 +1000 Subject: [PATCH] Wire up notifications and done screens to the combine state flow --- .../EligibilityHelper.kt | 4 ++ .../migration/JetpackMigrationViewModel.kt | 45 ++++++++----------- .../ui/utils/JetpackAppMigrationFlowUtils.kt | 11 ++++- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/localcontentmigration/EligibilityHelper.kt b/WordPress/src/main/java/org/wordpress/android/localcontentmigration/EligibilityHelper.kt index 3b53e0bb1d26..a256fdb7a30d 100644 --- a/WordPress/src/main/java/org/wordpress/android/localcontentmigration/EligibilityHelper.kt +++ b/WordPress/src/main/java/org/wordpress/android/localcontentmigration/EligibilityHelper.kt @@ -7,6 +7,10 @@ import javax.inject.Inject class EligibilityHelper @Inject constructor( private val localMigrationContentResolver: LocalMigrationContentResolver, ) { + /** TODO: This should perform some additional pre-flight checks. We should: + * * On WordPress: + * 1. Check for local-only data + */ fun validate() = localMigrationContentResolver.getResultForEntityType(EligibilityStatus).validate() } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/main/jetpack/migration/JetpackMigrationViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/main/jetpack/migration/JetpackMigrationViewModel.kt index b9e1ae69ef22..efbb76c4913a 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/main/jetpack/migration/JetpackMigrationViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/main/jetpack/migration/JetpackMigrationViewModel.kt @@ -7,7 +7,6 @@ import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combineTransform import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.launch @@ -27,7 +26,8 @@ import org.wordpress.android.ui.main.jetpack.migration.JetpackMigrationViewModel import org.wordpress.android.ui.main.jetpack.migration.JetpackMigrationViewModel.ActionButton.WelcomeSecondaryButton import org.wordpress.android.ui.main.jetpack.migration.JetpackMigrationViewModel.JetpackMigrationActionEvent.CompleteFlow import org.wordpress.android.ui.main.jetpack.migration.JetpackMigrationViewModel.JetpackMigrationActionEvent.ShowHelp -import org.wordpress.android.ui.main.jetpack.migration.JetpackMigrationViewModel.UiState.Content +import org.wordpress.android.ui.main.jetpack.migration.JetpackMigrationViewModel.UiState.Content.Done +import org.wordpress.android.ui.main.jetpack.migration.JetpackMigrationViewModel.UiState.Content.Notifications import org.wordpress.android.ui.main.jetpack.migration.JetpackMigrationViewModel.UiState.Content.Welcome import org.wordpress.android.ui.main.jetpack.migration.JetpackMigrationViewModel.UiState.Error.Generic import org.wordpress.android.ui.main.jetpack.migration.JetpackMigrationViewModel.UiState.Error.Networking @@ -51,25 +51,33 @@ class JetpackMigrationViewModel @Inject constructor( private val migrationStateFlow = MutableStateFlow(Migrating()) private val continueClickedFlow = MutableStateFlow(false) + private val notificationContinueClickedFlow = MutableStateFlow(false) - val uiState = combineTransform(migrationStateFlow, continueClickedFlow) { - migrationState, continueClicked -> + val uiState = combineTransform(migrationStateFlow, continueClickedFlow, notificationContinueClickedFlow) { + migrationState, continueClicked, notificationContinueClicked -> when { migrationState is Initial -> emit(Loading) migrationState is Migrating -> emit( Welcome( - userAvatarUrl = migrationState.avatarUrl, + userAvatarUrl = resizeAvatarUrl(migrationState.avatarUrl), isProcessing = continueClicked, sites = migrationState.sites.map(::siteUiFromModel), primaryActionButton = WelcomePrimaryButton(::onContinueClicked), secondaryActionButton = WelcomeSecondaryButton(::onHelpClicked), ) ) - migrationState is Successful && continueClicked -> emit( - Content.Notifications( - primaryActionButton = NotificationsPrimaryButton(::onContinueFromNotificationsClicked), - ) - ) + migrationState is Successful && continueClicked -> when { + !notificationContinueClicked -> emit( + Notifications( + primaryActionButton = NotificationsPrimaryButton(::onContinueFromNotificationsClicked), + ) + ) + else -> emit( + Done( + primaryActionButton = DonePrimaryButton(::onDoneClicked) + ) + ) + } migrationState is Failure -> emit( UiState.Error( primaryActionButton = ErrorPrimaryButton(::onTryAgainClicked), @@ -97,12 +105,6 @@ class JetpackMigrationViewModel @Inject constructor( continueClickedFlow.value = true } - private fun postNotificationsState() { - _uiState.value = Content.Notifications( - primaryActionButton = NotificationsPrimaryButton(::onContinueFromNotificationsClicked), - ) - } - @Suppress("ForbiddenComment", "unused") private fun postGenericErrorState() { // TODO: Call this method when migration fails with generic error @@ -134,22 +136,13 @@ class JetpackMigrationViewModel @Inject constructor( viewModelScope.launch(Dispatchers.IO) { localMigrationOrchestrator.tryLocalMigration(migrationStateFlow) } - - // TODO: Handle migration result properly and navigate to the right error screen if migration fails -// postNotificationsState() } @Suppress("ForbiddenComment") private fun onContinueFromNotificationsClicked() { // TODO: Disable notifications in WP app // See https://github.com/wordpress-mobile/WordPress-Android/pull/17371 - postDoneState() - } - - private fun postDoneState() { - _uiState.value = Content.Done( - primaryActionButton = DonePrimaryButton(::onDoneClicked), - ) + notificationContinueClickedFlow.value = true } private fun onDoneClicked() { diff --git a/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt b/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt index 5b54b67c2b2d..b18eae063dcf 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/utils/JetpackAppMigrationFlowUtils.kt @@ -1,6 +1,7 @@ package org.wordpress.android.ui.utils import org.wordpress.android.ui.ActivityLauncher +import org.wordpress.android.ui.prefs.AppPrefsWrapper import org.wordpress.android.util.BuildConfigWrapper import org.wordpress.android.util.config.JetpackMigrationFlowFeatureConfig import org.wordpress.android.viewmodel.ContextProvider @@ -10,8 +11,16 @@ class JetpackAppMigrationFlowUtils @Inject constructor( private val buildConfigWrapper: BuildConfigWrapper, private val jetpackMigrationFlowFeatureConfig: JetpackMigrationFlowFeatureConfig, private val contextProvider: ContextProvider, + private val appPrefsWrapper: AppPrefsWrapper, ) { - fun shouldShowMigrationFlow() = buildConfigWrapper.isJetpackApp && jetpackMigrationFlowFeatureConfig.isEnabled() + /** TODO: This should perform some additional pre-flight checks. We should: + * 1. Check that the user is not already logged in. + * 2. Check that the database is not already populated + * * This also covers the case of an already successful migration. + */ + fun shouldShowMigrationFlow() = buildConfigWrapper.isJetpackApp + && jetpackMigrationFlowFeatureConfig.isEnabled() + && appPrefsWrapper.getIsFirstTrySharedLoginJetpack() fun startJetpackMigrationFlow() { ActivityLauncher.startJetpackMigrationFlow(contextProvider.getContext())