aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorحسين <zidhussein@gmail.com>2018-08-20 10:13:38 +0100
committerMohamed Zenadi <zeapo@users.noreply.github.com>2018-08-20 11:13:38 +0200
commit30d4d5342ff6cdcc7d1adbd0be34bea4b1aa1a96 (patch)
tree339952842096fc9478a8bc9e459235e428bd117c /app/src/main/java
parent65ef17727c5ae9e845381807b90af34eff8a7a53 (diff)
change the sort order of password items (#421)
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java22
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/PasswordStore.java10
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.java10
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.java2
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java48
5 files changed, 73 insertions, 19 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java
index 2b0fa7c1..58af4a60 100644
--- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java
+++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java
@@ -60,7 +60,7 @@ public class PasswordFragment extends Fragment{
scrollPosition = new Stack<>();
pathStack = new Stack<>();
recyclerAdapter = new PasswordRecyclerAdapter((PasswordStore) getActivity(), mListener,
- PasswordRepository.getPasswords(new File(path), PasswordRepository.getRepositoryDirectory(getActivity())));
+ PasswordRepository.getPasswords(new File(path), PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()));
}
@Override
@@ -101,15 +101,15 @@ public class PasswordFragment extends Fragment{
if (item.getType() == PasswordItem.TYPE_CATEGORY) {
// push the current password list (non filtered plz!)
passListStack.push(pathStack.isEmpty() ?
- PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(context)) :
- PasswordRepository.getPasswords(pathStack.peek(), PasswordRepository.getRepositoryDirectory(context)));
+ PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(context), getSortOrder()) :
+ PasswordRepository.getPasswords(pathStack.peek(), PasswordRepository.getRepositoryDirectory(context), getSortOrder()));
//push the category were we're going
pathStack.push(item.getFile());
scrollPosition.push(recyclerView.getVerticalScrollbarPosition());
recyclerView.scrollToPosition(0);
recyclerAdapter.clear();
- recyclerAdapter.addAll(PasswordRepository.getPasswords(item.getFile(), PasswordRepository.getRepositoryDirectory(context)));
+ recyclerAdapter.addAll(PasswordRepository.getPasswords(item.getFile(), PasswordRepository.getRepositoryDirectory(context), getSortOrder()));
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else {
@@ -135,7 +135,7 @@ public class PasswordFragment extends Fragment{
pathStack.clear();
scrollPosition.clear();
recyclerAdapter.clear();
- recyclerAdapter.addAll(PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity())));
+ recyclerAdapter.addAll(PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()));
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
@@ -146,8 +146,8 @@ public class PasswordFragment extends Fragment{
public void refreshAdapter() {
recyclerAdapter.clear();
recyclerAdapter.addAll(pathStack.isEmpty() ?
- PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity())) :
- PasswordRepository.getPasswords(pathStack.peek(), PasswordRepository.getRepositoryDirectory(getActivity())));
+ PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()) :
+ PasswordRepository.getPasswords(pathStack.peek(), PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()));
}
/**
@@ -172,8 +172,8 @@ public class PasswordFragment extends Fragment{
private void recursiveFilter(String filter, File dir) {
// on the root the pathStack is empty
ArrayList<PasswordItem> passwordItems = dir == null ?
- PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity())) :
- PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(getActivity()));
+ PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()) :
+ PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder());
boolean rec = settings.getBoolean("filter_recursively", true);
for (PasswordItem item : passwordItems) {
@@ -223,4 +223,8 @@ public class PasswordFragment extends Fragment{
recyclerAdapter.mActionMode.finish();
}
}
+
+ private PasswordRepository.PasswordSortOrder getSortOrder() {
+ return PasswordRepository.PasswordSortOrder.getSortOrder(settings);
+ }
}
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java
index 890ab3e8..b4029ba4 100644
--- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java
+++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java
@@ -313,7 +313,9 @@ public class PasswordStore extends AppCompatActivity {
if (settings.getBoolean("git_external", false) && externalRepoPath != null) {
File dir = new File(externalRepoPath);
- if (dir.exists() && dir.isDirectory() && !PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(this)).isEmpty()) {
+ PasswordRepository.PasswordSortOrder sortOrder = PasswordRepository.PasswordSortOrder.valueOf(settings.getString("sort_order", null));
+
+ if (dir.exists() && dir.isDirectory() && !PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(this), getSortOrder()).isEmpty()) {
PasswordRepository.closeRepository();
checkLocalRepository();
return; // if not empty, just show me the passwords!
@@ -648,7 +650,7 @@ public class PasswordStore extends AppCompatActivity {
dir.exists() &&
dir.isDirectory() &&
!FileUtils.listFiles(dir, null, true).isEmpty() &&
- !PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(this)).isEmpty()) {
+ !PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(this), getSortOrder()).isEmpty()) {
PasswordRepository.closeRepository();
checkLocalRepository();
return; // if not empty, just show me the passwords!
@@ -769,4 +771,8 @@ public class PasswordStore extends AppCompatActivity {
setResult(RESULT_OK, data);
finish();
}
+
+ private PasswordRepository.PasswordSortOrder getSortOrder() {
+ return PasswordRepository.PasswordSortOrder.getSortOrder(settings);
+ }
}
diff --git a/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.java b/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.java
index 8f583a8a..d8c5ff16 100644
--- a/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.java
+++ b/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.java
@@ -1,7 +1,9 @@
package com.zeapo.pwdstore;
import android.content.Context;
+import android.content.SharedPreferences;
import android.os.Bundle;
+import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
@@ -51,7 +53,7 @@ public class SelectFolderFragment extends Fragment{
pathStack = new Stack<>();
recyclerAdapter = new FolderRecyclerAdapter((SelectFolderActivity) getActivity(), mListener,
- PasswordRepository.getPasswords(new File(path), PasswordRepository.getRepositoryDirectory(getActivity())));
+ PasswordRepository.getPasswords(new File(path), PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()));
}
@Override
@@ -87,7 +89,7 @@ public class SelectFolderFragment extends Fragment{
recyclerView.scrollToPosition(0);
recyclerAdapter.clear();
- recyclerAdapter.addAll(PasswordRepository.getPasswords(item.getFile(), PasswordRepository.getRepositoryDirectory(context)));
+ recyclerAdapter.addAll(PasswordRepository.getPasswords(item.getFile(), PasswordRepository.getRepositoryDirectory(context), getSortOrder()));
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@@ -109,4 +111,8 @@ public class SelectFolderFragment extends Fragment{
else
return pathStack.peek();
}
+
+ private PasswordRepository.PasswordSortOrder getSortOrder() {
+ return PasswordRepository.PasswordSortOrder.getSortOrder(PreferenceManager.getDefaultSharedPreferences(getActivity()));
+ }
}
diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.java
index f45abc38..3261cc58 100644
--- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.java
+++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordItem.java
@@ -55,7 +55,7 @@ public class PasswordItem implements Comparable{
return this.type;
}
- private String getName(){
+ String getName(){
return this.name;
}
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 e232b8a0..d724f090 100644
--- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java
+++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java
@@ -5,7 +5,6 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
-import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository;
@@ -19,6 +18,7 @@ import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Comparator;
import java.util.List;
import java.util.Set;
@@ -155,8 +155,8 @@ public class PasswordRepository {
*
* @return a list of passwords in the root direcotyr
*/
- public static ArrayList<PasswordItem> getPasswords(File rootDir) {
- return getPasswords(rootDir, rootDir);
+ public static ArrayList<PasswordItem> getPasswords(File rootDir, PasswordSortOrder sortOrder) {
+ return getPasswords(rootDir, rootDir, sortOrder);
}
/**
@@ -185,7 +185,7 @@ public class PasswordRepository {
* @param path the directory path
* @return a list of password items
*/
- public static ArrayList<PasswordItem> getPasswords(File path, File rootDir) {
+ public static ArrayList<PasswordItem> getPasswords(File path, File rootDir, PasswordSortOrder sortOrder) {
//We need to recover the passwords then parse the files
ArrayList<File> passList = getFilesList(path);
@@ -203,7 +203,7 @@ public class PasswordRepository {
passwordList.add(PasswordItem.newCategory(file.getName(), file, rootDir));
}
}
- sort(passwordList);
+ sort(passwordList, sortOrder.comparator);
return passwordList;
}
@@ -244,4 +244,42 @@ public class PasswordRepository {
}
}
}
+
+ public enum PasswordSortOrder {
+
+ FOLDER_FIRST(new Comparator<PasswordItem>() {
+ @Override
+ public int compare(PasswordItem p1, PasswordItem p2) {
+ return (p1.getType() + p1.getName())
+ .compareToIgnoreCase(p2.getType() + p2.getName());
+ }
+ }),
+
+ INDEPENDENT(new Comparator<PasswordItem>() {
+ @Override
+ public int compare(PasswordItem p1, PasswordItem p2) {
+ return p1.getName().compareToIgnoreCase(p2.getName());
+ }
+ }),
+
+ FILE_FIRST(new Comparator<PasswordItem>() {
+ @Override
+ public int compare(PasswordItem p1, PasswordItem p2) {
+ return (p2.getType() + p1.getName())
+ .compareToIgnoreCase(p1.getType() + p2.getName());
+ }
+ })
+
+ ;
+
+ private Comparator<PasswordItem> comparator;
+
+ PasswordSortOrder(Comparator<PasswordItem> comparator) {
+ this.comparator = comparator;
+ }
+
+ public static PasswordSortOrder getSortOrder(SharedPreferences settings) {
+ return valueOf(settings.getString("sort_order", null));
+ }
+ }
}