Added search to unified folders view

pull/152/head
M66B 6 years ago
parent 7b0dfb8f9f
commit 8508a936fc

@ -40,6 +40,7 @@ import java.util.List;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
@ -61,6 +62,7 @@ public class FragmentFolders extends FragmentBase {
private FloatingActionButton fab; private FloatingActionButton fab;
private long account; private long account;
private boolean searching = false;
private AdapterFolder adapter; private AdapterFolder adapter;
private Boolean show_hidden = null; private Boolean show_hidden = null;
@ -328,17 +330,55 @@ public class FragmentFolders extends FragmentBase {
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_folders, menu); inflater.inflate(R.menu.menu_folders, menu);
final MenuItem menuSearch = menu.findItem(R.id.menu_search);
menuSearch.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
searching = true;
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
searching = false;
return true;
}
});
if (searching)
menuSearch.expandActionView();
final SearchView searchView = (SearchView) menuSearch.getActionView();
searchView.setQueryHint(getString(R.string.title_search_device));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
searching = false;
menuSearch.collapseActionView();
FragmentMessages.search(getContext(), getViewLifecycleOwner(), getFragmentManager(), -1, query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
} }
@Override @Override
public void onPrepareOptionsMenu(Menu menu) { public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.menu_search).setVisible(account < 0);
MenuItem item = menu.findItem(R.id.menu_show_hidden); MenuItem item = menu.findItem(R.id.menu_show_hidden);
if (show_hidden != null) { if (show_hidden != null) {
item.setTitle(show_hidden ? R.string.title_hide_folders : R.string.title_show_folders); item.setTitle(show_hidden ? R.string.title_hide_folders : R.string.title_show_folders);
item.setIcon(show_hidden ? R.drawable.baseline_visibility_off_24 : R.drawable.baseline_visibility_24); item.setIcon(show_hidden ? R.drawable.baseline_visibility_off_24 : R.drawable.baseline_visibility_24);
} }
item.setVisible(show_hidden != null); item.setVisible(show_hidden != null);
super.onPrepareOptionsMenu(menu); super.onPrepareOptionsMenu(menu);
} }

@ -80,6 +80,7 @@ import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders; import androidx.lifecycle.ViewModelProviders;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -1672,40 +1673,7 @@ public class FragmentMessages extends FragmentBase {
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
searching = false; searching = false;
menuSearch.collapseActionView(); menuSearch.collapseActionView();
search(getContext(), getViewLifecycleOwner(), getFragmentManager(), folder, query);
if (Helper.isPro(getContext())) {
Bundle args = new Bundle();
args.putLong("folder", folder);
args.putString("search", query);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
DB.getInstance(context).message().resetSearch();
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
FragmentMessages fragment = new FragmentMessages();
fragment.setArguments(args);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("search");
fragmentTransaction.commit();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
}
}.execute(FragmentMessages.this, args, "messages:resetsearch");
} else {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
fragmentTransaction.commit();
}
return true; return true;
} }
@ -2504,6 +2472,43 @@ public class FragmentMessages extends FragmentBase {
return super.onCreateAnimation(transit, enter, nextAnim); return super.onCreateAnimation(transit, enter, nextAnim);
} }
static void search(
final Context context, final LifecycleOwner owner, final FragmentManager manager,
long folder, String query) {
if (Helper.isPro(context)) {
Bundle args = new Bundle();
args.putLong("folder", folder);
args.putString("search", query);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
DB.getInstance(context).message().resetSearch();
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
FragmentMessages fragment = new FragmentMessages();
fragment.setArguments(args);
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("search");
fragmentTransaction.commit();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(context, owner, ex);
}
}.execute(context, owner, args, "search:reset");
} else {
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
fragmentTransaction.commit();
}
}
private class MoreResult { private class MoreResult {
boolean seen; boolean seen;
boolean unseen; boolean unseen;

@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_search"
android:icon="@drawable/baseline_search_24"
android:title="@string/title_search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="collapseActionView|always" />
<item <item
android:id="@+id/menu_show_hidden" android:id="@+id/menu_show_hidden"
android:icon="@drawable/baseline_visibility_24" android:icon="@drawable/baseline_visibility_24"

Loading…
Cancel
Save