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 StripeEditText's error color logic #989

Merged
merged 1 commit into from
May 24, 2019
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 @@ -37,8 +37,8 @@ public class StripeEditText extends AppCompatEditText {
@Nullable private DeleteEmptyListener mDeleteEmptyListener;
@Nullable private ColorStateList mCachedColorStateList;
private boolean mShouldShowError;
@ColorRes private int mDefaultErrorColorResId;
@ColorInt private int mErrorColor;
@ColorInt private int mDefaultErrorColor;
@Nullable @ColorInt private Integer mErrorColor;

@NonNull private final Handler mHandler;
private String mErrorMessage;
Expand Down Expand Up @@ -82,7 +82,7 @@ public int getDefaultErrorColorInt() {
// It's possible that we need to verify this value again
// in case the user programmatically changes the text color.
determineDefaultErrorColor();
return ContextCompat.getColor(getContext(), mDefaultErrorColorResId);
return mDefaultErrorColor;
}

@Override
Expand Down Expand Up @@ -131,6 +131,11 @@ public void setErrorColor(@ColorInt int errorColor) {
mErrorColor = errorColor;
}

@ColorInt
private int getErrorColor() {
return mErrorColor != null ? mErrorColor : mDefaultErrorColor;
}

/**
* Change the hint value of this control after a delay.
*
Expand Down Expand Up @@ -161,7 +166,7 @@ public void setShouldShowError(boolean shouldShowError) {
} else {
mShouldShowError = shouldShowError;
if (mShouldShowError) {
setTextColor(mErrorColor);
setTextColor(getErrorColor());
} else {
setTextColor(mCachedColorStateList);
}
Expand All @@ -186,14 +191,18 @@ private void initView() {

private void determineDefaultErrorColor() {
mCachedColorStateList = getTextColors();
int color = mCachedColorStateList.getDefaultColor();
final int color = mCachedColorStateList.getDefaultColor();

@ColorRes final int defaultErrorColorResId;
if (isColorDark(color)) {
// Note: if the _text_ color is dark, then this is a
// light theme, and vice-versa.
mDefaultErrorColorResId = R.color.error_text_light_theme;
defaultErrorColorResId = R.color.error_text_light_theme;
} else {
mDefaultErrorColorResId = R.color.error_text_dark_theme;
defaultErrorColorResId = R.color.error_text_dark_theme;
}

mDefaultErrorColor = ContextCompat.getColor(getContext(), defaultErrorColorResId);
}

private void listenForTextChanges() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,11 @@ public void setErrorColor_whenInError_overridesDefault() {
public void getCachedColorStateList_afterInit_returnsNotNull() {
assertNotNull(mEditText.getCachedColorStateList());
}

@Test
public void setShouldShowError_whenErrorColorNotSet_shouldUseDefaultErrorColor() {
mEditText.setShouldShowError(true);
assertEquals(ContextCompat.getColor(mContext, R.color.error_text_light_theme),
mEditText.getTextColors().getDefaultColor());
}
}