[guides on map] suggested zoom level is propagated into guides manager

This commit is contained in:
Arsentiy Milchakov 2020-05-29 11:58:07 +03:00 committed by Daria Volvenkova
parent cda4b6b88f
commit 2fc9d7d5c8
5 changed files with 59 additions and 44 deletions

View file

@ -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)
{

View file

@ -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> 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);

View file

@ -41,7 +41,11 @@ struct GuidesNode
GuideInfo m_guideInfo;
};
using GuidesOnMap = std::vector<GuidesNode>;
struct GuidesOnMap
{
std::vector<GuidesNode> m_nodes;
uint8_t m_suggestedZoom = 0;
};
using GuidesOnMapCallback = platform::SafeCallback<void(GuidesOnMap const & gallery)>;
using OnError = platform::SafeCallback<void()>;
@ -61,9 +65,9 @@ public:
void SetDelegate(std::unique_ptr<Delegate> 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<Delegate> m_delegate;

View file

@ -27,10 +27,10 @@ UNIT_CLASS_TEST(AsyncGuiThread, GuidesOnMap_GetGalleryOnMap)
guides_on_map::Api api("http://localhost:34568/");
api.SetDelegate(std::make_unique<DelegateForTesting>());
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<DelegateForTesting>());
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<DelegateForTesting>());
// 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(), ());
// }
}

View file

@ -715,6 +715,9 @@ GUIDES_ON_MAP_GALLERY = """
},
"extra": null
}
]
],
"meta": {
"suggested_zoom_level": 5
}
}
"""