aboutsummaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorzeapo <mohamed@zenadi.com>2014-10-29 22:09:40 +0100
committerzeapo <mohamed@zenadi.com>2014-10-29 22:09:40 +0100
commit7ddc23ee839c8648b60217101862b739df4018c3 (patch)
tree621a39ff9828c3ba61c8d302e2a7f2cb2eb4e91d /app/src/main
parent892cd1b4d3b9f6474522d389ac2ea7b21c27c97b (diff)
corrected some issues with filtering and categories
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/GitAsyncTask.java7
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java38
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/PasswordStore.java18
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java27
4 files changed, 77 insertions, 13 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/GitAsyncTask.java b/app/src/main/java/com/zeapo/pwdstore/GitAsyncTask.java
index 26ad8c7c..c27bd0d4 100644
--- a/app/src/main/java/com/zeapo/pwdstore/GitAsyncTask.java
+++ b/app/src/main/java/com/zeapo/pwdstore/GitAsyncTask.java
@@ -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
}
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java
index 6058d169..1dcce1b8 100644
--- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java
+++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java
@@ -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() {
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java
index 759b0989..9c3afb56 100644
--- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java
+++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java
@@ -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 !=
diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java
index b35f9701..31dea887 100644
--- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java
+++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java
@@ -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) {