Refactoring

pull/199/head
M66B 4 years ago
parent 78d05820f9
commit bdd8dad04d

@ -34,7 +34,6 @@ import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.CompoundButton; import android.widget.CompoundButton;
@ -83,7 +82,7 @@ public class FragmentDialogSearch extends FragmentDialogBase {
View dview = LayoutInflater.from(context).inflate(R.layout.dialog_search, null); View dview = LayoutInflater.from(context).inflate(R.layout.dialog_search, null);
final AutoCompleteTextView etQuery = dview.findViewById(R.id.etQuery); final TextViewAutoCompleteAction etQuery = dview.findViewById(R.id.etQuery);
final TextView tvSearch1 = dview.findViewById(R.id.tvSearch1); final TextView tvSearch1 = dview.findViewById(R.id.tvSearch1);
final TextView tvSearch2 = dview.findViewById(R.id.tvSearch2); final TextView tvSearch2 = dview.findViewById(R.id.tvSearch2);
final TextView tvSearch3 = dview.findViewById(R.id.tvSearch3); final TextView tvSearch3 = dview.findViewById(R.id.tvSearch3);
@ -171,6 +170,14 @@ public class FragmentDialogSearch extends FragmentDialogBase {
} }
}); });
etQuery.setActionRunnable(new Runnable() {
@Override
public void run() {
etQuery.setText(null);
}
});
etQuery.setActionEnabled(true);
etQuery.setAdapter(adapter); etQuery.setAdapter(adapter);
View.OnClickListener onSearch = new View.OnClickListener() { View.OnClickListener onSearch = new View.OnClickListener() {

@ -20,6 +20,7 @@ package eu.faircode.email;
*/ */
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
@ -31,39 +32,48 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatAutoCompleteTextView; import androidx.appcompat.widget.AppCompatAutoCompleteTextView;
public class TextViewAutoCompleteClearable extends AppCompatAutoCompleteTextView { public class TextViewAutoCompleteAction extends AppCompatAutoCompleteTextView {
private Drawable drawable = null; private Drawable drawable = null;
private Runnable action = null;
private boolean enabled = false;
public TextViewAutoCompleteClearable(@NonNull Context context) { public TextViewAutoCompleteAction(@NonNull Context context) {
super(context); super(context);
init(); init(context, null);
} }
public TextViewAutoCompleteClearable(@NonNull Context context, @Nullable AttributeSet attrs) { public TextViewAutoCompleteAction(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs); super(context, attrs);
init(); init(context, attrs);
} }
public TextViewAutoCompleteClearable(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { public TextViewAutoCompleteAction(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); super(context, attrs, defStyleAttr);
init(); init(context, attrs);
} }
public void init() { public void init(Context context, AttributeSet attrs) {
drawable = getContext().getDrawable(R.drawable.twotone_close_24); if (attrs == null)
drawable = getContext().getDrawable(R.drawable.twotone_warning_24);
else {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextViewAutoCompleteAction, 0, 0);
drawable = a.getDrawable(R.styleable.TextViewAutoCompleteAction_end_drawable);
}
drawable.setTint(getCurrentTextColor()); drawable.setTint(getCurrentTextColor());
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
setOnTouchListener(new OnTouchListener() { setOnTouchListener(new OnTouchListener() {
@Override @Override
public boolean onTouch(View v, MotionEvent event) { public boolean onTouch(View v, MotionEvent event) {
if (!enabled)
return false;
if (getCompoundDrawables()[2] == null) if (getCompoundDrawables()[2] == null)
return false; return false;
if (event.getAction() != MotionEvent.ACTION_UP) if (event.getAction() != MotionEvent.ACTION_UP)
return false; return false;
if (event.getX() > getWidth() - getPaddingRight() - drawable.getIntrinsicWidth()) { if (event.getX() > getWidth() - getPaddingRight() - drawable.getIntrinsicWidth()) {
setText(""); if (action != null)
setCompoundDrawables(null, null, null, null); action.run();
} }
return false; return false;
} }
@ -77,7 +87,7 @@ public class TextViewAutoCompleteClearable extends AppCompatAutoCompleteTextView
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { public void onTextChanged(CharSequence s, int start, int before, int count) {
setCompoundDrawablesRelative(null, null, s.length() > 0 ? drawable : null, null); setActionEnabled(enabled);
} }
@Override @Override
@ -86,4 +96,14 @@ public class TextViewAutoCompleteClearable extends AppCompatAutoCompleteTextView
} }
}); });
} }
public void setActionRunnable(Runnable action) {
this.action = action;
}
public void setActionEnabled(boolean enabled) {
this.enabled = enabled;
Drawable d = (enabled && getText().length() > 0 ? drawable : null);
setCompoundDrawablesRelative(null, null, d, null);
}
} }

@ -110,7 +110,7 @@
app:srcCompat="@drawable/twotone_sticky_note_2_24" app:srcCompat="@drawable/twotone_sticky_note_2_24"
tools:ignore="MissingConstraints" /> tools:ignore="MissingConstraints" />
<eu.faircode.email.TextViewAutoCompleteClearable <eu.faircode.email.TextViewAutoCompleteAction
android:id="@+id/etQuery" android:id="@+id/etQuery"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -120,12 +120,13 @@
android:imeOptions="actionGo" android:imeOptions="actionGo"
android:inputType="text" android:inputType="text"
android:maxLines="1" android:maxLines="1"
app:end_drawable="@drawable/twotone_close_24"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/ibFlow"> app:layout_constraintTop_toBottomOf="@id/ibFlow">
<requestFocus /> <requestFocus />
</eu.faircode.email.TextViewAutoCompleteClearable> </eu.faircode.email.TextViewAutoCompleteAction>
<eu.faircode.email.FixedTextView <eu.faircode.email.FixedTextView
android:id="@+id/tvSearch1" android:id="@+id/tvSearch1"

@ -3,4 +3,7 @@
<declare-styleable name="ContentLoadingProgressBar"> <declare-styleable name="ContentLoadingProgressBar">
<attr name="show_delay" format="integer" /> <attr name="show_delay" format="integer" />
</declare-styleable> </declare-styleable>
<declare-styleable name="TextViewAutoCompleteAction">
<attr name="end_drawable" format="reference" />
</declare-styleable>
</resources> </resources>

Loading…
Cancel
Save