1
0
Fork 0

fixed the 'wait for the dictionary to load' toast being displayed for too long, when attempting to press many keys in a short period of time

This commit is contained in:
sspanak 2024-03-28 18:49:35 +02:00 committed by Dimo Karaivanov
parent d95eb807cc
commit cc0ce856c5
3 changed files with 22 additions and 11 deletions

View file

@ -104,7 +104,7 @@ public abstract class HotkeyHandler extends TypingHandler {
} }
if (DictionaryLoader.getInstance(this).isRunning()) { if (DictionaryLoader.getInstance(this).isRunning()) {
UI.toast(this, R.string.dictionary_loading_please_wait); UI.toastShortSingle(this, R.string.dictionary_loading_please_wait);
return true; return true;
} }
@ -223,7 +223,7 @@ public abstract class HotkeyHandler extends TypingHandler {
renderMainView(); renderMainView();
forceShowWindowIfHidden(); forceShowWindowIfHidden();
if (!suggestionOps.isEmpty()) { if (!suggestionOps.isEmpty()) {
UI.toastLanguage(this, mLanguage); UI.toastShortSingle(this, mLanguage.getClass().getSimpleName(), mLanguage.getName());
} }
if (mInputMode instanceof ModePredictive) { if (mInputMode instanceof ModePredictive) {

View file

@ -264,7 +264,7 @@ public abstract class TypingHandler extends KeyPadHandler {
protected void getSuggestions() { protected void getSuggestions() {
if (mInputMode instanceof ModePredictive && DictionaryLoader.getInstance(this).isRunning()) { if (mInputMode instanceof ModePredictive && DictionaryLoader.getInstance(this).isRunning()) {
mInputMode.reset(); mInputMode.reset();
UI.toast(this, R.string.dictionary_loading_please_wait); UI.toastShortSingle(this, R.string.dictionary_loading_please_wait);
} else { } else {
mInputMode.loadSuggestions(this::handleSuggestions, suggestionOps.getCurrent()); mInputMode.loadSuggestions(this::handleSuggestions, suggestionOps.getCurrent());
} }

View file

@ -10,11 +10,14 @@ import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import java.util.HashMap;
import io.github.sspanak.tt9.R;
import io.github.sspanak.tt9.languages.Language; import io.github.sspanak.tt9.languages.Language;
import io.github.sspanak.tt9.preferences.PreferencesActivity; import io.github.sspanak.tt9.preferences.PreferencesActivity;
public class UI { public class UI {
private static Toast toastLang = null; private static final HashMap<String, Toast> singleToasts = new HashMap<>();
public static void showChangeKeyboardDialog(Context context) { public static void showChangeKeyboardDialog(Context context) {
@ -79,14 +82,22 @@ public class UI {
Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
} }
public static void toastLanguage(@NonNull Context context, @NonNull Language language) { public static void toastShortSingle(@NonNull Context context, @NonNull String uniqueId, @NonNull String message) {
if (toastLang != null) { Toast toast = singleToasts.get(uniqueId);
toastLang.cancel();
if (toast != null) {
toast.cancel();
} }
// we recreate the toast, because if set new text, when it is fading out, // we recreate the toast, because if set new text, when it is fading out, it is ignored
// the new text is discarded toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
toastLang = Toast.makeText(context, language.getName(), Toast.LENGTH_SHORT); toast.show();
toastLang.show();
singleToasts.put(uniqueId, toast);
}
public static void toastShortSingle(@NonNull Context context, int resourceId) {
toastShortSingle(context, String.valueOf(resourceId), context.getString(resourceId));
} }
} }