Skip to content

Commit fa1fa2a

Browse files
authored
Jetpack REST connection log into wp.com (#22115)
* Jetpack connection UI (#22079) * Added shell for JetpackConnectionViewModel * Added shell for JetpackConnectionScreen * Fleshed out JetpackConnectionScreen * Minor tweaks * Added canInitiateJetpackConnection * Updated comment * Renamed const * Removed wpApiClient * Added activity shell * Added activity to manifest * Handle close * Pass states rather than the view model * Pass states rather than the view model,p2 * Dummy code to simulate login * Dummy code to simulate login, p2 * Added done button * Animate Done button, added Failed status * Use light MaterialTheme.colorScheme.error color background for failed steps * Animate changes to ConnectionStepItem * Added startNextStep * Added startNextStep, p2 * Added startNextStep, p3 * Fixed UI testing * Fixed UI testing, p2 * Changed color for completed and in-progress cards * Changed color for completed and in-progress cards again * Removed elevation and changed card to column * First pass at Retry * Second pass at Retry * Third pass at Retry * Added error states * Combined states * Fixed Detekt errors * Replaced hard-coded hex color values * Minor formatting changes * Minor formatting changes, p2 * Minor formatting changes, p3 * Updated preview to use JetpackConnectionScreen * Simplified ConnectionStepItem * Moved Screen to ScreenWithTopAppBarM3 * Added modifier * Changed two icons * Fix the done/retry button to the bottom * Removed the heading and changed the screen title * Increase elevation of in-progress step, remove elevation animation * First pass at confirming cancellation * Second pass at confirming cancellation * Fixed detekt warnings * Fixed detekt warnings * Changed fun name * Fixed isActive check * Use ErrorEvent instead of a string * Fixed new Detekt warnings * Renamed variable * Retry starts at the failed step * Moved getNextStep to its own fun * Simplified retrying from step * Added optional message to ErrorType * Updated wp.com error message * Moved color constants to color resources * Use existing color resources for in-progress step * Don't use !! for selected site * Replaced throwing exception with error() to resolve Detekt warning * Simplified JetpackConnectionViewModel * Simplified JetpackConnectionScreen * Simplified JetpackConnectionActivity * Added Jetpack Connection feature flag * Jetpack Connection cleanup (#22104) * Rely on isUsingSelfHostedRestApi to determine whether we can use the new JP REST connection * Removed starting the flow from the main activity * Launch the new flow from stats if available * Simplified canInitiateJetpackConnection * Renamed feature to Experimental Jetpack *REST* Connection to avoid confusion with existing web-based flow * Fixed Detekt line length warnings * Add "Simulated" to screen title to make it clear there's nothing happening * Renamed experimental key * Fixed title casing * Added start button * Wired up start button * Reset uiEvent when starting flow, removed unused init * Removed code for steps other than Step 1 (login to wp.com) * Removed code for steps other than Step 1 (login to wp.com), p2 * First attempt at wp.com login * Remove automatic step completion * Use correct discovery url * Added error type for discovery failure * First pass at launching and awaiting password flow * Retain when waiting for password flow * First pass at switching from app password to wp.com login (broken) * Second pass at switching from app password to wp.com login (broken) * Use showSignInForResultWpComOnly * Check for account token * Add sign-in flow for JP rest connect * Added error type for login wp.com failure, minor code reformatting * Don't show "Failed" status for errors, only show error message * Removed unused account store and deleted commented code * Added comment to LoginActivity * Added comment to loginWpCom * Fixed Detekt warning * Added comment to ActivityLauncher and shortened function name * Don't expose isWaitingForWPComLogin
1 parent 68035ff commit fa1fa2a

File tree

7 files changed

+140
-74
lines changed

7 files changed

+140
-74
lines changed

WordPress/src/main/java/org/wordpress/android/ui/ActivityLauncher.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
import static org.wordpress.android.analytics.AnalyticsTracker.Stat.STATS_ACCESS_ERROR;
150150
import static org.wordpress.android.imageeditor.preview.PreviewImageFragment.ARG_EDIT_IMAGE_DATA;
151151
import static org.wordpress.android.login.LoginMode.JETPACK_LOGIN_ONLY;
152+
import static org.wordpress.android.login.LoginMode.JETPACK_REST_CONNECT;
152153
import static org.wordpress.android.login.LoginMode.WPCOM_LOGIN_ONLY;
153154
import static org.wordpress.android.push.NotificationsProcessingService.ARG_NOTIFICATION_TYPE;
154155
import static org.wordpress.android.ui.WPWebViewActivity.ENCODING_UTF8;
@@ -1415,6 +1416,20 @@ public static void showSignInForResultWpComOnly(Activity activity) {
14151416
activity.startActivityForResult(intent, RequestCodes.ADD_ACCOUNT);
14161417
}
14171418

1419+
/**
1420+
* Sign in to WordPress.com from the Jetpack REST connection flow.
1421+
* This method is specifically for the Jetpack connection process where
1422+
* we need to authenticate with WordPress.com to establish the connection
1423+
* and return immediately to the connection flow.
1424+
*
1425+
* @param activity The activity requesting the sign-in
1426+
*/
1427+
public static void showWpComSignInForRestConnect(@NonNull Activity activity) {
1428+
Intent intent = new Intent(activity, LoginActivity.class);
1429+
JETPACK_REST_CONNECT.putInto(intent);
1430+
activity.startActivityForResult(intent, RequestCodes.ADD_ACCOUNT);
1431+
}
1432+
14181433
public static void showSignInForResultJetpackOnly(Activity activity) {
14191434
Intent intent = new Intent(activity, LoginActivity.class);
14201435
intent.setFlags(

WordPress/src/main/java/org/wordpress/android/ui/accounts/LoginActivity.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
194194
loginFromPrologue();
195195
break;
196196
case WPCOM_LOGIN_ONLY:
197+
case JETPACK_REST_CONNECT:
197198
mUnifiedLoginTracker.setSource(Source.ADD_WORDPRESS_COM_ACCOUNT);
198199
mIsSignupFromLoginEnabled = mBuildConfigWrapper.isSignupEnabled();
199200
checkSmartLockPasswordAndStartLogin();
@@ -357,6 +358,12 @@ private void loggedInAndFinish(ArrayList<Integer> oldSitesIds, boolean doLoginUp
357358
case JETPACK_STATS:
358359
ActivityLauncher.showLoginEpilogueForResult(this, oldSitesIds, true);
359360
break;
361+
case JETPACK_REST_CONNECT:
362+
// for the Jetpack REST connection we want to return to the caller activity instead of
363+
// showing the login epilogue
364+
setResult(Activity.RESULT_OK);
365+
finish();
366+
break;
360367
case WPCOM_LOGIN_DEEPLINK:
361368
case WPCOM_REAUTHENTICATE:
362369
ActivityLauncher.showLoginEpilogueForResult(this, oldSitesIds, false);

WordPress/src/main/java/org/wordpress/android/ui/jetpackrestconnection/JetpackRestConnectionActivity.kt

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,24 @@ import dagger.hilt.android.AndroidEntryPoint
1111
import kotlinx.coroutines.flow.filterNotNull
1212
import kotlinx.coroutines.launch
1313
import org.wordpress.android.R
14+
import org.wordpress.android.fluxc.store.AccountStore
15+
import org.wordpress.android.ui.ActivityLauncher
16+
import org.wordpress.android.ui.ActivityNavigator
17+
import org.wordpress.android.ui.RequestCodes
1418
import org.wordpress.android.ui.main.BaseAppCompatActivity
1519
import org.wordpress.android.util.extensions.setContent
20+
import javax.inject.Inject
1621

1722
@AndroidEntryPoint
1823
class JetpackRestConnectionActivity : BaseAppCompatActivity() {
1924
private val viewModel: JetpackRestConnectionViewModel by viewModels()
2025

26+
@Inject
27+
lateinit var activityNavigator: ActivityNavigator
28+
29+
@Inject
30+
lateinit var accountStore: AccountStore
31+
2132
override fun onCreate(savedInstanceState: Bundle?) {
2233
super.onCreate(savedInstanceState)
2334
setContent {
@@ -34,13 +45,34 @@ class JetpackRestConnectionActivity : BaseAppCompatActivity() {
3445
lifecycleScope.launch {
3546
viewModel.uiEvent.filterNotNull().collect { event ->
3647
when (event) {
37-
JetpackRestConnectionViewModel.UiEvent.Close -> finish()
38-
JetpackRestConnectionViewModel.UiEvent.ShowCancelConfirmation -> showCancelConfirmationDialog()
48+
JetpackRestConnectionViewModel.UiEvent.StartWPComLogin ->
49+
startWPComLogin()
50+
51+
JetpackRestConnectionViewModel.UiEvent.Close ->
52+
finish()
53+
54+
JetpackRestConnectionViewModel.UiEvent.ShowCancelConfirmation ->
55+
showCancelConfirmationDialog()
3956
}
4057
}
4158
}
4259
}
4360

61+
private fun startWPComLogin() {
62+
ActivityLauncher.showWpComSignInForRestConnect(this)
63+
}
64+
65+
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
66+
super.onActivityResult(requestCode, resultCode, data)
67+
68+
// User returned from WordPress.com login - note the resultCode will always be RESULT_OK but
69+
// we check it here in case that ever changes
70+
if (requestCode == RequestCodes.ADD_ACCOUNT) {
71+
val loginSuccessful = resultCode == RESULT_OK && (accountStore.accessToken?.isNotEmpty() == true)
72+
viewModel.onWPComLoginCompleted(success = loginSuccessful)
73+
}
74+
}
75+
4476
private fun showCancelConfirmationDialog() {
4577
MaterialAlertDialogBuilder(this)
4678
.setTitle(R.string.jetpack_rest_connection_cancel_title)

WordPress/src/main/java/org/wordpress/android/ui/jetpackrestconnection/JetpackRestConnectionScreen.kt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,30 +249,29 @@ private fun ConnectionStepContent(
249249

250250
Spacer(modifier = Modifier.height(4.dp))
251251

252-
Text(
253-
text = getStatusText(status),
254-
style = MaterialTheme.typography.bodyMedium,
255-
color = style.statusColor
256-
)
257-
258252
if (errorType != null && status == ConnectionStatus.Failed) {
259-
Spacer(modifier = Modifier.height(4.dp))
260253
Text(
261254
text = getErrorText(LocalContext.current, errorType),
262255
style = MaterialTheme.typography.bodySmall,
263256
color = MaterialTheme.colorScheme.error
264257
)
258+
} else {
259+
Text(
260+
text = getStatusText(status),
261+
style = MaterialTheme.typography.bodyMedium,
262+
color = style.statusColor
263+
)
265264
}
266265
}
267266
}
268267

269268
private fun getErrorText(context: Context, errorType: ErrorType): String {
270269
@StringRes val messageRes = when (errorType) {
271-
is ErrorType.JetpackAlreadyInstalled -> R.string.jetpack_rest_connection_error_jetpack_already_installed
270+
ErrorType.FailedToLoginWpCom -> R.string.jetpack_rest_connection_error_login_wpcom
271+
ErrorType.FailedToConnectWpCom -> R.string.jetpack_rest_connection_error_connect_wpcom
272272
is ErrorType.Timeout -> R.string.jetpack_rest_connection_error_timeout
273273
is ErrorType.Offline -> R.string.jetpack_rest_connection_error_offline
274274
is ErrorType.Unknown -> R.string.jetpack_rest_connection_error_unknown
275-
is ErrorType.FailedToConnectWpCom -> R.string.jetpack_rest_connection_error_wpcom
276275
}
277276
val baseMessage = context.getString(messageRes)
278277
return errorType.message?.let { "$baseMessage: $it" } ?: baseMessage
@@ -422,7 +421,7 @@ private fun JetpackRestConnectionScreenPreview() {
422421
ConnectionStep.ConnectSite to StepState(ConnectionStatus.InProgress),
423422
ConnectionStep.ConnectWpCom to StepState(
424423
ConnectionStatus.Failed,
425-
ErrorType.FailedToConnectWpCom()
424+
ErrorType.FailedToConnectWpCom
426425
),
427426
ConnectionStep.Finalize to StepState(ConnectionStatus.NotStarted)
428427
)
@@ -439,5 +438,7 @@ private fun JetpackRestConnectionScreenPreview() {
439438
)
440439
}
441440

442-
@ColorRes private val IN_PROGRESS_BACKGROUND_COLOR = R.color.yellow_10 // Light yellow
443-
@ColorRes private val IN_PROGRESS_FOREGROUND_COLOR = R.color.yellow_90 // Dark brown for readability on the above
441+
@ColorRes
442+
private val IN_PROGRESS_BACKGROUND_COLOR = R.color.yellow_10 // Light yellow
443+
@ColorRes
444+
private val IN_PROGRESS_FOREGROUND_COLOR = R.color.yellow_90 // Dark brown for readability on the above

0 commit comments

Comments
 (0)