diff --git a/android/jni/com/mapswithme/maps/SearchEngine.cpp b/android/jni/com/mapswithme/maps/SearchEngine.cpp index 69bb282b2a..67e55c2224 100644 --- a/android/jni/com/mapswithme/maps/SearchEngine.cpp +++ b/android/jni/com/mapswithme/maps/SearchEngine.cpp @@ -6,16 +6,20 @@ #include "search/result.hpp" #include "search/viewport_search_params.hpp" +#include "base/assert.hpp" #include "base/logging.hpp" -#include "std/cstdint.hpp" - #include "../core/jni_helper.hpp" #include "../platform/Language.hpp" #include "../platform/Platform.hpp" -using search::Results; +#include +#include +#include + +using namespace std; using search::Result; +using search::Results; namespace { @@ -222,7 +226,8 @@ jmethodID g_mapResultsMethod; jclass g_mapResultClass; jmethodID g_mapResultCtor; -jobject ToJavaResult(Result & result, bool hasPosition, double lat, double lon) +jobject ToJavaResult(Result & result, bool isLocalAdsCustomer, bool hasPosition, double lat, + double lon) { JNIEnv * env = jni::GetEnv(); ::Framework * fr = g_framework->NativeFramework(); @@ -273,13 +278,14 @@ jobject ToJavaResult(Result & result, bool hasPosition, double lat, double lon) jni::TScopedLocalRef name(env, jni::ToJavaString(env, result.GetString())); jobject ret = env->NewObject(g_resultClass, g_resultConstructor, name.get(), desc.get(), ll.lat, - ll.lon, ranges.get(), result.IsHotel(), result.IsLocalAdsCustomer()); + ll.lon, ranges.get(), result.IsHotel(), isLocalAdsCustomer); ASSERT(ret, ()); return ret; } -jobjectArray BuildJavaResults(Results const & results, bool hasPosition, double lat, double lon) +jobjectArray BuildJavaResults(Results const & results, vector const & isLocalAdsCustomer, + bool hasPosition, double lat, double lon) { JNIEnv * env = jni::GetEnv(); @@ -287,16 +293,19 @@ jobjectArray BuildJavaResults(Results const & results, bool hasPosition, double int const count = g_results.GetCount(); jobjectArray const jResults = env->NewObjectArray(count, g_resultClass, nullptr); + + ASSERT_EQUAL(results.GetCount(), isLocalAdsCustomer.size(), ()); for (int i = 0; i < count; i++) { - jni::TScopedLocalRef jRes(env, ToJavaResult(g_results[i], hasPosition, lat, lon)); + jni::TScopedLocalRef jRes( + env, ToJavaResult(g_results[i], isLocalAdsCustomer[i], hasPosition, lat, lon)); env->SetObjectArrayElement(jResults, i, jRes.get()); } return jResults; } -void OnResults(Results const & results, long long timestamp, bool isMapAndTable, - bool hasPosition, double lat, double lon) +void OnResults(Results const & results, vector const & isLocalAdsCustomer, + long long timestamp, bool isMapAndTable, bool hasPosition, double lat, double lon) { // Ignore results from obsolete searches. if (g_queryTimestamp > timestamp) @@ -306,7 +315,8 @@ void OnResults(Results const & results, long long timestamp, bool isMapAndTable, if (!results.IsEndMarker() || results.IsEndedNormal()) { - jni::TScopedLocalObjectArrayRef jResults(env, BuildJavaResults(results, hasPosition, lat, lon)); + jni::TScopedLocalObjectArrayRef jResults( + env, BuildJavaResults(results, isLocalAdsCustomer, hasPosition, lat, lon)); env->CallVoidMethod(g_javaListener, g_updateResultsId, jResults.get(), static_cast(timestamp), search::HotelsClassifier::IsHotelResults(results)); @@ -381,7 +391,7 @@ extern "C" search::EverywhereSearchParams params; params.m_query = jni::ToNativeString(env, bytes); params.m_inputLocale = ReplaceDeprecatedLanguageCode(jni::ToNativeString(env, lang)); - params.m_onResults = bind(&OnResults, _1, timestamp, false, hasPosition, lat, lon); + params.m_onResults = bind(&OnResults, _1, _2, timestamp, false, hasPosition, lat, lon); params.m_hotelsFilter = g_hotelsFilterBuilder.Build(env, hotelsFilter); bool const searchStarted = g_framework->NativeFramework()->SearchEverywhere(params); @@ -408,8 +418,8 @@ extern "C" search::EverywhereSearchParams eparams; eparams.m_query = vparams.m_query; eparams.m_inputLocale = vparams.m_inputLocale; - eparams.m_onResults = bind(&OnResults, _1, timestamp, isMapAndTable, false /* hasPosition */, - 0.0 /* lat */, 0.0 /* lon */); + eparams.m_onResults = bind(&OnResults, _1, _2, timestamp, isMapAndTable, + false /* hasPosition */, 0.0 /* lat */, 0.0 /* lon */); eparams.m_hotelsFilter = vparams.m_hotelsFilter; if (g_framework->NativeFramework()->SearchEverywhere(eparams)) g_queryTimestamp = timestamp; diff --git a/iphone/Maps/Core/Search/MWMSearch.h b/iphone/Maps/Core/Search/MWMSearch.h index b3c3947159..a5e2a6aa08 100644 --- a/iphone/Maps/Core/Search/MWMSearch.h +++ b/iphone/Maps/Core/Search/MWMSearch.h @@ -18,6 +18,7 @@ + (MWMSearchItemType)resultTypeWithRow:(NSUInteger)row; + (NSUInteger)containerIndexWithRow:(NSUInteger)row; + (search::Result const &)resultWithContainerIndex:(NSUInteger)index; ++ (BOOL)isLocalAdsWithContainerIndex:(NSUInteger)index; + (id)adWithContainerIndex:(NSUInteger)index; + (void)update; diff --git a/iphone/Maps/Core/Search/MWMSearch.mm b/iphone/Maps/Core/Search/MWMSearch.mm index fb55861fea..6abd1bebca 100644 --- a/iphone/Maps/Core/Search/MWMSearch.mm +++ b/iphone/Maps/Core/Search/MWMSearch.mm @@ -52,6 +52,7 @@ using TObservers = NSHashTable<__kindof TObserver>; search::EverywhereSearchParams m_everywhereParams; search::ViewportSearchParams m_viewportParams; search::Results m_everywhereResults; + vector m_isLocalAdsCustomer; string m_filterQuery; } @@ -80,7 +81,8 @@ using TObservers = NSHashTable<__kindof TObserver>; NSUInteger const timestamp = ++self.lastSearchStamp; { __weak auto weakSelf = self; - m_everywhereParams.m_onResults = [weakSelf, timestamp](search::Results const & results) { + m_everywhereParams.m_onResults = [weakSelf, timestamp]( + search::Results const & results, vector const & isLocalAdsCustomer) { __strong auto self = weakSelf; if (!self) return; @@ -104,6 +106,7 @@ using TObservers = NSHashTable<__kindof TObserver>; else { self->m_everywhereResults = results; + self->m_isLocalAdsCustomer = isLocalAdsCustomer; self.suggestionsCount = results.GetSuggestsCount(); [self updateItemsIndex]; [self onSearchResultsUpdated]; @@ -222,6 +225,11 @@ using TObservers = NSHashTable<__kindof TObserver>; return [MWMSearch manager]->m_everywhereResults[index]; } ++ (BOOL)isLocalAdsWithContainerIndex:(NSUInteger)index +{ + return [MWMSearch manager]->m_isLocalAdsCustomer[index]; +} + + (id)adWithContainerIndex:(NSUInteger)index { return [[MWMSearch manager].banners bannerAtIndex:index]; diff --git a/iphone/Maps/UI/Search/TableView/MWMSearchCommonCell.h b/iphone/Maps/UI/Search/TableView/MWMSearchCommonCell.h index 41319c0f25..5b011511e2 100644 --- a/iphone/Maps/UI/Search/TableView/MWMSearchCommonCell.h +++ b/iphone/Maps/UI/Search/TableView/MWMSearchCommonCell.h @@ -4,6 +4,6 @@ @interface MWMSearchCommonCell : MWMSearchCell -- (void)config:(search::Result const &)result; +- (void)config:(search::Result const &)result isLocalAds:(BOOL)isLocalAds; @end diff --git a/iphone/Maps/UI/Search/TableView/MWMSearchCommonCell.mm b/iphone/Maps/UI/Search/TableView/MWMSearchCommonCell.mm index 210995b2c5..ef0fc9adaf 100644 --- a/iphone/Maps/UI/Search/TableView/MWMSearchCommonCell.mm +++ b/iphone/Maps/UI/Search/TableView/MWMSearchCommonCell.mm @@ -27,7 +27,7 @@ @implementation MWMSearchCommonCell -- (void)config:(search::Result const &)result +- (void)config:(search::Result const &)result isLocalAds:(BOOL)isLocalAds { [super config:result]; self.typeLabel.text = @(result.GetFeatureType().c_str()).capitalizedString; @@ -69,7 +69,7 @@ self.distanceLabel.text = @(distanceStr.c_str()); } - self.backgroundColor = result.IsLocalAdsCustomer() ? [UIColor bannerBackground] : [UIColor white]; + self.backgroundColor = isLocalAds ? [UIColor bannerBackground] : [UIColor white]; } - (void)setInfoText:(NSString *)infoText diff --git a/iphone/Maps/UI/Search/TableView/MWMSearchTableViewController.mm b/iphone/Maps/UI/Search/TableView/MWMSearchTableViewController.mm index dc73de8543..c430d6b766 100644 --- a/iphone/Maps/UI/Search/TableView/MWMSearchTableViewController.mm +++ b/iphone/Maps/UI/Search/TableView/MWMSearchTableViewController.mm @@ -84,7 +84,8 @@ dequeueReusableCellWithCellClass:[MWMSearchCommonCell class] indexPath:indexPath]); auto const & result = [MWMSearch resultWithContainerIndex:containerIndex]; - [cell config:result]; + auto const isLocalAds = [MWMSearch isLocalAdsWithContainerIndex:containerIndex]; + [cell config:result isLocalAds:isLocalAds]; return cell; } case MWMSearchItemTypeMopub: diff --git a/map/framework.cpp b/map/framework.cpp index 69f350f566..ccc9f1857f 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -1314,9 +1314,11 @@ bool Framework::SearchEverywhere(search::EverywhereSearchParams const & params) p.m_onResults = search::EverywhereSearchCallback( static_cast(*this), - [this, params](search::Results const & results) { + [this, params](search::Results const & results, vector const & isLocalAdsCustomer) { if (params.m_onResults) - GetPlatform().RunOnGuiThread([params, results]() { params.m_onResults(results); }); + GetPlatform().RunOnGuiThread([params, results, isLocalAdsCustomer]() { + params.m_onResults(results, isLocalAdsCustomer); + }); }); SetCurrentPositionIfPossible(p); return Search(p); @@ -3567,18 +3569,21 @@ void Framework::VisualizeRoadsInRect(m2::RectD const & rect) }, kScale); } -void Framework::MarkLocalAdsCustomer(search::Result & result) const -{ - if (m_localAdsManager.Contains(result.GetFeatureID())) - result.SetLocalAdsCustomer(true); -} - ads::Engine const & Framework::GetAdsEngine() const { ASSERT(m_adsEngine, ()); return *m_adsEngine; } +bool Framework::IsLocalAdsCustomer(search::Result const & result) const +{ + if (result.IsSuggest()) + return false; + if (result.GetResultType() != search::Result::ResultType::RESULT_FEATURE) + return false; + return m_localAdsManager.Contains(result.GetFeatureID()); +} + vector Framework::GetMwmsByRect(m2::RectD const & rect, bool rough) const { vector result; diff --git a/map/framework.hpp b/map/framework.hpp index 5d33439ccd..efa73794a6 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -345,8 +345,6 @@ public: // Utilities void VisualizeRoadsInRect(m2::RectD const & rect); - void MarkLocalAdsCustomer(search::Result & result) const override; - ads::Engine const & GetAdsEngine() const; protected: @@ -370,6 +368,9 @@ protected: void ClearViewportSearchResults() override { ClearSearchResultsMarks(); } + // EverywhereSearchCallback::Delegate overrides: + bool IsLocalAdsCustomer(search::Result const & result) const override; + private: void ActivateMapSelection(bool needAnimation, df::SelectionShape::ESelectedObject selectionType, diff --git a/map/local_ads_manager.cpp b/map/local_ads_manager.cpp index cd10055e7e..d3d48e4425 100644 --- a/map/local_ads_manager.cpp +++ b/map/local_ads_manager.cpp @@ -458,12 +458,9 @@ bool LocalAdsManager::IsSupportedType(feature::TypesHolder const & types) const return m_supportedTypes.Contains(types); } -std::string const & LocalAdsManager::GetStartCompanyUrl() const +std::string LocalAdsManager::GetStartCompanyUrl() const { return LOCAL_ADS_START_COMPANY_PAGE_HOST; } -std::string const & LocalAdsManager::GetShowStatisticUrl() const -{ - return LOCAL_ADS_STATISTICS_PAGE_HOST; -} +std::string LocalAdsManager::GetShowStatisticUrl() const { return LOCAL_ADS_STATISTICS_PAGE_HOST; } diff --git a/map/local_ads_manager.hpp b/map/local_ads_manager.hpp index 565b2f3aa0..05cd65d261 100644 --- a/map/local_ads_manager.hpp +++ b/map/local_ads_manager.hpp @@ -59,8 +59,8 @@ public: bool Contains(FeatureID const & featureId) const; bool IsSupportedType(feature::TypesHolder const & types) const; - std::string const & GetStartCompanyUrl() const; - std::string const & GetShowStatisticUrl() const; + std::string GetStartCompanyUrl() const; + std::string GetShowStatisticUrl() const; private: enum class RequestType diff --git a/search/everywhere_search_callback.cpp b/search/everywhere_search_callback.cpp index d50acfab47..9b80cda188 100644 --- a/search/everywhere_search_callback.cpp +++ b/search/everywhere_search_callback.cpp @@ -12,22 +12,13 @@ EverywhereSearchCallback::EverywhereSearchCallback(Delegate & delegate, OnResult void EverywhereSearchCallback::operator()(Results const & results) { - auto const prevSize = m_results.GetCount(); + auto const prevSize = m_isLocalAdsCustomer.size(); ASSERT_LESS_OR_EQUAL(prevSize, results.GetCount(), ()); - m_results.AddResultsNoChecks(results.begin() + prevSize, results.end()); + for (size_t i = prevSize; i < results.GetCount(); ++i) + m_isLocalAdsCustomer.push_back(m_delegate.IsLocalAdsCustomer(results[i])); - for (size_t i = prevSize; i < m_results.GetCount(); ++i) - { - if (m_results[i].IsSuggest() || - m_results[i].GetResultType() != search::Result::ResultType::RESULT_FEATURE) - { - continue; - } - - m_delegate.MarkLocalAdsCustomer(m_results[i]); - } - - m_onResults(m_results); + ASSERT_EQUAL(m_isLocalAdsCustomer.size(), results.GetCount(), ()); + m_onResults(results, m_isLocalAdsCustomer); } } // namespace search diff --git a/search/everywhere_search_callback.hpp b/search/everywhere_search_callback.hpp index 785edb0cf9..f45d98dc9d 100644 --- a/search/everywhere_search_callback.hpp +++ b/search/everywhere_search_callback.hpp @@ -1,11 +1,14 @@ #pragma once +#include "search/everywhere_search_params.hpp" #include "search/result.hpp" -#include "search/search_params.hpp" + +#include namespace search { -// An on-results-callback that should be used for interactive search. +// An on-results-callback that should be used for search over all +// maps. // // *NOTE* the class is NOT thread safe. class EverywhereSearchCallback @@ -16,10 +19,10 @@ public: public: virtual ~Delegate() = default; - virtual void MarkLocalAdsCustomer(Result & result) const = 0; + virtual bool IsLocalAdsCustomer(Result const & result) const = 0; }; - using OnResults = SearchParams::TOnResults; + using OnResults = EverywhereSearchParams::OnResults; EverywhereSearchCallback(Delegate & delegate, OnResults onResults); @@ -28,6 +31,6 @@ public: private: Delegate & m_delegate; OnResults m_onResults; - Results m_results; + std::vector m_isLocalAdsCustomer; }; } // namespace search diff --git a/search/everywhere_search_params.hpp b/search/everywhere_search_params.hpp index e6d668f7ff..1eaa264cd1 100644 --- a/search/everywhere_search_params.hpp +++ b/search/everywhere_search_params.hpp @@ -1,18 +1,24 @@ #pragma once #include "search/hotels_filter.hpp" +#include "search/result.hpp" #include "search/search_params.hpp" +#include "std/functional.hpp" #include "std/string.hpp" +#include "std/vector.hpp" namespace search { struct EverywhereSearchParams { + using OnResults = + function const & isLocalAdsCustomer)>; + string m_query; string m_inputLocale; shared_ptr m_hotelsFilter; - SearchParams::TOnResults m_onResults; + OnResults m_onResults; }; } // namespace search diff --git a/search/result.hpp b/search/result.hpp index fb792d3229..b583b5032c 100644 --- a/search/result.hpp +++ b/search/result.hpp @@ -42,8 +42,6 @@ public: osm::YesNoUnknown m_isOpenNow = osm::Unknown; // Valid for any result. bool m_isInitialized = false; - - bool m_isLocalAdsCustomer = false; }; /// For RESULT_FEATURE. @@ -116,9 +114,6 @@ public: // the quality of our search engine. string ToStringForStats() const; - void SetLocalAdsCustomer(bool customer) { m_metadata.m_isLocalAdsCustomer = customer; } - bool IsLocalAdsCustomer() const { return m_metadata.m_isLocalAdsCustomer; } - private: FeatureID m_id; m2::PointD m_center;