Open password deletion dialogs one-by-one instead of all at once, don't rely on the order of opening (potentially prevent #115)

This commit is contained in:
Matthew Wong 2015-08-11 17:39:35 -04:00
parent 083f340f29
commit 113dadd227
2 changed files with 30 additions and 15 deletions

View file

@ -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();

View file

@ -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);
}
}