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

Fix/10930 email error dissapears on rotation - continued #11537

Merged
merged 8 commits into from
Apr 2, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class LoginEmailFragment extends LoginBaseFormFragment<LoginListener> imp
private static final String KEY_IS_SOCIAL = "KEY_IS_SOCIAL";
private static final String KEY_OLD_SITES_IDS = "KEY_OLD_SITES_IDS";
private static final String KEY_REQUESTED_EMAIL = "KEY_REQUESTED_EMAIL";
private static final String KEY_EMAIL_ERROR_RES = "KEY_EMAIL_ERROR_RES";
private static final String LOG_TAG = LoginEmailFragment.class.getSimpleName();
private static final int GOOGLE_API_CLIENT_ID = 1002;
private static final int EMAIL_CREDENTIALS_REQUEST_CODE = 25100;
Expand All @@ -78,6 +79,7 @@ public class LoginEmailFragment extends LoginBaseFormFragment<LoginListener> imp
private String mGoogleEmail;
private String mRequestedEmail;
private boolean mIsSocialLogin;
private Integer mCurrentEmailErrorRes = null;

protected WPLoginInputRow mEmailInput;
protected boolean mHasDismissedEmailHints;
Expand Down Expand Up @@ -135,7 +137,12 @@ protected void setupContent(ViewGroup rootView) {
if (BuildConfig.DEBUG) {
mEmailInput.getEditText().setText(BuildConfig.DEBUG_WPCOM_LOGIN_EMAIL);
}
mEmailInput.addTextChangedListener(this);
mEmailInput.post(new Runnable() {
@Override public void run() {
mEmailInput.addTextChangedListener(LoginEmailFragment.this);
}
});

mEmailInput.setOnEditorCommitListener(this);
mEmailInput.getEditText().setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
Expand Down Expand Up @@ -288,6 +295,7 @@ public void onStart() {
.enableAutoManage(getActivity(), GOOGLE_API_CLIENT_ID, LoginEmailFragment.this)
.addApi(Auth.CREDENTIALS_API)
.build();
showEmailError();
}

@Override
Expand All @@ -312,6 +320,9 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
mIsSocialLogin = savedInstanceState.getBoolean(KEY_IS_SOCIAL);
mIsDisplayingEmailHints = savedInstanceState.getBoolean(KEY_IS_DISPLAYING_EMAIL_HINTS);
mHasDismissedEmailHints = savedInstanceState.getBoolean(KEY_HAS_DISMISSED_EMAIL_HINTS);
if (savedInstanceState.containsKey(KEY_EMAIL_ERROR_RES)) {
mCurrentEmailErrorRes = savedInstanceState.getInt(KEY_EMAIL_ERROR_RES);
}
} else {
mAnalyticsListener.trackEmailFormViewed();
}
Expand All @@ -326,6 +337,9 @@ public void onSaveInstanceState(Bundle outState) {
outState.putBoolean(KEY_IS_SOCIAL, mIsSocialLogin);
outState.putBoolean(KEY_IS_DISPLAYING_EMAIL_HINTS, mIsDisplayingEmailHints);
outState.putBoolean(KEY_HAS_DISMISSED_EMAIL_HINTS, mHasDismissedEmailHints);
if (mCurrentEmailErrorRes != null) {
outState.putInt(KEY_EMAIL_ERROR_RES, mCurrentEmailErrorRes);
}
}

protected void next(String email) {
Expand All @@ -334,6 +348,7 @@ protected void next(String email) {
}

if (isValidEmail(email)) {
clearEmailError();
startProgress();
mRequestedEmail = email;
mDispatcher.dispatch(AccountActionBuilder.newIsAvailableEmailAction(email));
Expand All @@ -342,6 +357,20 @@ protected void next(String email) {
}
}

/**
* This is cleared every time the text is changed or the email is valid so that if the user rotates the device, they
* don't receive an unnecessary warning from a previous error.
*/
private void clearEmailError() {
mCurrentEmailErrorRes = null;
}

private void showEmailError() {
if (mCurrentEmailErrorRes != null) {
showEmailError(mCurrentEmailErrorRes);
}
}

@Override
public void onDetach() {
super.onDetach();
Expand Down Expand Up @@ -376,9 +405,11 @@ public void beforeTextChanged(CharSequence s, int start, int count, int after) {
public void onTextChanged(CharSequence s, int start, int before, int count) {
mEmailInput.setError(null);
mIsSocialLogin = false;
clearEmailError();
}

private void showEmailError(int messageId) {
mCurrentEmailErrorRes = messageId;
mEmailInput.setError(getString(messageId));
}

Expand Down