Add search in groups
This commit is contained in:
parent
9b1a750642
commit
a5d8bada63
@ -13,6 +13,7 @@ import androidx.activity.result.contract.ActivityResultContracts;
|
|||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.ActionBar;
|
import androidx.appcompat.app.ActionBar;
|
||||||
import androidx.appcompat.app.AlertDialog;
|
import androidx.appcompat.app.AlertDialog;
|
||||||
|
import androidx.appcompat.widget.SearchView;
|
||||||
import androidx.core.util.Consumer;
|
import androidx.core.util.Consumer;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
|
|
||||||
@ -255,12 +256,16 @@ public class MainActivity extends BaseActivity {
|
|||||||
ActionBar bar = getSupportActionBar();
|
ActionBar bar = getSupportActionBar();
|
||||||
if(bar != null) bar.setTitle(((NamedFragment) fragment).getName());
|
if(bar != null) bar.setTitle(((NamedFragment) fragment).getName());
|
||||||
}else {
|
}else {
|
||||||
List<String> groups = SettingsUtil.getGroups(this);
|
navigateToMainGroup();
|
||||||
if(!groups.isEmpty()) {
|
}
|
||||||
Bundle bundle = new Bundle();
|
}
|
||||||
bundle.putString(GroupFragment.BUNDLE_GROUP, SettingsUtil.getGroups(this).get(0));
|
|
||||||
NavigationUtil.navigate(this, GroupFragment.class, bundle);
|
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_view_otp);
|
||||||
menu.removeItem(R.id.action_edit_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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -328,6 +350,11 @@ public class MainActivity extends BaseActivity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!(fragment instanceof GroupFragment)) {
|
||||||
|
navigateToMainGroup();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if(System.currentTimeMillis() - backLastPressed < BACK_BUTTON_DELAY) {
|
if(System.currentTimeMillis() - backLastPressed < BACK_BUTTON_DELAY) {
|
||||||
finishAffinity();
|
finishAffinity();
|
||||||
}else {
|
}else {
|
||||||
|
@ -186,6 +186,10 @@ public class GroupFragment extends NamedFragment {
|
|||||||
}, null, null);
|
}, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void filter(String newText) {
|
||||||
|
otpListAdapter.filter(newText);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isEditing() {
|
public boolean isEditing() {
|
||||||
return otpListAdapter.isEditing();
|
return otpListAdapter.isEditing();
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,8 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
|
|||||||
|
|
||||||
private final List<OTPData> items;
|
private final List<OTPData> items;
|
||||||
|
|
||||||
|
private List<OTPData> filteredItems;
|
||||||
|
|
||||||
private final Handler handler;
|
private final Handler handler;
|
||||||
|
|
||||||
private final Runnable saveOTPs;
|
private final Runnable saveOTPs;
|
||||||
@ -47,6 +49,7 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
|
|||||||
this.recyclerView = recyclerView;
|
this.recyclerView = recyclerView;
|
||||||
this.inflater = LayoutInflater.from(context);
|
this.inflater = LayoutInflater.from(context);
|
||||||
this.items = new ArrayList<>();
|
this.items = new ArrayList<>();
|
||||||
|
this.filteredItems = items;
|
||||||
this.handler = new Handler(Looper.getMainLooper());
|
this.handler = new Handler(Looper.getMainLooper());
|
||||||
this.saveOTPs = saveOTPs;
|
this.saveOTPs = saveOTPs;
|
||||||
|
|
||||||
@ -62,7 +65,7 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(@NonNull OTPListItem holder, int position) {
|
public void onBindViewHolder(@NonNull OTPListItem holder, int position) {
|
||||||
OTPData data = items.get(position);
|
OTPData data = filteredItems.get(position);
|
||||||
|
|
||||||
holder.setOTPData(data);
|
holder.setOTPData(data);
|
||||||
holder.setSelected(false);
|
holder.setSelected(false);
|
||||||
@ -132,7 +135,7 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getItemCount() {
|
public int getItemCount() {
|
||||||
return items.size();
|
return filteredItems.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<OTPData> getItems() {
|
public List<OTPData> getItems() {
|
||||||
@ -200,7 +203,7 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
|
|||||||
item.setCodeShown(true);
|
item.setCodeShown(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<OTPListItem> getCodes() {
|
public List<OTPListItem> getCodes() {
|
||||||
List<OTPListItem> is = new ArrayList<>();
|
List<OTPListItem> is = new ArrayList<>();
|
||||||
for(int i = 0; i < items.size(); i++) {
|
for(int i = 0; i < items.size(); i++) {
|
||||||
OTPListItem vh = (OTPListItem) recyclerView.findViewHolderForAdapterPosition(i);
|
OTPListItem vh = (OTPListItem) recyclerView.findViewHolderForAdapterPosition(i);
|
||||||
@ -210,6 +213,27 @@ public class OTPListAdapter extends RecyclerView.Adapter<OTPListItem> {
|
|||||||
return is;
|
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) {
|
private void attachTouchHelper(RecyclerView view) {
|
||||||
new ItemTouchHelper(new OTPListAdapter.TouchHelperCallback()).attachToRecyclerView(view);
|
new ItemTouchHelper(new OTPListAdapter.TouchHelperCallback()).attachToRecyclerView(view);
|
||||||
}
|
}
|
||||||
|
5
app/src/main/res/drawable/baseline_search_24.xml
Normal file
5
app/src/main/res/drawable/baseline_search_24.xml
Normal 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>
|
@ -1,6 +1,12 @@
|
|||||||
<?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/action_search"
|
||||||
|
android:icon="@drawable/baseline_search_24"
|
||||||
|
app:showAsAction="always|collapseActionView"
|
||||||
|
app:actionViewClass="androidx.appcompat.widget.SearchView"
|
||||||
|
android:title="@string/search"/>
|
||||||
<item
|
<item
|
||||||
android:id="@+id/action_add_otp"
|
android:id="@+id/action_add_otp"
|
||||||
android:orderInCategory="100"
|
android:orderInCategory="100"
|
||||||
|
Loading…
Reference in New Issue
Block a user