Add basic sorting by label

Might need some additional testing

Closes #12
This commit is contained in:
Jakob Nixdorf 2017-08-07 11:06:16 +02:00
parent dc0f942c9a
commit 0165a00ffb
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
5 changed files with 105 additions and 12 deletions

View file

@ -46,16 +46,23 @@ import org.shadowice.flocke.andotp.ItemTouchHelper.ItemTouchHelperAdapter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
implements ItemTouchHelperAdapter, Filterable {
public enum SortMode {
UNSORTED, LABEL
}
private Context context;
private SharedPreferences sharedPrefs;
private EntryFilter filter;
private ArrayList<Entry> entries;
private ArrayList<Entry> displayedEntries;
public Callback callback;
private Callback callback;
private SortMode sortMode = SortMode.UNSORTED;
public EntriesCardAdapter(Context context) {
this.context = context;
@ -83,11 +90,7 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
}
public void entriesChanged() {
if (displayedEntries != null)
displayedEntries.clear();
displayedEntries = entries;
displayedEntries = sortEntries(entries);
notifyDataSetChanged();
}
@ -156,10 +159,14 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
if (sortMode == SortMode.UNSORTED && displayedEntries.equals(entries)) {
Collections.swap(entries, fromPosition, toPosition);
displayedEntries = new ArrayList<>(entries);
notifyItemMoved(fromPosition, toPosition);
DatabaseHelper.saveDatabase(context, entries);
}
return true;
}
@ -187,7 +194,12 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
String newLabel = input.getEditableText().toString();
displayedEntries.get(pos).setLabel(newLabel);
if (sortMode == SortMode.LABEL) {
displayedEntries = sortEntries(displayedEntries);
notifyDataSetChanged();
} else {
notifyItemChanged(pos);
}
entries.get(realIndex).setLabel(newLabel);
DatabaseHelper.saveDatabase(context, entries);
@ -260,6 +272,25 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
Toast.makeText(context, R.string.toast_copied_to_clipboard, Toast.LENGTH_LONG).show();
}
public void setSortMode(SortMode mode) {
this.sortMode = mode;
entriesChanged();
}
public SortMode getSortMode() {
return this.sortMode;
}
private ArrayList<Entry> sortEntries(ArrayList<Entry> unsorted) {
ArrayList<Entry> sorted = new ArrayList<>(unsorted);
if (sortMode == SortMode.LABEL) {
Collections.sort(sorted, new LabelComparator());
}
return sorted;
}
public void setCallback(Callback cb) {
this.callback = cb;
}
@ -295,11 +326,18 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
displayedEntries = (ArrayList<Entry>) results.values;
displayedEntries = sortEntries((ArrayList<Entry>) results.values);
notifyDataSetChanged();
}
}
public class LabelComparator implements Comparator<Entry> {
@Override
public int compare(Entry o1, Entry o2) {
return o1.getLabel().compareTo(o2.getLabel());
}
}
public interface Callback {
void onMoveEventStart();
void onMoveEventStop();

View file

@ -63,6 +63,7 @@ public class MainActivity extends BaseActivity
private EntriesCardAdapter adapter;
private FloatingActionMenu floatingActionMenu;
private SearchView searchView;
private MenuItem sortMenu;
private SimpleItemTouchHelperCallback touchHelperCallback;
private SharedPreferences sharedPref;
@ -319,6 +320,8 @@ public class MainActivity extends BaseActivity
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
sortMenu = menu.findItem(R.id.menu_sort);
MenuItem searchItem = menu.findItem(R.id.menu_search);
searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@ -339,13 +342,18 @@ public class MainActivity extends BaseActivity
public boolean onMenuItemActionExpand(MenuItem item) {
floatingActionMenu.hide();
touchHelperCallback.setDragEnabled(false);
if (sortMenu != null)
sortMenu.setVisible(false);
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
floatingActionMenu.show();
if (adapter == null || adapter.getSortMode() == EntriesCardAdapter.SortMode.UNSORTED)
touchHelperCallback.setDragEnabled(true);
if (sortMenu != null)
sortMenu.setVisible(true);
return true;
}
});
@ -367,7 +375,20 @@ public class MainActivity extends BaseActivity
Intent aboutIntent = new Intent(this, AboutActivity.class);
startActivity(aboutIntent);
return true;
} else if (id == R.id.menu_sort_none) {
item.setChecked(true);
if (adapter != null) {
adapter.setSortMode(EntriesCardAdapter.SortMode.UNSORTED);
touchHelperCallback.setDragEnabled(true);
}
} else if (id == R.id.menu_sort_label) {
item.setChecked(true);
if (adapter != null) {
adapter.setSortMode(EntriesCardAdapter.SortMode.LABEL);
touchHelperCallback.setDragEnabled(false);
}
}
return super.onOptionsItemSelected(item);
}
}

View file

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M3,18h6v-2L3,16v2zM3,6v2h18L21,6L3,6zM3,13h12v-2L3,11v2z"/>
</vector>

View file

@ -1,6 +1,27 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_sort"
android:title="@string/menu_main_sort"
android:icon="@drawable/ic_sort_white"
app:showAsAction="always" >
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/menu_sort_none"
android:title="@string/menu_sort_none"
android:checked="true" />
<item
android:id="@+id/menu_sort_label"
android:title="@string/menu_sort_label" />
</group>
</menu>
</item>
<item
android:id="@+id/menu_search"
android:title="@string/menu_main_search"

View file

@ -27,6 +27,10 @@
<string name="menu_main_backup">Backup</string>
<string name="menu_main_search">Search</string>
<string name="menu_main_settings">Settings</string>
<string name="menu_main_sort">Sort</string>
<string name="menu_sort_none">Unsorted</string>
<string name="menu_sort_label">Label</string>
<string name="menu_popup_edit_label">Edit label</string>
<string name="menu_popup_remove">Remove</string>