Background search of custom poi types

This commit is contained in:
max-klaus 2020-06-03 22:02:27 +03:00
parent a9bdd48bb7
commit 2f1531f337

View file

@ -37,33 +37,35 @@ import androidx.fragment.app.FragmentManager;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.Collator; import net.osmand.Collator;
import net.osmand.OsmAndCollator; import net.osmand.OsmAndCollator;
import net.osmand.ResultMatcher;
import net.osmand.osm.PoiCategory; import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiType; import net.osmand.osm.PoiType;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.poi.PoiFiltersHelper; import net.osmand.plus.poi.PoiFiltersHelper;
import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.poi.PoiUIFilter;
import net.osmand.plus.render.RenderingIcons; import net.osmand.plus.render.RenderingIcons;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.search.SearchUICore; import net.osmand.search.SearchUICore;
import net.osmand.search.core.SearchCoreFactory; import net.osmand.search.SearchUICore.SearchResultCollection;
import net.osmand.search.core.ObjectType;
import net.osmand.search.core.SearchResult; import net.osmand.search.core.SearchResult;
import net.osmand.search.core.SearchSettings;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static net.osmand.search.core.ObjectType.SEARCH_FINISHED;
public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFiltersSelectedListener { public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFiltersSelectedListener {
public static final String TAG = "QuickSearchCustomPoiFragment"; public static final String TAG = "QuickSearchCustomPoiFragment";
@ -93,6 +95,7 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
private ProgressBar searchProgressBar; private ProgressBar searchProgressBar;
private ImageView searchCloseIcon; private ImageView searchCloseIcon;
private SearchUICore searchUICore; private SearchUICore searchUICore;
private boolean searchCancelled;
private Collator collator; private Collator collator;
public QuickSearchCustomPoiFragment() { public QuickSearchCustomPoiFragment() {
@ -305,6 +308,7 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
private void searchSubCategory(String text) { private void searchSubCategory(String text) {
if (text.isEmpty()) { if (text.isEmpty()) {
cancelSearchSubCategories();
if (subCategoriesAdapter.getSelectedItems().isEmpty()) { if (subCategoriesAdapter.getSelectedItems().isEmpty()) {
listView.setAdapter(categoryListAdapter); listView.setAdapter(categoryListAdapter);
categoryListAdapter.notifyDataSetChanged(); categoryListAdapter.notifyDataSetChanged();
@ -314,37 +318,64 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
} else { } else {
listView.setAdapter(subCategoriesAdapter); listView.setAdapter(subCategoriesAdapter);
subCategoriesAdapter.clear(); subCategoriesAdapter.clear();
subCategoriesAdapter.addAll(new ArrayList<>(subCategoriesAdapter.getSelectedItems())); subCategoriesAdapter.addAll(subCategoriesAdapter.getSelectedItems());
subCategoriesAdapter.notifyDataSetChanged(); subCategoriesAdapter.notifyDataSetChanged();
removeAllHeaders(); removeAllHeaders();
listView.addHeaderView(headerShadow, null, false); listView.addHeaderView(headerShadow, null, false);
setupAddButton(); setupAddButton();
} }
} else { } else {
updateCloseSearchIcon(true);
subCategoriesAdapter.setSelectedItems(getSelectedSubCategories());
startSearchSubCategories(text); startSearchSubCategories(text);
} }
} }
private void startSearchSubCategories(String text) { private void cancelSearchSubCategories() {
try { searchCancelled = true;
SearchUICore.SearchResultCollection res = searchUICore.shallowSearch(SearchCoreFactory.SearchAmenityTypesAPI.class, text, null); searchUICore.getSearchSettings().resetSearchTypes();
List<PoiType> results = new ArrayList<>(); updateCloseSearchIcon(false);
for (SearchResult result : res.getCurrentSearchResults()) { }
Object poiObject = result.object;
if (poiObject instanceof PoiType) { private void startSearchSubCategories(final String text) {
PoiType poiType = (PoiType) poiObject; updateCloseSearchIcon(true);
if (!poiType.isAdditional()) { searchCancelled = false;
results.add(poiType); SearchSettings searchSettings = searchUICore.getSearchSettings().setSearchTypes(ObjectType.POI_TYPE);
searchUICore.updateSettings(searchSettings);
searchUICore.search(text, true, new ResultMatcher<SearchResult>() {
@Override
public boolean publish(SearchResult searchResult) {
if (searchResult.objectType == SEARCH_FINISHED) {
final List<PoiType> selectedSubCategories = getSelectedSubCategories();
SearchResultCollection resultCollection = searchUICore.getCurrentSearchResult();
final List<PoiType> results = new ArrayList<>();
for (SearchResult result : resultCollection.getCurrentSearchResults()) {
Object poiObject = result.object;
if (poiObject instanceof PoiType) {
PoiType poiType = (PoiType) poiObject;
if (!poiType.isAdditional()) {
results.add(poiType);
}
}
} }
app.runInUIThread(new Runnable() {
@Override
public void run() {
searchUICore.getSearchSettings().resetSearchTypes();
if (!searchCancelled) {
subCategoriesAdapter.setSelectedItems(selectedSubCategories);
showSearchResults(results);
}
updateCloseSearchIcon(false);
}
});
} }
return true;
} }
showSearchResults(results);
} catch (IOException e) { @Override
app.showToastMessage(e.getMessage()); public boolean isCancelled() {
updateCloseSearchIcon(false); return searchCancelled;
} }
});
} }
private void showSearchResults(List<PoiType> poiTypes) { private void showSearchResults(List<PoiType> poiTypes) {
@ -355,7 +386,6 @@ public class QuickSearchCustomPoiFragment extends DialogFragment implements OnFi
removeAllHeaders(); removeAllHeaders();
listView.addHeaderView(headerShadow, null, false); listView.addHeaderView(headerShadow, null, false);
setupAddButton(); setupAddButton();
updateCloseSearchIcon(false);
} }
private List<PoiType> getSelectedSubCategories() { private List<PoiType> getSelectedSubCategories() {