Skip to content

Commit

Permalink
[DatePicker] Fix auto-adding delimiters
Browse files Browse the repository at this point in the history
  • Loading branch information
pubiqq committed Nov 3, 2023
1 parent 1756f23 commit ba63024
Showing 1 changed file with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
import com.google.android.material.textfield.TextInputLayout;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

abstract class DateFormatTextWatcher extends TextWatcherAdapter {

@NonNull private final TextInputLayout textInputLayout;

private final String formatHint;
private final DateFormat dateFormat;
private final SimpleDateFormat dateFormat;
private final CalendarConstraints constraints;
private final String outOfRange;
private final Runnable setErrorCallback;
Expand All @@ -45,11 +44,10 @@ abstract class DateFormatTextWatcher extends TextWatcherAdapter {

DateFormatTextWatcher(
final String formatHint,
DateFormat dateFormat,
SimpleDateFormat dateFormat,
@NonNull TextInputLayout textInputLayout,
CalendarConstraints constraints) {

this.formatHint = formatHint;
this.dateFormat = dateFormat;
this.textInputLayout = textInputLayout;
this.constraints = constraints;
Expand Down Expand Up @@ -85,7 +83,8 @@ public void onTextChanged(@NonNull CharSequence s, int start, int before, int co
textInputLayout.setError(null);
onValidDate(null);

if (TextUtils.isEmpty(s) || s.length() < formatHint.length()) {
String datePattern = dateFormat.toPattern();
if (TextUtils.isEmpty(s) || s.length() < datePattern.length()) {
return;
}

Expand Down Expand Up @@ -113,19 +112,20 @@ public void beforeTextChanged(@NonNull CharSequence s, int start, int count, int

@Override
public void afterTextChanged(@NonNull Editable s) {
// Exclude some languages from automatically adding delimiters.
if (Locale.getDefault().getLanguage().equals(Locale.KOREAN.getLanguage())) {
String datePattern = dateFormat.toPattern();
if (s.length() == 0 || s.length() >= datePattern.length() || s.length() < lastLength) {
return;
}

if (s.length() == 0 || s.length() >= formatHint.length() || s.length() < lastLength) {
return;
char nextPatternChar = datePattern.charAt(s.length());
if (isDelimiter(nextPatternChar)) {
s.append(nextPatternChar);
}
}

char nextCharHint = formatHint.charAt(s.length());
if (!Character.isLetterOrDigit(nextCharHint)) {
s.append(nextCharHint);
}
private boolean isDelimiter(char ch) {
return !(ch >= 'A' && ch <= 'Z')
&& !(ch >= 'a' && ch <= 'z');
}

private Runnable createRangeErrorCallback(final long milliseconds) {
Expand Down

0 comments on commit ba63024

Please sign in to comment.