Added support of FC2018 category in the core

This commit is contained in:
r.kuznetsov 2018-04-03 16:16:57 +03:00 committed by Vlad Mihaylenko
parent 8041b04a49
commit 48635d9780
3 changed files with 60 additions and 17 deletions

View file

@ -3,29 +3,45 @@
#include "base/macros.hpp"
#include <algorithm>
#include <unordered_set>
namespace
{
std::unordered_set<std::string> const kLuggageHeroesSupportedCities{"London", "New York",
"Copenhagen"};
std::unordered_set<std::string> const kFc2018SupportedCities{
"Moscow", "Saint Petersburg", "Kazan", "Yekaterinburg", "Saransk", "Samara",
"Sochi", "Volgograd", "Rostov-on-Don", "Kaliningrad", "Nizhny Novgorod"};
} // namespace
LuggageHeroModifier::LuggageHeroModifier(std::string const & city) : m_city(city) {}
SponsoredCategoryModifier::SponsoredCategoryModifier(std::string const & currentCity,
SupportedCities const & supportedCities,
std::string const & categoryName,
uint32_t position)
: m_currentCity(currentCity)
, m_supportedCities(supportedCities)
, m_categoryName(categoryName)
, m_position(position)
{}
void LuggageHeroModifier::Modify(search::DisplayedCategories::Keys & keys)
void SponsoredCategoryModifier::Modify(search::DisplayedCategories::Keys & keys)
{
static int const kPos = 4;
static std::string const kCategoryName = "luggagehero";
auto const supported = m_supportedCities.find(m_currentCity) != m_supportedCities.cend();
auto const contains = std::find(keys.cbegin(), keys.cend(), m_categoryName) != keys.cend();
auto const supported =
kLuggageHeroesSupportedCities.find(m_city) != kLuggageHeroesSupportedCities.cend();
auto const contains = std::find(keys.cbegin(), keys.cend(), kCategoryName) != keys.cend();
ASSERT_LESS(kPos, keys.size(), ());
ASSERT_LESS(m_position, keys.size(), ());
if (supported && !contains)
keys.insert(keys.cbegin() + kPos, kCategoryName);
keys.insert(keys.cbegin() + m_position, m_categoryName);
else if (!supported && contains)
keys.erase(std::remove(keys.begin(), keys.end(), kCategoryName), keys.end());
keys.erase(std::remove(keys.begin(), keys.end(), m_categoryName), keys.end());
}
LuggageHeroModifier::LuggageHeroModifier(std::string const & currentCity)
: SponsoredCategoryModifier(currentCity, kLuggageHeroesSupportedCities, "luggagehero",
4 /* position */)
{}
Fc2018Modifier::Fc2018Modifier(std::string const & currentCity)
: SponsoredCategoryModifier(currentCity, kFc2018SupportedCities, "fc2018", 3 /* position */)
{}

View file

@ -2,14 +2,37 @@
#include "search/displayed_categories.hpp"
class LuggageHeroModifier : public search::CategoriesModifier
#include <cstdint>
#include <unordered_set>
class SponsoredCategoryModifier : public search::CategoriesModifier
{
public:
explicit LuggageHeroModifier(std::string const & city);
using SupportedCities = std::unordered_set<std::string>;
SponsoredCategoryModifier(std::string const & currentCity,
SupportedCities const & supportedCities,
std::string const & categoryName,
uint32_t position);
// CategoriesModifier overrides:
void Modify(search::DisplayedCategories::Keys & keys) override;
private:
std::string m_city;
std::string const m_currentCity;
SupportedCities const m_supportedCities;
std::string const m_categoryName;
uint32_t const m_position;
};
class LuggageHeroModifier : public SponsoredCategoryModifier
{
public:
explicit LuggageHeroModifier(std::string const & currentCity);
};
class Fc2018Modifier : public SponsoredCategoryModifier
{
public:
explicit Fc2018Modifier(std::string const & currentCity);
};

View file

@ -1441,8 +1441,12 @@ search::DisplayedCategories const & Framework::GetDisplayedCategories()
if (auto const position = GetCurrentPosition())
city = m_cityFinder->GetCityName(*position, StringUtf8Multilang::kEnglishCode);
LuggageHeroModifier modifier(city);
m_displayedCategories->Modify(modifier);
// Apply sponsored modifiers.
std::vector<std::unique_ptr<SponsoredCategoryModifier>> modifiers;
modifiers.push_back(std::make_unique<LuggageHeroModifier>(city));
modifiers.push_back(std::make_unique<Fc2018Modifier>(city));
for (auto & modifier : modifiers)
m_displayedCategories->Modify(*modifier);
return *m_displayedCategories;
}