Skip to content

Commit

Permalink
Version 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DIG- committed Apr 4, 2023
2 parents e535572 + 33acfda commit d633280
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ class MainActivity : AppCompatActivity() {
TextMultiMaskTextWatcher.Option(TextMask("## ### ##")) { it.length < 8 },
TextMultiMaskTextWatcher.Option(TextMask("##.##-####")) { it.length >= 8 },
)).insert()
TextMaskTextWatcher(binding.mainCustom, TextMask("!##-###==####")).insert()
}
}
16 changes: 16 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,21 @@

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/main_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Custom"
android:inputType="number" />

</com.google.android.material.textfield.TextInputLayout>


</LinearLayout>
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "1.0.0-{build}"
version: "1.1.0-{build}"
skip_tags: true
clone_depth: 1
branches:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ buildscript {
"java" : JavaVersion.VERSION_1_8,
"mask" : [
"major": "1",
"minor": "0",
"minor": "1",
"pkg" : "",
],
"android": [
Expand Down
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Changelog
=========
1.1.0
-----
- Fix cursor position in Android TextWatcher
- Added custom `.toString()` to all classes

1.0.0
-----
- Library working
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,37 @@ public void afterTextChanged(@Nullable final Editable text) {
final CharSequence formatted = mask.format(text);
final int previousLength = text.length();
final int currentLength = formatted.length();
final CharSequence previous = text.subSequence(0, previousLength);
final int previousSelection = editText.getSelectionStart();
text.replace(0, previousLength, formatted, 0, currentLength);
final int currentSelection = editText.getSelectionStart();

// Update cursor
if (currentLength < previousLength) {
editText.setSelection(findCursorPosition(text, editText.getSelectionStart()));
if (currentSelection < previousSelection) {
if (mask.keepMask && previousSelection > 0 && previousSelection < currentLength) {
editText.setSelection(findCursorPosition(text, previousSelection) - 1);
} else if (previousSelection <= currentLength) {
editText.setSelection(previousSelection);
}
} else if (currentSelection == previousSelection) {
if (currentSelection != 0) {
int i = currentSelection - 1;
if (mask.mask.charAt(i) != mask.maskCharacter && mask.mask.charAt(i) != previous.charAt(i)) {
editText.setSelection(findCursorPosition(text, i));
}
}
} else {
if (currentSelection >= mask.mask.length() && mask.keepMask) {
int i = currentLength - 1;
for (; i > 0; i--) {
if (mask.mask.charAt(i) != text.charAt(i)) {
break;
}
}
editText.setSelection(i + 1);
} else {
editText.setSelection(findCursorPosition(text, previousSelection));
}
}

// Restore InputFilters
Expand Down Expand Up @@ -94,16 +120,17 @@ public String toString() {
}

private int findCursorPosition(@NotNull final Editable text, final int start) {
if (text.length() == 0) return start;
int position = start;
for (int i = start; i < mask.mask.length(); i++) {
int i = start;
for (; i < mask.mask.length(); i++) {
if (i >= text.length()) {
break;
}
if (mask.mask.charAt(i) == mask.maskCharacter) {
i++;
break;
}
position++;
}
position++;
return Math.min(position, text.length());
return i;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,37 @@ private void format(@NotNull final Editable text) {
final CharSequence formatted = mask.format(text);
final int previousLength = text.length();
final int currentLength = formatted.length();
final CharSequence previous = text.subSequence(0, previousLength);
final int previousSelection = editText.getSelectionStart();
text.replace(0, previousLength, formatted, 0, currentLength);
final int currentSelection = editText.getSelectionStart();

// Update cursor
if (currentLength < previousLength) {
editText.setSelection(findCursorPosition(text, editText.getSelectionStart()));
if (currentSelection < previousSelection) {
if (mask.keepMask && previousSelection > 0 && previousSelection < currentLength) {
editText.setSelection(findCursorPosition(text, previousSelection) - 1);
} else if (previousSelection <= currentLength) {
editText.setSelection(previousSelection);
}
} else if (currentSelection == previousSelection) {
if (currentSelection != 0) {
int i = currentSelection - 1;
if (mask.mask.charAt(i) != mask.maskCharacter && mask.mask.charAt(i) != previous.charAt(i)) {
editText.setSelection(findCursorPosition(text, i));
}
}
} else {
if (currentSelection >= mask.mask.length() && mask.keepMask) {
int i = currentLength - 1;
for (; i > 0; i--) {
if (mask.mask.charAt(i) != text.charAt(i)) {
break;
}
}
editText.setSelection(i + 1);
} else {
editText.setSelection(findCursorPosition(text, previousSelection));
}
}

// Restore InputFilters
Expand Down Expand Up @@ -153,16 +179,17 @@ public String toString() {
}

private int findCursorPosition(@NotNull final Editable text, final int start) {
if (text.length() == 0) return start;
int position = start;
for (int i = start; i < mask.mask.length(); i++) {
int i = start;
for (; i < mask.mask.length(); i++) {
if (i >= text.length()) {
break;
}
if (mask.mask.charAt(i) == mask.maskCharacter) {
i++;
break;
}
position++;
}
position++;
return Math.min(position, text.length());
return i;
}

}

0 comments on commit d633280

Please sign in to comment.