Refactored partners

This commit is contained in:
r.kuznetsov 2018-04-28 19:00:39 +03:00 committed by Arsentiy Milchakov
parent ee06a502da
commit d0faaca2ea
12 changed files with 170 additions and 102 deletions

View file

@ -14,7 +14,9 @@ import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
@ -94,8 +96,6 @@ final class PlacePageButtons
}
},
PARTNER2,
PARTNER3
{
@Override
@ -111,10 +111,6 @@ final class PlacePageButtons
}
},
PARTNER4,
PARTNER5,
BACK
{
@Override
@ -261,9 +257,11 @@ final class PlacePageButtons
}
}
private static final Item[] PARTNERS_ITEMS = new Item[] { Item.PARTNER1, Item.PARTNER2,
Item.PARTNER3, Item.PARTNER4,
Item.PARTNER5 };
private static final Map<Integer, Item> PARTNERS_ITEMS = new HashMap<Integer, Item>()
{{
put(1, Item.PARTNER1);
put(3, Item.PARTNER3);
}};
interface ItemListener
{
@ -283,9 +281,9 @@ final class PlacePageButtons
@NonNull
static Item getPartnerItem(int partnerIndex)
{
if (partnerIndex < 0 || partnerIndex >= PARTNERS_ITEMS.length)
if (!PARTNERS_ITEMS.containsKey(partnerIndex))
throw new AssertionError("Wrong partner index: " + partnerIndex);
return PARTNERS_ITEMS[partnerIndex];
return PARTNERS_ITEMS.get(partnerIndex);
}
private @NonNull List<Item> collectButtons(List<Item> items)

View file

@ -581,10 +581,7 @@ public class PlacePageView extends RelativeLayout
case BOOKING:
case OPENTABLE:
case PARTNER1:
case PARTNER2:
case PARTNER3:
case PARTNER4:
case PARTNER5:
onSponsoredClick(true /* book */, false);
break;

View file

@ -1,15 +1,9 @@
#include "indexer/ftypes_sponsored.hpp"
#include "indexer/classificator.hpp"
#include "indexer/feature.hpp"
#include "indexer/feature_data.hpp"
#include "base/string_utils.hpp"
#include "private.h"
#include <algorithm>
namespace ftypes
{
BaseSponsoredChecker::BaseSponsoredChecker(std::string const & sponsoredType)
@ -17,46 +11,8 @@ BaseSponsoredChecker::BaseSponsoredChecker(std::string const & sponsoredType)
m_types.push_back(classif().GetTypeByPath({"sponsored", sponsoredType}));
}
SponsoredPartnerChecker::SponsoredPartnerChecker()
{
Classificator const & c = classif();
for (size_t i = 1; i <= MAX_PARTNERS_COUNT; i++)
m_types.push_back(c.GetTypeByPath({"sponsored", "partner" + strings::to_string(i)}));
}
int SponsoredPartnerChecker::GetPartnerIndex(FeatureType const & ft) const
{
auto const types = feature::TypesHolder(ft);
int index = 0;
for (auto t : m_types)
{
if (std::find(types.begin(), types.end(), PrepareToMatch(t, 2 /* level */)) != types.end())
return index;
index++;
}
return -1;
}
Fc2018Checker::Fc2018Checker()
{
m_types.push_back(classif().GetTypeByPath({"event", "fc2018"}));
}
} // namespace ftypes
std::string GetPartnerNameByIndex(int partnerIndex)
{
static std::vector<std::string> kIds = {PARTNER1_NAME, PARTNER2_NAME, PARTNER3_NAME,
PARTNER4_NAME, PARTNER5_NAME};
if (partnerIndex < 0 || partnerIndex >= kIds.size())
return {};
return kIds[partnerIndex];
}
bool IsPartnerButtonExist(int partnerIndex)
{
static std::vector<bool> kButtons = {PARTNER1_HAS_BUTTON, PARTNER2_HAS_BUTTON, PARTNER3_HAS_BUTTON,
PARTNER4_HAS_BUTTON, PARTNER5_HAS_BUTTON};
if (partnerIndex < 0 || partnerIndex >= kButtons.size())
return false;
return kButtons[partnerIndex];
}

