From a69b56582a834b6854f785d33bbf2c21a6a574fa Mon Sep 17 00:00:00 2001 From: "r.kuznetsov" Date: Tue, 29 Jan 2019 12:56:51 +0300 Subject: [PATCH] [core] Added megafon category banner logic --- android/jni/com/mapswithme/maps/Framework.cpp | 21 ++++++++- .../src/com/mapswithme/maps/Framework.java | 5 ++- .../mapswithme/maps/search/PromoCategory.java | 2 +- .../SearchCategoriesViewController.swift | 2 +- .../UI/Search/Tabs/MWMSearchFrameworkHelper.h | 2 +- .../Search/Tabs/MWMSearchFrameworkHelper.mm | 19 +++++++- map/framework.cpp | 18 -------- map/framework.hpp | 1 - partners_api/megafon_countries.cpp | 43 ++++++++++++++++--- partners_api/megafon_countries.hpp | 4 ++ 10 files changed, 83 insertions(+), 34 deletions(-) diff --git a/android/jni/com/mapswithme/maps/Framework.cpp b/android/jni/com/mapswithme/maps/Framework.cpp index 2a1c5342f1..7c9098b11e 100644 --- a/android/jni/com/mapswithme/maps/Framework.cpp +++ b/android/jni/com/mapswithme/maps/Framework.cpp @@ -1790,9 +1790,20 @@ Java_com_mapswithme_maps_Framework_nativeHasMegafonDownloaderBanner(JNIEnv * env } JNIEXPORT jboolean JNICALL -Java_com_mapswithme_maps_Framework_nativeHasRuTaxiCategoryBanner(JNIEnv * env, jclass) +Java_com_mapswithme_maps_Framework_nativeHasMegafonCategoryBanner(JNIEnv * env, jclass) { - return static_cast(frm()->HasRuTaxiCategoryBanner()); + auto const & purchase = frm()->GetPurchase(); + if (purchase && purchase->IsSubscriptionActive(SubscriptionType::RemoveAds)) + return static_cast(false); + + auto const position = frm()->GetCurrentPosition(); + if (!position) + return static_cast(false); + + auto const latLon = MercatorBounds::ToLatLon(position.get()); + return static_cast(ads::HasMegafonCategoryBanner(frm()->GetStorage(), + frm()->GetTopmostCountries(latLon), + languages::GetCurrentNorm())); } JNIEXPORT jstring JNICALL @@ -1801,6 +1812,12 @@ Java_com_mapswithme_maps_Framework_nativeGetMegafonDownloaderBannerUrl(JNIEnv * return jni::ToJavaString(env, ads::GetMegafonDownloaderBannerUrl()); } +JNIEXPORT jstring JNICALL +Java_com_mapswithme_maps_Framework_nativeGetMegafonCategoryBannerUrl(JNIEnv * env, jclass) +{ + return jni::ToJavaString(env, ads::GetMegafonCategoryBannerUrl()); +} + JNIEXPORT void JNICALL Java_com_mapswithme_maps_Framework_nativeMakeCrash(JNIEnv *env, jclass type) { diff --git a/android/src/com/mapswithme/maps/Framework.java b/android/src/com/mapswithme/maps/Framework.java index 88bd9b4f56..fd4684c558 100644 --- a/android/src/com/mapswithme/maps/Framework.java +++ b/android/src/com/mapswithme/maps/Framework.java @@ -482,7 +482,10 @@ public class Framework @NonNull public static native String nativeGetMegafonDownloaderBannerUrl(); - public static native boolean nativeHasRuTaxiCategoryBanner(); + public static native boolean nativeHasMegafonCategoryBanner(); + + @NonNull + public static native String nativeGetMegafonCategoryBannerUrl(); public static native void nativeMakeCrash(); diff --git a/android/src/com/mapswithme/maps/search/PromoCategory.java b/android/src/com/mapswithme/maps/search/PromoCategory.java index bbc7963735..6bb65ed00d 100644 --- a/android/src/com/mapswithme/maps/search/PromoCategory.java +++ b/android/src/com/mapswithme/maps/search/PromoCategory.java @@ -51,7 +51,7 @@ public enum PromoCategory @Override boolean isSupported() { - return Framework.nativeHasRuTaxiCategoryBanner(); + return Framework.nativeHasMegafonCategoryBanner(); } }; diff --git a/iphone/Maps/UI/Search/Tabs/CategoriesTab/SearchCategoriesViewController.swift b/iphone/Maps/UI/Search/Tabs/CategoriesTab/SearchCategoriesViewController.swift index 623a73956a..2d18ef9d39 100644 --- a/iphone/Maps/UI/Search/Tabs/CategoriesTab/SearchCategoriesViewController.swift +++ b/iphone/Maps/UI/Search/Tabs/CategoriesTab/SearchCategoriesViewController.swift @@ -12,7 +12,7 @@ final class SearchCategoriesViewController: MWMTableViewController { init(frameworkHelper: MWMSearchFrameworkHelper, delegate: SearchCategoriesViewControllerDelegate?) { self.delegate = delegate self.categories = frameworkHelper.searchCategories() - self.rutaxi = frameworkHelper.hasRutaxiBanner() + self.rutaxi = frameworkHelper.hasMegafonCategoryBanner() super.init(nibName: nil, bundle: nil) } diff --git a/iphone/Maps/UI/Search/Tabs/MWMSearchFrameworkHelper.h b/iphone/Maps/UI/Search/Tabs/MWMSearchFrameworkHelper.h index 832af05619..15eaf067c5 100644 --- a/iphone/Maps/UI/Search/Tabs/MWMSearchFrameworkHelper.h +++ b/iphone/Maps/UI/Search/Tabs/MWMSearchFrameworkHelper.h @@ -3,7 +3,7 @@ NS_ASSUME_NONNULL_BEGIN @interface MWMSearchFrameworkHelper : NSObject - (NSArray *)searchCategories; -- (BOOL)hasRutaxiBanner; +- (BOOL)hasMegafonCategoryBanner; - (BOOL)isSearchHistoryEmpty; - (NSArray *)lastSearchQueries; diff --git a/iphone/Maps/UI/Search/Tabs/MWMSearchFrameworkHelper.mm b/iphone/Maps/UI/Search/Tabs/MWMSearchFrameworkHelper.mm index 8c197e8291..99c837f4ae 100644 --- a/iphone/Maps/UI/Search/Tabs/MWMSearchFrameworkHelper.mm +++ b/iphone/Maps/UI/Search/Tabs/MWMSearchFrameworkHelper.mm @@ -1,5 +1,9 @@ #import "MWMSearchFrameworkHelper.h" +#include "partners_api/megafon_countries.hpp" + +#include "platform/preferred_languages.hpp" + #include "Framework.h" @implementation MWMSearchFrameworkHelper @@ -15,9 +19,20 @@ return [result copy]; } -- (BOOL)hasRutaxiBanner +- (BOOL)hasMegafonCategoryBanner { - return GetFramework().HasRuTaxiCategoryBanner(); + auto & f = GetFramework(); + auto const & purchase = f.GetPurchase(); + if (purchase && purchase->IsSubscriptionActive(SubscriptionType::RemoveAds)) + return NO; + + auto const position = f.GetCurrentPosition(); + if (!position) + return NO; + + auto const latLon = MercatorBounds::ToLatLon(position.get()); + return ads::HasMegafonCategoryBanner(f.GetStorage(), f.GetTopmostCountries(latLon), + languages::GetCurrentNorm()); } - (BOOL)isSearchHistoryEmpty diff --git a/map/framework.cpp b/map/framework.cpp index 1b122f34cc..23ee93b54f 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -3414,24 +3414,6 @@ void Framework::DisableAdProvider(ads::Banner::Type const type, ads::Banner::Pla m_adsEngine.get()->DisableAdProvider(type, place); } -bool Framework::HasRuTaxiCategoryBanner() -{ - auto const & purchase = GetPurchase(); - if (purchase && purchase->IsSubscriptionActive(SubscriptionType::RemoveAds)) - return false; - - auto const position = GetCurrentPosition(); - if (!position) - return false; - - auto const taxiEngine = GetTaxiEngine(platform::GetCurrentNetworkPolicy()); - if (!taxiEngine) - return false; - - auto const providers = taxiEngine->GetProvidersAtPos(MercatorBounds::ToLatLon(position.get())); - return std::find(providers.begin(), providers.end(), taxi::Provider::Rutaxi) != providers.end(); -} - void Framework::RunUITask(function fn) { GetPlatform().RunTask(Platform::Thread::Gui, move(fn)); diff --git a/map/framework.hpp b/map/framework.hpp index 023355a9d2..ea20372c2a 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -361,7 +361,6 @@ public: ads::Engine const & GetAdsEngine() const; void DisableAdProvider(ads::Banner::Type const type, ads::Banner::Place const place); - bool HasRuTaxiCategoryBanner(); public: // SearchAPI::Delegate overrides: diff --git a/partners_api/megafon_countries.cpp b/partners_api/megafon_countries.cpp index 1be466f3d4..3f78c8a93b 100644 --- a/partners_api/megafon_countries.cpp +++ b/partners_api/megafon_countries.cpp @@ -49,26 +49,55 @@ storage::TCountriesVec const kCountries = { "Ukraine", "United Arab Emirates", }; + +bool IsRussianLocale(std::string const & currentLocale) +{ + return currentLocale.find("ru") != std::string::npos; +} + +bool ContainsCountry(storage::Storage const & storage, storage::TCountriesVec const & countries, + storage::TCountriesVec const & referenceCountries) +{ + for (auto const & c : countries) + { + if (std::find(referenceCountries.cbegin(), referenceCountries.cend(), c) != + referenceCountries.cend()) + { + return true; + } + } + return false; +} } // namespace bool HasMegafonDownloaderBanner(storage::Storage const & storage, std::string const & mwmId, std::string const & currentLocale) { - if (currentLocale.find("ru") == std::string::npos) + if (!IsRussianLocale(currentLocale)) return false; storage::TCountriesVec countries; storage.GetTopmostNodesFor(mwmId, countries); - for (auto const & c : countries) - { - if (std::find(kCountries.cbegin(), kCountries.cend(), c) != kCountries.cend()) - return true; - } - return false; + return ContainsCountry(storage, countries, kCountries); +} + +bool HasMegafonCategoryBanner(storage::Storage const & storage, + storage::TCountriesVec const & countries, + std::string const & currentLocale) +{ + if (!IsRussianLocale(currentLocale)) + return false; + + return ContainsCountry(storage, countries, kCountries); } std::string GetMegafonDownloaderBannerUrl() { return "https://localads.maps.me/redirects/megafon_downloader"; } + +std::string GetMegafonCategoryBannerUrl() +{ + return "https://localads.maps.me/redirects/megafon_search_category"; +} } // namespace ads diff --git a/partners_api/megafon_countries.hpp b/partners_api/megafon_countries.hpp index c1378f73a5..98fb7f530c 100644 --- a/partners_api/megafon_countries.hpp +++ b/partners_api/megafon_countries.hpp @@ -8,5 +8,9 @@ namespace ads { extern bool HasMegafonDownloaderBanner(storage::Storage const & storage, std::string const & mwmId, std::string const & currentLocale); +extern bool HasMegafonCategoryBanner(storage::Storage const & storage, + storage::TCountriesVec const & countries, + std::string const & currentLocale); extern std::string GetMegafonDownloaderBannerUrl(); +extern std::string GetMegafonCategoryBannerUrl(); } // namespace ads