Skip to content

Commit

Permalink
[types_strings][android][search] types localization logic for search …
Browse files Browse the repository at this point in the history
…results is moved from core to android
  • Loading branch information
Arsentiy Milchakov authored and beloal committed Sep 25, 2018
1 parent 57751bb commit dbf2f98
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 89 deletions.
4 changes: 3 additions & 1 deletion android/jni/com/mapswithme/maps/SearchEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,9 @@ jobject ToJavaResult(Result & result, search::ProductInfo const & productInfo, b
jni::TScopedLocalRef featureId(env, usermark_helper::CreateFeatureId(env, isFeature ?
result.GetFeatureID() :
kEmptyFeatureId));
jni::TScopedLocalRef featureType(env, jni::ToJavaString(env, result.GetFeatureTypeName()));
string readableType = isFeature ? classif().GetReadableObjectName(result.GetFeatureType()) : "";

jni::TScopedLocalRef featureType(env, jni::ToJavaString(env, readableType));
jni::TScopedLocalRef address(env, jni::ToJavaString(env, result.GetAddress()));
jni::TScopedLocalRef dist(env, jni::ToJavaString(env, distance));
jni::TScopedLocalRef cuisine(env, jni::ToJavaString(env, result.GetCuisine()));
Expand Down
11 changes: 8 additions & 3 deletions android/src/com/mapswithme/maps/discovery/DiscoveryFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,15 @@ private void showFilter()
}

