From 73fd1307c27453877353249348e290095f89caec Mon Sep 17 00:00:00 2001 From: Dmitry Kunin Date: Mon, 24 Feb 2014 12:59:27 +0300 Subject: [PATCH] Separate cleaner logic for single/multiple search result visualization. --- android/jni/com/mapswithme/maps/Framework.cpp | 10 ++- android/jni/com/mapswithme/maps/Framework.hpp | 5 +- .../com/mapswithme/maps/SearchActivity.cpp | 31 +++++--- .../com/mapswithme/maps/SearchActivity.java | 32 ++++++-- map/framework.cpp | 79 +++++++++++-------- map/framework.hpp | 4 +- 6 files changed, 101 insertions(+), 60 deletions(-) diff --git a/android/jni/com/mapswithme/maps/Framework.cpp b/android/jni/com/mapswithme/maps/Framework.cpp index cdd52f8c1e..80123881d9 100644 --- a/android/jni/com/mapswithme/maps/Framework.cpp +++ b/android/jni/com/mapswithme/maps/Framework.cpp @@ -437,10 +437,16 @@ namespace android m_mask = mask; } - void Framework::ShowSearchResult(search::Result const & r, int index) + void Framework::ShowSearchResult(search::Result const & r) { m_doLoadState = false; - m_work.ShowSearchResult(r, index); + m_work.ShowSearchResult(r); + } + + void Framework::ShowAllSearchResults() + { + m_doLoadState = false; + m_work.ShowAllSearchResults(); } void Framework::CleanSearchLayerOnMap() diff --git a/android/jni/com/mapswithme/maps/Framework.hpp b/android/jni/com/mapswithme/maps/Framework.hpp index e650d37b13..0213724869 100644 --- a/android/jni/com/mapswithme/maps/Framework.hpp +++ b/android/jni/com/mapswithme/maps/Framework.hpp @@ -89,8 +89,9 @@ namespace android void Touch(int action, int mask, double x1, double y1, double x2, double y2); /// Show rect from another activity. Ensure that no LoadState will be called, - /// when maim map activity will become active. - void ShowSearchResult(search::Result const & r, int index); + /// when main map activity will become active. + void ShowSearchResult(search::Result const & r); + void ShowAllSearchResults(); bool Search(search::SearchParams const & params); string GetLastSearchQuery() { return m_searchQuery; } diff --git a/android/jni/com/mapswithme/maps/SearchActivity.cpp b/android/jni/com/mapswithme/maps/SearchActivity.cpp index 262741d954..9e1f4a34a2 100644 --- a/android/jni/com/mapswithme/maps/SearchActivity.cpp +++ b/android/jni/com/mapswithme/maps/SearchActivity.cpp @@ -157,19 +157,18 @@ public: void ShowItem(int position) { - // 0 -- show all - // 1 -- first - // - // ... - // n -- last - const int positionInResults = position - 1; - if (CheckPosition(positionInResults)) - { - if (position == 0) // all result, first arguemnt does not matter - g_framework->ShowSearchResult(m_results.GetResult(0), positionInResults); - else - g_framework->ShowSearchResult(m_results.GetResult(positionInResults), positionInResults); - } + if (CheckPosition(position)) + g_framework->ShowSearchResult(m_results.GetResult(position)); + else + LOG(LERROR, ("Invalid position", position)); + } + + void ShowAllResults() + { + if (m_results.GetCount() > 0) + g_framework->ShowAllSearchResults(); + else + LOG(LERROR, ("There is no results to show.")); } search::Result const * GetResult(int position, int resultID) @@ -223,6 +222,12 @@ Java_com_mapswithme_maps_SearchActivity_nativeShowItem(JNIEnv * env, jobject thi SearchAdapter::Instance().ShowItem(position); } +JNIEXPORT void JNICALL +Java_com_mapswithme_maps_SearchActivity_nativeShowAllSearchResults(JNIEnv * env, jclass clazz) +{ + SearchAdapter::Instance().ShowAllResults(); +} + JNIEXPORT jobject JNICALL Java_com_mapswithme_maps_SearchActivity_nativeGetResult( JNIEnv * env, jobject thiz, jint position, jint queryID, diff --git a/android/src/com/mapswithme/maps/SearchActivity.java b/android/src/com/mapswithme/maps/SearchActivity.java index 342d61f10b..8d9953e806 100644 --- a/android/src/com/mapswithme/maps/SearchActivity.java +++ b/android/src/com/mapswithme/maps/SearchActivity.java @@ -393,23 +393,37 @@ public class SearchActivity extends MapsWithMeBaseListActivity implements Locati } else { - final SearchResult r = m_context.getResult(position, m_resultID); - if (r != null) + // TODO handle all cases + if (m_count > 0) { - if (r.m_type == 1) + // Show all items was clicked + if (position == 0) { - // show country and close activity - SearchActivity.nativeShowItem(position); + SearchActivity.nativeShowAllSearchResults(); return null; } - else + + // Specific result was clicked + final int resIndex = position - 1; + final SearchResult r = m_context.getResult(resIndex, m_resultID); + if (r != null) { - // advise suggestion - return r.m_name + ' '; + if (r.m_type == 1) + { + // show country and close activity + SearchActivity.nativeShowItem(resIndex); + return null; + } + else + { + // advise suggestion + return r.m_name + ' '; + } } } } + // close activity in case of any error return null; } @@ -848,7 +862,9 @@ public class SearchActivity extends MapsWithMeBaseListActivity implements Locati private native boolean nativeRunSearch(String s, String lang, double lat, double lon, int flags, int searchMode, int queryID); + private static native void nativeShowItem(int position); + private static native void nativeShowAllSearchResults(); private native String getCountryNameIfAbsent(double lat, double lon); private native String getViewportCountryNameIfAbsent(); diff --git a/map/framework.cpp b/map/framework.cpp index 2cd5ba444e..fde1620dd9 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -1221,43 +1221,19 @@ bool Framework::GetCurrentPosition(double & lat, double & lon) const else return false; } -void Framework::ShowSearchResult(search::Result const & res, int index) +void Framework::ShowSearchResult(search::Result const & res) { m_bmManager.AdditionalPoiLayerSetVisible(); m_bmManager.AdditionalPoiLayerClear(); - if (index == -1) // That means show all results + LOG(LDEBUG, ("MwmSearch", "Single result", res.GetString())); + m_bmManager.AdditionalPoiLayerAddPoi(Bookmark(res.GetFeatureCenter(), res.GetString(), "api_pin")); + + int scale; + m2::PointD center; + + switch (res.GetResultType()) { - search::Results searchRes; - GetSearchEngine()->GetResults(searchRes); - - LOG(LDEBUG, ("MwmSearch", "All result,", index)); - - m2::RectD resultsRect(searchRes.GetResult(0).GetFeatureCenter(), - searchRes.GetResult(1).GetFeatureCenter()); - - for (size_t i = 0; i < searchRes.GetCount(); ++i) - { - search::Result const & tmpRes = searchRes.GetResult(i); - m_bmManager.AdditionalPoiLayerAddPoi(Bookmark(tmpRes.GetFeatureCenter(), tmpRes.GetString(), "api_pin")); - - if (i > 1) - resultsRect.Add(tmpRes.GetFeatureCenter()); - } - - ShowRectEx(resultsRect); - } - else // single result - { - m_bmManager.AdditionalPoiLayerAddPoi(Bookmark(res.GetFeatureCenter(), res.GetString(), "api_pin")); - - LOG(LDEBUG, ("MwmSearch", "Single result,", index)); - - int scale; - m2::PointD center; - - switch (res.GetResultType()) - { case search::Result::RESULT_FEATURE: { FeatureID const id = res.GetFeatureID(); @@ -1279,10 +1255,45 @@ void Framework::ShowSearchResult(search::Result const & res, int index) default: ASSERT(false, ()); return; - } - ShowRectExVisibleScale(m_scales.GetRectForDrawScale(scale, center)); } + ShowRectExVisibleScale(m_scales.GetRectForDrawScale(scale, center)); + StopLocationFollow(); +} + +void Framework::ShowAllSearchResults() +{ + m_bmManager.AdditionalPoiLayerSetVisible(); + m_bmManager.AdditionalPoiLayerClear(); + + search::Results searchRes; + GetSearchEngine()->GetResults(searchRes); + + LOG(LDEBUG, ("MwmSearch", "All result", searchRes.GetCount())); + + // @todo what to do if we have less then 2 results? + // as quick fix, use signe result function for that + if (searchRes.GetCount() == 1) + { + ShowSearchResult(searchRes.GetResult(0)); + return; + } + + ASSERT_GREATER_OR_EQUAL(searchRes.GetCount(), 2, ()); + + m2::RectD resultsRect(searchRes.GetResult(0).GetFeatureCenter(), + searchRes.GetResult(1).GetFeatureCenter()); + + for (size_t i = 0; i < searchRes.GetCount(); ++i) + { + search::Result const & tmpRes = searchRes.GetResult(i); + m_bmManager.AdditionalPoiLayerAddPoi(Bookmark(tmpRes.GetFeatureCenter(), tmpRes.GetString(), "api_pin")); + + if (i > 1) + resultsRect.Add(tmpRes.GetFeatureCenter()); + } + + ShowRectEx(resultsRect); StopLocationFollow(); } diff --git a/map/framework.hpp b/map/framework.hpp index 496d7be574..43cc2eb9ba 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -278,7 +278,9 @@ public: void PrepareSearch(bool hasPt, double lat = 0.0, double lon = 0.0); bool Search(search::SearchParams const & params); bool GetCurrentPosition(double & lat, double & lon) const; - void ShowSearchResult(search::Result const & res, int index); + + void ShowSearchResult(search::Result const & res); + void ShowAllSearchResults(); /// Calculate distance and direction to POI for the given position. /// @param[in] point POI's position;