Get rid of the index array

This commit is contained in:
Jakob Nixdorf 2017-08-06 17:26:22 +02:00
parent 72a8754d09
commit 2fb740809b
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC

View file

@ -54,7 +54,7 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
private SharedPreferences sharedPrefs; private SharedPreferences sharedPrefs;
private EntryFilter filter; private EntryFilter filter;
private ArrayList<Entry> entries; private ArrayList<Entry> entries;
private ArrayList<Integer> displayedEntries; private ArrayList<Entry> displayedEntries;
public Callback callback; public Callback callback;
public EntriesCardAdapter(Context context) { public EntriesCardAdapter(Context context) {
@ -85,7 +85,8 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
public void entriesChanged() { public void entriesChanged() {
if (displayedEntries != null) if (displayedEntries != null)
displayedEntries.clear(); displayedEntries.clear();
displayedEntries = defaultIndices();
displayedEntries = entries;
notifyDataSetChanged(); notifyDataSetChanged();
} }
@ -99,32 +100,9 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
entriesChanged(); entriesChanged();
} }
public ArrayList<Integer> defaultIndices() {
ArrayList<Integer> newIdx = new ArrayList<>();
for (int i = 0; i < entries.size(); i++)
newIdx.add(i);
return newIdx;
}
public int removeIndex(int pos) {
int removed = displayedEntries.remove(pos);
ArrayList<Integer> newIdx = new ArrayList<>();
for (int i = 0; i < displayedEntries.size(); i++) {
int idx = displayedEntries.get(i);
if (idx > removed)
idx -= 1;
newIdx.add(idx);
}
displayedEntries = newIdx;
return removed;
}
@Override @Override
public void onBindViewHolder(EntryViewHolder entryViewHolder, int i) { public void onBindViewHolder(EntryViewHolder entryViewHolder, int i) {
Entry entry = entries.get(displayedEntries.get(i)); Entry entry = displayedEntries.get(i);
entryViewHolder.updateValues(entry.getLabel(), entry.getCurrentOTP()); entryViewHolder.updateValues(entry.getLabel(), entry.getCurrentOTP());
@ -190,7 +168,7 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
AlertDialog.Builder builder = new AlertDialog.Builder(context); AlertDialog.Builder builder = new AlertDialog.Builder(context);
final EditText input = new EditText(context); final EditText input = new EditText(context);
input.setText(entries.get(displayedEntries.get(pos)).getLabel()); input.setText(displayedEntries.get(pos).getLabel());
input.setSingleLine(); input.setSingleLine();
FrameLayout container = new FrameLayout(context); FrameLayout container = new FrameLayout(context);
@ -205,9 +183,13 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
.setPositiveButton(R.string.button_save, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.button_save, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialogInterface, int i) { public void onClick(DialogInterface dialogInterface, int i) {
entries.get(displayedEntries.get(pos)).setLabel(input.getEditableText().toString()); int realIndex = entries.indexOf(displayedEntries.get(pos));
String newLabel = input.getEditableText().toString();
displayedEntries.get(pos).setLabel(newLabel);
notifyItemChanged(pos); notifyItemChanged(pos);
entries.get(realIndex).setLabel(newLabel);
DatabaseHelper.saveDatabase(context, entries); DatabaseHelper.saveDatabase(context, entries);
} }
}) })
@ -227,9 +209,12 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialogInterface, int i) { public void onClick(DialogInterface dialogInterface, int i) {
entries.remove(removeIndex(position)); int realIndex = entries.indexOf(displayedEntries.get(position));
displayedEntries.remove(position);
notifyItemRemoved(position); notifyItemRemoved(position);
entries.remove(realIndex);
DatabaseHelper.saveDatabase(context, entries); DatabaseHelper.saveDatabase(context, entries);
} }
}) })
@ -291,26 +276,26 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
protected FilterResults performFiltering(CharSequence constraint) { protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults filterResults = new FilterResults(); final FilterResults filterResults = new FilterResults();
ArrayList<Integer> newIdx = new ArrayList<>(); ArrayList<Entry> filtered = new ArrayList<>();
if (constraint != null && constraint.length() != 0){ if (constraint != null && constraint.length() != 0){
for (int i = 0; i < entries.size(); i++) { for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getLabel().toLowerCase().contains(constraint.toString().toLowerCase())) { if (entries.get(i).getLabel().toLowerCase().contains(constraint.toString().toLowerCase())) {
newIdx.add(i); filtered.add(entries.get(i));
} }
} }
} else { } else {
newIdx = defaultIndices(); filtered = entries;
} }
filterResults.count = newIdx.size(); filterResults.count = filtered.size();
filterResults.values = newIdx; filterResults.values = filtered;
return filterResults; return filterResults;
} }
@Override @Override
protected void publishResults(CharSequence constraint, FilterResults results) { protected void publishResults(CharSequence constraint, FilterResults results) {
displayedEntries = (ArrayList<Integer>) results.values; displayedEntries = (ArrayList<Entry>) results.values;
notifyDataSetChanged(); notifyDataSetChanged();
} }
} }