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

View file

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

View file

@ -167,7 +167,7 @@ public class PasswordStore extends ActionBarActivity {
return true;
case R.id.refresh:
refreshListAdapter();
updateListAdapter();
return true;
case android.R.id.home:
@ -398,7 +398,10 @@ public class PasswordStore extends ActionBarActivity {
.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;
if (null !=
(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) {
PasswordFragment plist;
if (null !=

View file

@ -28,6 +28,11 @@ public class PasswordRepository {
protected PasswordRepository(){ }
/**
* Returns the git repository
* @param localDir needed only on the creation
* @return the git repository
*/
public static Repository getRepository(File localDir) {
if (repository == null) {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
@ -92,6 +97,10 @@ public class PasswordRepository {
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() {
return getPasswords(repository.getWorkTree());
}
@ -100,27 +109,41 @@ public class PasswordRepository {
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) {
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){
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())));
files.addAll( new ArrayList<File>((List<File>)FileUtils.listFiles(path, new String[] {"gpg"}, false)));
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) {
//We need to recover the passwords then parse the files
ArrayList<File> passList = getFilesList(path);
if (passList.size() == 0) return new ArrayList<PasswordItem>();
// TODO replace with a set
ArrayList<PasswordItem> passwordList = new ArrayList<PasswordItem>();
for (File file : passList) {