0
0
mirror of https://github.com/TrianguloY/UrlChecker.git synced 2024-09-19 20:02:16 +02:00

DefaultTextWatcher

This commit is contained in:
TrianguloY 2023-04-23 21:11:27 +02:00
parent 97ffd0c46e
commit 570b6b89a0
5 changed files with 58 additions and 78 deletions

View File

@ -5,7 +5,6 @@ import static com.trianguloy.urlchecker.utilities.JavaUtils.valueOrDefault;
import android.app.AlertDialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
@ -27,6 +26,7 @@ import com.trianguloy.urlchecker.modules.AModuleDialog;
import com.trianguloy.urlchecker.modules.companions.Flags;
import com.trianguloy.urlchecker.url.UrlData;
import com.trianguloy.urlchecker.utilities.AndroidUtils;
import com.trianguloy.urlchecker.utilities.DefaultTextWatcher;
import com.trianguloy.urlchecker.utilities.GenericPref;
import com.trianguloy.urlchecker.utilities.Inflater;
import com.trianguloy.urlchecker.utilities.InternalFile;
@ -129,15 +129,7 @@ class FlagsDialog extends AModuleDialog {
// SEARCH
// Set up search text
searchInput.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
searchInput.addTextChangedListener(new DefaultTextWatcher() {
@Override
public void afterTextChanged(Editable text) {
for (int i = 0; i < hiddenFlagsVG.getChildCount(); i++) {
@ -368,15 +360,7 @@ class FlagsConfig extends AModuleConfig {
// Search
((EditText) flagsDialogLayout.findViewById(R.id.search)).addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
((EditText) flagsDialogLayout.findViewById(R.id.search)).addTextChangedListener(new DefaultTextWatcher() {
@Override
public void afterTextChanged(Editable text) {
for (int i = 0; i < box.getChildCount(); i++) {

View File

@ -1,7 +1,6 @@
package com.trianguloy.urlchecker.modules.list;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
@ -13,6 +12,7 @@ import com.trianguloy.urlchecker.modules.AModuleData;
import com.trianguloy.urlchecker.modules.AModuleDialog;
import com.trianguloy.urlchecker.modules.DescriptionConfig;
import com.trianguloy.urlchecker.url.UrlData;
import com.trianguloy.urlchecker.utilities.DefaultTextWatcher;
import com.trianguloy.urlchecker.utilities.DoubleEvent;
/**
@ -41,9 +41,10 @@ public class TextInputModule extends AModuleData {
}
}
class TextInputDialog extends AModuleDialog implements TextWatcher {
class TextInputDialog extends AModuleDialog {
private final DoubleEvent doubleEdit = new DoubleEvent(1000); // if two updates happens in less than this milliseconds, they are considered as the same
private boolean skipUpdate = false;
private EditText edtxt_url;
@ -59,39 +60,31 @@ class TextInputDialog extends AModuleDialog implements TextWatcher {
@Override
public void onInitialize(View views) {
edtxt_url = views.findViewById(R.id.url);
edtxt_url.addTextChangedListener(this);
edtxt_url.addTextChangedListener(new DefaultTextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if (skipUpdate) return;
// new url by the user
var newUrlData = new UrlData(s.toString())
.dontTriggerOwn()
.disableUpdates();
// mark as minor if too quick
if (doubleEdit.checkAndTrigger()) newUrlData.asMinorUpdate();
// set
setUrl(newUrlData);
}
});
}
@Override
public void onDisplayUrl(UrlData urlData) {
// setText fires the afterTextChanged listener, so we need to remove it
edtxt_url.removeTextChangedListener(this);
// setText fires the afterTextChanged listener, so we need to skip it
skipUpdate = true;
edtxt_url.setText(urlData.url);
edtxt_url.addTextChangedListener(this);
skipUpdate = false;
doubleEdit.reset(); // next user update, even if immediately after, will be considered new
}
// ------------------- TextWatcher -------------------
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
// new url by the user
var newUrlData = new UrlData(s.toString())
.dontTriggerOwn()
.disableUpdates();
// mark as minor if too quick
if (doubleEdit.checkAndTrigger()) newUrlData.asMinorUpdate();
// set
setUrl(newUrlData);
}
}

View File

@ -4,10 +4,8 @@ import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.trianguloy.urlchecker.R;
@ -19,6 +17,7 @@ import com.trianguloy.urlchecker.modules.AModuleDialog;
import com.trianguloy.urlchecker.modules.companions.VirusTotalUtility;
import com.trianguloy.urlchecker.url.UrlData;
import com.trianguloy.urlchecker.utilities.AndroidUtils;
import com.trianguloy.urlchecker.utilities.DefaultTextWatcher;
import com.trianguloy.urlchecker.utilities.GenericPref;
import com.trianguloy.urlchecker.utilities.UrlUtils;
@ -57,7 +56,7 @@ public class VirusTotalModule extends AModuleData {
}
}
class VirusTotalConfig extends AModuleConfig implements TextWatcher {
class VirusTotalConfig extends AModuleConfig {
final GenericPref.Str api_key;
@ -79,23 +78,15 @@ class VirusTotalConfig extends AModuleConfig implements TextWatcher {
@Override
public void onInitialize(View views) {
final EditText edit_key = (EditText) views.findViewById(R.id.api_key);
var edit_key = views.<TextView>findViewById(R.id.api_key);
edit_key.setText(api_key.get());
edit_key.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
api_key.set(s.toString());
if (!canBeEnabled()) disable();
edit_key.addTextChangedListener(new DefaultTextWatcher() {
@Override
public void afterTextChanged(Editable s) {
api_key.set(s.toString());
if (!canBeEnabled()) disable();
}
});
}
}

View File

@ -0,0 +1,21 @@
package com.trianguloy.urlchecker.utilities;
import android.text.Editable;
import android.text.TextWatcher;
/***
* TextWatcher empty implementation, so you can override only what you need
*/
public class DefaultTextWatcher implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
}

View File

@ -3,7 +3,6 @@ package com.trianguloy.urlchecker.utilities;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
@ -198,15 +197,7 @@ public abstract class GenericPref<T> {
*/
public void attachToEditText(EditText editText, JavaUtils.UnaryOperator<String> loadMod, JavaUtils.UnaryOperator<String> storeMod) {
editText.setText(loadMod.apply(get()));
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
editText.addTextChangedListener(new DefaultTextWatcher() {
@Override
public void afterTextChanged(Editable s) {
set(storeMod.apply(s.toString()));