diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyBackspace.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyBackspace.java index 4663c971..69c7a735 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyBackspace.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyBackspace.java @@ -1,6 +1,7 @@ package io.github.sspanak.tt9.ui.main.keys; import android.content.Context; +import android.os.Handler; import android.util.AttributeSet; import android.view.KeyEvent; @@ -11,6 +12,7 @@ import io.github.sspanak.tt9.ui.Vibration; public class SoftKeyBackspace extends SwipeableKey { private int repeat = 0; + private final Handler waitForSwipe = new Handler(); public SoftKeyBackspace(Context context) { super(context); @@ -61,7 +63,15 @@ public class SoftKeyBackspace extends SwipeableKey { @Override final protected boolean handlePress() { super.handlePress(); - return deleteText(); + waitForSwipe.postDelayed(this::handlePressDebounced, 15); + return true; + } + + + private void handlePressDebounced() { + if (notSwiped()) { + deleteText(); + } } @@ -80,6 +90,12 @@ public class SoftKeyBackspace extends SwipeableKey { } + @Override + protected void handleStartSwipeX(float position, float delta) { + waitForSwipe.removeCallbacksAndMessages(null); + } + + @Override protected void handleEndSwipeX(float position, float delta) { if (validateTT9Handler()) { @@ -88,16 +104,13 @@ public class SoftKeyBackspace extends SwipeableKey { } - private boolean deleteText() { + private void deleteText() { if (validateTT9Handler() && !tt9.onBackspace(repeat)) { // Limited or special numeric field (e.g. formatted money or dates) cannot always return // the text length, therefore onBackspace() seems them as empty and does nothing. This results // in fallback to the default hardware key action. Here we simulate the hardware BACKSPACE. tt9.sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL); - return true; } - - return false; } diff --git a/app/src/main/java/io/github/sspanak/tt9/util/Text.java b/app/src/main/java/io/github/sspanak/tt9/util/Text.java index 967e90f3..35175550 100644 --- a/app/src/main/java/io/github/sspanak/tt9/util/Text.java +++ b/app/src/main/java/io/github/sspanak/tt9/util/Text.java @@ -175,12 +175,13 @@ public class Text extends TextTools { boolean endsWithWhitespaceBlock = Character.isWhitespace(lastChar) && Character.isWhitespace(penultimateChar); boolean endsWithPunctuationBlock = (lastChar == '.' || lastChar == ',') && (penultimateChar == '.' || penultimateChar == ','); - for (int i = text.length() - 1; i >= 0; i--) { + for (int i = text.length() - 1, firstChar = 1; i >= 0; i--, firstChar = 0) { char currentChar = text.charAt(i); + if ( (endsWithPunctuationBlock && currentChar != '.' && currentChar != ',') || (endsWithWhitespaceBlock && !Character.isWhitespace(currentChar)) - || (!endsWithWhitespaceBlock && !endsWithPunctuationBlock && (Character.isWhitespace(currentChar) || currentChar == '.' || currentChar == ',')) + || (!endsWithWhitespaceBlock && !endsWithPunctuationBlock && firstChar == 0 && Character.isWhitespace(currentChar)) ) { return i + 1; }