corrected some issues with filtering and categories

This commit is contained in:
zeapo
2014-10-29 22:09:40 +01:00
parent 892cd1b4d3
commit 7ddc23ee83
4 changed files with 77 additions and 13 deletions

View File

@@ -5,17 +5,12 @@ import android.app.AlertDialog;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.util.Log;
import com.zeapo.pwdstore.utils.PasswordRepository; import com.zeapo.pwdstore.utils.PasswordRepository;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.GitCommand; import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.api.errors.TransportException;
public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> { public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
@@ -86,7 +81,7 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
if (refreshListOnEnd) { if (refreshListOnEnd) {
try { try {
((PasswordStore) this.activity).refreshListAdapter(); ((PasswordStore) this.activity).updateListAdapter();
} catch (ClassCastException e) { } catch (ClassCastException e) {
// oups, mistake // oups, mistake
} }

View File

@@ -23,6 +23,7 @@ import com.zeapo.pwdstore.utils.PasswordRepository;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Stack; import java.util.Stack;
/** /**
@@ -40,6 +41,7 @@ public class PasswordFragment extends Fragment{
// store the pass files list in a stack // store the pass files list in a stack
private Stack<ArrayList<PasswordItem>> passListStack; private Stack<ArrayList<PasswordItem>> passListStack;
private Stack<File> pathStack;
private Stack<Integer> scrollPosition; private Stack<Integer> scrollPosition;
private PasswordRecyclerAdapter recyclerAdapter; private PasswordRecyclerAdapter recyclerAdapter;
private RecyclerView recyclerView; private RecyclerView recyclerView;
@@ -60,6 +62,7 @@ public class PasswordFragment extends Fragment{
passListStack = new Stack<ArrayList<PasswordItem>>(); passListStack = new Stack<ArrayList<PasswordItem>>();
scrollPosition = new Stack<Integer>(); scrollPosition = new Stack<Integer>();
pathStack = new Stack<File>();
recyclerAdapter = new PasswordRecyclerAdapter((PasswordStore) getActivity(), mListener, PasswordRepository.getPasswords(new File(path))); recyclerAdapter = new PasswordRecyclerAdapter((PasswordStore) getActivity(), mListener, PasswordRepository.getPasswords(new File(path)));
} }
@@ -89,6 +92,7 @@ public class PasswordFragment extends Fragment{
public void onFragmentInteraction(PasswordItem item) { public void onFragmentInteraction(PasswordItem item) {
if (item.getType() == PasswordItem.TYPE_CATEGORY) { if (item.getType() == PasswordItem.TYPE_CATEGORY) {
passListStack.push((ArrayList<PasswordItem>) recyclerAdapter.getValues().clone()); passListStack.push((ArrayList<PasswordItem>) recyclerAdapter.getValues().clone());
pathStack.push(item.getFile());
scrollPosition.push(recyclerView.getVerticalScrollbarPosition()); scrollPosition.push(recyclerView.getVerticalScrollbarPosition());
recyclerView.scrollToPosition(0); recyclerView.scrollToPosition(0);
recyclerAdapter.clear(); recyclerAdapter.clear();
@@ -117,8 +121,12 @@ public class PasswordFragment extends Fragment{
// mListView.closeOpenedItems(); // mListView.closeOpenedItems();
} }
/**
* clears the adapter content and sets it back to the root view
*/
public void updateAdapter() { public void updateAdapter() {
passListStack.clear(); passListStack.clear();
pathStack.clear();
scrollPosition.clear(); scrollPosition.clear();
recyclerAdapter.clear(); recyclerAdapter.clear();
recyclerAdapter.addAll(PasswordRepository.getPasswords()); recyclerAdapter.addAll(PasswordRepository.getPasswords());
@@ -126,12 +134,29 @@ public class PasswordFragment extends Fragment{
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false); ((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
} }
/**
* refreshes the adapter with the latest opened category
*/
public void refreshAdapter() {
recyclerAdapter.clear();
recyclerAdapter.addAll(pathStack.isEmpty() ?
PasswordRepository.getPasswords() :
PasswordRepository.getPasswords(pathStack.peek()));
}
public void filterAdapter(String filter) { public void filterAdapter(String filter) {
Log.d("FRAG", "filter: " + filter);
if (filter.isEmpty()) { if (filter.isEmpty()) {
updateAdapter(); refreshAdapter();
} else { } else {
for (PasswordItem item : PasswordRepository.getPasswords()) { // on the root the pathStack is empty
boolean matches = item.getName().toLowerCase().contains(filter); List<PasswordItem> passwordItems = pathStack.isEmpty() ?
PasswordRepository.getPasswords() :
PasswordRepository.getPasswords(pathStack.peek());
for (PasswordItem item : passwordItems) {
boolean matches = item.toString().toLowerCase().contains(filter.toLowerCase());
boolean inAdapter = recyclerAdapter.getValues().contains(item); boolean inAdapter = recyclerAdapter.getValues().contains(item);
if (matches && !inAdapter) { if (matches && !inAdapter) {
recyclerAdapter.add(item); recyclerAdapter.add(item);
@@ -142,10 +167,17 @@ public class PasswordFragment extends Fragment{
} }
} }
/**
* Goes back one level back in the path
*/
public void popBack() { public void popBack() {
if (passListStack.isEmpty())
return;
recyclerView.scrollToPosition(scrollPosition.pop()); recyclerView.scrollToPosition(scrollPosition.pop());
recyclerAdapter.clear(); recyclerAdapter.clear();
recyclerAdapter.addAll(passListStack.pop()); recyclerAdapter.addAll(passListStack.pop());
pathStack.pop();
} }
public boolean isNotEmpty() { public boolean isNotEmpty() {

View File

@@ -167,7 +167,7 @@ public class PasswordStore extends ActionBarActivity {
return true; return true;
case R.id.refresh: case R.id.refresh:
refreshListAdapter(); updateListAdapter();
return true; return true;
case android.R.id.home: case android.R.id.home:
@@ -398,7 +398,10 @@ public class PasswordStore extends ActionBarActivity {
.show(); .show();
} }
public void refreshListAdapter() { /**
* clears adapter's content and updates it with a fresh list of passwords from the root
*/
public void updateListAdapter() {
PasswordFragment plist; PasswordFragment plist;
if (null != if (null !=
(plist = (PasswordFragment) getFragmentManager().findFragmentByTag("PasswordsList"))) { (plist = (PasswordFragment) getFragmentManager().findFragmentByTag("PasswordsList"))) {
@@ -406,6 +409,17 @@ public class PasswordStore extends ActionBarActivity {
} }
} }
/**
* Updates the adapter with the current view of passwords
*/
public void refreshListAdapter() {
PasswordFragment plist;
if (null !=
(plist = (PasswordFragment) getFragmentManager().findFragmentByTag("PasswordsList"))) {
plist.refreshAdapter();
}
}
public void filterListAdapter(String filter) { public void filterListAdapter(String filter) {
PasswordFragment plist; PasswordFragment plist;
if (null != if (null !=

View File

@@ -28,6 +28,11 @@ public class PasswordRepository {
protected PasswordRepository(){ } protected PasswordRepository(){ }
/**
* Returns the git repository
* @param localDir needed only on the creation
* @return the git repository
*/
public static Repository getRepository(File localDir) { public static Repository getRepository(File localDir) {
if (repository == null) { if (repository == null) {
FileRepositoryBuilder builder = new FileRepositoryBuilder(); FileRepositoryBuilder builder = new FileRepositoryBuilder();
@@ -92,6 +97,10 @@ public class PasswordRepository {
return getFilesList(repository.getWorkTree()); return getFilesList(repository.getWorkTree());
} }
/**
* Gets the password items in the root directory
* @return a list of passwords in the root direcotyr
*/
public static ArrayList<PasswordItem> getPasswords() { public static ArrayList<PasswordItem> getPasswords() {
return getPasswords(repository.getWorkTree()); return getPasswords(repository.getWorkTree());
} }
@@ -100,27 +109,41 @@ public class PasswordRepository {
return repository.getWorkTree(); return repository.getWorkTree();
} }
/**
* Gets a file from the working tree
* @param name the relative path of the file
* @return the file in the repository
*/
public static File getFile(String name) { public static File getFile(String name) {
return new File(repository.getWorkTree() + "/" + name); return new File(repository.getWorkTree() + "/" + name);
} }
/**
* Gets the .gpg files in a directory
* @param path the directory path
* @return the list of gpg files in that directory
*/
public static ArrayList<File> getFilesList(File path){ public static ArrayList<File> getFilesList(File path){
if (!path.exists()) return new ArrayList<File>(); if (!path.exists()) return new ArrayList<File>();
Log.d("REPO", path.getAbsolutePath()); Log.d("REPO", "current path: " + path.getPath());
ArrayList<File> files = new ArrayList<File>(Arrays.asList(path.listFiles((FileFilter) FileFilterUtils.directoryFileFilter()))); ArrayList<File> files = new ArrayList<File>(Arrays.asList(path.listFiles((FileFilter) FileFilterUtils.directoryFileFilter())));
files.addAll( new ArrayList<File>((List<File>)FileUtils.listFiles(path, new String[] {"gpg"}, false))); files.addAll( new ArrayList<File>((List<File>)FileUtils.listFiles(path, new String[] {"gpg"}, false)));
return new ArrayList<File>(files); return new ArrayList<File>(files);
} }
/**
* Gets the passwords (PasswordItem) in a directory
* @param path the directory path
* @return a list of password items
*/
public static ArrayList<PasswordItem> getPasswords(File path) { public static ArrayList<PasswordItem> getPasswords(File path) {
//We need to recover the passwords then parse the files //We need to recover the passwords then parse the files
ArrayList<File> passList = getFilesList(path); ArrayList<File> passList = getFilesList(path);
if (passList.size() == 0) return new ArrayList<PasswordItem>(); if (passList.size() == 0) return new ArrayList<PasswordItem>();
// TODO replace with a set
ArrayList<PasswordItem> passwordList = new ArrayList<PasswordItem>(); ArrayList<PasswordItem> passwordList = new ArrayList<PasswordItem>();
for (File file : passList) { for (File file : passList) {