@NonNull
private static MapObject createMapObject(@NonNull Items.SearchItem item)
private MapObject createMapObject(@NonNull Items.SearchItem item)
{
String title = TextUtils.isEmpty(item.getTitle()) ? "" : item.getTitle();
String subtitle = TextUtils.isEmpty(item.getSubtitle()) ? "" : item.getSubtitle();
String featureType = item.getFeatureType();
String subtitle = TextUtils.isEmpty(featureType)
? ""
: Utils.getLocalizedFeatureType(getContext(), featureType);

String title = TextUtils.isEmpty(item.getTitle()) ? subtitle : item.getTitle();

return MapObject.createMapObject(FeatureId.EMPTY, MapObject.SEARCH, title, subtitle,
item.getLat(), item.getLon());
}
Expand Down
21 changes: 17 additions & 4 deletions android/src/com/mapswithme/maps/gallery/Holders.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,15 @@ public SearchViewHolder(@NonNull View itemView, @NonNull List<Items.SearchItem>
public void bind(@NonNull Items.SearchItem item)
{
super.bind(item);
UiUtils.setTextAndHideIfEmpty(mTitle, item.getTitle());
UiUtils.setTextAndHideIfEmpty(mSubtitle, item.getSubtitle());

String featureType = item.getFeatureType();
String localizedType = TextUtils.isEmpty(featureType)
? ""
: Utils.getLocalizedFeatureType(mSubtitle.getContext(), featureType);
String title = TextUtils.isEmpty(item.getTitle()) ? localizedType : item.getTitle();

UiUtils.setTextAndHideIfEmpty(mTitle, title);
UiUtils.setTextAndHideIfEmpty(mSubtitle, localizedType);
UiUtils.setTextAndHideIfEmpty(mDistance, item.getDistance());
UiUtils.showIf(item.getPopularity().getType() == Popularity.Type.POPULAR, mPopularTagRating);

Expand Down Expand Up @@ -282,9 +289,15 @@ public HotelViewHolder(@NonNull View itemView, @NonNull List<Items.SearchItem> i
@Override
public void bind(@NonNull Items.SearchItem item)
{
UiUtils.setTextAndHideIfEmpty(mTitle, item.getTitle());
String featureType = item.getFeatureType();
String localizedType = TextUtils.isEmpty(featureType)
? ""
: Utils.getLocalizedFeatureType(mSubtitle.getContext(), featureType);
String title = TextUtils.isEmpty(item.getTitle()) ? localizedType : item.getTitle();

UiUtils.setTextAndHideIfEmpty(mTitle, title);
UiUtils.setTextAndHideIfEmpty(mSubtitle, formatDescription(item.getStars(),
item.getFeatureType(),
localizedType,
item.getPrice(),
mSubtitle.getResources()));

Expand Down
20 changes: 17 additions & 3 deletions android/src/com/mapswithme/maps/search/SearchAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.mapswithme.util.Graphics;
import com.mapswithme.util.ThemeUtils;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;

import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -84,7 +85,18 @@ void bind(@NonNull SearchData result, int order)
{
mResult = (SearchResult)result;
mOrder = order;
SpannableStringBuilder builder = new SpannableStringBuilder(mResult.name);
TextView titleView = getTitleView();

String title = mResult.name;
if (TextUtils.isEmpty(title))
{
SearchResult.Description description = mResult.description;
title = description != null
? Utils.getLocalizedFeatureType(titleView.getContext(), description.featureType)
: "";
}

SpannableStringBuilder builder = new SpannableStringBuilder(title);
if (mResult.highlightRanges != null)
{
final int size = mResult.highlightRanges.length / 2;
Expand All @@ -99,7 +111,6 @@ void bind(@NonNull SearchData result, int order)
}
}

TextView titleView = getTitleView();
if (titleView != null)
titleView.setText(builder);
}
Expand Down Expand Up @@ -201,7 +212,10 @@ int getTintAttr()
// FIXME: Better format based on result type
private CharSequence formatDescription(SearchResult result, boolean isHotelAvailable)
{
final SpannableStringBuilder res = new SpannableStringBuilder(result.description.featureType);

String localizedType = Utils.getLocalizedFeatureType(mFrame.getContext(),
result.description.featureType);
final SpannableStringBuilder res = new SpannableStringBuilder(localizedType);
final SpannableStringBuilder tail = new SpannableStringBuilder();

int stars = result.description.stars;
Expand Down
10 changes: 7 additions & 3 deletions android/src/com/mapswithme/maps/search/SearchFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,13 @@ private void processSelected(@NonNull SearchResult result)
if (RoutingController.get().isWaitingPoiPick())
{
SearchResult.Description description = result.description;
final MapObject point = MapObject.createMapObject(FeatureId.EMPTY, MapObject.SEARCH, result.name,
description != null ? description.featureType : "",
result.lat, result.lon);
String subtitle = description != null
? Utils.getLocalizedFeatureType(getContext(), description.featureType)
: "";
String title = TextUtils.isEmpty(result.name) ? subtitle : "";

final MapObject point = MapObject.createMapObject(FeatureId.EMPTY, MapObject.SEARCH,
title, subtitle, result.lat, result.lon);
RoutingController.get().onPoiSelected(point);
}

Expand Down
9 changes: 2 additions & 7 deletions android/src/com/mapswithme/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -712,17 +712,12 @@ public interface Proc<T>
void invoke(@NonNull T param);
}

@StringRes
public static int getStringIdForFeatureType(@NonNull Context context, String type)
public static String getLocalizedFeatureType(@NonNull Context context, @NonNull String type)
{
String key = "type." + type.replace('-', '.');
return getStringIdByKey(context, key);
}

public static String getLocalizedFeatureType(@NonNull Context context, String type)
{
@StringRes
int id = getStringIdForFeatureType(context, type);
int id = getStringIdByKey(context, key);

try
{
Expand Down
38 changes: 10 additions & 28 deletions editor/editor_tests/new_feature_categories_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,31 @@ UNIT_TEST(NewFeatureCategories_UniqueNames)
using namespace std;

classificator::Load();
auto const & cl = classif();

editor::EditorConfig config;
osm::NewFeatureCategories categories(config);

auto const & disabled = CategoriesHolder::kDisabledLanguages;

bool noDuplicates = true;
for (auto const & locale : CategoriesHolder::kLocaleMapping)
{
string const lang(locale.m_name);
if (find(disabled.begin(), disabled.end(), lang) != disabled.end())
continue;
categories.AddLanguage(lang);
auto const & names = categories.GetAllCategoryNames(lang);
auto names = categories.GetAllCategoryNames();

auto firstFn = bind(&pair<string, uint32_t>::first, placeholders::_1);
set<string> uniqueNames(make_transform_iterator(names.begin(), firstFn),
make_transform_iterator(names.end(), firstFn));
if (uniqueNames.size() == names.size())
continue;

LOG(LWARNING, ("Invalid category translations", lang));

map<string, vector<uint32_t>> typesByName;
for (auto const & entry : names)
typesByName[entry.first].push_back(entry.second);
auto result = std::unique(names.begin(), names.end());

for (auto const & entry : typesByName)
if (result != names.end())
{
if (entry.second.size() <= 1)
continue;
noDuplicates = false;
ostringstream str;
str << entry.first << ":";
for (auto const & type : entry.second)
str << " " << cl.GetReadableObjectName(type);
LOG(LWARNING, (str.str()));
}
LOG(LWARNING, ("Types duplication detected! The following types are duplicated:"));
do
{
LOG(LWARNING, (*result));
} while (++result != names.end());

LOG(LWARNING,
("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"));
TEST(false, ("Please look at output above"));
}
};

TEST(noDuplicates, ());
}
5 changes: 2 additions & 3 deletions map/address_finder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,9 @@ vector<string> Framework::GetPrintableFeatureTypes(FeatureType & ft) const

feature::TypesHolder types(ft);
types.SortBySpec();
// Try to add types from categories.
CategoriesHolder const & cats = GetDefaultCategories();
auto const & c = classif();
for (uint32_t type : types)
results.push_back(cats.GetReadableFeatureType(type, locale));
results.push_back(c.GetReadableObjectName(type));
return results;
}

Expand Down
5 changes: 1 addition & 4 deletions map/discovery/discovery_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ search::Result MakeResultFromFeatureType(FeatureType & ft)

feature::TypesHolder holder(ft);
holder.SortBySpec();
CategoriesHolder const & categories = GetDefaultCategories();
auto const readableType = categories.GetReadableFeatureType(
holder.GetBestType(), categories.MapLocaleToInteger(languages::GetCurrentOrig()));

search::Result::Metadata metadata;
search::ProcessMetadata(ft, metadata);

return {ft.GetID(), feature::GetCenter(ft), name, "", readableType, holder.GetBestType(), metadata};
return {ft.GetID(), feature::GetCenter(ft), name, "", holder.GetBestType(), metadata};
}

FeatureType MakeFeatureTypeWithCachedGuard(DataSource const & dataSource, MwmSet::MwmId & mwmId,
Expand Down
4 changes: 2 additions & 2 deletions map/framework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2792,8 +2792,8 @@ bool Framework::ParseEditorDebugCommand(search::SearchParams const & params)
ft.GetReadableName(name);
feature::TypesHolder const types(ft);
search::Result::Metadata smd;
results.AddResultNoChecks(search::Result(fid, feature::GetCenter(ft), name, edit.second,
DebugPrint(types), types.GetBestType(), smd));
results.AddResultNoChecks(
search::Result(fid, feature::GetCenter(ft), name, edit.second, types.GetBestType(), smd));
}
params.m_onResults(results);

Expand Down
4 changes: 2 additions & 2 deletions map/map_tests/booking_filter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ UNIT_CLASS_TEST(TestMwmEnvironment, BookingFilter_AvailabilitySmoke)
[&results, &expectedResults](FeatureType & ft) {
search::Result::Metadata metadata;
metadata.m_isSponsoredHotel = true;
search::Result result(ft.GetID(), ft.GetCenter(), "", "", "", 0, metadata);
search::Result result(ft.GetID(), ft.GetCenter(), "", "", 0, metadata);
auto copy = result;
results.AddResult(std::move(result));
expectedResults.AddResult(std::move(copy));
Expand Down Expand Up @@ -164,7 +164,7 @@ UNIT_CLASS_TEST(TestMwmEnvironment, BookingFilter_ProcessorSmoke)
metadata.m_isSponsoredHotel = true;
std::string name;
ft.GetName(StringUtf8Multilang::kDefaultCode, name);
search::Result result(ft.GetID(), ft.GetCenter(), name, "", "", 0, metadata);
search::Result result(ft.GetID(), ft.GetCenter(), name, "", 0, metadata);
InsertResult(result, results);

auto const sponsoredId = ft.GetMetadata().Get(feature::Metadata::FMD_SPONSORED_ID);
Expand Down
5 changes: 2 additions & 3 deletions qt/create_feature_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ CreateFeatureDialog::CreateFeatureDialog(QWidget * parent, osm::NewFeatureCatego

QListWidget * allSortedList = new QListWidget();

auto const & categories = cats.GetAllCategoryNames(languages::GetCurrentNorm());
auto const & categories = cats.GetAllCategoryNames();
for (auto const & entry : categories)
{
QListWidgetItem * lwi = new QListWidgetItem(entry.first.c_str() /* name */, allSortedList);
lwi->setData(Qt::UserRole, entry.second /* type */);
new QListWidgetItem(entry.c_str() /* name */, allSortedList);
}
connect(allSortedList, SIGNAL(clicked(QModelIndex const &)), this,
SLOT(OnListItemSelected(QModelIndex const &)));
Expand Down
6 changes: 2 additions & 4 deletions qt/editor_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ EditorDialog::EditorDialog(QWidget * parent, osm::EditableMapObject & emo)

{ // Feature types.
grid->addWidget(new QLabel("Type:"), row, 0);
string localized = m_feature.GetLocalizedType();

string const raw = DebugPrint(m_feature.GetTypes());
if (!strings::EqualNoCase(localized, raw))
localized += " (" + raw + ")";
QLabel * label = new QLabel(QString::fromStdString(localized));
QLabel * label = new QLabel(QString::fromStdString(raw));
label->setTextInteractionFlags(Qt::TextSelectableByMouse);
grid->addWidget(label, row++, 1);
}
Expand Down
3 changes: 2 additions & 1 deletion qt/search_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ void SearchPanel::OnSearchResults(uint64_t timestamp, search::Results const & re

if (res.GetResultType() == search::Result::Type::Feature)
{
m_pTable->setItem(rowCount, 0, CreateItem(QString::fromStdString(res.GetFeatureTypeName())));
string readableType = classif().GetReadableObjectName(res.GetFeatureType());
m_pTable->setItem(rowCount, 0, CreateItem(QString::fromStdString(readableType)));
m_pTable->setItem(rowCount, 3, CreateItem(m_pDrawWidget->GetDistance(res).c_str()));
}

Expand Down
4 changes: 1 addition & 3 deletions search/ranker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,7 @@ Result Ranker::MakeResult(RankerResult const & rankerResult, bool needAddress,
case RankerResult::Type::TYPE_BUILDING:
{
auto const type = rankerResult.GetBestType(m_params.m_preferredTypes);
return Result(r.GetID(), r.GetCenter(), name, address,
m_categories.GetReadableFeatureType(type, m_params.m_currentLocaleCode), type,
r.GetMetadata());
return Result(r.GetID(), r.GetCenter(), name, address, type, r.GetMetadata());
}
case RankerResult::Type::TYPE_LATLON: return Result(r.GetCenter(), name, address);
}
Expand Down
26 changes: 19 additions & 7 deletions search/result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "search/common.hpp"
#include "search/geometry_utils.hpp"

#include "indexer/classificator.hpp"

#include "base/string_utils.hpp"

#include <sstream>
Expand Down Expand Up @@ -34,14 +36,12 @@ string Join(string const & s, Args &&... args)

// Result ------------------------------------------------------------------------------------------
Result::Result(FeatureID const & id, m2::PointD const & pt, string const & str,
string const & address, string const & featureTypeName, uint32_t featureType,
Metadata const & meta)
string const & address, uint32_t featureType, Metadata const & meta)
: m_resultType(Type::Feature)
, m_id(id)
, m_center(pt)
, m_str(str.empty() ? featureTypeName : str)
, m_str(str)
, m_address(address)
, m_featureTypeName(featureTypeName)
, m_featureType(featureType)
, m_metadata(meta)
{
Expand All @@ -62,7 +62,6 @@ Result::Result(Result const & res, string const & suggest)
, m_center(res.m_center)
, m_str(res.m_str)
, m_address(res.m_address)
, m_featureTypeName(res.m_featureTypeName)
, m_featureType(res.m_featureType)
, m_suggestionStr(suggest)
, m_hightlightRanges(res.m_hightlightRanges)
Expand All @@ -86,6 +85,11 @@ FeatureID const & Result::GetFeatureID() const
return m_id;
}

uint32_t Result::GetFeatureType() const
{
return m_featureType;
}

m2::PointD Result::GetFeatureCenter() const
{
ASSERT(HasPoint(), ());
Expand Down Expand Up @@ -146,10 +150,14 @@ void Result::PrependCity(string const & name)

string Result::ToStringForStats() const
{
string readableType;
if (GetResultType() == Type::Feature)
readableType = classif().GetReadableObjectName(m_featureType);

string s;
s.append(GetString());
s.append("|");
s.append(GetFeatureTypeName());
s.append(readableType);
s.append("|");
s.append(IsSuggest() ? "1" : "0");
return s;
Expand All @@ -170,10 +178,14 @@ string DebugPrint(Result::Type type)

string DebugPrint(Result const & result)
{
string readableType;
if (result.GetResultType() == Result::Type::Feature)
readableType = classif().GetReadableObjectName(result.GetFeatureType());

ostringstream os;
os << "Result [";
os << "name: " << result.GetString() << ", ";
os << "type: " << result.GetFeatureTypeName() << ", ";
os << "type: " << readableType << ", ";
os << "info: " << DebugPrint(result.GetRankingInfo());
os << "]";
return os.str();
Expand Down
Loading

0 comments on commit dbf2f98

Please sign in to comment.