Search in sources :

Example 1 with QuickSearchHeaderListItem

use of net.osmand.plus.search.listitems.QuickSearchHeaderListItem in project Osmand by osmandapp.

the class QuickSearchDialogFragment method reloadCitiesInternal.

private void reloadCitiesInternal() {
    if (SearchUICore.isDebugMode()) {
        LOG.info("UI >> Start loading nearest cities");
    }
    startNearestCitySearch();
    runCoreSearch("", false, false, new SearchResultListener() {

        @Override
        public void searchStarted(SearchPhrase phrase) {
        }

        @Override
        public void publish(SearchResultCollection res, boolean append) {
        }

        @Override
        public boolean searchFinished(SearchPhrase phrase) {
            SearchResultCollection res = getResultCollection();
            if (SearchUICore.isDebugMode()) {
                LOG.info("UI >> Nearest cities found: " + (res != null ? res.getCurrentSearchResults().size() : 0));
            }
            final OsmandSettings settings = app.getSettings();
            List<QuickSearchListItem> rows = new ArrayList<>();
            if (SearchUICore.isDebugMode()) {
                LOG.info("UI >> Start last city searching (within nearests)");
            }
            SearchResult lastCity = null;
            if (res != null) {
                citiesLoaded = res.getCurrentSearchResults().size() > 0;
                final long lastCityId = settings.getLastSearchedCity();
                for (SearchResult sr : res.getCurrentSearchResults()) {
                    if (sr.objectType == ObjectType.CITY && ((City) sr.object).getId() == lastCityId) {
                        lastCity = sr;
                        break;
                    }
                }
            }
            if (SearchUICore.isDebugMode()) {
                LOG.info("UI >> Last city found: " + (lastCity != null ? lastCity.localeName : "-"));
            }
            final String lastCityName = lastCity == null ? settings.getLastSearchedCityName() : lastCity.localeName;
            if (!Algorithms.isEmpty(lastCityName)) {
                String selectStreets = app.getString(R.string.select_street);
                String inCityName = app.getString(R.string.shared_string_in_name, lastCityName);
                Spannable spannable = new SpannableString(selectStreets + " " + inCityName);
                boolean light = settings.isLightContent();
                spannable.setSpan(new ForegroundColorSpan(getResources().getColor(light ? R.color.icon_color : R.color.color_white)), selectStreets.length() + 1, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                final SearchResult lastCityFinal = lastCity;
                rows.add(new QuickSearchButtonListItem(app, R.drawable.ic_action_street_name, spannable, new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (lastCityFinal == null) {
                            final long lastCityId = settings.getLastSearchedCity();
                            final LatLon lastCityPoint = settings.getLastSearchedPoint();
                            if (lastCityId != -1 && lastCityPoint != null) {
                                startLastCitySearch(lastCityPoint);
                                if (SearchUICore.isDebugMode()) {
                                    LOG.info("UI >> Start last city searching (standalone)");
                                }
                                runCoreSearch("", false, false, new SearchResultListener() {

                                    boolean cityFound = false;

                                    @Override
                                    public void publish(SearchResultCollection res, boolean append) {
                                        if (res != null) {
                                            for (SearchResult sr : res.getCurrentSearchResults()) {
                                                if (sr.objectType == ObjectType.CITY && ((City) sr.object).getId() == lastCityId) {
                                                    if (SearchUICore.isDebugMode()) {
                                                        LOG.info("UI >> Last city found: " + sr.localeName);
                                                    }
                                                    cityFound = true;
                                                    completeQueryWithObject(sr);
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    @Override
                                    public void searchStarted(SearchPhrase phrase) {
                                    }

                                    @Override
                                    public boolean searchFinished(SearchPhrase phrase) {
                                        if (!cityFound) {
                                            replaceQueryWithText(lastCityName + " ");
                                        }
                                        return false;
                                    }
                                });
                                restoreSearch();
                            } else {
                                replaceQueryWithText(lastCityName + " ");
                            }
                        } else {
                            completeQueryWithObject(lastCityFinal);
                        }
                        openKeyboard();
                    }
                }));
            }
            rows.add(new QuickSearchButtonListItem(app, R.drawable.ic_action_building_number, app.getString(R.string.select_city), new OnClickListener() {

                @Override
                public void onClick(View v) {
                    searchEditText.setHint(R.string.type_city_town);
                    startCitySearch();
                    updateTabbarVisibility(false);
                    runCoreSearch("", false, false);
                    openKeyboard();
                }
            }));
            rows.add(new QuickSearchButtonListItem(app, R.drawable.ic_action_postcode, app.getString(R.string.select_postcode), new OnClickListener() {

                @Override
                public void onClick(View v) {
                    searchEditText.setHint(R.string.type_postcode);
                    startPostcodeSearch();
                    mainSearchFragment.getAdapter().clear();
                    updateTabbarVisibility(false);
                    openKeyboard();
                }
            }));
            rows.add(new QuickSearchButtonListItem(app, R.drawable.ic_action_marker_dark, app.getString(R.string.coords_search), new OnClickListener() {

                @Override
                public void onClick(View v) {
                    LatLon latLon = searchUICore.getSearchSettings().getOriginalLocation();
                    QuickSearchCoordinatesFragment.showDialog(QuickSearchDialogFragment.this, latLon.getLatitude(), latLon.getLongitude());
                }
            }));
            if (res != null) {
                rows.add(new QuickSearchHeaderListItem(app, app.getString(R.string.nearest_cities), true));
                int limit = 15;
                for (SearchResult sr : res.getCurrentSearchResults()) {
                    if (limit > 0) {
                        rows.add(new QuickSearchListItem(app, sr));
                    }
                    limit--;
                }
            }
            addressSearchFragment.updateListAdapter(rows, false);
            if (SearchUICore.isDebugMode()) {
                LOG.info("UI >> Nearest cities loaded");
            }
            return true;
        }
    });
    restoreSearch();
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) QuickSearchButtonListItem(net.osmand.plus.search.listitems.QuickSearchButtonListItem) QuickSearchHeaderListItem(net.osmand.plus.search.listitems.QuickSearchHeaderListItem) SearchResult(net.osmand.search.core.SearchResult) SpannableString(android.text.SpannableString) City(net.osmand.data.City) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) SearchPhrase(net.osmand.search.core.SearchPhrase) OsmandSettings(net.osmand.plus.OsmandSettings) SpannableString(android.text.SpannableString) LatLon(net.osmand.data.LatLon) SearchResultCollection(net.osmand.search.SearchUICore.SearchResultCollection) SearchMoreItemOnClickListener(net.osmand.plus.search.listitems.QuickSearchMoreListItem.SearchMoreItemOnClickListener) OnClickListener(android.view.View.OnClickListener) QuickSearchListItem(net.osmand.plus.search.listitems.QuickSearchListItem) ArrayList(java.util.ArrayList) List(java.util.List) Spannable(android.text.Spannable)

Aggregations

Spannable (android.text.Spannable)1 SpannableString (android.text.SpannableString)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 AdapterView (android.widget.AdapterView)1 ImageView (android.widget.ImageView)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 City (net.osmand.data.City)1 LatLon (net.osmand.data.LatLon)1 OsmandSettings (net.osmand.plus.OsmandSettings)1 QuickSearchButtonListItem (net.osmand.plus.search.listitems.QuickSearchButtonListItem)1 QuickSearchHeaderListItem (net.osmand.plus.search.listitems.QuickSearchHeaderListItem)1 QuickSearchListItem (net.osmand.plus.search.listitems.QuickSearchListItem)1 SearchMoreItemOnClickListener (net.osmand.plus.search.listitems.QuickSearchMoreListItem.SearchMoreItemOnClickListener)1 SearchResultCollection (net.osmand.search.SearchUICore.SearchResultCollection)1 SearchPhrase (net.osmand.search.core.SearchPhrase)1