Skip to content

Commit

Permalink
- focus change behavior can now keep up with very fast typing
Browse files Browse the repository at this point in the history
- several minor performance tweaks
  • Loading branch information
dbachelder committed Aug 21, 2015
1 parent ae6258e commit b4514dc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class CreditEntryFieldBase extends EditText implements

CreditCardFieldDelegate delegate;
final Context context;
String lastValue = null;
CharSequence lastValue = null;

private boolean valid = false;

Expand Down Expand Up @@ -66,7 +66,7 @@ void init() {
void init(AttributeSet attrs) {
setGravity(Gravity.CENTER);
setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
setBackgroundColor(color.transparent);
setBackgroundColor(getResources().getColor(color.transparent));
setInputType(InputType.TYPE_CLASS_NUMBER);
addTextChangedListener(this);
setOnKeyListener(this);
Expand Down Expand Up @@ -94,8 +94,8 @@ public void onTextChanged(CharSequence s, int start, int before, int end) {
if (delegate != null) {
delegate.focusOnPreviousField(this);
}
} else if(!String.valueOf(s).equals(String.valueOf(lastValue))) {
lastValue = String.valueOf(s);
} else if(!s.equals(lastValue)) {
lastValue = s;
textChanged(s, start, before, end);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public void afterTextChanged(Editable s) {

public void textChanged(CharSequence s, int start, int before, int count) {
if (type != null) {
String number = s.toString();

if (number.length() == length) {
if (s.length() == length) {
setValid(true);
delegate.onSecurityCodeValid();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public String helperText() {

@Override
public void textChanged(CharSequence s, int start, int before, int end) {
String zipCode = s.toString();
if (zipCode.length() == 5) {
if (s.length() == 5) {
setValid(true);
delegate.onZipCodeValid();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public class CreditCardEntry extends HorizontalScrollView implements
private final ExpDateText expDateText;
private final SecurityCodeText securityCodeText;
private final ZipCodeText zipCodeText;
private final LinearLayout container;

private Map<CreditEntryFieldBase, CreditEntryFieldBase> nextFocusField = new HashMap<>(4);
private Map<CreditEntryFieldBase, CreditEntryFieldBase> prevFocusField = new HashMap<>(4);
Expand All @@ -81,7 +80,7 @@ public class CreditCardEntry extends HorizontalScrollView implements
private CardValidCallback onCardValidCallback;

@SuppressWarnings("deprecation")
public CreditCardEntry(Context context, boolean includeExp, boolean includeSecurity, boolean includeZip, AttributeSet attrs, int style) {
public CreditCardEntry(Context context, boolean includeExp, boolean includeSecurity, boolean includeZip, AttributeSet attrs, @SuppressWarnings("UnusedParameters") int style) {
super(context);

this.context = context;
Expand Down Expand Up @@ -109,11 +108,7 @@ public CreditCardEntry(Context context, boolean includeExp, boolean includeSecur
this.setHorizontalScrollBarEnabled(false);
this.setOnTouchListener(this);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
container = new LinearLayout(context);
} else {
container = new LinearLayout(context);
}
LinearLayout container = new LinearLayout(context);
container.setId(R.id.cc_entry_internal);
container.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
container.setOrientation(LinearLayout.HORIZONTAL);
Expand Down Expand Up @@ -242,6 +237,7 @@ protected void dispatchRestoreInstanceState(@NonNull SparseArray<Parcelable> con
dispatchThawSelfOnly(container);
}

@SuppressWarnings("unchecked")
@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState) state;
Expand All @@ -251,6 +247,7 @@ public void onRestoreInstanceState(Parcelable state) {
}
}

@SuppressWarnings("unchecked")
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
Expand Down Expand Up @@ -287,21 +284,28 @@ public void setOnFocusChangeListener(OnFocusChangeListener l) {

@Override
public void focusOnField(final CreditEntryFieldBase field) {
if (this.textHelper != null) {
this.textHelper.setText(field.helperText());
}

if (!scrolling) {
field.requestFocus();
if(!scrolling) {
scrolling = true;
scrollToTarget(field instanceof CreditCardText ? 0 : field.getLeft(), new Runnable() {
@Override
public void run() {
scrolling = false;
field.requestFocus();
// if there was another focus before we were done.. catch up.
if(!field.hasFocus()) {
View newFocus = getFocusedChild();
if (newFocus instanceof CreditEntryFieldBase) {
focusOnField((CreditEntryFieldBase) newFocus);
}
}
}
});
}

if (this.textHelper != null) {
this.textHelper.setText(field.helperText());
}

if (field instanceof SecurityCodeText) {
((SecurityCodeText) field).setType(creditCardText.getType());
updateCardImage(true);
Expand Down Expand Up @@ -582,6 +586,7 @@ private SavedState(Parcel in, ClassLoader classLoader) {
childrenStates = in.readSparseArray(classLoader);
}

@SuppressWarnings("unchecked")
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Add the project to your `dependencies`
...
compile 'com.github.dbachelder:CreditCardEntry:1.4.2'
compile 'com.github.dbachelder:CreditCardEntry:1.4.3'
}
```

Expand Down Expand Up @@ -114,6 +114,10 @@ In code:

# Version History

###8/21/2015
- focus change behavior can now keep up with very fast typing
- several minor performance tweaks

###8/18/2015
- fix typo in hint for exp date
- fix for dates not being able to exceed 2034
Expand Down

0 comments on commit b4514dc

Please sign in to comment.