From 941f9aa0c85eff7f604bda62092a2bae9293915d Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 7 Jun 2017 17:14:44 +0900 Subject: [PATCH] Always sort folders (fixes #902) --- .../fragments/FolderListFragment.java | 13 ------------- .../syncthingandroid/service/RestApi.java | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderListFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderListFragment.java index 9afbd30b..f73bf7e4 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderListFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderListFragment.java @@ -16,8 +16,6 @@ import com.nutomic.syncthingandroid.model.Folder; import com.nutomic.syncthingandroid.service.SyncthingService; import com.nutomic.syncthingandroid.views.FoldersAdapter; -import java.util.Collections; -import java.util.Comparator; import java.util.List; import java.util.Timer; import java.util.TimerTask; @@ -28,16 +26,6 @@ import java.util.TimerTask; public class FolderListFragment extends ListFragment implements SyncthingService.OnApiChangeListener, AdapterView.OnItemClickListener { - /** - * Compares folders by labels, uses the folder ID as fallback if the label is empty - */ - private final static Comparator FOLDERS_COMPARATOR = (lhs, rhs) -> { - String lhsLabel = lhs.label != null && !lhs.label.isEmpty() ? lhs.label : lhs.id; - String rhsLabel = rhs.label != null && !rhs.label.isEmpty() ? rhs.label : rhs.id; - - return lhsLabel.compareTo(rhsLabel); - }; - private FoldersAdapter mAdapter; private Timer mTimer; @@ -96,7 +84,6 @@ public class FolderListFragment extends ListFragment implements SyncthingService mAdapter.setNotifyOnChange(false); mAdapter.clear(); List folders = activity.getApi().getFolders(); - Collections.sort(folders, FOLDERS_COMPARATOR); mAdapter.addAll(folders); mAdapter.updateModel(activity.getApi()); mAdapter.notifyDataSetChanged(); diff --git a/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java index 77836022..b4141901 100644 --- a/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java @@ -34,6 +34,8 @@ import com.nutomic.syncthingandroid.util.FolderObserver; import java.lang.reflect.Type; import java.net.URL; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -48,6 +50,16 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, private static final String TAG = "RestApi"; + /** + * Compares folders by labels, uses the folder ID as fallback if the label is empty + */ + private final static Comparator FOLDERS_COMPARATOR = (lhs, rhs) -> { + String lhsLabel = lhs.label != null && !lhs.label.isEmpty() ? lhs.label : lhs.id; + String rhsLabel = rhs.label != null && !rhs.label.isEmpty() ? rhs.label : rhs.id; + + return lhsLabel.compareTo(rhsLabel); + }; + public interface OnConfigChangedListener { void onConfigChanged(); } @@ -200,7 +212,9 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, } public List getFolders() { - return deepCopy(mConfig.folders, new TypeToken>(){}.getType()); + List folders = deepCopy(mConfig.folders, new TypeToken>(){}.getType()); + Collections.sort(folders, FOLDERS_COMPARATOR); + return folders; } public void addFolder(Folder folder) {