summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/PasswordStore.java16
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/utils/PasswordRecyclerAdapter.java29
2 files changed, 30 insertions, 15 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java
index 97ac2d02..3d17ce10 100644
--- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java
+++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java
@@ -32,7 +32,9 @@ import org.eclipse.jgit.lib.Repository;
import java.io.File;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.Set;
+import java.util.TreeSet;
public class PasswordStore extends AppCompatActivity {
private static final String TAG = "PwdStrAct";
@@ -355,7 +357,13 @@ public class PasswordStore extends AppCompatActivity {
startActivityForResult(intent, PgpHandler.REQUEST_CODE_ENCRYPT);
}
- public void deletePassword(final PasswordRecyclerAdapter adapter, final int position) {
+ // deletes passwords in order from top to bottom
+ public void deletePasswords(final PasswordRecyclerAdapter adapter, final Set<Integer> selectedItems) {
+ final Iterator it = selectedItems.iterator();
+ if (!it.hasNext()) {
+ return;
+ }
+ final int position = (int) it.next();
final PasswordItem item = adapter.getValues().get(position);
new AlertDialog.Builder(this).
setMessage(this.getResources().getString(R.string.delete_dialog_text) +
@@ -366,6 +374,8 @@ public class PasswordStore extends AppCompatActivity {
String path = item.getFile().getAbsolutePath();
item.getFile().delete();
adapter.remove(position);
+ it.remove();
+ adapter.updateSelectedItems(position, selectedItems);
setResult(RESULT_CANCELED);
Repository repo = PasswordRepository.getRepository(PasswordRepository.getRepositoryDirectory(activity));
@@ -375,12 +385,14 @@ public class PasswordStore extends AppCompatActivity {
git.rm().addFilepattern(path.replace(PasswordRepository.getWorkTree() + "/", "")),
git.commit().setMessage("[ANDROID PwdStore] Remove " + item + " from store.")
);
+ deletePasswords(adapter, selectedItems);
}
})
.setNegativeButton(this.getResources().getString(R.string.dialog_no), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
-
+ it.remove();
+ deletePasswords(adapter, selectedItems);
}
})
.show();
diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRecyclerAdapter.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRecyclerAdapter.java
index b4ad1d81..4bf9cb98 100644
--- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRecyclerAdapter.java
+++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRecyclerAdapter.java
@@ -140,9 +140,7 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter<PasswordRecycl
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_delete_password:
- for (int position : selectedItems) {
- activity.deletePassword(PasswordRecyclerAdapter.this, position);
- }
+ activity.deletePasswords(PasswordRecyclerAdapter.this, new TreeSet<>(selectedItems));
mode.finish(); // Action picked, so close the CAB
return true;
default:
@@ -193,16 +191,7 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter<PasswordRecycl
// keep selectedItems updated so we know what to notifyItemChanged
// (instead of just using notifyDataSetChanged)
- Set<Integer> temp = new TreeSet<>();
- for (int selected : selectedItems) {
- if (selected > position) {
- temp.add(selected - 1);
- } else {
- temp.add(selected);
- }
- }
- selectedItems.clear();
- selectedItems.addAll(temp);
+ updateSelectedItems(position, selectedItems);
}
public void toggleSelection(int position, View view) {
@@ -214,4 +203,18 @@ public class PasswordRecyclerAdapter extends RecyclerView.Adapter<PasswordRecycl
}
}
+ // use this after an item is removed to update the positions of items in set
+ // that followed the removed position
+ public void updateSelectedItems(int position, Set<Integer> selectedItems) {
+ Set<Integer> temp = new TreeSet<>();
+ for (int selected : selectedItems) {
+ if (selected > position) {
+ temp.add(selected - 1);
+ } else {
+ temp.add(selected);
+ }
+ }
+ selectedItems.clear();
+ selectedItems.addAll(temp);
+ }
} \ No newline at end of file