Save/delete search

pull/206/head
M66B 3 years ago
parent 2ef248f535
commit 95be264f4c

@ -51,7 +51,7 @@ public class AdapterNavSearch extends RecyclerView.Adapter<AdapterNavSearch.View
private boolean expanded = true;
private List<EntitySearch> items = new ArrayList<>();
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private View view;
private ImageView ivItem;
private ImageView ivBadge;
@ -74,12 +74,10 @@ public class AdapterNavSearch extends RecyclerView.Adapter<AdapterNavSearch.View
private void wire() {
view.setOnClickListener(this);
view.setOnLongClickListener(this);
}
private void unwire() {
view.setOnClickListener(null);
view.setOnLongClickListener(null);
}
private void bindTo(EntitySearch search) {
@ -107,6 +105,7 @@ public class AdapterNavSearch extends RecyclerView.Adapter<AdapterNavSearch.View
JSONObject json = new JSONObject(search.data);
BoundaryCallbackMessages.SearchCriteria criteria =
BoundaryCallbackMessages.SearchCriteria.fromJSON(json);
criteria.id = search.id;
FragmentMessages.search(
context, owner, manager,
-1L, -1L, false, criteria);
@ -114,39 +113,6 @@ public class AdapterNavSearch extends RecyclerView.Adapter<AdapterNavSearch.View
Log.e(ex);
}
}
@Override
public boolean onLongClick(View v) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return false;
EntitySearch search = items.get(pos);
if (search == null)
return false;
Bundle args = new Bundle();
args.putLong("id", search.id);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
DB db = DB.getInstance(context);
db.search().deleteSearch(id);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(manager, ex);
}
}.execute(context, owner, args, "search:delete");
return true;
}
}
AdapterNavSearch(Context context, LifecycleOwner owner, FragmentManager manager) {

@ -728,6 +728,7 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
}
static class SearchCriteria implements Serializable {
long id = -1;
String query;
boolean fts = false;
boolean in_senders = true;

@ -58,44 +58,6 @@ import java.util.List;
import io.requery.android.database.sqlite.SQLiteDatabase;
public class FragmentDialogSearch extends FragmentDialogBase {
private TextViewAutoCompleteAction etQuery;
private TextView tvSearch1;
private TextView tvSearch2;
private TextView tvSearch3;
private ImageButton ibResetSearches;
private ImageButton ibInfo;
private ImageButton ibFlagged;
private ImageButton ibUnseen;
private ImageButton ibInvite;
private ImageButton ibAttachment;
private ImageButton ibNotes;
private ImageButton ibMore;
private TextView tvMore;
private CheckBox cbSearchIndex;
private CheckBox cbSenders;
private CheckBox cbRecipients;
private CheckBox cbSubject;
private CheckBox cbKeywords;
private CheckBox cbMessage;
private TextView tvSearchTextUnsupported;
private CheckBox cbNotes;
private CheckBox cbHeaders;
private CheckBox cbHtml;
private CheckBox cbSearchTrash;
private CheckBox cbSearchJunk;
private CheckBox cbUnseen;
private CheckBox cbFlagged;
private CheckBox cbHidden;
private CheckBox cbEncrypted;
private CheckBox cbAttachments;
private Spinner spMessageSize;
private Button btnBefore;
private Button btnAfter;
private TextView tvBefore;
private TextView tvAfter;
private Group grpMore;
private static final int MAX_SUGGESTIONS = 3;
@NonNull
@ -120,43 +82,43 @@ public class FragmentDialogSearch extends FragmentDialogBase {
View dview = LayoutInflater.from(context).inflate(R.layout.dialog_search, null);
etQuery = dview.findViewById(R.id.etQuery);
tvSearch1 = dview.findViewById(R.id.tvSearch1);
tvSearch2 = dview.findViewById(R.id.tvSearch2);
tvSearch3 = dview.findViewById(R.id.tvSearch3);
ibResetSearches = dview.findViewById(R.id.ibResetSearches);
ibInfo = dview.findViewById(R.id.ibInfo);
ibFlagged = dview.findViewById(R.id.ibFlagged);
ibUnseen = dview.findViewById(R.id.ibUnseen);
ibInvite = dview.findViewById(R.id.ibInvite);
ibAttachment = dview.findViewById(R.id.ibAttachment);
ibNotes = dview.findViewById(R.id.ibNotes);
ibMore = dview.findViewById(R.id.ibMore);
tvMore = dview.findViewById(R.id.tvMore);
cbSearchIndex = dview.findViewById(R.id.cbSearchIndex);
cbSenders = dview.findViewById(R.id.cbSenders);
cbRecipients = dview.findViewById(R.id.cbRecipients);
cbSubject = dview.findViewById(R.id.cbSubject);
cbKeywords = dview.findViewById(R.id.cbKeywords);
cbMessage = dview.findViewById(R.id.cbMessage);
tvSearchTextUnsupported = dview.findViewById(R.id.tvSearchTextUnsupported);
cbNotes = dview.findViewById(R.id.cbNotes);
cbHeaders = dview.findViewById(R.id.cbHeaders);
cbHtml = dview.findViewById(R.id.cbHtml);
cbSearchTrash = dview.findViewById(R.id.cbSearchTrash);
cbSearchJunk = dview.findViewById(R.id.cbSearchJunk);
cbUnseen = dview.findViewById(R.id.cbUnseen);
cbFlagged = dview.findViewById(R.id.cbFlagged);
cbHidden = dview.findViewById(R.id.cbHidden);
cbEncrypted = dview.findViewById(R.id.cbEncrypted);
cbAttachments = dview.findViewById(R.id.cbAttachments);
spMessageSize = dview.findViewById(R.id.spMessageSize);
btnBefore = dview.findViewById(R.id.btnBefore);
btnAfter = dview.findViewById(R.id.btnAfter);
tvBefore = dview.findViewById(R.id.tvBefore);
tvAfter = dview.findViewById(R.id.tvAfter);
grpMore = dview.findViewById(R.id.grpMore);
TextViewAutoCompleteAction etQuery = dview.findViewById(R.id.etQuery);
TextView tvSearch1 = dview.findViewById(R.id.tvSearch1);
TextView tvSearch2 = dview.findViewById(R.id.tvSearch2);
TextView tvSearch3 = dview.findViewById(R.id.tvSearch3);
ImageButton ibResetSearches = dview.findViewById(R.id.ibResetSearches);
ImageButton ibInfo = dview.findViewById(R.id.ibInfo);
ImageButton ibFlagged = dview.findViewById(R.id.ibFlagged);
ImageButton ibUnseen = dview.findViewById(R.id.ibUnseen);
ImageButton ibInvite = dview.findViewById(R.id.ibInvite);
ImageButton ibAttachment = dview.findViewById(R.id.ibAttachment);
ImageButton ibNotes = dview.findViewById(R.id.ibNotes);
ImageButton ibMore = dview.findViewById(R.id.ibMore);
TextView tvMore = dview.findViewById(R.id.tvMore);
CheckBox cbSearchIndex = dview.findViewById(R.id.cbSearchIndex);
CheckBox cbSenders = dview.findViewById(R.id.cbSenders);
CheckBox cbRecipients = dview.findViewById(R.id.cbRecipients);
CheckBox cbSubject = dview.findViewById(R.id.cbSubject);
CheckBox cbKeywords = dview.findViewById(R.id.cbKeywords);
CheckBox cbMessage = dview.findViewById(R.id.cbMessage);
TextView tvSearchTextUnsupported = dview.findViewById(R.id.tvSearchTextUnsupported);
CheckBox cbNotes = dview.findViewById(R.id.cbNotes);
CheckBox cbHeaders = dview.findViewById(R.id.cbHeaders);
CheckBox cbHtml = dview.findViewById(R.id.cbHtml);
CheckBox cbSearchTrash = dview.findViewById(R.id.cbSearchTrash);
CheckBox cbSearchJunk = dview.findViewById(R.id.cbSearchJunk);
CheckBox cbUnseen = dview.findViewById(R.id.cbUnseen);
CheckBox cbFlagged = dview.findViewById(R.id.cbFlagged);
CheckBox cbHidden = dview.findViewById(R.id.cbHidden);
CheckBox cbEncrypted = dview.findViewById(R.id.cbEncrypted);
CheckBox cbAttachments = dview.findViewById(R.id.cbAttachments);
Spinner spMessageSize = dview.findViewById(R.id.spMessageSize);
Button btnBefore = dview.findViewById(R.id.btnBefore);
Button btnAfter = dview.findViewById(R.id.btnAfter);
TextView tvBefore = dview.findViewById(R.id.tvBefore);
TextView tvAfter = dview.findViewById(R.id.tvAfter);
Group grpMore = dview.findViewById(R.id.grpMore);
ibInfo.setOnClickListener(new View.OnClickListener() {
@Override
@ -412,7 +374,45 @@ public class FragmentDialogSearch extends FragmentDialogBase {
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
BoundaryCallbackMessages.SearchCriteria criteria = getSearchCriteria();
BoundaryCallbackMessages.SearchCriteria criteria = new BoundaryCallbackMessages.SearchCriteria();
criteria.query = etQuery.getText().toString().trim();
if (TextUtils.isEmpty(criteria.query))
criteria.query = null;
criteria.fts = cbSearchIndex.isChecked();
if (!criteria.fts) {
criteria.in_senders = cbSenders.isChecked();
criteria.in_recipients = cbRecipients.isChecked();
criteria.in_subject = cbSubject.isChecked();
criteria.in_keywords = cbKeywords.isChecked();
criteria.in_message = cbMessage.isChecked();
criteria.in_notes = cbNotes.isChecked();
criteria.in_headers = cbHeaders.isChecked();
criteria.in_html = cbHtml.isChecked();
criteria.with_unseen = cbUnseen.isChecked();
criteria.with_flagged = cbFlagged.isChecked();
criteria.with_hidden = cbHidden.isChecked();
criteria.with_encrypted = cbEncrypted.isChecked();
criteria.with_attachments = cbAttachments.isChecked();
int pos = spMessageSize.getSelectedItemPosition();
if (pos > 0) {
int[] sizes = getResources().getIntArray(R.array.sizeValues);
criteria.with_size = sizes[pos];
}
}
criteria.in_trash = cbSearchTrash.isChecked();
criteria.in_junk = cbSearchJunk.isChecked();
Object after = tvAfter.getTag();
Object before = tvBefore.getTag();
if (after != null)
criteria.after = ((Calendar) after).getTimeInMillis();
if (before != null)
criteria.before = ((Calendar) before).getTimeInMillis();
if (criteria.query != null) {
List<String> searches = new ArrayList<>();
@ -481,38 +481,6 @@ public class FragmentDialogSearch extends FragmentDialogBase {
account, folder, false, criteria);
}
})
.setNeutralButton(R.string.title_save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
BoundaryCallbackMessages.SearchCriteria criteria = getSearchCriteria();
Bundle args = new Bundle();
args.putString("name", criteria.getTitle(context));
args.putString("data", criteria.toJson().toString());
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
EntitySearch search = new EntitySearch();
search.name = args.getString("name");
search.data = args.getString("data");
DB db = DB.getInstance(context);
db.search().insertSearch(search);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(FragmentDialogSearch.this, args, "search:save");
} catch (Throwable ex) {
Log.e(ex);
}
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
@ -578,50 +546,6 @@ public class FragmentDialogSearch extends FragmentDialogBase {
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
private BoundaryCallbackMessages.SearchCriteria getSearchCriteria() {
BoundaryCallbackMessages.SearchCriteria criteria = new BoundaryCallbackMessages.SearchCriteria();
criteria.query = etQuery.getText().toString().trim();
if (TextUtils.isEmpty(criteria.query))
criteria.query = null;
criteria.fts = cbSearchIndex.isChecked();
if (!criteria.fts) {
criteria.in_senders = cbSenders.isChecked();
criteria.in_recipients = cbRecipients.isChecked();
criteria.in_subject = cbSubject.isChecked();
criteria.in_keywords = cbKeywords.isChecked();
criteria.in_message = cbMessage.isChecked();
criteria.in_notes = cbNotes.isChecked();
criteria.in_headers = cbHeaders.isChecked();
criteria.in_html = cbHtml.isChecked();
criteria.with_unseen = cbUnseen.isChecked();
criteria.with_flagged = cbFlagged.isChecked();
criteria.with_hidden = cbHidden.isChecked();
criteria.with_encrypted = cbEncrypted.isChecked();
criteria.with_attachments = cbAttachments.isChecked();
int pos = spMessageSize.getSelectedItemPosition();
if (pos > 0) {
int[] sizes = getResources().getIntArray(R.array.sizeValues);
criteria.with_size = sizes[pos];
}
}
criteria.in_trash = cbSearchTrash.isChecked();
criteria.in_junk = cbSearchJunk.isChecked();
Object after = tvAfter.getTag();
Object before = tvBefore.getTag();
if (after != null)
criteria.after = ((Calendar) after).getTimeInMillis();
if (before != null)
criteria.before = ((Calendar) before).getTimeInMillis();
return criteria;
}
private void pickDate(TextView tv) {
Object tag = tv.getTag();
final Calendar cal = (tag == null ? Calendar.getInstance() : (Calendar) tag);

@ -111,6 +111,7 @@ import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
@ -383,6 +384,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
static final int REQUEST_PICK_CONTACT = 23;
static final int REQUEST_BUTTONS = 24;
private static final int REQUEST_ALL_READ = 25;
private static final int REQUEST_SAVE_SEARCH = 26;
static final String ACTION_STORE_RAW = BuildConfig.APPLICATION_ID + ".STORE_RAW";
static final String ACTION_DECRYPT = BuildConfig.APPLICATION_ID + ".DECRYPT";
@ -4399,6 +4401,13 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
MenuItem menuSearch = menu.findItem(R.id.menu_search);
menuSearch.setVisible(folder);
menu.findItem(R.id.menu_save_search).setVisible(
viewType == AdapterMessage.ViewType.SEARCH &&
criteria != null && criteria.id < 0);
menu.findItem(R.id.menu_delete_search).setVisible(
viewType == AdapterMessage.ViewType.SEARCH &&
criteria != null && criteria.id >= 0);
menu.findItem(R.id.menu_folders).setVisible(
viewType == AdapterMessage.ViewType.UNIFIED &&
type == null && primary >= 0);
@ -4490,6 +4499,12 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
if (itemId == R.id.menu_search) {
onMenuSearch();
return true;
} else if (itemId == R.id.menu_save_search) {
onMenuSaveSearch();
return true;
} else if (itemId == R.id.menu_delete_search) {
onMenuDeleteSearch();
return true;
} else if (itemId == R.id.menu_folders) {// Obsolete
onMenuFolders(primary);
return true;
@ -4600,6 +4615,43 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
fragment.show(getParentFragmentManager(), "search");
}
private void onMenuSaveSearch() {
Bundle args = new Bundle();
args.putSerializable("criteria", criteria);
FragmentDialogSaveSearch fragment = new FragmentDialogSaveSearch();
fragment.setArguments(args);
fragment.setTargetFragment(this, REQUEST_SAVE_SEARCH);
fragment.show(getParentFragmentManager(), "search:save");
}
private void onMenuDeleteSearch() {
Bundle args = new Bundle();
args.putLong("id", criteria.id);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
DB db = DB.getInstance(context);
db.search().deleteSearch(id);
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
finish();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "search:delete");
}
private void onMenuFolders(long account) {
if (!isAdded())
return;
@ -4835,7 +4887,6 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
ask.setArguments(args);
ask.setTargetFragment(FragmentMessages.this, REQUEST_ALL_READ);
ask.show(getParentFragmentManager(), "messages:allread");
}
private void onMenuViewThread() {
@ -4898,6 +4949,35 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
}.execute(FragmentMessages.this, args, "messages:allread");
}
private void onSaveSearch(Bundle args) {
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
BoundaryCallbackMessages.SearchCriteria criteria =
(BoundaryCallbackMessages.SearchCriteria) args.getSerializable("criteria");
EntitySearch search = new EntitySearch();
search.name = args.getString("name");
search.data = criteria.toJson().toString();
DB db = DB.getInstance(context);
search.id = db.search().insertSearch(search);
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
finish();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "search:save");
}
private void onMenuSyncMore() {
Bundle args = new Bundle();
args.putLong("folder", folder);
@ -6609,6 +6689,10 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
if (resultCode == RESULT_OK)
markAllRead();
break;
case REQUEST_SAVE_SEARCH:
if (resultCode == RESULT_OK && data != null)
onSaveSearch(data.getBundleExtra("args"));
break;
}
} catch (Throwable ex) {
Log.e(ex);
@ -8918,4 +9002,35 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
.create();
}
}
public static class FragmentDialogSaveSearch extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Context context = getContext();
View dview = LayoutInflater.from(context).inflate(R.layout.dialog_save_search, null);
EditText etName = dview.findViewById(R.id.etName);
BoundaryCallbackMessages.SearchCriteria criteria =
(BoundaryCallbackMessages.SearchCriteria) getArguments().getSerializable("criteria");
etName.setText(criteria.getTitle(context));
return new AlertDialog.Builder(context)
.setView(dview)
.setPositiveButton(R.string.title_save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getArguments().putString("name", etName.getText().toString());
sendResult(Activity.RESULT_OK);
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
sendResult(Activity.RESULT_CANCELED);
}
})
.create();
}
}
}

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="24dp">
<eu.faircode.email.FixedTextView
android:id="@+id/tvSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/twotone_search_24"
android:drawablePadding="6dp"
android:labelFor="@+id/etName"
android:text="@string/title_search"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<eu.faircode.email.EditTextPlain
android:id="@+id/etName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:imeOptions="actionDone"
android:inputType="textPersonName|textCapWords"
android:text="Name"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvSearch">
<requestFocus />
</eu.faircode.email.EditTextPlain>
</androidx.constraintlayout.widget.ConstraintLayout>

@ -8,6 +8,18 @@
android:title="@string/title_search"
app:showAsAction="always" />
<item
android:id="@+id/menu_save_search"
android:icon="@drawable/twotone_save_alt_24"
android:title="@string/title_save"
app:showAsAction="always" />
<item
android:id="@+id/menu_delete_search"
android:icon="@drawable/twotone_delete_24"
android:title="@string/title_delete"
app:showAsAction="always" />
<item
android:id="@+id/menu_folders"
android:actionLayout="@layout/action_button"

Loading…
Cancel
Save