Skip to content

Commit

Permalink
[V3] Rename BrowserSwitchStartResult .Success to .Started (#105)
Browse files Browse the repository at this point in the history
* Rename BrowserSwitchStartResult.Success to BrowserSwitchStartResult.Started.

* Annotate PendingRequestStore methods with @JvmStatic.
  • Loading branch information
sshropshire authored Jul 1, 2024
1 parent fa92641 commit b86cad7
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
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
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

0 comments on commit b86cad7

Please sign in to comment.