Resort poi filters

This commit is contained in:
vshcherb 2013-11-03 17:23:11 +01:00
parent c9901e606c
commit ddc62db00e
3 changed files with 55 additions and 45 deletions

View file

@ -19,6 +19,7 @@ public class PoiFiltersHelper {
private final ClientContext application; private final ClientContext application;
private NameFinderPoiFilter nameFinderPOIFilter; private NameFinderPoiFilter nameFinderPOIFilter;
private List<PoiFilter> cacheTopStandardFilters;
private List<PoiFilter> cacheUserDefinedFilters; private List<PoiFilter> cacheUserDefinedFilters;
private List<PoiFilter> cacheOsmDefinedFilters; private List<PoiFilter> cacheOsmDefinedFilters;
@ -47,6 +48,18 @@ public class PoiFiltersHelper {
} }
private PoiFilter findPoiFilter(String filterId, List<PoiFilter>... collections) {
for(List<PoiFilter> c : collections) {
for(PoiFilter f : c) {
if(f.getFilterId().equals(filterId)){
return f;
}
}
}
return null;
}
@SuppressWarnings("unchecked")
public PoiFilter getFilterById(String filterId){ public PoiFilter getFilterById(String filterId){
if(filterId == null){ if(filterId == null){
return null; return null;
@ -54,22 +67,7 @@ public class PoiFiltersHelper {
if(filterId.equals(NameFinderPoiFilter.FILTER_ID)){ if(filterId.equals(NameFinderPoiFilter.FILTER_ID)){
return getNameFinderPOIFilter(); return getNameFinderPOIFilter();
} }
if(filterId.startsWith(PoiFilter.USER_PREFIX)){ return findPoiFilter(filterId, getUserDefinedPoiFilters(), getTopStandardFilters(), getOsmDefinedPoiFilters());
List<PoiFilter> filters = getUserDefinedPoiFilters();
for(PoiFilter f : filters){
if(f.getFilterId().equals(filterId)){
return f;
}
}
} else if(filterId.startsWith(PoiFilter.STD_PREFIX)){
List<PoiFilter> filters = getOsmDefinedPoiFilters();
for(PoiFilter f : filters){
if(f.getFilterId().equals(filterId)){
return f;
}
}
}
return null;
} }
private void putAll(Map<AmenityType, LinkedHashSet<String>> types, AmenityType tp){ private void putAll(Map<AmenityType, LinkedHashSet<String>> types, AmenityType tp){
@ -171,12 +169,6 @@ public class PoiFiltersHelper {
public List<PoiFilter> getUserDefinedPoiFilters(){ public List<PoiFilter> getUserDefinedPoiFilters(){
if(cacheUserDefinedFilters == null){ if(cacheUserDefinedFilters == null){
cacheUserDefinedFilters = new ArrayList<PoiFilter>(); cacheUserDefinedFilters = new ArrayList<PoiFilter>();
PoiFilter filter = new PoiFilter(application.getString(R.string.poi_filter_custom_filter), PoiFilter.CUSTOM_FILTER_ID,
new LinkedHashMap<AmenityType, LinkedHashSet<String>>(), application); //$NON-NLS-1$
filter.setStandardFilter(true);
cacheUserDefinedFilters.add(filter);
filter = new SearchByNameFilter(application);
cacheUserDefinedFilters.add(filter);
PoiFilterDbHelper helper = openDbHelper(); PoiFilterDbHelper helper = openDbHelper();
List<PoiFilter> userDefined = helper.getFilters(helper.getReadableDatabase()); List<PoiFilter> userDefined = helper.getFilters(helper.getReadableDatabase());
final Collator instance = Collator.getInstance(); final Collator instance = Collator.getInstance();
@ -192,6 +184,21 @@ public class PoiFiltersHelper {
return Collections.unmodifiableList(cacheUserDefinedFilters); return Collections.unmodifiableList(cacheUserDefinedFilters);
} }
public List<PoiFilter> getTopStandardFilters() {
if (cacheTopStandardFilters == null) {
cacheTopStandardFilters = new ArrayList<PoiFilter>();
PoiFilter filter = new PoiFilter(application.getString(R.string.poi_filter_custom_filter),
PoiFilter.CUSTOM_FILTER_ID, new LinkedHashMap<AmenityType, LinkedHashSet<String>>(), application); //$NON-NLS-1$
filter.setStandardFilter(true);
cacheTopStandardFilters.add(filter);
cacheTopStandardFilters.add(new PoiFilter(null, application));
filter = new SearchByNameFilter(application);
filter.setStandardFilter(true);
cacheTopStandardFilters.add(filter);
}
return Collections.unmodifiableList(cacheTopStandardFilters);
}
public static String getOsmDefinedFilterId(AmenityType t){ public static String getOsmDefinedFilterId(AmenityType t){
return PoiFilter.STD_PREFIX + t; return PoiFilter.STD_PREFIX + t;
} }
@ -216,7 +223,6 @@ public class PoiFiltersHelper {
return instance.compare(object1.getName(), object2.getName()); return instance.compare(object1.getName(), object2.getName());
} }
}); });
cacheOsmDefinedFilters.add(0, new PoiFilter(null, application));
} }
return Collections.unmodifiableList(cacheOsmDefinedFilters); return Collections.unmodifiableList(cacheOsmDefinedFilters);
} }
@ -227,7 +233,8 @@ public class PoiFiltersHelper {
public boolean removePoiFilter(PoiFilter filter){ public boolean removePoiFilter(PoiFilter filter){
if(filter.getFilterId().equals(PoiFilter.CUSTOM_FILTER_ID) || if(filter.getFilterId().equals(PoiFilter.CUSTOM_FILTER_ID) ||
filter.getFilterId().equals(PoiFilter.BY_NAME_FILTER_ID)){ filter.getFilterId().equals(PoiFilter.BY_NAME_FILTER_ID) ||
filter.getFilterId().startsWith(PoiFilter.STD_PREFIX)){
return false; return false;
} }
PoiFilterDbHelper helper = openDbHelper(); PoiFilterDbHelper helper = openDbHelper();
@ -259,7 +266,7 @@ public class PoiFiltersHelper {
public boolean editPoiFilter(PoiFilter filter) { public boolean editPoiFilter(PoiFilter filter) {
if (filter.getFilterId().equals(PoiFilter.CUSTOM_FILTER_ID) || if (filter.getFilterId().equals(PoiFilter.CUSTOM_FILTER_ID) ||
filter.getFilterId().equals(PoiFilter.BY_NAME_FILTER_ID)) { filter.getFilterId().equals(PoiFilter.BY_NAME_FILTER_ID) || filter.getFilterId().startsWith(PoiFilter.STD_PREFIX)) {
return false; return false;
} }
PoiFilterDbHelper helper = openDbHelper(); PoiFilterDbHelper helper = openDbHelper();

View file

@ -544,18 +544,18 @@ public class MapActivityLayers {
is.icon(RenderingIcons.getBigIconResourceId("null")); is.icon(RenderingIcons.getBigIconResourceId("null"));
} }
is.reg(); is.reg();
// 2nd custom
adapter.item(getString(R.string.poi_filter_custom_filter)).icon(RenderingIcons.getBigIconResourceId("user_defined")).reg();
for (PoiFilter f : poiFilters.getUserDefinedPoiFilters()) { for (PoiFilter f : poiFilters.getUserDefinedPoiFilters()) {
if(!f.getFilterId().equals(PoiFilter.BY_NAME_FILTER_ID)){ Item it = adapter.item(f.getName());
Item it = adapter.item(f.getName()); if (RenderingIcons.containsBigIcon(f.getSimplifiedId())) {
if(RenderingIcons.containsBigIcon(f.getSimplifiedId())) { it.icon(RenderingIcons.getBigIconResourceId(f.getSimplifiedId()));
it.icon(RenderingIcons.getBigIconResourceId(f.getSimplifiedId())); } else {
} else { it.icon(RenderingIcons.getBigIconResourceId("user_defined"));
it.icon(RenderingIcons.getBigIconResourceId("user_defined"));
}
it.reg();
userDefined.add(f);
} }
it.reg();
userDefined.add(f);
} }
for(AmenityType t : AmenityType.values()){ for(AmenityType t : AmenityType.values()){
Item it = adapter.item(OsmAndFormatter.toPublicString(t, activity.getMyApplication())); Item it = adapter.item(OsmAndFormatter.toPublicString(t, activity.getMyApplication()));
@ -577,15 +577,8 @@ public class MapActivityLayers {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
String filterId; if(which == 1){
if (which == 0) { String filterId = PoiFilter.CUSTOM_FILTER_ID;
filterId = PoiFiltersHelper.getOsmDefinedFilterId(null);
} else if (which <= userDefined.size()) {
filterId = userDefined.get(which - 1).getFilterId();
} else {
filterId = PoiFiltersHelper.getOsmDefinedFilterId(AmenityType.values()[which - userDefined.size() - 1]);
}
if(filterId.equals(PoiFilter.CUSTOM_FILTER_ID)){
getApplication().getSettings().setPoiFilterForMap(filterId); getApplication().getSettings().setPoiFilterForMap(filterId);
Intent newIntent = new Intent(activity, EditPOIFilterActivity.class); Intent newIntent = new Intent(activity, EditPOIFilterActivity.class);
newIntent.putExtra(EditPOIFilterActivity.AMENITY_FILTER, filterId); newIntent.putExtra(EditPOIFilterActivity.AMENITY_FILTER, filterId);
@ -593,9 +586,17 @@ public class MapActivityLayers {
newIntent.putExtra(EditPOIFilterActivity.SEARCH_LON, mapView.getLongitude()); newIntent.putExtra(EditPOIFilterActivity.SEARCH_LON, mapView.getLongitude());
activity.startActivity(newIntent); activity.startActivity(newIntent);
} else { } else {
String filterId;
if (which == 0) {
filterId = PoiFiltersHelper.getOsmDefinedFilterId(null);
} else if (which <= userDefined.size()) {
filterId = userDefined.get(which - 2).getFilterId();
} else {
filterId = PoiFiltersHelper.getOsmDefinedFilterId(AmenityType.values()[which - userDefined.size() - 2]);
}
getApplication().getSettings().setPoiFilterForMap(filterId); getApplication().getSettings().setPoiFilterForMap(filterId);
PoiFilter f = poiFilters.getFilterById(filterId); PoiFilter f = poiFilters.getFilterById(filterId);
if(f != null){ if (f != null) {
f.clearNameFilter(); f.clearNameFilter();
} }
poiMapLayer.setFilter(f); poiMapLayer.setFilter(f);

View file

@ -60,7 +60,9 @@ public class SearchPoiFilterActivity extends SherlockListFragment implements Se
}); });
PoiFiltersHelper poiFilters = getApp().getPoiFilters(); PoiFiltersHelper poiFilters = getApp().getPoiFilters();
List<PoiFilter> filters = new ArrayList<PoiFilter>(poiFilters.getUserDefinedPoiFilters()) ; List<PoiFilter> filters = new ArrayList<PoiFilter>() ;
filters.addAll(poiFilters.getTopStandardFilters());
filters.addAll(poiFilters.getUserDefinedPoiFilters());
filters.addAll(poiFilters.getOsmDefinedPoiFilters()); filters.addAll(poiFilters.getOsmDefinedPoiFilters());
filters.add(poiFilters.getNameFinderPOIFilter()); filters.add(poiFilters.getNameFinderPOIFilter());
setListAdapter(new AmenityAdapter(filters)); setListAdapter(new AmenityAdapter(filters));