Skip to content

Save phone verification ID across process deaths #1393

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

Merged
merged 1 commit into from
Jul 18, 2018
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 @@ -46,6 +46,8 @@
*/
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class PhoneActivity extends AppCompatBase {
private PhoneNumberVerificationHandler mPhoneVerifier;

public static Intent createIntent(Context context, FlowParameters params, Bundle args) {
return createBaseIntent(context, PhoneActivity.class, params)
.putExtra(ExtraConstants.PARAMS, args);
Expand All @@ -72,10 +74,10 @@ protected void onFailure(@NonNull Exception e) {
}
});

final PhoneNumberVerificationHandler phoneVerifier =
ViewModelProviders.of(this).get(PhoneNumberVerificationHandler.class);
phoneVerifier.init(getFlowParams());
phoneVerifier.getOperation().observe(this, new ResourceObserver<PhoneVerification>(
mPhoneVerifier = ViewModelProviders.of(this).get(PhoneNumberVerificationHandler.class);
mPhoneVerifier.init(getFlowParams());
mPhoneVerifier.onRestoreInstanceState(savedInstanceState);
mPhoneVerifier.getOperation().observe(this, new ResourceObserver<PhoneVerification>(
this, R.string.fui_verifying) {
@Override
protected void onSuccess(@NonNull PhoneVerification verification) {
Expand Down Expand Up @@ -121,6 +123,12 @@ protected void onFailure(@NonNull Exception e) {
.commit();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mPhoneVerifier.onSaveInstanceState(outState);
}

@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.firebase.ui.auth.ui.phone;

import android.app.Application;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.firebase.ui.auth.data.model.PhoneNumberVerificationRequiredException;
import com.firebase.ui.auth.data.model.Resource;
Expand All @@ -15,6 +17,7 @@

public class PhoneNumberVerificationHandler extends AuthViewModelBase<PhoneVerification> {
private static final long AUTO_RETRIEVAL_TIMEOUT_SECONDS = 120;
private static final String VERIFICATION_ID_KEY = "verification_id";

private String mVerificationId;
private PhoneAuthProvider.ForceResendingToken mForceResendingToken;
Expand Down Expand Up @@ -60,4 +63,14 @@ public void submitVerificationCode(String number, String code) {
PhoneAuthProvider.getCredential(mVerificationId, code),
false)));
}

public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putString(VERIFICATION_ID_KEY, mVerificationId);
Copy link
Contributor

Choose a reason for hiding this comment

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

I have no issue with this, but just wondering have you seen this pattern of handing the instance state in the ViewModel elsewhere or is this your own invention? Seems a to tie it a little too close to the Activity but I can't think of a reason why it's any worse than having the logic directly in the Activity.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I looked around, but couldn't find sample code with onSaveInstanceState. 😕 So I guess it's time to patent my new invention, right? 😂

I wouldn't mind moving it to the activity, but it made more sense to me to let the ViewModel manage its saved state. (Otherwise, we'd end up with getters and setters for mVerificationId which shouldn't be used outside of the state restoration context.) Whad'ya think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I am cool with it, let's just pinky swear not to show Jake Wharton or Yigit the monster we've created!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👌😂

}

public void onRestoreInstanceState(@Nullable Bundle savedInstanceState) {
if (mVerificationId == null && savedInstanceState != null) {
mVerificationId = savedInstanceState.getString(VERIFICATION_ID_KEY);
}
}
}