Skip to content

Commit

Permalink
Wire up notifications and done screens to the combine state flow
Browse files Browse the repository at this point in the history
  • Loading branch information
mkevins committed Nov 23, 2022
1 parent fd9851c commit 5cde933
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<EligibilityStatusData>(EligibilityStatus).validate()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -51,25 +51,33 @@ class JetpackMigrationViewModel @Inject constructor(

private val migrationStateFlow = MutableStateFlow<LocalMigrationState>(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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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())
Expand Down

0 comments on commit 5cde933

Please sign in to comment.