From 2fc9d7d5c8f1a287244a005aabe54bf5d80bfded Mon Sep 17 00:00:00 2001 From: Arsentiy Milchakov Date: Fri, 29 May 2020 11:58:07 +0300 Subject: [PATCH] [guides on map] suggested zoom level is propagated into guides manager --- map/guides_manager.cpp | 10 ++-- partners_api/guides_on_map_api.cpp | 18 ++++-- partners_api/guides_on_map_api.hpp | 12 ++-- .../guides_on_map_tests.cpp | 58 ++++++++++--------- tools/python/jsons.py | 5 +- 5 files changed, 59 insertions(+), 44 deletions(-) diff --git a/map/guides_manager.cpp b/map/guides_manager.cpp index fe951ac168..dbc9ed9f09 100644 --- a/map/guides_manager.cpp +++ b/map/guides_manager.cpp @@ -165,7 +165,7 @@ void GuidesManager::RequestGuides() auto const requestNumber = ++m_requestCounter; auto const id = m_api.GetGuidesOnMap( - corners, m_zoom, + corners, m_zoom, m_shownGuides.empty(), [this, requestNumber](guides_on_map::GuidesOnMap const & guides) { if (m_state == GuidesState::Disabled || requestNumber != m_requestCounter) return; @@ -173,7 +173,7 @@ void GuidesManager::RequestGuides() m_guides = guides; m_errorRequestsCount = 0; - if (!m_guides.empty()) + if (!m_guides.m_nodes.empty()) ChangeState(GuidesState::HasData); else ChangeState(GuidesState::NoData); @@ -221,7 +221,7 @@ void GuidesManager::RequestGuides() void GuidesManager::Clear() { m_activeGuide.clear(); - m_guides.clear(); + m_guides = {}; m_errorRequestsCount = 0; UpdateGuidesMarks(); @@ -231,7 +231,7 @@ void GuidesManager::Clear() GuidesManager::GuidesGallery GuidesManager::GetGallery() const { GuidesGallery gallery; - for (auto const & guide : m_guides) + for (auto const & guide : m_guides.m_nodes) { if (guide.m_outdoorCount + guide.m_sightsCount != 1) continue; @@ -324,7 +324,7 @@ void GuidesManager::UpdateGuidesMarks() auto es = m_bmManager->GetEditSession(); es.ClearGroup(UserMark::GUIDE_CLUSTER); es.ClearGroup(UserMark::GUIDE); - for (auto & guide : m_guides) + for (auto & guide : m_guides.m_nodes) { if (guide.m_sightsCount + guide.m_outdoorCount > 1) { diff --git a/partners_api/guides_on_map_api.cpp b/partners_api/guides_on_map_api.cpp index a47701a12b..cd8c00cb70 100644 --- a/partners_api/guides_on_map_api.cpp +++ b/partners_api/guides_on_map_api.cpp @@ -24,7 +24,7 @@ void ParseGallery(std::string const & src, guides_on_map::GuidesOnMap & result) auto const size = json_array_size(dataArray); - result.reserve(size); + result.m_nodes.reserve(size); for (size_t i = 0; i < size; ++i) { guides_on_map::GuidesNode item; @@ -70,12 +70,15 @@ void ParseGallery(std::string const & src, guides_on_map::GuidesOnMap & result) FromJSONObjectOptionalField(extraObj, "ascent", info.m_ascent); } - result.emplace_back(std::move(item)); + result.m_nodes.emplace_back(std::move(item)); } + + auto const meta = json_object_get(root.get(), "meta"); + FromJSONObjectOptionalField(meta, "suggested_zoom_level", result.m_suggestedZoom); } std::string MakeGalleryUrl(std::string const & baseUrl, m2::AnyRectD::Corners const & corners, - int zoomLevel, std::string const & lang) + int zoomLevel, bool suggestZoom, std::string const & lang) { // Support empty baseUrl for opensource build. if (baseUrl.empty()) @@ -83,6 +86,8 @@ std::string MakeGalleryUrl(std::string const & baseUrl, m2::AnyRectD::Corners co url::Params params = {{"zoom_level", strings::to_string(zoomLevel)}, {"locale", lang}}; + if (suggestZoom) + params.emplace_back("suggest_zoom_level", "1"); auto const toLatLonFormatted = [](m2::PointD const & point) { @@ -124,10 +129,11 @@ void Api::SetDelegate(std::unique_ptr delegate) } base::TaskLoop::TaskId Api::GetGuidesOnMap(m2::AnyRectD::Corners const & corners, uint8_t zoomLevel, - GuidesOnMapCallback const & onSuccess, + bool suggestZoom, GuidesOnMapCallback const & onSuccess, OnError const & onError) const { - auto const url = MakeGalleryUrl(m_baseUrl, corners, zoomLevel, languages::GetCurrentNorm()); + auto const url = + MakeGalleryUrl(m_baseUrl, corners, zoomLevel, suggestZoom, languages::GetCurrentNorm()); if (url.empty()) { onSuccess({}); @@ -152,7 +158,7 @@ base::TaskLoop::TaskId Api::GetGuidesOnMap(m2::AnyRectD::Corners const & corners catch (Json::Exception const & e) { LOG(LERROR, (e.Msg(), httpResult)); - result.clear(); + result = {}; } onSuccess(result); diff --git a/partners_api/guides_on_map_api.hpp b/partners_api/guides_on_map_api.hpp index 76ffd1557c..a8e1adea28 100644 --- a/partners_api/guides_on_map_api.hpp +++ b/partners_api/guides_on_map_api.hpp @@ -41,7 +41,11 @@ struct GuidesNode GuideInfo m_guideInfo; }; -using GuidesOnMap = std::vector; +struct GuidesOnMap +{ + std::vector m_nodes; + uint8_t m_suggestedZoom = 0; +}; using GuidesOnMapCallback = platform::SafeCallback; using OnError = platform::SafeCallback; @@ -61,9 +65,9 @@ public: void SetDelegate(std::unique_ptr delegate); - base::TaskLoop::TaskId GetGuidesOnMap(m2::AnyRectD::Corners const & corners, uint8_t zoomLevel, - GuidesOnMapCallback const & onSuccess, - OnError const & onError) const; + base::TaskLoop::TaskId GetGuidesOnMap(m2::AnyRectD::Corners const & corners, uint8_t zoomLevel, + bool suggestZoom, GuidesOnMapCallback const & onSuccess, + OnError const & onError) const; private: std::unique_ptr m_delegate; diff --git a/partners_api/partners_api_tests/guides_on_map_tests.cpp b/partners_api/partners_api_tests/guides_on_map_tests.cpp index c844f49593..ae178122ac 100644 --- a/partners_api/partners_api_tests/guides_on_map_tests.cpp +++ b/partners_api/partners_api_tests/guides_on_map_tests.cpp @@ -27,10 +27,10 @@ UNIT_CLASS_TEST(AsyncGuiThread, GuidesOnMap_GetGalleryOnMap) guides_on_map::Api api("http://localhost:34568/"); api.SetDelegate(std::make_unique()); m2::AnyRectD::Corners corners = {}; - uint8_t zoomlevel = 1; + uint8_t zoomLevel = 1; guides_on_map::GuidesOnMap result{}; - api.GetGuidesOnMap(corners, zoomlevel, [&result](guides_on_map::GuidesOnMap const & gallery) + api.GetGuidesOnMap(corners, zoomLevel, true, [&result](guides_on_map::GuidesOnMap const & gallery) { result = gallery; testing::Notify(); @@ -41,31 +41,33 @@ UNIT_CLASS_TEST(AsyncGuiThread, GuidesOnMap_GetGalleryOnMap) }); testing::Wait(); - TEST_EQUAL(result.size(), 2, ()); + TEST_EQUAL(result.m_nodes.size(), 2, ()); + TEST_EQUAL(result.m_suggestedZoom, 5, ()); + } + + { + guides_on_map::Api api; + api.SetDelegate(std::make_unique()); + m2::PointD leftTop = mercator::FromLatLon(55.781177, 37.564582); + m2::PointD rightBottom = mercator::FromLatLon(55.725608, 37.699851); + m2::RectD rect(leftTop, rightBottom); + m2::AnyRectD viewport(rect); + m2::AnyRectD::Corners corners; + viewport.GetGlobalPoints(corners); + uint8_t zoomLevel = 10; + + guides_on_map::GuidesOnMap result{}; + api.GetGuidesOnMap(corners, zoomLevel, true, [&result](guides_on_map::GuidesOnMap const & gallery) + { + result = gallery; + testing::Notify(); + }, + [] + { + testing::Notify(); + }); + + testing::Wait(); + TEST(!result.m_nodes.empty(), ()); } -// TODO(a): Uncomment when server will work correct. -// -// { -// guides_on_map::Api api; -// api.SetDelegate(std::make_unique()); -// m2::PointD leftTop = mercator::FromLatLon(55.781177, 37.564582); -// m2::PointD rightBottom = mercator::FromLatLon(55.725608, 37.699851); -// m2::RectD rect(leftTop, rightBottom); -// m2::AnyRectD viewport(rect); -// uint8_t zoomlevel = 1; -// -// guides_on_map::GuidesOnMap result{}; -// api.GetGuidesOnMap(viewport, zoomlevel, [&result](guides_on_map::GuidesOnMap const & gallery) -// { -// result = gallery; -// testing::Notify(); -// }, -// [] -// { -// testing::Notify(); -// }); -// -// testing::Wait(); -// TEST(!result.empty(), ()); -// } } diff --git a/tools/python/jsons.py b/tools/python/jsons.py index 4c365b2c16..b6a01590b9 100644 --- a/tools/python/jsons.py +++ b/tools/python/jsons.py @@ -715,6 +715,9 @@ GUIDES_ON_MAP_GALLERY = """ }, "extra": null } - ] + ], + "meta": { + "suggested_zoom_level": 5 + } } """