1
0
Fork 0

simplified word adding

This commit is contained in:
sspanak 2023-08-14 15:26:42 +03:00 committed by Dimo Karaivanov
parent bd5385d17d
commit d8c2f7fc15
17 changed files with 110 additions and 117 deletions

View file

@ -4,6 +4,7 @@ import android.content.Context;
import android.database.sqlite.SQLiteConstraintException;
import android.os.Handler;
import androidx.annotation.NonNull;
import androidx.sqlite.db.SimpleSQLiteQuery;
import java.util.ArrayList;
@ -118,11 +119,7 @@ public class DictionaryDb {
}
public static void insertWord(ConsumerCompat<Integer> statusHandler, Language language, String word) throws Exception {
if (language == null) {
throw new InvalidLanguageException();
}
public static void insertWord(ConsumerCompat<Integer> statusHandler, @NonNull Language language, String word) throws Exception {
if (word == null || word.length() == 0) {
throw new InsertBlankWordException();
}

View file

@ -1,6 +1,7 @@
package io.github.sspanak.tt9.ime;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
@ -9,12 +10,13 @@ import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import org.jetbrains.annotations.NotNull;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
import io.github.sspanak.tt9.Logger;
import io.github.sspanak.tt9.R;
import io.github.sspanak.tt9.db.DictionaryDb;
import io.github.sspanak.tt9.ime.helpers.InputModeValidator;
import io.github.sspanak.tt9.ime.helpers.InputType;
@ -24,6 +26,7 @@ import io.github.sspanak.tt9.languages.Language;
import io.github.sspanak.tt9.languages.LanguageCollection;
import io.github.sspanak.tt9.preferences.SettingsStore;
import io.github.sspanak.tt9.preferences.helpers.Hotkeys;
import io.github.sspanak.tt9.ui.AddWordAct;
import io.github.sspanak.tt9.ui.UI;
import io.github.sspanak.tt9.ui.main.MainView;
import io.github.sspanak.tt9.ui.tray.StatusBar;
@ -32,9 +35,9 @@ import io.github.sspanak.tt9.ui.tray.SuggestionsBar;
public class TraditionalT9 extends KeyPadHandler {
// internal settings/data
private boolean isActive = false;
@NotNull private TextField textField = new TextField(null, null);
@NotNull private InputType inputType = new InputType(null, null);
@NotNull private final Handler autoAcceptHandler = new Handler(Looper.getMainLooper());
@NonNull private TextField textField = new TextField(null, null);
@NonNull private InputType inputType = new InputType(null, null);
@NonNull private final Handler autoAcceptHandler = new Handler(Looper.getMainLooper());
// input mode
private ArrayList<Integer> allowedInputModes = new ArrayList<>();
@ -49,7 +52,6 @@ public class TraditionalT9 extends KeyPadHandler {
private StatusBar statusBar = null;
private SuggestionsBar suggestionBar = null;
private static TraditionalT9 self;
public static Context getMainContext() {
return self.getApplicationContext();
@ -120,6 +122,20 @@ public class TraditionalT9 extends KeyPadHandler {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int result = super.onStartCommand(intent, flags, startId);
String message = intent.getStringExtra(AddWordAct.INTENT_FILTER);
if (message != null && !message.isEmpty()) {
forceShowWindowIfHidden();
UI.toastLong(self, message);
}
return result;
}
protected void onInit() {
self = this;
@ -133,7 +149,6 @@ public class TraditionalT9 extends KeyPadHandler {
loadSettings();
validateFunctionKeys();
settings.clearLastWord();
}
@ -185,7 +200,6 @@ public class TraditionalT9 extends KeyPadHandler {
initTyping();
initUi();
restoreAddedWordIfAny();
isActive = true;
}
@ -333,7 +347,16 @@ public class TraditionalT9 extends KeyPadHandler {
}
cancelAutoAccept();
showAddWord();
textField.finishComposingText();
clearSuggestions();
String word = textField.getSurroundingWord();
if (word.isEmpty()) {
UI.toastLong(this, R.string.add_word_no_selection);
} else {
UI.showAddWordDialog(this, mLanguage.getId(), word);
}
return true;
}
@ -726,36 +749,6 @@ public class TraditionalT9 extends KeyPadHandler {
}
private void showAddWord() {
textField.finishComposingText();
clearSuggestions();
UI.showAddWordDialog(this, mLanguage.getId(), textField.getSurroundingWord());
}
/**
* restoreAddedWordIfAny
* If a new word was added to the dictionary, this function will append add it to the current input field.
*/
private void restoreAddedWordIfAny() {
String word = settings.getLastWord();
settings.clearLastWord();
if (word.length() == 0 || word.equals(textField.getSurroundingWord())) {
return;
}
try {
Logger.d("restoreAddedWordIfAny", "Restoring word: '" + word + "'...");
textField.setText(word);
mInputMode.reset();
} catch (Exception e) {
Logger.w("restoreLastWord", "Could not restore the last added word. " + e.getMessage());
}
}
/**
* createSoftKeyView
* Generates the actual UI of TT9.

View file

@ -10,6 +10,8 @@ import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -189,7 +191,7 @@ public class TextField {
* getSurroundingWord
* Returns the word next or around the cursor. Scanning length is up to 50 chars in each direction.
*/
public String getSurroundingWord() {
@NonNull public String getSurroundingWord() {
Matcher before = beforeCursorWordRegex.matcher(getTextBeforeCursor());
Matcher after = afterCursorWordRegex.matcher(getTextAfterCursor());

View file

@ -284,22 +284,4 @@ public class SettingsStore {
public int getWordFrequencyMax() { return 25500; }
public int getWordFrequencyNormalizationDivider() { return 100; } // normalized frequency = getWordFrequencyMax() / getWordFrequencyNormalizationDivider()
/************* add word, last word *************/
public String getLastWord() {
return prefs.getString("last_word", "");
}
public void saveLastWord(String lastWord) {
// "last_word" was part of the original Settings implementation.
// It is weird, but it is simple and it works, so I decided to keep it.
prefsEditor.putString("last_word", lastWord);
prefsEditor.apply();
}
public void clearLastWord() {
this.saveLastWord("");
}
}

View file

@ -3,90 +3,109 @@ package io.github.sspanak.tt9.ui;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import io.github.sspanak.tt9.Logger;
import io.github.sspanak.tt9.R;
import io.github.sspanak.tt9.db.DictionaryDb;
import io.github.sspanak.tt9.db.exceptions.InsertBlankWordException;
import io.github.sspanak.tt9.ime.TraditionalT9;
import io.github.sspanak.tt9.languages.InvalidLanguageException;
import io.github.sspanak.tt9.languages.Language;
import io.github.sspanak.tt9.languages.LanguageCollection;
import io.github.sspanak.tt9.preferences.SettingsStore;
public class AddWordAct extends AppCompatActivity {
public static final String INTENT_FILTER = "tt9.add_word";
private View main;
private int lang;
private SettingsStore settings;
private Language language;
private String word;
@Override
protected void onCreate(Bundle savedData) {
settings = new SettingsStore(this);
AppCompatDelegate.setDefaultNightMode(settings.getTheme());
super.onCreate(savedData);
readInput();
render(getMessage());
}
private void readInput() {
Intent i = getIntent();
word = i.getStringExtra("io.github.sspanak.tt9.word");
lang = i.getIntExtra("io.github.sspanak.tt9.lang", -1);
language = LanguageCollection.getLanguage(this, i.getIntExtra("io.github.sspanak.tt9.lang", -1));
}
main = View.inflate(this, R.layout.addwordview, null);
private String getMessage() {
if (language == null) {
Logger.e("WordManager.confirmAddWord", "Cannot insert a word for NULL language");
UI.toastLong(getApplicationContext(), R.string.add_word_invalid_language);
return null;
}
EditText et = main.findViewById(R.id.add_word_text);
et.setOnClickListener(this::addWord);
et.setText(word);
et.setSelection(word.length());
return getString(R.string.add_word_confirm, word, language.getName());
}
private void render(String message) {
if (message == null || word == null || word.isEmpty()) {
finish();
return;
}
View main = View.inflate(this, R.layout.addwordview, null);
((TextView) main.findViewById(R.id.add_word_dialog_text)).append(message);
setContentView(main);
}
private void onAddedWord(int statusCode) {
String message;
switch (statusCode) {
case 0:
Logger.d("onAddedWord", "Successfully added word: '" + word + '"');
settings.saveLastWord(word);
message = getString(R.string.add_word_success, word);
break;
case 1:
UI.toastLongFromAsync(
main.getContext(),
getResources().getString(R.string.add_word_exist, word)
);
message = getResources().getString(R.string.add_word_exist, word);
break;
default:
UI.toastLongFromAsync(main.getContext(), R.string.error_unexpected);
message = getString(R.string.error_unexpected);
break;
}
finish();
sendMessageToMain(message);
}
public void addWord(View v) {
try {
// re-fetch the word, in case the user has changed it after the initialization
word = ((EditText) main.findViewById(R.id.add_word_text)).getText().toString();
Logger.d("addWord", "Attempting to add word: '" + word + "'...");
DictionaryDb.insertWord(this::onAddedWord, LanguageCollection.getLanguage(this, lang), word);
DictionaryDb.insertWord(this::onAddedWord, language, word);
} catch (InsertBlankWordException e) {
Logger.e("AddWordAct.addWord", e.getMessage());
UI.toastLong(this, R.string.add_word_blank);
finish();
sendMessageToMain(getString(R.string.add_word_blank));
} catch (InvalidLanguageException e) {
Logger.e("AddWordAct.addWord", "Cannot insert a word for language with ID: '" + lang + "'. " + e.getMessage());
UI.toastLong(this, R.string.add_word_invalid_language);
Logger.e("AddWordAct.addWord", "Cannot insert a word for language: '" + language.getName() + "'. " + e.getMessage());
finish();
sendMessageToMain(getString(R.string.add_word_invalid_language));
} catch (Exception e) {
Logger.e("AddWordAct.addWord", e.getMessage());
UI.toastLong(this, e.getMessage());
finish();
sendMessageToMain(e.getMessage());
}
}
private void sendMessageToMain(String message) {
Intent intent = new Intent(this, TraditionalT9.class);
intent.putExtra(INTENT_FILTER, message);
startService(intent);
}
public void cancelAddingWord(View v) {
finish();
}