From 28c95665c304973616b588f15ed2d829d7bb223f Mon Sep 17 00:00:00 2001 From: sspanak Date: Tue, 27 Feb 2024 15:00:47 +0200 Subject: [PATCH] optimized the text case changing code --- .../github/sspanak/tt9/ime/TraditionalT9.java | 43 +++++++++++++------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/github/sspanak/tt9/ime/TraditionalT9.java b/app/src/main/java/io/github/sspanak/tt9/ime/TraditionalT9.java index 92563a45..5a0d1485 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ime/TraditionalT9.java +++ b/app/src/main/java/io/github/sspanak/tt9/ime/TraditionalT9.java @@ -715,19 +715,7 @@ public class TraditionalT9 extends KeyPadHandler { } // when typing a word or viewing scrolling the suggestions, only change the case else if (!isSuggestionViewHidden()) { - String currentSuggestionBefore = getComposingText(); - - // When we are in AUTO mode and the dictionary word is in uppercase, - // the mode would switch to UPPERCASE, but visually, the word would not change. - // This is why we retry, until there is a visual change. - for (int retries = 0; retries < 2 && mInputMode.nextTextCase(); retries++) { - setSuggestions(mInputMode.getSuggestions(), suggestionBar.getCurrentIndex()); - textField.setComposingText(suggestionBar.getCurrentSuggestion()); - - if (!currentSuggestionBefore.equals(getComposingText())) { - break; - } - } + nextTextCase(); } // make "abc" and "ABC" separate modes from user perspective else if (mInputMode instanceof ModeABC && mLanguage.hasUpperCase() && mInputMode.getTextCase() == InputMode.CASE_LOWER) { @@ -762,6 +750,35 @@ public class TraditionalT9 extends KeyPadHandler { } + private void nextTextCase() { + String currentSuggestionBefore = getComposingText(); + int currentSuggestionIndex = suggestionBar.getCurrentIndex(); + + // When we are in AUTO mode and the dictionary word is in uppercase, + // the mode would switch to UPPERCASE, but visually, the word would not change. + // This is why we retry, until there is a visual change. + for (int retries = 0; retries < 2 && mInputMode.nextTextCase(); retries++) { + String currentSuggestionAfter = mInputMode.getSuggestions().size() >= suggestionBar.getCurrentIndex() ? mInputMode.getSuggestions().get(suggestionBar.getCurrentIndex()) : ""; + // If the suggestions are special characters, changing the text case means selecting the + // next character group. Hence, "before" and "after" are different. Also, if the new suggestion + // list is shorter, the "before" index may be invalid, so "after" would be empty. + // In these cases, we scroll to the first one, for consistency. + if (currentSuggestionAfter.isEmpty() || !currentSuggestionBefore.equalsIgnoreCase(currentSuggestionAfter)) { + currentSuggestionIndex = 0; + break; + } + + // the suggestion list is the same and the text case is different, so let's use it + if (!currentSuggestionBefore.equals(currentSuggestionAfter)) { + break; + } + } + + setSuggestions(mInputMode.getSuggestions(), currentSuggestionIndex); + textField.setComposingText(suggestionBar.getCurrentSuggestion()); + } + + private void autoCorrectSpace(String currentWord, boolean isWordAcceptedManually, int nextKey) { if (mInputMode.shouldDeletePrecedingSpace(inputType)) { textField.deletePrecedingSpace(currentWord);