Add sorting by issuer

This commit is contained in:
Jakob Nixdorf 2020-02-04 08:11:19 +01:00
parent 3084596931
commit 7a6922d06c
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
5 changed files with 36 additions and 3 deletions

View file

@ -506,6 +506,9 @@ public class MainActivity extends BaseActivity
if (mode == SortMode.UNSORTED) {
sortMenu.setIcon(R.drawable.ic_sort_inverted_white);
menu.findItem(R.id.menu_sort_none).setChecked(true);
} else if (mode == SortMode.ISSUER) {
sortMenu.setIcon(R.drawable.ic_sort_inverted_label_white);
menu.findItem(R.id.menu_sort_issuer).setChecked(true);
} else if (mode == SortMode.LABEL) {
sortMenu.setIcon(R.drawable.ic_sort_inverted_label_white);
menu.findItem(R.id.menu_sort_label).setChecked(true);
@ -594,6 +597,14 @@ public class MainActivity extends BaseActivity
adapter.setSortMode(SortMode.UNSORTED);
touchHelperCallback.setDragEnabled(true);
}
} else if (id == R.id.menu_sort_issuer) {
item.setChecked(true);
sortMenu.setIcon(R.drawable.ic_sort_inverted_label_white);
saveSortMode(SortMode.ISSUER);
if(adapter != null) {
adapter.setSortMode(SortMode.ISSUER);
touchHelperCallback.setDragEnabled(false);
}
} else if (id == R.id.menu_sort_label) {
item.setChecked(true);
sortMenu.setIcon(R.drawable.ic_sort_inverted_label_white);

View file

@ -37,7 +37,7 @@ public class Constants {
}
public enum SortMode {
UNSORTED, LABEL, LAST_USED, MOST_USED
UNSORTED, ISSUER, LABEL, LAST_USED, MOST_USED
}
public enum BackupType {

View file

@ -455,7 +455,7 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
String newIssuer = input.getEditableText().toString();
displayedEntries.get(pos).setIssuer(newIssuer);
if (sortMode == SortMode.LABEL) {
if (sortMode == SortMode.ISSUER) {
displayedEntries = sortEntries(displayedEntries);
notifyDataSetChanged();
} else {
@ -733,7 +733,9 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
private ArrayList<Entry> sortEntries(List<Entry> unsorted) {
ArrayList<Entry> sorted = new ArrayList<>(unsorted);
if (sortMode == SortMode.LABEL) {
if (sortMode == SortMode.ISSUER) {
Collections.sort(sorted, new IssuerComparator());
} else if (sortMode == SortMode.LABEL) {
Collections.sort(sorted, new LabelComparator());
} else if (sortMode == SortMode.LAST_USED) {
Collections.sort(sorted, new LastUsedComparator());
@ -812,6 +814,20 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
}
}
public class IssuerComparator implements Comparator<Entry> {
Collator collator;
IssuerComparator(){
collator = Collator.getInstance();
collator.setStrength(Collator.PRIMARY);
}
@Override
public int compare(Entry o1, Entry o2) {
return collator.compare(o1.getIssuer(), o2.getIssuer());
}
}
public class LabelComparator implements Comparator<Entry> {
Collator collator;

View file

@ -14,6 +14,10 @@
android:title="@string/menu_sort_none"
android:checked="true" />
<item
android:id="@+id/menu_sort_issuer"
android:title="@string/menu_sort_issuer" />
<item
android:id="@+id/menu_sort_label"
android:title="@string/menu_sort_label" />
@ -21,6 +25,7 @@
<item
android:id="@+id/menu_sort_last_used"
android:title="@string/menu_sort_last_used" />
<item
android:id="@+id/menu_sort_most_used"
android:title="@string/menu_sort_most_used" />

View file

@ -47,6 +47,7 @@
<string name="menu_main_sort">Sort</string>
<string name="menu_sort_none">Unsorted</string>
<string name="menu_sort_issuer">Issuer</string>
<string name="menu_sort_label">Label</string>
<string name="menu_sort_last_used">Last used</string>
<string name="menu_sort_most_used">Most used</string>