View file

@ -25,17 +25,6 @@ SPONSORED_CHECKER(IsHolidayChecker, "holiday");
#undef SPONSORED_CHECKER
class SponsoredPartnerChecker : public BaseChecker
{
protected:
SponsoredPartnerChecker();
public:
int GetPartnerIndex(FeatureType const & ft) const;
DECLARE_CHECKER_INSTANCE(SponsoredPartnerChecker);
};
class Fc2018Checker : public BaseChecker
{
Fc2018Checker();
@ -43,6 +32,3 @@ public:
DECLARE_CHECKER_INSTANCE(Fc2018Checker);
};
} // namespace ftypes
extern std::string GetPartnerNameByIndex(int partnerIndex);
extern bool IsPartnerButtonExist(int partnerIndex);

View file

@ -5,7 +5,7 @@
NSString * titleForPartner(int partnerIndex)
{
NSString * str = [NSString stringWithFormat:@"sponsored_partner%d_action", partnerIndex + 1];
NSString * str = [NSString stringWithFormat:@"sponsored_partner%d_action", partnerIndex];
NSString * localizedStr = L(str);
NSCAssert(![str isEqualToString:localizedStr], @"Localization is absent.");
return localizedStr;
@ -33,7 +33,7 @@ NSString * titleForButton(EButton type, int partnerIndex, BOOL isSelected)
NSString * imageNameForPartner(int partnerIndex)
{
return [NSString stringWithFormat:@"ic_28px_logo_partner%d", partnerIndex + 1];
return [NSString stringWithFormat:@"ic_28px_logo_partner%d", partnerIndex];
}
UIImage * imageForPartner(int partnerIndex)
@ -45,7 +45,7 @@ UIImage * imageForPartner(int partnerIndex)
UIColor * textColorForPartner(int partnerIndex)
{
NSString * textColor = [NSString stringWithFormat:@"partner%dTextColor", partnerIndex + 1];
NSString * textColor = [NSString stringWithFormat:@"partner%dTextColor", partnerIndex];
UIColor * color = [UIColor colorWithName:textColor];
NSCAssert(color != nil, @"Partner text color is absent.");
return color;
@ -53,7 +53,7 @@ UIColor * textColorForPartner(int partnerIndex)
UIColor * backgroundColorForPartner(int partnerIndex)
{
NSString * colorName = [NSString stringWithFormat:@"partner%dBackground", partnerIndex + 1];
NSString * colorName = [NSString stringWithFormat:@"partner%dBackground", partnerIndex];
UIColor * color = [UIColor colorWithName:colorName];
NSCAssert(color != nil, @"Partner background color is absent.");
return color;

View file

@ -85,6 +85,7 @@
#include "partners_api/ads_engine.hpp"
#include "partners_api/opentable_api.hpp"
#include "partners_api/partners.hpp"
#include "base/logging.hpp"
#include "base/math.hpp"
@ -102,8 +103,6 @@
#include "3party/Alohalytics/src/alohalytics.h"
#define DEFAULT_BOOKMARK_TYPE "placemark-red"
using namespace storage;
using namespace routing;
using namespace location;
@ -855,12 +854,12 @@ void Framework::FillInfoFromFeatureType(FeatureType const & ft, place_page::Info
info.SetBookingSearchUrl(url);
LOG(LINFO, (url));
}
else if (ftypes::SponsoredPartnerChecker::Instance()(ft))
else if (PartnerChecker::Instance()(ft))
{
info.SetSponsoredType(place_page::SponsoredType::Partner);
auto const partnerIndex = ftypes::SponsoredPartnerChecker::Instance().GetPartnerIndex(ft);
auto const partnerIndex = PartnerChecker::Instance().GetPartnerIndex(ft);
info.SetPartnerIndex(partnerIndex);
if (IsPartnerButtonExist(partnerIndex))
if (GetPartnerByIndex(partnerIndex).m_hasButton)
{
auto const & url = info.GetMetadata().Get(feature::Metadata::FMD_BANNER_URL);
info.SetSponsoredUrl(url);

View file

@ -4,6 +4,7 @@
#include "partners_api/ads_engine.hpp"
#include "partners_api/banner.hpp"
#include "partners_api/partners.hpp"
#include "indexer/feature_utils.hpp"
#include "indexer/ftypes_sponsored.hpp"
@ -323,7 +324,7 @@ vector<ads::Banner> Info::GetBanners() const
void Info::SetPartnerIndex(int index)
{
m_partnerIndex = index;
m_partnerName = GetPartnerNameByIndex(m_partnerIndex);
m_partnerName = GetPartnerByIndex(m_partnerIndex).m_name;
}
namespace rating

View file

@ -25,6 +25,8 @@ set(
mopub_ads.hpp
opentable_api.cpp
opentable_api.hpp
partners.cpp
partners.hpp
rb_ads.cpp
rb_ads.hpp
taxi_base.cpp

View file

@ -1,6 +1,5 @@
#include "partners_api/mopub_ads.hpp"
#include "base/string_utils.hpp"
#include "partners_api/partners.hpp"
#include "private.h"
@ -17,14 +16,6 @@ namespace
auto const kNonTourismPlacementId = "94b8d70370a643929aa4c8c764d25e5b";
auto const kSponsoredBannerPlacementId = "2bab47102d38485996788ab9b602ce2c";
#endif
bool IsValidPlacementId(std::string const & placementId)
{
return !placementId.empty();
}
#define APPEND_PARTNER(type, placementId) \
if (IsValidPlacementId(placementId)) AppendEntry({{"sponsored", type}}, placementId);
} // namespace
namespace ads
@ -80,12 +71,12 @@ Mopub::Mopub()
AppendEntry({{"sponsored", "banner"}}, kSponsoredBannerPlacementId);
// Partners.
APPEND_PARTNER("partner1", PARTNER1_BANNER_PLACEMENT_ID);
APPEND_PARTNER("partner2", PARTNER2_BANNER_PLACEMENT_ID);
APPEND_PARTNER("partner3", PARTNER3_BANNER_PLACEMENT_ID);
APPEND_PARTNER("partner4", PARTNER4_BANNER_PLACEMENT_ID);
APPEND_PARTNER("partner5", PARTNER5_BANNER_PLACEMENT_ID);
for (auto const & p : kPartners)
{
auto const placementId = p.GetBannerPlacementId();
if (!placementId.empty())
AppendEntry({{"sponsored", p.m_type.c_str()}}, placementId);
}
}
std::string Mopub::GetBannerIdForOtherTypes() const

91
partners_api/partners.cpp Normal file
View file

@ -0,0 +1,91 @@
#include "partners_api/partners.hpp"
#include "indexer/classificator.hpp"
#include "indexer/feature.hpp"
#include "indexer/feature_data.hpp"
#include "base/string_utils.hpp"
#include "std/target_os.hpp"
#include <algorithm>
#include <vector>
#include <utility>
std::vector<PartnerInfo> const kPartners = {
PartnerInfo(1, "Megabonus.Ru", true /* m_hasButton */,
"e0c1d4d9c88c4670b9541116bcb75c5f" /* m_iosBannerPlacementId */,
"2bab47102d38485996788ab9b602ce2c" /* m_androidBannerPlacementId */),
PartnerInfo(2, "LuggageHero"),
PartnerInfo(3, "BurgerKing", true /* m_hasButton */)
};
namespace
{
int constexpr kFakePartnerIndex = -1;
PartnerInfo const kFakePartner(kFakePartnerIndex, {});
} // namespace
PartnerInfo::PartnerInfo(int partnerIndex, std::string && name, bool hasButton,
std::string && iosBannerPlacementId, std::string && androidBannerPlacementId)
: m_partnerIndex(partnerIndex)
, m_type("partner" + strings::to_string(partnerIndex))
, m_name(std::move(name))
, m_hasButton(hasButton)
, m_iosBannerPlacementId(std::move(iosBannerPlacementId))
, m_androidBannerPlacementId(std::move(androidBannerPlacementId))
{}
PartnerInfo::PartnerInfo(int partnerIndex, std::string && name, bool hasButton)
: PartnerInfo(partnerIndex, std::move(name), hasButton, {}, {})
{}
PartnerInfo::PartnerInfo(int partnerIndex, std::string && name)
: PartnerInfo(partnerIndex, std::move(name), false /* hasButton */)
{}
std::string PartnerInfo::GetBannerPlacementId() const
{
#if defined(OMIM_OS_IPHONE)
return m_iosBannerPlacementId;
#elif defined(OMIM_OS_ANDROID)
return m_androidBannerPlacementId;
#endif
return {};
}
PartnerChecker::PartnerChecker()
{
Classificator const & c = classif();
for (auto const & p : kPartners)
m_types.push_back(c.GetTypeByPath({"sponsored", p.m_type}));
}
int PartnerChecker::GetPartnerIndex(FeatureType const & ft) const
{
auto const types = feature::TypesHolder(ft);
int index = 0;
CHECK_EQUAL(kPartners.size(), m_types.size(), ());
for (auto t : m_types)
{
if (std::find(types.begin(), types.end(), PrepareToMatch(t, 2 /* level */)) != types.end())
return kPartners[index].m_partnerIndex;
index++;
}
return kFakePartnerIndex;
}
PartnerInfo const & GetPartnerByIndex(int partnerIndex)
{
auto const it = std::find_if(kPartners.cbegin(), kPartners.cend(),
[partnerIndex](PartnerInfo const & partherInfo)
{
return partnerIndex == partherInfo.m_partnerIndex;
});
if (it == kPartners.cend())
return kFakePartner;
return *it;
}

39
partners_api/partners.hpp Normal file
View file

@ -0,0 +1,39 @@
#pragma once
#include "indexer/ftypes_matcher.hpp"
#include <string>
struct PartnerInfo
{
int const m_partnerIndex;
std::string const m_type;
std::string const m_name;
bool const m_hasButton = false;
std::string const m_iosBannerPlacementId;
std::string const m_androidBannerPlacementId;
PartnerInfo(int partnerIndex, std::string && name, bool hasButton,
std::string && iosBannerPlacementId, std::string && androidBannerPlacementId);
PartnerInfo(int partnerIndex, std::string && name, bool hasButton);
PartnerInfo(int partnerIndex, std::string && name);
std::string GetBannerPlacementId() const;
};
class PartnerChecker : public ftypes::BaseChecker
{
protected:
PartnerChecker();
public:
int GetPartnerIndex(FeatureType const & ft) const;
DECLARE_CHECKER_INSTANCE(PartnerChecker);
};
extern std::vector<PartnerInfo> const kPartners;
extern PartnerInfo const & GetPartnerByIndex(int partnerIndex);

View file

@ -55,6 +55,8 @@
3DFEBF9F1EFBFC1500317D5C /* yandex_api.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3DFEBF991EFBFC1500317D5C /* yandex_api.hpp */; };
3DFEBFA31EFBFC2300317D5C /* taxi_engine_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3DFEBFA01EFBFC2300317D5C /* taxi_engine_tests.cpp */; };
3DFEBFA41EFBFC2300317D5C /* yandex_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3DFEBFA11EFBFC2300317D5C /* yandex_tests.cpp */; };
45C380772094C5B400C18D81 /* partners.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 45C380752094C5B400C18D81 /* partners.cpp */; };
45C380782094C5B400C18D81 /* partners.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 45C380762094C5B400C18D81 /* partners.hpp */; };
BB1956E61F543D7C003ECE6C /* locals_api.cpp in Sources */ = {isa = PBXBuildFile; fileRef = BB1956E41F543D7B003ECE6C /* locals_api.cpp */; };
BB1956E71F543D7C003ECE6C /* locals_api.hpp in Headers */ = {isa = PBXBuildFile; fileRef = BB1956E51F543D7C003ECE6C /* locals_api.hpp */; };
F6539537207FBED70057B97C /* taxi_places_loader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3DF9C21A207CAC3B00DA0793 /* taxi_places_loader.cpp */; };
@ -132,6 +134,8 @@
3DFEBF991EFBFC1500317D5C /* yandex_api.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = yandex_api.hpp; sourceTree = "<group>"; };
3DFEBFA01EFBFC2300317D5C /* taxi_engine_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = taxi_engine_tests.cpp; sourceTree = "<group>"; };
3DFEBFA11EFBFC2300317D5C /* yandex_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = yandex_tests.cpp; sourceTree = "<group>"; };
45C380752094C5B400C18D81 /* partners.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = partners.cpp; sourceTree = "<group>"; };
45C380762094C5B400C18D81 /* partners.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = partners.hpp; sourceTree = "<group>"; };
BB1956E41F543D7B003ECE6C /* locals_api.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = locals_api.cpp; sourceTree = "<group>"; };
BB1956E51F543D7C003ECE6C /* locals_api.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = locals_api.hpp; sourceTree = "<group>"; };
F67E75231DB8F06F00D6741F /* opentable_api.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = opentable_api.cpp; sourceTree = "<group>"; };
@ -212,10 +216,6 @@
F6B5363B1DA520B20067EEA5 /* partners_api */ = {
isa = PBXGroup;
children = (
3DF9C21A207CAC3B00DA0793 /* taxi_places_loader.cpp */,
3DF9C21B207CAC3B00DA0793 /* taxi_places_loader.hpp */,
3DF01C2A20652462005DDF8C /* taxi_places.cpp */,
3DF01C2B20652463005DDF8C /* taxi_places.hpp */,
346E888F1E9D087400D4CE9B /* ads_base.cpp */,
346E88901E9D087400D4CE9B /* ads_base.hpp */,
346E88911E9D087400D4CE9B /* ads_engine.cpp */,
@ -237,12 +237,18 @@
3430643B1E9FBCF500DC7665 /* mopub_ads.hpp */,
F67E75231DB8F06F00D6741F /* opentable_api.cpp */,
F67E75241DB8F06F00D6741F /* opentable_api.hpp */,
45C380752094C5B400C18D81 /* partners.cpp */,
45C380762094C5B400C18D81 /* partners.hpp */,
346E88941E9D087400D4CE9B /* rb_ads.cpp */,
346E88951E9D087400D4CE9B /* rb_ads.hpp */,
3D47B2961F054C89000828D2 /* taxi_base.cpp */,
3DFEBF941EFBFC1500317D5C /* taxi_base.hpp */,
3DFEBF951EFBFC1500317D5C /* taxi_engine.cpp */,
3DFEBF961EFBFC1500317D5C /* taxi_engine.hpp */,
3DF9C21A207CAC3B00DA0793 /* taxi_places_loader.cpp */,
3DF9C21B207CAC3B00DA0793 /* taxi_places_loader.hpp */,
3DF01C2A20652462005DDF8C /* taxi_places.cpp */,
3DF01C2B20652463005DDF8C /* taxi_places.hpp */,
3DFEBF971EFBFC1500317D5C /* taxi_provider.hpp */,
F6B5363E1DA520E40067EEA5 /* uber_api.cpp */,
F6B5363F1DA520E40067EEA5 /* uber_api.hpp */,
@ -323,6 +329,7 @@
F6B536411DA520E40067EEA5 /* booking_api.hpp in Headers */,
3430643D1E9FBCF500DC7665 /* mopub_ads.hpp in Headers */,
3D47B2B11F14FA14000828D2 /* utils.hpp in Headers */,
45C380782094C5B400C18D81 /* partners.hpp in Headers */,
3DFEBF861EF82BEA00317D5C /* viator_api.hpp in Headers */,
346E889C1E9D087400D4CE9B /* rb_ads.hpp in Headers */,
3DFEBF9A1EFBFC1500317D5C /* taxi_base.hpp in Headers */,
@ -436,6 +443,7 @@
3D452AF01EE6D202009EAB9B /* mopub_tests.cpp in Sources */,
3D4E997F1FB439300025B48C /* utils.cpp in Sources */,
3430643C1E9FBCF500DC7665 /* mopub_ads.cpp in Sources */,
45C380772094C5B400C18D81 /* partners.cpp in Sources */,
346E88961E9D087400D4CE9B /* ads_base.cpp in Sources */,
3DFEBF891EF82C1300317D5C /* viator_tests.cpp in Sources */,
3DFEBF851EF82BEA00317D5C /* viator_api.cpp in Sources */,