mirror of https://github.com/M66B/FairEmail.git
parent
c9efe89ca7
commit
a8eaa1a7eb
@ -0,0 +1,134 @@
|
||||
package eu.faircode.email;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.MatrixCursor;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.AutoCompleteTextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.widget.SearchView;
|
||||
import androidx.cursoradapter.widget.SimpleCursorAdapter;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
|
||||
public class SearchViewEx extends SearchView {
|
||||
private String _searching = null;
|
||||
|
||||
public SearchViewEx(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public SearchViewEx(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
public SearchViewEx(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
}
|
||||
|
||||
void setup(LifecycleOwner owner, MenuItem menuSearch, String searching, ISearch intf) {
|
||||
_searching = searching;
|
||||
Log.i("SearchView searching=" + _searching);
|
||||
|
||||
setQueryHint(getContext().getString(R.string.title_search));
|
||||
|
||||
if (!TextUtils.isEmpty(_searching)) {
|
||||
menuSearch.expandActionView();
|
||||
setQuery(_searching, false);
|
||||
}
|
||||
|
||||
AutoCompleteTextView autoCompleteTextView = findViewById(androidx.appcompat.R.id.search_src_text);
|
||||
autoCompleteTextView.setThreshold(0);
|
||||
|
||||
setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
||||
@Override
|
||||
public boolean onQueryTextChange(String newText) {
|
||||
_searching = newText;
|
||||
intf.onSave(_searching);
|
||||
|
||||
if (TextUtils.isEmpty(newText)) {
|
||||
MatrixCursor cursor = new MatrixCursor(new String[]{"_id", "suggestion"});
|
||||
String prefix = getContext().getString(R.string.title_search_special_prefix);
|
||||
cursor.addRow(new Object[]{-1, prefix + ":" + getContext().getString(R.string.title_search_special_unseen)});
|
||||
cursor.addRow(new Object[]{-2, prefix + ":" + getContext().getString(R.string.title_search_special_flagged)});
|
||||
cursor.addRow(new Object[]{-3, prefix + ":" + getContext().getString(R.string.title_search_special_snoozed)});
|
||||
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
|
||||
getContext(),
|
||||
R.layout.search_suggestion,
|
||||
cursor,
|
||||
new String[]{"suggestion"},
|
||||
new int[]{android.R.id.text1},
|
||||
0);
|
||||
setSuggestionsAdapter(adapter);
|
||||
adapter.notifyDataSetChanged();
|
||||
} else {
|
||||
Bundle args = new Bundle();
|
||||
args.putString("query", newText);
|
||||
|
||||
new SimpleTask<Cursor>() {
|
||||
@Override
|
||||
protected Cursor onExecute(Context context, Bundle args) {
|
||||
String query = args.getString("query");
|
||||
|
||||
DB db = DB.getInstance(context);
|
||||
return db.message().getSuggestions("%" + query + "%");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onExecuted(Bundle args, Cursor cursor) {
|
||||
Log.i("Suggestions=" + cursor.getCount());
|
||||
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
|
||||
getContext(),
|
||||
R.layout.search_suggestion,
|
||||
cursor,
|
||||
new String[]{"suggestion"},
|
||||
new int[]{android.R.id.text1},
|
||||
0);
|
||||
setSuggestionsAdapter(adapter);
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onException(Bundle args, Throwable ex) {
|
||||
ToastEx.makeText(getContext(), Helper.formatThrowable(ex), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}.execute(getContext(), owner, args, "messages:suggestions");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onQueryTextSubmit(String query) {
|
||||
_searching = null;
|
||||
intf.onSave(_searching);
|
||||
menuSearch.collapseActionView();
|
||||
intf.onSearch(query);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
setOnSuggestionListener(new SearchView.OnSuggestionListener() {
|
||||
@Override
|
||||
public boolean onSuggestionSelect(int position) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onSuggestionClick(int position) {
|
||||
Cursor cursor = (Cursor) getSuggestionsAdapter().getItem(position);
|
||||
setQuery(cursor.getString(1), true);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
interface ISearch {
|
||||
void onSave(String query);
|
||||
|
||||
void onSearch(String query);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue