[search] Fixed search completion.

This commit is contained in:
Yuri Gorshenin 2017-04-21 13:06:09 +03:00
parent 7f96c9a7fb
commit 99e81fbb04
14 changed files with 78 additions and 60 deletions

View file

@ -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 <cstdint>
#include <memory>
#include <vector>
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<bool> 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<bool> 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<jlong>(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;

View file

@ -18,6 +18,7 @@
+ (MWMSearchItemType)resultTypeWithRow:(NSUInteger)row;
+ (NSUInteger)containerIndexWithRow:(NSUInteger)row;
+ (search::Result const &)resultWithContainerIndex:(NSUInteger)index;
+ (BOOL)isLocalAdsWithContainerIndex:(NSUInteger)index;
+ (id<MWMBanner>)adWithContainerIndex:(NSUInteger)index;
+ (void)update;

View file

@ -52,6 +52,7 @@ using TObservers = NSHashTable<__kindof TObserver>;
search::EverywhereSearchParams m_everywhereParams;
search::ViewportSearchParams m_viewportParams;
search::Results m_everywhereResults;
vector<bool> 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<bool> 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<MWMBanner>)adWithContainerIndex:(NSUInteger)index
{
return [[MWMSearch manager].banners bannerAtIndex:index];

View file

@ -4,6 +4,6 @@
@interface MWMSearchCommonCell : MWMSearchCell
- (void)config:(search::Result const &)result;
- (void)config:(search::Result const &)result isLocalAds:(BOOL)isLocalAds;
@end

View file

@ -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

View file

@ -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:

View file

@ -1314,9 +1314,11 @@ bool Framework::SearchEverywhere(search::EverywhereSearchParams const & params)
p.m_onResults = search::EverywhereSearchCallback(
static_cast<search::EverywhereSearchCallback::Delegate &>(*this),
[this, params](search::Results const & results) {
[this, params](search::Results const & results, vector<bool> 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<MwmSet::MwmId> Framework::GetMwmsByRect(m2::RectD const & rect, bool rough) const
{
vector<MwmSet::MwmId> result;

View file

@ -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,

View file

@ -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; }

View file

@ -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

View file

@ -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

View file

@ -1,11 +1,14 @@
#pragma once
#include "search/everywhere_search_params.hpp"
#include "search/result.hpp"
#include "search/search_params.hpp"
#include <vector>
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<bool> m_isLocalAdsCustomer;
};
} // namespace search

View file

@ -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<void(Results const & results, vector<bool> const & isLocalAdsCustomer)>;
string m_query;
string m_inputLocale;
shared_ptr<hotels_filter::Rule> m_hotelsFilter;
SearchParams::TOnResults m_onResults;
OnResults m_onResults;
};
} // namespace search

View file

@ -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;