From e4e56e9d00eee9b14100b41b0da539c29841e280 Mon Sep 17 00:00:00 2001 From: Arsentiy Milchakov Date: Tue, 18 Jul 2017 14:43:20 +0300 Subject: [PATCH] [core][search] optional cian category in search --- map/framework.cpp | 23 ++++++++++++++++++++++ map/framework.hpp | 5 +---- search/displayed_categories.cpp | 34 ++++++++++++++++++++++++--------- search/displayed_categories.hpp | 17 +++++++++-------- 4 files changed, 58 insertions(+), 21 deletions(-) diff --git a/map/framework.cpp b/map/framework.cpp index 00a533083d..c8dbbe5e28 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -1568,6 +1568,29 @@ Framework::DoAfterUpdate Framework::ToDoAfterUpdate() const : DoAfterUpdate::AutoupdateMaps; } +search::DisplayedCategories const & Framework::GetDisplayedCategories() +{ + ASSERT(m_displayedCategories, ()); + ASSERT(m_cityFinder, ()); + + ms::LatLon latlon; + GetCurrentPosition(latlon.lat, latlon.lon); + auto const city = m_cityFinder->GetCityName(MercatorBounds::FromLatLon(latlon), + StringUtf8Multilang::kEnglishCode); + + static string const kCian = "cian"; + + bool contains = m_displayedCategories->Contains(kCian); + bool supported = cian::Api::IsCitySupported(city); + + if (supported && !contains) + m_displayedCategories->InsertKey(kCian, 4); + else if (!supported && contains) + m_displayedCategories->RemoveKey(kCian); + + return *m_displayedCategories; +} + bool Framework::Search(search::SearchParams const & params) { if (ParseDrapeDebugCommand(params.m_query)) diff --git a/map/framework.hpp b/map/framework.hpp index 4333ee0193..a64cb747ed 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -304,10 +304,7 @@ public: storage::Storage & GetStorage() { return m_storage; } storage::Storage const & GetStorage() const { return m_storage; } - search::DisplayedCategories const & GetDisplayedCategories() const - { - return *m_displayedCategories; - } + search::DisplayedCategories const & GetDisplayedCategories(); storage::CountryInfoGetter & GetCountryInfoGetter() { return *m_infoGetter; } StorageDownloadingPolicy & GetDownloadingPolicy() { return m_storageDownloadingPolicy; } diff --git a/search/displayed_categories.cpp b/search/displayed_categories.cpp index 775dd25804..e72799bdf5 100644 --- a/search/displayed_categories.cpp +++ b/search/displayed_categories.cpp @@ -1,16 +1,32 @@ #include "search/displayed_categories.hpp" -namespace -{ -vector const kKeys = {"food", "hotel", "tourism", "wifi", "transport", "fuel", - "parking", "shop", "atm", "bank", "entertainment", "hospital", - "pharmacy", "police", "toilet", "post"}; -} // namespace +#include "base/macros.hpp" + +#include namespace search { -DisplayedCategories::DisplayedCategories(CategoriesHolder const & holder) : m_holder(holder) {} +DisplayedCategories::DisplayedCategories(CategoriesHolder const & holder) : m_holder(holder) +{ + m_keys = {"food", "hotel", "tourism", "wifi", "transport", "fuel", "parking", "shop", + "atm", "bank", "entertainment", "hospital", "pharmacy", "police", "toilet", "post"}; +} -// static -vector const & DisplayedCategories::GetKeys() { return kKeys; } +std::vector const & DisplayedCategories::GetKeys() const { return m_keys; } + +void DisplayedCategories::InsertKey(std::string const & key, size_t pos) +{ + CHECK_LESS(pos, m_keys.size(), ()); + m_keys.insert(m_keys.cbegin() + pos, key); +} + +void DisplayedCategories::RemoveKey(std::string const & key) +{ + m_keys.erase(std::remove(m_keys.begin(), m_keys.end(), key), m_keys.end()); +} + +bool DisplayedCategories::Contains(std::string const & key) const +{ + return std::find(m_keys.cbegin(), m_keys.cend(), key) != m_keys.cend(); +} } // namespace search diff --git a/search/displayed_categories.hpp b/search/displayed_categories.hpp index 8f3cbd614c..7127a38890 100644 --- a/search/displayed_categories.hpp +++ b/search/displayed_categories.hpp @@ -2,8 +2,8 @@ #include "indexer/categories_holder.hpp" -#include "std/string.hpp" -#include "std/vector.hpp" +#include +#include namespace search { @@ -12,16 +12,16 @@ class DisplayedCategories public: DisplayedCategories(CategoriesHolder const & holder); - // Returns a list of English names of displayed categories for the - // categories search tab. It's guaranteed that the list remains the - // same during the application lifetime, keys may be used as parts - // of resources ids. - static vector const & GetKeys(); + // Returns a list of English names of displayed categories for the categories search tab. + std::vector const & GetKeys() const; + void InsertKey(std::string const & key, size_t pos); + void RemoveKey(std::string const & key); + bool Contains(std::string const & key) const; // Calls |fn| on each pair (synonym name, synonym locale) for the // |key|. template - void ForEachSynonym(string const & key, Fn && fn) const + void ForEachSynonym(std::string const & key, Fn && fn) const { auto const & translations = m_holder.GetGroupTranslations(); auto const it = translations.find("@" + key); @@ -34,5 +34,6 @@ public: private: CategoriesHolder const & m_holder; + std::vector m_keys; }; } // namespace search