Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V3] Rename BrowserSwitchStartResult .Success to .Started #105

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public BrowserSwitchClient() {
*
* @param activity the activity used to start browser switch
* @param browserSwitchOptions {@link BrowserSwitchOptions} the options used to configure the browser switch
* @return a {@link BrowserSwitchStartResult.Success} that should be stored and passed to
* @return a {@link BrowserSwitchStartResult.Started} that should be stored and passed to
* {@link BrowserSwitchClient#completeRequest(Intent, String)} upon return to the app,
* or {@link BrowserSwitchStartResult.Failure} if browser could not be launched.
*/
Expand Down Expand Up @@ -77,7 +77,7 @@ public BrowserSwitchStartResult start(@NonNull ComponentActivity activity, @NonN
appLinkUri
);
customTabsInternalClient.launchUrl(activity, browserSwitchUrl, launchAsNewTask);
return new BrowserSwitchStartResult.Success(request.toBase64EncodedJSON());
return new BrowserSwitchStartResult.Started(request.toBase64EncodedJSON());
} catch (ActivityNotFoundException | BrowserSwitchException e) {
return new BrowserSwitchStartResult.Failure(new BrowserSwitchException("Unable to start browser switch without a web browser.", e));
}
Expand Down Expand Up @@ -126,7 +126,7 @@ private boolean isValidRequestCode(int requestCode) {
*
* @param intent the intent to return to your application containing a deep link result from the
* browser flow
* @param pendingRequest the pending request string returned from {@link BrowserSwitchStartResult.Success} via
* @param pendingRequest the pending request string returned from {@link BrowserSwitchStartResult.Started} via
* {@link BrowserSwitchClient#start(ComponentActivity, BrowserSwitchOptions)}
* @return a {@link BrowserSwitchFinalResult.Success} if the browser switch was successfully
* completed, or {@link BrowserSwitchFinalResult.NoResult} if no result can be found for the given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ sealed class BrowserSwitchStartResult {
* The browser switch was successfully completed. Store pendingRequest String to complete
* browser switch after deeplinking back into the application (see [BrowserSwitchClient.completeRequest]).
*/
class Success(val pendingRequest: String) : BrowserSwitchStartResult()
class Started(val pendingRequest: String) : BrowserSwitchStartResult()

/**
* Browser switch failed with an [error].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ public void start_whenSuccessful_returnsBrowserSwitchRequest() throws BrowserSwi
verify(customTabsInternalClient).launchUrl(componentActivity, browserSwitchDestinationUrl, false);

assertNotNull(browserSwitchPendingRequest);
assertTrue(browserSwitchPendingRequest instanceof BrowserSwitchStartResult.Success);
assertTrue(browserSwitchPendingRequest instanceof BrowserSwitchStartResult.Started);

String pendingRequest =
((BrowserSwitchStartResult.Success) browserSwitchPendingRequest).getPendingRequest();
((BrowserSwitchStartResult.Started) browserSwitchPendingRequest).getPendingRequest();
BrowserSwitchRequest browserSwitchRequest =
BrowserSwitchRequest.fromBase64EncodedJSON(pendingRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ComposeActivity : ComponentActivity() {
.launchAsNewTask(false)
.returnUrlScheme(RETURN_URL_SCHEME)
when (val startResult = browserSwitchClient.start(this, browserSwitchOptions)) {
is BrowserSwitchStartResult.Success ->
is BrowserSwitchStartResult.Started ->
PendingRequestStore.put(this, startResult.pendingRequest)

is BrowserSwitchStartResult.Failure -> viewModel.browserSwitchError = startResult.error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,31 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);

String pendingRequest = PendingRequestStore.Companion.get(this);
String pendingRequest = PendingRequestStore.get(this);
if (pendingRequest != null) {
BrowserSwitchFinalResult result = browserSwitchClient.completeRequest(intent, pendingRequest);
if (result instanceof BrowserSwitchFinalResult.Success) {
Objects.requireNonNull(getDemoFragment()).onBrowserSwitchResult((BrowserSwitchFinalResult.Success) result);
}
PendingRequestStore.Companion.clear(this);
PendingRequestStore.clear(this);
}
}

@Override
protected void onResume() {
super.onResume();

String pendingRequest = PendingRequestStore.Companion.get(this);
String pendingRequest = PendingRequestStore.get(this);
if (pendingRequest != null) {
Objects.requireNonNull(getDemoFragment()).onBrowserSwitchError(new Exception("User did not complete browser switch"));
PendingRequestStore.Companion.clear(this);
PendingRequestStore.clear(this);
}
}

public void startBrowserSwitch(BrowserSwitchOptions options) throws BrowserSwitchException {
BrowserSwitchStartResult result = browserSwitchClient.start(this, options);
if (result instanceof BrowserSwitchStartResult.Success) {
PendingRequestStore.Companion.put(this, ((BrowserSwitchStartResult.Success) result).getPendingRequest());
if (result instanceof BrowserSwitchStartResult.Started) {
PendingRequestStore.put(this, ((BrowserSwitchStartResult.Started) result).getPendingRequest());
} else if (result instanceof BrowserSwitchStartResult.Failure) {
Objects.requireNonNull(getDemoFragment()).onBrowserSwitchError(((BrowserSwitchStartResult.Failure) result).getError());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class PendingRequestStore {
private const val SHARED_PREFS_KEY = "PENDING_REQUESTS"
private const val PENDING_REQUEST_KEY = "BROWSER_SWITCH_REQUEST"

@JvmStatic
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

general cleanup to make static methods more Java friendly.

fun put(context: Context, pendingRequest: String) {
val sharedPreferences: SharedPreferences = context.getSharedPreferences(
SHARED_PREFS_KEY,
Expand All @@ -18,6 +19,7 @@ class PendingRequestStore {
sharedPreferences.edit().putString(PENDING_REQUEST_KEY, pendingRequest).apply()
}

@JvmStatic
fun get(context: Context): String? {
val sharedPreferences: SharedPreferences = context.getSharedPreferences(
SHARED_PREFS_KEY,
Expand All @@ -26,6 +28,7 @@ class PendingRequestStore {
return sharedPreferences.getString(PENDING_REQUEST_KEY, null)
}

@JvmStatic
fun clear(context: Context) {
val sharedPreferences: SharedPreferences = context.getSharedPreferences(
SHARED_PREFS_KEY,
Expand Down
Loading