Add search in groups

This commit is contained in:
MrLetsplay 2023-10-08 13:33:50 +02:00
parent 9b1a750642
commit a5d8bada63
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
5 changed files with 75 additions and 9 deletions

View File

@ -13,6 +13,7 @@ import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.SearchView;
import androidx.core.util.Consumer;
import androidx.fragment.app.Fragment;
@ -255,12 +256,16 @@ public class MainActivity extends BaseActivity {
ActionBar bar = getSupportActionBar();
if(bar != null) bar.setTitle(((NamedFragment) fragment).getName());
}else {
List<String> groups = SettingsUtil.getGroups(this);
if(!groups.isEmpty()) {
Bundle bundle = new Bundle();
bundle.putString(GroupFragment.BUNDLE_GROUP, SettingsUtil.getGroups(this).get(0));
NavigationUtil.navigate(this, GroupFragment.class, bundle);
}
navigateToMainGroup();
}
}
private void navigateToMainGroup() {
List<String> groups = SettingsUtil.getGroups(this);
if(!groups.isEmpty()) {
Bundle bundle = new Bundle();
bundle.putString(GroupFragment.BUNDLE_GROUP, SettingsUtil.getGroups(this).get(0));
NavigationUtil.navigate(this, GroupFragment.class, bundle);
}
}
@ -279,6 +284,23 @@ public class MainActivity extends BaseActivity {
menu.removeItem(R.id.action_view_otp);
menu.removeItem(R.id.action_edit_otp);
}
if(!frag.isEditing()) {
MenuItem search = menu.findItem(R.id.action_search);
SearchView v = (SearchView) search.getActionView();
v.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
frag.filter(newText);
return true;
}
});
}
return true;
}
@ -328,6 +350,11 @@ public class MainActivity extends BaseActivity {
return;
}
if(!(fragment instanceof GroupFragment)) {
navigateToMainGroup();
return;
}
if(System.currentTimeMillis() - backLastPressed < BACK_BUTTON_DELAY) {
finishAffinity();
}else {

View File

@ -186,6 +186,10 @@ public class GroupFragment extends NamedFragment {
}, null, null);
}
public void filter(String newText) {
otpListAdapter.filter(newText);
}
public boolean isEditing() {
return otpListAdapter.isEditing();
}

View File

@ -36,6 +36,8 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
private final List<OTPData> items;
private List<OTPData> filteredItems;
private final Handler handler;
private final Runnable saveOTPs;
@ -47,6 +49,7 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
this.recyclerView = recyclerView;
this.inflater = LayoutInflater.from(context);
this.items = new ArrayList<>();
this.filteredItems = items;
this.handler = new Handler(Looper.getMainLooper());
this.saveOTPs = saveOTPs;
@ -62,7 +65,7 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
@Override
public void onBindViewHolder(@NonNull OTPListItem holder, int position) {
OTPData data = items.get(position);
OTPData data = filteredItems.get(position);
holder.setOTPData(data);
holder.setSelected(false);
@ -132,7 +135,7 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
@Override
public int getItemCount() {
return items.size();
return filteredItems.size();
}
public List<OTPData> getItems() {
@ -200,7 +203,7 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
item.setCodeShown(true);
}
private List<OTPListItem> getCodes() {
public List<OTPListItem> getCodes() {
List<OTPListItem> is = new ArrayList<>();
for(int i = 0; i < items.size(); i++) {
OTPListItem vh = (OTPListItem) recyclerView.findViewHolderForAdapterPosition(i);
@ -210,6 +213,27 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
return is;
}
public void filter(String query) {
if(isEditing()) return;
if(query == null || query.isEmpty()) {
filteredItems = items;
notifyDataSetChanged();
return;
}
query = query.toLowerCase();
List<OTPData> filtered = new ArrayList<>();
for(OTPData d : items) {
if(d.getName().toLowerCase().contains(query)
|| d.getIssuer().toLowerCase().contains(query)) filtered.add(d);
}
filteredItems = filtered;
notifyDataSetChanged();
}
private void attachTouchHelper(RecyclerView view) {
new ItemTouchHelper(new OTPListAdapter.TouchHelperCallback()).attachToRecyclerView(view);
}

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="?android:attr/textColor"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
</vector>

View File

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