Added sorting by name, date or size ascending/descending

This commit is contained in:
tobiasKaminsky 2014-09-14 15:51:09 +02:00
parent e630b8a342
commit 20cd00b22c
7 changed files with 162 additions and 1 deletions

View file

@ -43,6 +43,11 @@
android:orderInCategory="2"
android:showAsAction="never"
android:title="@string/actionbar_settings"/>
<item
android:id="@+id/action_sort"
android:orderInCategory="2"
android:showAsAction="never"
android:title="@string/actionbar_sort"/>
<!-- <item android:id="@+id/search" android:title="@string/actionbar_search" android:icon="@drawable/ic_action_search"></item> -->

View file

@ -11,6 +11,16 @@
<string name="actionbar_settings">Einstellungen</string>
<string name="actionbar_see_details">Details</string>
<string name="actionbar_send_file">Senden</string>
<string name="actionbar_sort">Sortieren</string>
<string name="actionbar_sort_title">Sortieren nach</string>
<string-array name="actionbar_sortby">
<item>Dateiname - aufsteigend</item>
<item>Dateiname - absteigend</item>
<item>Datum - aufsteigend</item>
<item>Datum - absteigend</item>
<item>Dateigröße - aufsteigend</item>
<item>Dateigröße - absteigend</item>
</string-array>
<string name="prefs_category_general">Allgemein</string>
<string name="prefs_category_more">Mehr</string>
<string name="prefs_accounts">Konten</string>

View file

@ -12,6 +12,16 @@
<string name="actionbar_settings">Settings</string>
<string name="actionbar_see_details">Details</string>
<string name="actionbar_send_file">Send</string>
<string name="actionbar_sort">Sort</string>
<string name="actionbar_sort_title">Sort by</string>
<string-array name="actionbar_sortby">
<item>Filename - ascending</item>
<item>Filename - descending</item>
<item>Date - ascending</item>
<item>Date - descending</item>
<item>Filesize - ascending</item>
<item>Filesize - descending</item>
</string-array>
<string name="prefs_category_general">General</string>
<string name="prefs_category_more">More</string>
<string name="prefs_accounts">Accounts</string>

View file

@ -446,7 +446,7 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
} else if (another.isFolder()) {
return 1;
}
return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
return new AlphanumComparator().compare(this, another);
}
@Override

View file

@ -491,6 +491,38 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener {
}
break;
}
case R.id.action_sort: {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.actionbar_sort_title)
.setItems(R.array.actionbar_sortby, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log_OC.d("default view", " number " + which + "clicked");
switch (which){
case 0:
sortByName(true);
break;
case 1:
sortByName(false);
break;
case 2:
sortByDate(true);
break;
case 3:
sortByDate(false);
break;
case 4:
sortBySize(true);
break;
case 5:
sortBySize(false);
break;
}
}
});
builder.create();
builder.show();
break;
}
default:
retval = super.onOptionsItemSelected(item);
}
@ -1725,5 +1757,17 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener {
}
}
}
private void sortByDate(boolean ascending){
getListOfFilesFragment().sortByDate(ascending);
}
private void sortBySize(boolean ascending){
getListOfFilesFragment().sortBySize(ascending);
}
private void sortByName(boolean ascending){
getListOfFilesFragment().sortByName(ascending);
}
}

View file

@ -17,6 +17,8 @@
*/
package com.owncloud.android.ui.adapter;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
import android.accounts.Account;
@ -58,6 +60,9 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
private FileDataStorageManager mStorageManager;
private Account mAccount;
private ComponentsGetter mTransferServiceGetter;
public enum sortOrders { NAME, DATE, SIZE }
private sortOrders sort = sortOrders.NAME;
private boolean sortAscending = true;
public FileListListAdapter(
boolean justFolders,
@ -243,6 +248,32 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
} else {
mFiles = null;
}
sortDirectory();
}
/**
* Sorts all filenames, regarding last user decision
*/
private void sortDirectory(){
switch (sort){
case NAME:
sortByName();
break;
case SIZE:
sortBySize();
break;
case DATE:
sortByDate();
break;
}
if (!sortAscending){
Collections.reverse(mFiles);
}
notifyDataSetChanged();
}
@ -276,4 +307,53 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
return (mFile.getPermissions() != null && !mFile.getPermissions().contains(PERMISSION_SHARED_WITH_ME)
&& file.getPermissions() != null && file.getPermissions().contains(PERMISSION_SHARED_WITH_ME));
}
private void sortByDate(){
Collections.sort(mFiles, new Comparator<OCFile>() {
public int compare(OCFile o1, OCFile o2) {
if (o1.isFolder() && o2.isFolder()) {
return Long.compare(o1.getModificationTimestamp(), o2.getModificationTimestamp());
}
else if (o1.isFolder()) {
return -1;
} else if (o2.isFolder()) {
return 1;
} else if (o1.getModificationTimestamp() == 0 || o2.getModificationTimestamp() == 0){
return 0;
} else {
return Long.compare(o1.getModificationTimestamp(), o2.getModificationTimestamp());
}
}
});
}
private void sortBySize(){
Collections.sort(mFiles, new Comparator<OCFile>() {
public int compare(OCFile o1, OCFile o2) {
if (o1.isFolder() && o2.isFolder()) {
return o1.getRemotePath().toLowerCase().compareTo(o2.getRemotePath().toLowerCase());
}
else if (o1.isFolder()) {
return -1;
} else if (o2.isFolder()) {
return 1;
} else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
return 0;
} else {
return Long.compare(o1.getFileLength(), o2.getFileLength());
}
}
});
}
private void sortByName(){
Collections.sort(mFiles);
}
public void setSortOrder(sortOrders order, boolean descending) {
sort = order;
sortAscending = descending;
sortDirectory();
}
}

View file

@ -37,6 +37,7 @@ import com.owncloud.android.files.FileMenuFilter;
import com.owncloud.android.ui.activity.FileDisplayActivity;
import com.owncloud.android.ui.activity.MoveActivity;
import com.owncloud.android.ui.adapter.FileListListAdapter;
import com.owncloud.android.ui.adapter.FileListListAdapter.sortOrders;
import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
@ -385,5 +386,16 @@ public class OCFileListFragment extends ExtendedListFragment {
mFile = directory;
}
}
public void sortByDate(boolean descending){
mAdapter.setSortOrder(sortOrders.DATE, descending);
}
public void sortBySize(boolean descending){
mAdapter.setSortOrder(sortOrders.SIZE, descending);
}
public void sortByName(boolean descending){
mAdapter.setSortOrder(sortOrders.NAME, descending);
}
}