Mimic old folder ordering in all locales. (#607) Also ensure root folder stays on top.

This commit is contained in:
dosiecki 2014-11-06 12:51:03 -08:00
parent 6cde9627f9
commit 3a536b3e9a

View file

@ -2,6 +2,7 @@ package com.newsblur.database;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -291,7 +292,7 @@ public class FolderListAdapter extends BaseExpandableListAdapter {
public synchronized void setFolderFeedMapCursor(Cursor cursor) {
if ((cursor.getCount() < 1) || (!cursor.isBeforeFirst())) return;
this.folderFeedMap = new TreeMap<String,List<String>>();
folderFeedMap = newCustomSortedMap();
while (cursor.moveToNext()) {
String folderName = getStr(cursor, DatabaseConstants.FEED_FOLDER_FOLDER_NAME);
String feedId = getStr(cursor, DatabaseConstants.FEED_FOLDER_FEED_ID);
@ -464,4 +465,24 @@ public class FolderListAdapter extends BaseExpandableListAdapter {
}
return count;
}
/**
* Custom sorting for folders. Handles the special case to keep the root
* folder on top, and also the expectation that *despite locale*, folders
* starting with an underscore should show up on top.
*/
private Map<String,List<String>> newCustomSortedMap() {
Comparator<String> c = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
if (TextUtils.equals(s1, s2)) return 0;
if (s1.equals(AppConstants.ROOT_FOLDER)) return -1;
if (s2.equals(AppConstants.ROOT_FOLDER)) return 1;
if (s1.startsWith("_")) return -1;
if (s2.startsWith("_")) return 1;
return String.CASE_INSENSITIVE_ORDER.compare(s1, s2);
}
};
return new TreeMap<String,List<String>>(c);
}
}