change the sort order of password items (#421)

This commit is contained in:
حسين 2018-08-20 10:13:38 +01:00 committed by Mohamed Zenadi
parent 65ef17727c
commit 30d4d5342f
9 changed files with 96 additions and 20 deletions

View file

@ -60,7 +60,7 @@ public class PasswordFragment extends Fragment{
scrollPosition = new Stack<>(); scrollPosition = new Stack<>();
pathStack = new Stack<>(); pathStack = new Stack<>();
recyclerAdapter = new PasswordRecyclerAdapter((PasswordStore) getActivity(), mListener, recyclerAdapter = new PasswordRecyclerAdapter((PasswordStore) getActivity(), mListener,
PasswordRepository.getPasswords(new File(path), PasswordRepository.getRepositoryDirectory(getActivity()))); PasswordRepository.getPasswords(new File(path), PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()));
} }
@Override @Override
@ -101,15 +101,15 @@ public class PasswordFragment extends Fragment{
if (item.getType() == PasswordItem.TYPE_CATEGORY) { if (item.getType() == PasswordItem.TYPE_CATEGORY) {
// push the current password list (non filtered plz!) // push the current password list (non filtered plz!)
passListStack.push(pathStack.isEmpty() ? passListStack.push(pathStack.isEmpty() ?
PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(context)) : PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(context), getSortOrder()) :
PasswordRepository.getPasswords(pathStack.peek(), PasswordRepository.getRepositoryDirectory(context))); PasswordRepository.getPasswords(pathStack.peek(), PasswordRepository.getRepositoryDirectory(context), getSortOrder()));
//push the category were we're going //push the category were we're going
pathStack.push(item.getFile()); pathStack.push(item.getFile());
scrollPosition.push(recyclerView.getVerticalScrollbarPosition()); scrollPosition.push(recyclerView.getVerticalScrollbarPosition());
recyclerView.scrollToPosition(0); recyclerView.scrollToPosition(0);
recyclerAdapter.clear(); 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); ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else { } else {
@ -135,7 +135,7 @@ public class PasswordFragment extends Fragment{
pathStack.clear(); pathStack.clear();
scrollPosition.clear(); scrollPosition.clear();
recyclerAdapter.clear(); recyclerAdapter.clear();
recyclerAdapter.addAll(PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity()))); recyclerAdapter.addAll(PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()));
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false); ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
} }
@ -146,8 +146,8 @@ public class PasswordFragment extends Fragment{
public void refreshAdapter() { public void refreshAdapter() {
recyclerAdapter.clear(); recyclerAdapter.clear();
recyclerAdapter.addAll(pathStack.isEmpty() ? recyclerAdapter.addAll(pathStack.isEmpty() ?
PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity())) : PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()) :
PasswordRepository.getPasswords(pathStack.peek(), PasswordRepository.getRepositoryDirectory(getActivity()))); PasswordRepository.getPasswords(pathStack.peek(), PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()));
} }
/** /**
@ -172,8 +172,8 @@ public class PasswordFragment extends Fragment{
private void recursiveFilter(String filter, File dir) { private void recursiveFilter(String filter, File dir) {
// on the root the pathStack is empty // on the root the pathStack is empty
ArrayList<PasswordItem> passwordItems = dir == null ? ArrayList<PasswordItem> passwordItems = dir == null ?
PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity())) : PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()) :
PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(getActivity())); PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder());
boolean rec = settings.getBoolean("filter_recursively", true); boolean rec = settings.getBoolean("filter_recursively", true);
for (PasswordItem item : passwordItems) { for (PasswordItem item : passwordItems) {
@ -223,4 +223,8 @@ public class PasswordFragment extends Fragment{
recyclerAdapter.mActionMode.finish(); recyclerAdapter.mActionMode.finish();
} }
} }
private PasswordRepository.PasswordSortOrder getSortOrder() {
return PasswordRepository.PasswordSortOrder.getSortOrder(settings);
}
} }

View file

@ -313,7 +313,9 @@ public class PasswordStore extends AppCompatActivity {
if (settings.getBoolean("git_external", false) && externalRepoPath != null) { if (settings.getBoolean("git_external", false) && externalRepoPath != null) {
File dir = new File(externalRepoPath); 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(); PasswordRepository.closeRepository();
checkLocalRepository(); checkLocalRepository();
return; // if not empty, just show me the passwords! return; // if not empty, just show me the passwords!
@ -648,7 +650,7 @@ public class PasswordStore extends AppCompatActivity {
dir.exists() && dir.exists() &&
dir.isDirectory() && dir.isDirectory() &&
!FileUtils.listFiles(dir, null, true).isEmpty() && !FileUtils.listFiles(dir, null, true).isEmpty() &&
!PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(this)).isEmpty()) { !PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(this), getSortOrder()).isEmpty()) {
PasswordRepository.closeRepository(); PasswordRepository.closeRepository();
checkLocalRepository(); checkLocalRepository();
return; // if not empty, just show me the passwords! return; // if not empty, just show me the passwords!
@ -769,4 +771,8 @@ public class PasswordStore extends AppCompatActivity {
setResult(RESULT_OK, data); setResult(RESULT_OK, data);
finish(); finish();
} }
private PasswordRepository.PasswordSortOrder getSortOrder() {
return PasswordRepository.PasswordSortOrder.getSortOrder(settings);
}
} }

View file

@ -1,7 +1,9 @@
package com.zeapo.pwdstore; package com.zeapo.pwdstore;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
@ -51,7 +53,7 @@ public class SelectFolderFragment extends Fragment{
pathStack = new Stack<>(); pathStack = new Stack<>();
recyclerAdapter = new FolderRecyclerAdapter((SelectFolderActivity) getActivity(), mListener, recyclerAdapter = new FolderRecyclerAdapter((SelectFolderActivity) getActivity(), mListener,
PasswordRepository.getPasswords(new File(path), PasswordRepository.getRepositoryDirectory(getActivity()))); PasswordRepository.getPasswords(new File(path), PasswordRepository.getRepositoryDirectory(getActivity()), getSortOrder()));
} }
@Override @Override
@ -87,7 +89,7 @@ public class SelectFolderFragment extends Fragment{
recyclerView.scrollToPosition(0); recyclerView.scrollToPosition(0);
recyclerAdapter.clear(); 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); ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} }
@ -109,4 +111,8 @@ public class SelectFolderFragment extends Fragment{
else else
return pathStack.peek(); return pathStack.peek();
} }
private PasswordRepository.PasswordSortOrder getSortOrder() {
return PasswordRepository.PasswordSortOrder.getSortOrder(PreferenceManager.getDefaultSharedPreferences(getActivity()));
}
} }

View file

@ -55,7 +55,7 @@ public class PasswordItem implements Comparable{
return this.type; return this.type;
} }
private String getName(){ String getName(){
return this.name; return this.name;
} }

View file

@ -5,7 +5,6 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.commons.io.filefilter.FileFilterUtils;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
@ -19,6 +18,7 @@ import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -155,8 +155,8 @@ public class PasswordRepository {
* *
* @return a list of passwords in the root direcotyr * @return a list of passwords in the root direcotyr
*/ */
public static ArrayList<PasswordItem> getPasswords(File rootDir) { public static ArrayList<PasswordItem> getPasswords(File rootDir, PasswordSortOrder sortOrder) {
return getPasswords(rootDir, rootDir); return getPasswords(rootDir, rootDir, sortOrder);
} }
/** /**
@ -185,7 +185,7 @@ public class PasswordRepository {
* @param path the directory path * @param path the directory path
* @return a list of password items * @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 //We need to recover the passwords then parse the files
ArrayList<File> passList = getFilesList(path); ArrayList<File> passList = getFilesList(path);
@ -203,7 +203,7 @@ public class PasswordRepository {
passwordList.add(PasswordItem.newCategory(file.getName(), file, rootDir)); passwordList.add(PasswordItem.newCategory(file.getName(), file, rootDir));
} }
} }
sort(passwordList); sort(passwordList, sortOrder.comparator);
return passwordList; 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));
}
}
} }

View file

@ -8,4 +8,14 @@
<item>ssh://</item> <item>ssh://</item>
<item>https://</item> <item>https://</item>
</string-array> </string-array>
<string-array name="sort_order_entries">
<item>@string/pref_folder_first_sort_order</item>
<item>@string/pref_file_first_sort_order</item>
<item>@string/pref_type_independent_sort_order</item>
</string-array>
<string-array name="sort_order_values">
<item>FOLDER_FIRST</item>
<item>FILE_FIRST</item>
<item>INDEPENDENT</item>
</string-array>
</resources> </resources>

View file

@ -123,6 +123,10 @@
<string name="ssh_key_error_dialog_text">Message : \n</string> <string name="ssh_key_error_dialog_text">Message : \n</string>
<string name="pref_recursive_filter">Recursive filtering</string> <string name="pref_recursive_filter">Recursive filtering</string>
<string name="pref_recursive_filter_hint">Recursively find passwords of the current directory.</string> <string name="pref_recursive_filter_hint">Recursively find passwords of the current directory.</string>
<string name="pref_sort_order_title">Password sort order</string>
<string name="pref_folder_first_sort_order">Folders First</string>
<string name="pref_file_first_sort_order">Files First</string>
<string name="pref_type_independent_sort_order">Type Independent</string>
<string name="pref_autofill_title">Autofill</string> <string name="pref_autofill_title">Autofill</string>
<string name="pref_autofill_enable_title">Enable autofill</string> <string name="pref_autofill_enable_title">Enable autofill</string>
<string name="pref_autofill_enable_msg">Tap OK to go to Accessibility settings. There, tap Password Store under Services then tap the switch in the top right to turn it on or off.</string> <string name="pref_autofill_enable_msg">Tap OK to go to Accessibility settings. There, tap Password Store under Services then tap the switch in the top right to turn it on or off.</string>

View file

@ -76,6 +76,14 @@
android:key="filter_recursively" android:key="filter_recursively"
android:summary="@string/pref_recursive_filter_hint" android:summary="@string/pref_recursive_filter_hint"
android:title="@string/pref_recursive_filter" /> android:title="@string/pref_recursive_filter" />
<ListPreference
android:title="@string/pref_sort_order_title"
android:defaultValue="FOLDER_FIRST"
android:key="sort_order"
android:entries="@array/sort_order_entries"
android:entryValues="@array/sort_order_values"
android:persistent="true"
/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/pref_autofill_title"> <PreferenceCategory android:title="@string/pref_autofill_title">

View file

@ -11,7 +11,7 @@ buildscript {
google() google()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.1.0' classpath 'com.android.tools.build:gradle:3.1.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong