Add basic sorting by label
Might need some additional testing Closes #12
This commit is contained in:
parent
dc0f942c9a
commit
0165a00ffb
5 changed files with 105 additions and 12 deletions
|
@ -46,16 +46,23 @@ import org.shadowice.flocke.andotp.ItemTouchHelper.ItemTouchHelperAdapter;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
|
public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
|
||||||
implements ItemTouchHelperAdapter, Filterable {
|
implements ItemTouchHelperAdapter, Filterable {
|
||||||
|
|
||||||
|
public enum SortMode {
|
||||||
|
UNSORTED, LABEL
|
||||||
|
}
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
private SharedPreferences sharedPrefs;
|
private SharedPreferences sharedPrefs;
|
||||||
private EntryFilter filter;
|
private EntryFilter filter;
|
||||||
private ArrayList<Entry> entries;
|
private ArrayList<Entry> entries;
|
||||||
private ArrayList<Entry> displayedEntries;
|
private ArrayList<Entry> displayedEntries;
|
||||||
public Callback callback;
|
private Callback callback;
|
||||||
|
|
||||||
|
private SortMode sortMode = SortMode.UNSORTED;
|
||||||
|
|
||||||
public EntriesCardAdapter(Context context) {
|
public EntriesCardAdapter(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
@ -83,11 +90,7 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
|
||||||
}
|
}
|
||||||
|
|
||||||
public void entriesChanged() {
|
public void entriesChanged() {
|
||||||
if (displayedEntries != null)
|
displayedEntries = sortEntries(entries);
|
||||||
displayedEntries.clear();
|
|
||||||
|
|
||||||
displayedEntries = entries;
|
|
||||||
|
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,10 +159,14 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onItemMove(int fromPosition, int toPosition) {
|
public boolean onItemMove(int fromPosition, int toPosition) {
|
||||||
|
if (sortMode == SortMode.UNSORTED && displayedEntries.equals(entries)) {
|
||||||
Collections.swap(entries, fromPosition, toPosition);
|
Collections.swap(entries, fromPosition, toPosition);
|
||||||
|
|
||||||
|
displayedEntries = new ArrayList<>(entries);
|
||||||
notifyItemMoved(fromPosition, toPosition);
|
notifyItemMoved(fromPosition, toPosition);
|
||||||
|
|
||||||
DatabaseHelper.saveDatabase(context, entries);
|
DatabaseHelper.saveDatabase(context, entries);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -187,7 +194,12 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
|
||||||
String newLabel = input.getEditableText().toString();
|
String newLabel = input.getEditableText().toString();
|
||||||
|
|
||||||
displayedEntries.get(pos).setLabel(newLabel);
|
displayedEntries.get(pos).setLabel(newLabel);
|
||||||
|
if (sortMode == SortMode.LABEL) {
|
||||||
|
displayedEntries = sortEntries(displayedEntries);
|
||||||
|
notifyDataSetChanged();
|
||||||
|
} else {
|
||||||
notifyItemChanged(pos);
|
notifyItemChanged(pos);
|
||||||
|
}
|
||||||
|
|
||||||
entries.get(realIndex).setLabel(newLabel);
|
entries.get(realIndex).setLabel(newLabel);
|
||||||
DatabaseHelper.saveDatabase(context, entries);
|
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();
|
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) {
|
public void setCallback(Callback cb) {
|
||||||
this.callback = cb;
|
this.callback = cb;
|
||||||
}
|
}
|
||||||
|
@ -295,11 +326,18 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void publishResults(CharSequence constraint, FilterResults results) {
|
protected void publishResults(CharSequence constraint, FilterResults results) {
|
||||||
displayedEntries = (ArrayList<Entry>) results.values;
|
displayedEntries = sortEntries((ArrayList<Entry>) results.values);
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class LabelComparator implements Comparator<Entry> {
|
||||||
|
@Override
|
||||||
|
public int compare(Entry o1, Entry o2) {
|
||||||
|
return o1.getLabel().compareTo(o2.getLabel());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public interface Callback {
|
public interface Callback {
|
||||||
void onMoveEventStart();
|
void onMoveEventStart();
|
||||||
void onMoveEventStop();
|
void onMoveEventStop();
|
||||||
|
|
|
@ -63,6 +63,7 @@ public class MainActivity extends BaseActivity
|
||||||
private EntriesCardAdapter adapter;
|
private EntriesCardAdapter adapter;
|
||||||
private FloatingActionMenu floatingActionMenu;
|
private FloatingActionMenu floatingActionMenu;
|
||||||
private SearchView searchView;
|
private SearchView searchView;
|
||||||
|
private MenuItem sortMenu;
|
||||||
private SimpleItemTouchHelperCallback touchHelperCallback;
|
private SimpleItemTouchHelperCallback touchHelperCallback;
|
||||||
|
|
||||||
private SharedPreferences sharedPref;
|
private SharedPreferences sharedPref;
|
||||||
|
@ -319,6 +320,8 @@ public class MainActivity extends BaseActivity
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
getMenuInflater().inflate(R.menu.menu_main, menu);
|
getMenuInflater().inflate(R.menu.menu_main, menu);
|
||||||
|
|
||||||
|
sortMenu = menu.findItem(R.id.menu_sort);
|
||||||
|
|
||||||
MenuItem searchItem = menu.findItem(R.id.menu_search);
|
MenuItem searchItem = menu.findItem(R.id.menu_search);
|
||||||
searchView = (SearchView) searchItem.getActionView();
|
searchView = (SearchView) searchItem.getActionView();
|
||||||
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
||||||
|
@ -339,13 +342,18 @@ public class MainActivity extends BaseActivity
|
||||||
public boolean onMenuItemActionExpand(MenuItem item) {
|
public boolean onMenuItemActionExpand(MenuItem item) {
|
||||||
floatingActionMenu.hide();
|
floatingActionMenu.hide();
|
||||||
touchHelperCallback.setDragEnabled(false);
|
touchHelperCallback.setDragEnabled(false);
|
||||||
|
if (sortMenu != null)
|
||||||
|
sortMenu.setVisible(false);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onMenuItemActionCollapse(MenuItem item) {
|
public boolean onMenuItemActionCollapse(MenuItem item) {
|
||||||
floatingActionMenu.show();
|
floatingActionMenu.show();
|
||||||
|
if (adapter == null || adapter.getSortMode() == EntriesCardAdapter.SortMode.UNSORTED)
|
||||||
touchHelperCallback.setDragEnabled(true);
|
touchHelperCallback.setDragEnabled(true);
|
||||||
|
if (sortMenu != null)
|
||||||
|
sortMenu.setVisible(true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -367,7 +375,20 @@ public class MainActivity extends BaseActivity
|
||||||
Intent aboutIntent = new Intent(this, AboutActivity.class);
|
Intent aboutIntent = new Intent(this, AboutActivity.class);
|
||||||
startActivity(aboutIntent);
|
startActivity(aboutIntent);
|
||||||
return true;
|
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);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
9
app/src/main/res/drawable/ic_sort_white.xml
Normal file
9
app/src/main/res/drawable/ic_sort_white.xml
Normal 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>
|
|
@ -1,6 +1,27 @@
|
||||||
<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_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
|
<item
|
||||||
android:id="@+id/menu_search"
|
android:id="@+id/menu_search"
|
||||||
android:title="@string/menu_main_search"
|
android:title="@string/menu_main_search"
|
||||||
|
|
|
@ -27,6 +27,10 @@
|
||||||
<string name="menu_main_backup">Backup</string>
|
<string name="menu_main_backup">Backup</string>
|
||||||
<string name="menu_main_search">Search</string>
|
<string name="menu_main_search">Search</string>
|
||||||
<string name="menu_main_settings">Settings</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_edit_label">Edit label</string>
|
||||||
<string name="menu_popup_remove">Remove</string>
|
<string name="menu_popup_remove">Remove</string>
|
||||||
|
|
Loading…
Reference in a new issue