This commit is contained in:
Nazar-Kutz 2020-02-03 13:16:06 +02:00
parent 3e5e152933
commit 5a75243c36
7 changed files with 51 additions and 34 deletions

View file

@ -39,6 +39,7 @@ import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
@ -344,6 +345,14 @@ public class SearchUICore {
}
}
}
public void setFilterOrders(Map<String, Integer> filterOrders) {
for (SearchCoreAPI capi : apis) {
if (capi instanceof SearchAmenityTypesAPI) {
((SearchAmenityTypesAPI) capi).setFilterOrders(filterOrders);
}
}
}
public void registerAPI(SearchCoreAPI api) {
apis.add(api);

View file

@ -6,10 +6,11 @@ import net.osmand.data.Amenity;
public interface CustomSearchPoiFilter extends SearchPoiTypeFilter {
public String getFilterId();
public String getName();
public Object getIconResource();
public ResultMatcher<Amenity> wrapResultMatcher(final ResultMatcher<Amenity> matcher);
}

View file

@ -639,6 +639,7 @@ public class SearchCoreFactory {
private List<CustomSearchPoiFilter> customPoiFilters = new ArrayList<>();
private TIntArrayList customPoiFiltersPriorites = new TIntArrayList();
private MapPoiTypes types;
private Map<String, Integer> filterOrders = new HashMap<>();
public SearchAmenityTypesAPI(MapPoiTypes types) {
super(ObjectType.POI_TYPE);
@ -655,6 +656,10 @@ public class SearchCoreFactory {
this.customPoiFiltersPriorites.add(priority);
}
public void setFilterOrders(Map<String, Integer> filterOrders) {
this.filterOrders = filterOrders;
}
@Override
public boolean search(SearchPhrase phrase, SearchResultMatcher resultMatcher) throws IOException {
if (translatedNames.isEmpty()) {
@ -720,28 +725,44 @@ public class SearchCoreFactory {
phrase.setUnknownSearchWordPoiTypes(searchWordTypes);
if (resultMatcher != null) {
boolean defaultMode = !phrase.isUnknownSearchWordPresent();
String word = phrase.getUnknownSearchWord();
NameStringMatcher startMatch = new NameStringMatcher(word, StringMatcherMode.CHECK_ONLY_STARTS_WITH);
for (AbstractPoiType pt : results) {
SearchResult res = new SearchResult(phrase);
res.localeName = pt.getTranslation();
res.object = pt;
res.priority = SEARCH_AMENITY_TYPE_PRIORITY;
res.priorityDistance = 0;
res.objectType = ObjectType.POI_TYPE;
res.firstUnknownWordMatches = startMatch.matches(res.localeName);
resultMatcher.publish(res);
if (defaultMode) {
String stdFilterId = "std_" + pt.getKeyName();
if (filterOrders.containsKey(stdFilterId)) {
res.priority = filterOrders.get(stdFilterId);
resultMatcher.publish(res);
}
} else {
res.priority = SEARCH_AMENITY_TYPE_PRIORITY;
resultMatcher.publish(res);
}
}
for (int i = 0; i < customPoiFilters.size(); i++) {
CustomSearchPoiFilter csf = customPoiFilters.get(i);
int p = customPoiFiltersPriorites.get(i);
if (!phrase.isUnknownSearchWordPresent() || nm.matches(csf.getName())) {
SearchResult res = new SearchResult(phrase);
res.localeName = csf.getName();
res.object = csf;
res.priority = SEARCH_AMENITY_TYPE_PRIORITY + p;
res.objectType = ObjectType.POI_TYPE;
resultMatcher.publish(res);
if (defaultMode) {
if (filterOrders.containsKey(csf.getFilterId())) {
res.priority = filterOrders.get(csf.getFilterId());
resultMatcher.publish(res);
}
} else {
int p = customPoiFiltersPriorites.get(i);
res.priority = SEARCH_AMENITY_TYPE_PRIORITY + p;
resultMatcher.publish(res);
}
}
}
}

View file

@ -247,6 +247,15 @@ public class PoiFiltersHelper {
return result;
}
public Map<String, Integer> getPoiFilterOrders(boolean onlyActive) {
Map<String, Integer> filterOrders = new HashMap<>();
List<PoiUIFilter> sortedFilters = getSortedPoiFilters(onlyActive);
for (PoiUIFilter filter : sortedFilters) {
filterOrders.put(filter.getFilterId(), filter.getOrder());
}
return filterOrders;
}
public List<PoiUIFilter> getSortedPoiFilters(boolean onlyActive) {
initPoiUIFiltersState();
List<PoiUIFilter> allFilters = new ArrayList<>();

View file

@ -721,6 +721,7 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable<PoiUIFilter>
updatePoiAdditionals();
}
@Override
public String getFilterId() {
return filterId;
}

View file

@ -103,11 +103,7 @@ import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static net.osmand.plus.search.SendSearchQueryBottomSheet.MISSING_SEARCH_LOCATION_KEY;
import static net.osmand.plus.search.SendSearchQueryBottomSheet.MISSING_SEARCH_QUERY_KEY;
@ -1202,29 +1198,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
SearchResultCollection res = searchUICore.shallowSearch(SearchAmenityTypesAPI.class, "", null);
if (res != null) {
List<QuickSearchListItem> rows = new ArrayList<>();
List<PoiUIFilter> activePoiFilters = app.getPoiFilters().getSortedPoiFilters(true);
final Map<String, Integer> searchResultOrders = new HashMap<>();
final List<SearchResult> matchedSearchResults = new ArrayList<>();
for (SearchResult sr : res.getCurrentSearchResults()) {
for (PoiUIFilter filter : activePoiFilters) {
String localeName = sr.localeName;
if (localeName != null && localeName.equals(filter.getName())) {
activePoiFilters.remove(filter);
searchResultOrders.put(localeName, filter.getOrder());
matchedSearchResults.add(sr);
break;
}
}
}
Collections.sort(matchedSearchResults, new Comparator<SearchResult>() {
@Override
public int compare(SearchResult o1, SearchResult o2) {
int order1 = searchResultOrders.get(o1.localeName);
int order2 = searchResultOrders.get(o2.localeName);
return ((order1 < order2) ? -1 : (order1 == order2) ? 0 : 1);
}
});
for (SearchResult sr : matchedSearchResults) {
rows.add(new QuickSearchListItem(app, sr));
}
rows.add(new QuickSearchButtonListItem(app, R.drawable.ic_world_globe_dark,
@ -1261,6 +1235,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
@Override
public boolean processResult(Boolean changed) {
if (changed) {
searchUICore.setFilterOrders(app.getPoiFilters().getPoiFilterOrders(true));
reloadCategoriesInternal();
}
return false;

View file

@ -115,6 +115,7 @@ public class QuickSearchHelper implements ResourceListener {
core.addCustomSearchPoiFilter(localWikiPoiFilter, 1);
}
core.addCustomSearchPoiFilter(poiFilters.getShowAllPOIFilter(), 1);
core.setFilterOrders(poiFilters.getPoiFilterOrders(true));
}
public void setRepositoriesForSearchUICore(final OsmandApplication app) {