From cff35512bd2141ef4c9a0b8af39837141a460b5c Mon Sep 17 00:00:00 2001 From: Logan Date: Mon, 6 Jul 2015 12:04:00 +0800 Subject: [PATCH 1/2] refactor --- src/com/seafile/seadroid2/data/SeafGroup.java | 14 +++++------ .../seadroid2/ui/adapter/SeafItemAdapter.java | 25 ++++++++++--------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/com/seafile/seadroid2/data/SeafGroup.java b/src/com/seafile/seadroid2/data/SeafGroup.java index 6dad55c26..0387df214 100644 --- a/src/com/seafile/seadroid2/data/SeafGroup.java +++ b/src/com/seafile/seadroid2/data/SeafGroup.java @@ -44,16 +44,14 @@ public void addIfAbsent(SeafRepo repo) { */ public void sortByType(int type, int order) { if (type == SeafItemAdapter.SORT_BY_NAME) { - if (order == SeafItemAdapter.SORT_ORDER_ASCENDING) { - Collections.sort(repos, new RepoNameComparator()); - } else if (order == SeafItemAdapter.SORT_ORDER_DESCENDING) { - Collections.sort(repos, Collections.reverseOrder(new RepoNameComparator())); + Collections.sort(repos, new RepoNameComparator()); + if (order == SeafItemAdapter.SORT_ORDER_DESCENDING) { + Collections.reverse(repos); } } else if (type == SeafItemAdapter.SORT_BY_LAST_MODIFIED_TIME) { - if (order == SeafItemAdapter.SORT_ORDER_ASCENDING) { - Collections.sort(repos, new RepoLastMTimeComparator()); - } else if (order == SeafItemAdapter.SORT_ORDER_DESCENDING) { - Collections.sort(repos, Collections.reverseOrder(new RepoLastMTimeComparator())); + Collections.sort(repos, new RepoLastMTimeComparator()); + if (order == SeafItemAdapter.SORT_ORDER_DESCENDING) { + Collections.reverse(repos); } } } diff --git a/src/com/seafile/seadroid2/ui/adapter/SeafItemAdapter.java b/src/com/seafile/seadroid2/ui/adapter/SeafItemAdapter.java index 93433a015..6f49d40cf 100644 --- a/src/com/seafile/seadroid2/ui/adapter/SeafItemAdapter.java +++ b/src/com/seafile/seadroid2/ui/adapter/SeafItemAdapter.java @@ -1,6 +1,7 @@ package com.seafile.seadroid2.ui.adapter; import android.content.res.Resources; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; @@ -625,20 +626,20 @@ public void sortFiles(int type, int order) { // sort SeafDirents if (type == SORT_BY_NAME) { - if (order == SORT_ORDER_ASCENDING) { - Collections.sort(folders, new SeafDirent.DirentNameComparator()); - Collections.sort(files, new SeafDirent.DirentNameComparator()); - } else if (order == SORT_ORDER_DESCENDING) { - Collections.sort(folders, Collections.reverseOrder(new SeafDirent.DirentNameComparator())); - Collections.sort(files, Collections.reverseOrder(new SeafDirent.DirentNameComparator())); + // sort by name, in ascending order + Collections.sort(folders, new SeafDirent.DirentNameComparator()); + Collections.sort(files, new SeafDirent.DirentNameComparator()); + if (order == SORT_ORDER_DESCENDING) { + Collections.reverse(folders); + Collections.reverse(files); } } else if (type == SORT_BY_LAST_MODIFIED_TIME) { - if (order == SORT_ORDER_ASCENDING) { - Collections.sort(folders, new SeafDirent.DirentLastMTimeComparator()); - Collections.sort(files, new SeafDirent.DirentLastMTimeComparator()); - } else if (order == SORT_ORDER_DESCENDING) { - Collections.sort(folders, Collections.reverseOrder(new SeafDirent.DirentLastMTimeComparator())); - Collections.sort(files, Collections.reverseOrder(new SeafDirent.DirentLastMTimeComparator())); + // sort by last modified time, in ascending order + Collections.sort(folders, new SeafDirent.DirentLastMTimeComparator()); + Collections.sort(files, new SeafDirent.DirentLastMTimeComparator()); + if (order == SORT_ORDER_DESCENDING) { + Collections.reverse(folders); + Collections.reverse(files); } } // Adds the objects in the specified collection to this ArrayList From 50009f9f87bf1092d48029c69316fc081e4ea143 Mon Sep 17 00:00:00 2001 From: Logan Date: Mon, 6 Jul 2015 14:36:37 +0800 Subject: [PATCH 2/2] compare SeafItems correctly --- .../seafile/seadroid2/data/SeafDirent.java | 2 +- src/com/seafile/seadroid2/data/SeafGroup.java | 26 ++----------------- src/com/seafile/seadroid2/data/SeafRepo.java | 23 ++++++++++++++++ 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/com/seafile/seadroid2/data/SeafDirent.java b/src/com/seafile/seadroid2/data/SeafDirent.java index 0874fa70d..c054c26d5 100644 --- a/src/com/seafile/seadroid2/data/SeafDirent.java +++ b/src/com/seafile/seadroid2/data/SeafDirent.java @@ -86,7 +86,7 @@ public static class DirentNameComparator implements Comparator { @Override public int compare(SeafDirent itemA, SeafDirent itemB) { - return itemB.name.toLowerCase().compareTo(itemA.name.toLowerCase()); + return itemA.name.toLowerCase().compareTo(itemB.name.toLowerCase()); } } } diff --git a/src/com/seafile/seadroid2/data/SeafGroup.java b/src/com/seafile/seadroid2/data/SeafGroup.java index 0387df214..ea17b723a 100644 --- a/src/com/seafile/seadroid2/data/SeafGroup.java +++ b/src/com/seafile/seadroid2/data/SeafGroup.java @@ -44,38 +44,16 @@ public void addIfAbsent(SeafRepo repo) { */ public void sortByType(int type, int order) { if (type == SeafItemAdapter.SORT_BY_NAME) { - Collections.sort(repos, new RepoNameComparator()); + Collections.sort(repos, new SeafRepo.RepoNameComparator()); if (order == SeafItemAdapter.SORT_ORDER_DESCENDING) { Collections.reverse(repos); } } else if (type == SeafItemAdapter.SORT_BY_LAST_MODIFIED_TIME) { - Collections.sort(repos, new RepoLastMTimeComparator()); + Collections.sort(repos, new SeafRepo.RepoLastMTimeComparator()); if (order == SeafItemAdapter.SORT_ORDER_DESCENDING) { Collections.reverse(repos); } } } - /** - * Repository last modified time comparator class - */ - private class RepoLastMTimeComparator implements Comparator { - - @Override - public int compare(SeafRepo itemA, SeafRepo itemB) { - return (int) (itemB.mtime - itemA.mtime); - } - } - - /** - * Repository name comparator class - */ - private class RepoNameComparator implements Comparator { - - @Override - public int compare(SeafRepo itemA, SeafRepo itemB) { - return itemA.name.toLowerCase().compareTo(itemB.name.toLowerCase()); - } - } - } diff --git a/src/com/seafile/seadroid2/data/SeafRepo.java b/src/com/seafile/seadroid2/data/SeafRepo.java index 670a281bf..b2922d22e 100644 --- a/src/com/seafile/seadroid2/data/SeafRepo.java +++ b/src/com/seafile/seadroid2/data/SeafRepo.java @@ -1,5 +1,6 @@ package com.seafile.seadroid2.data; +import java.util.Comparator; import java.util.Date; import android.util.Log; @@ -83,4 +84,26 @@ public int getIcon() { public boolean hasWritePermission() { return permission.indexOf('w') != -1; } + + /** + * Repository last modified time comparator class + */ + public static class RepoLastMTimeComparator implements Comparator { + + @Override + public int compare(SeafRepo itemA, SeafRepo itemB) { + return (int) (itemA.mtime - itemB.mtime); + } + } + + /** + * Repository name comparator class + */ + public static class RepoNameComparator implements Comparator { + + @Override + public int compare(SeafRepo itemA, SeafRepo itemB) { + return itemA.name.toLowerCase().compareTo(itemB.name.toLowerCase()); + } + } }