[ios] Use cuisine translations from the core.

This commit is contained in:
Alex Zolotarev 2016-02-27 23:43:40 +03:00 committed by Sergey Yershov
parent c72ff3cb24
commit bb8f275bf9
8 changed files with 69 additions and 130 deletions

View file

@ -1,11 +1,16 @@
#include "std/string.hpp"
@protocol MWMCuisineEditorTableViewCellProtocol <NSObject>
- (void)change:(NSString *)key selected:(BOOL)selected;
- (void)change:(string const &)key selected:(BOOL)selected;
@end
@interface MWMCuisineEditorTableViewCell : UITableViewCell
- (void)configWithDelegate:(id<MWMCuisineEditorTableViewCellProtocol>)delegate key:(NSString *)key selected:(BOOL)selected;
- (void)configWithDelegate:(id<MWMCuisineEditorTableViewCellProtocol>)delegate
key:(string const &)key
translation:(string const &)translation
selected:(BOOL)selected;
@end

View file

@ -1,23 +1,27 @@
#import "MWMCuisineEditorTableViewCell.h"
@interface MWMCuisineEditorTableViewCell ()
{
string m_key;
}
@property (weak, nonatomic) IBOutlet UILabel * label;
@property (weak, nonatomic) IBOutlet UIImageView * selectedIcon;
@property (weak, nonatomic) id<MWMCuisineEditorTableViewCellProtocol> delegate;
@property (copy, nonatomic) NSString * key;
@end
@implementation MWMCuisineEditorTableViewCell
- (void)configWithDelegate:(id<MWMCuisineEditorTableViewCellProtocol>)delegate key:(NSString *)key selected:(BOOL)selected
- (void)configWithDelegate:(id<MWMCuisineEditorTableViewCellProtocol>)delegate
key:(string const &)key
translation:(string const &)translation
selected:(BOOL)selected
{
self.delegate = delegate;
self.key = key;
NSString * cuisine = [NSString stringWithFormat:@"cuisine_%@", key];
self.label.text = [L(cuisine) capitalizedStringWithLocale:[NSLocale currentLocale]];
m_key = key;
self.label.text = @(translation.c_str());
self.selectedIcon.hidden = !selected;
}
@ -27,7 +31,7 @@
{
BOOL const selected = self.selectedIcon.hidden;
self.selectedIcon.hidden = !selected;
[self.delegate change:self.key selected:selected];
[self.delegate change:m_key selected:selected];
}
@end

View file

@ -1,8 +1,12 @@
#import "MWMTableViewController.h"
#include "std/string.hpp"
#include "std/vector.hpp"
@protocol MWMCuisineEditorProtocol <NSObject>
@property (nonatomic) NSSet<NSString *> * cuisines;
- (vector<string>)getSelectedCuisines;
- (void)setSelectedCuisines:(vector<string> const &)cuisines;
@end

View file

@ -2,18 +2,31 @@
#import "MWMCuisineEditorViewController.h"
#import "UIColor+MapsMeColor.h"
#include "indexer/search_string_utils.hpp"
#include "indexer/cuisines.hpp"
#include "std/algorithm.hpp"
#include "std/set.hpp"
namespace
{
NSString * const kCuisineEditorCell = @"MWMCuisineEditorTableViewCell";
NSString * const kCuisineEditorCell = @"MWMCuisineEditorTableViewCell";
/// @returns pair.first in a separate vector.
vector<string> SliceKeys(vector<pair<string, string>> const & v)
{
vector<string> res;
for (auto const & kv : v)
res.push_back(kv.first);
return res;
}
} // namespace
@interface MWMCuisineEditorViewController ()<MWMCuisineEditorTableViewCellProtocol, UISearchBarDelegate>
@property (copy, nonatomic) NSArray<NSString *> * cuisineKeys;
@property (copy, nonatomic) NSArray<NSString *> * filtredKeys;
@property (nonatomic) NSMutableSet<NSString *> * selectedCuisines;
{
osm::TAllCuisines m_allCuisines;
vector<string> m_selectedCuisines;
vector<string> m_displayedKeys;
}
@property (weak, nonatomic) IBOutlet UISearchBar * searchBar;
@ -34,18 +47,19 @@ namespace
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (!searchText.length)
m_displayedKeys.clear();
if (searchText.length)
{
self.filtredKeys = nil;
return;
string const st = searchText.UTF8String;
for (auto const & kv : m_allCuisines)
if (search::ContainsNormalized(kv.second, st))
m_displayedKeys.push_back(kv.first);
}
NSLocale * locale = [NSLocale currentLocale];
NSString * query = [searchText lowercaseStringWithLocale:locale];
self.filtredKeys = [self.cuisineKeys filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString * value, NSDictionary<NSString *,id> *)
else
{
NSString * cuisine = [NSString stringWithFormat:@"cuisine_%@", value];
return [[L(cuisine) lowercaseStringWithLocale:locale] containsString:query];
}]];
m_displayedKeys = SliceKeys(m_allCuisines);
}
[self.tableView reloadData];
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
@ -66,7 +80,8 @@ namespace
[searchBar resignFirstResponder];
searchBar.text = @"";
[self searchBar:searchBar setActiveState:NO];
self.filtredKeys = nil;
m_displayedKeys = SliceKeys(m_allCuisines);
[self.tableView reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
@ -112,23 +127,9 @@ namespace
- (void)configData
{
NSString * stringsPath = [[NSBundle mainBundle] pathForResource:@"Localizable" ofType:@"strings"];
NSArray<NSString *> * allKeys = [NSDictionary dictionaryWithContentsOfFile:stringsPath].allKeys;
NSMutableSet<NSString *> * cuisineKeys = [NSMutableSet set];
NSString * prefix = @"cuisine_";
NSUInteger const prefixLength = prefix.length;
for (NSString * key in allKeys)
{
if ([key hasPrefix:prefix])
[cuisineKeys addObject:[key substringFromIndex:prefixLength]];
}
self.cuisineKeys = [cuisineKeys.allObjects sortedArrayUsingComparator:^NSComparisonResult(NSString * s1, NSString * s2)
{
NSString * cus1 = L([prefix stringByAppendingString:s1]);
NSString * cus2 = L([prefix stringByAppendingString:s2]);
return [cus1 compare:cus2 options:NSCaseInsensitiveSearch range:{0, cus1.length} locale:[NSLocale currentLocale]];
}];
self.selectedCuisines = [self.delegate.cuisines mutableCopy];
m_allCuisines = osm::Cuisines::Instance().AllSupportedCuisines();
m_displayedKeys = SliceKeys(m_allCuisines);
m_selectedCuisines = [self.delegate getSelectedCuisines];
}
- (void)configTable
@ -137,19 +138,6 @@ namespace
forCellReuseIdentifier:kCuisineEditorCell];
}
#pragma mark - Accessors
- (NSArray<NSString *> *)displayKeys
{
return self.filtredKeys != nil ? self.filtredKeys : self.cuisineKeys;
}
- (void)setFiltredKeys:(NSArray<NSString *> *)filtredKeys
{
_filtredKeys = filtredKeys;
[self.tableView reloadData];
}
#pragma mark - Actions
- (void)onCancel
@ -159,18 +147,18 @@ namespace
- (void)onDone
{
[self.delegate setCuisines:self.selectedCuisines];
[self.delegate setSelectedCuisines:m_selectedCuisines];
[self onCancel];
}
#pragma mark - MWMCuisineEditorTableViewCellProtocol
- (void)change:(NSString *)key selected:(BOOL)selected
- (void)change:(string const &)key selected:(BOOL)selected
{
if (selected)
[self.selectedCuisines addObject:key];
m_selectedCuisines.push_back(key);
else
[self.selectedCuisines removeObject:key];
m_selectedCuisines.erase(find(m_selectedCuisines.begin(), m_selectedCuisines.end(), key));
}
#pragma mark - UITableViewDataSource
@ -182,7 +170,7 @@ namespace
- (NSInteger)tableView:(UITableView * _Nonnull)tableView numberOfRowsInSection:(NSInteger)section
{
return self.displayKeys.count;
return m_displayedKeys.size();
}
#pragma mark - UITableViewDelegate
@ -190,9 +178,9 @@ namespace
- (void)tableView:(UITableView * _Nonnull)tableView willDisplayCell:(MWMCuisineEditorTableViewCell * _Nonnull)cell forRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath
{
NSInteger const index = indexPath.row;
NSString * cuisine = self.displayKeys[index];
BOOL const selected = [self.selectedCuisines containsObject:cuisine];
[cell configWithDelegate:self key:cuisine selected:selected];
string const & key = m_displayedKeys[index];
BOOL const selected = find(m_selectedCuisines.begin(), m_selectedCuisines.end(), key) != m_selectedCuisines.end();
[cell configWithDelegate:self key:key translation:osm::Cuisines::Instance().Translate(key) selected:selected];
}
@end

View file

@ -308,7 +308,6 @@ NSString * reuseIdentifier(MWMPlacePageCellType cellType)
case MWMPlacePageCellTypeCuisine:
{
MWMEditorSelectTableViewCell * tCell = (MWMEditorSelectTableViewCell *)cell;
// NSString * text = [entityValue capitalizedStringWithLocale:[NSLocale currentLocale]];
[tCell configWithDelegate:self
icon:[UIImage imageNamed:@"ic_placepage_cuisine"]
text:@(m_mapObject.FormatCuisines().c_str())

View file

@ -12,9 +12,10 @@
#import "Statistics.h"
#import "UIColor+MapsMeColor.h"
#include "map/place_page_info.hpp"
extern CGFloat const kBottomPlacePageOffset = 15.;
extern CGFloat const kLabelsBetweenOffset = 8.;
extern NSString * const kMWMCuisineSeparator;
namespace
{
@ -82,7 +83,7 @@ CGFloat placePageWidth()
vector<NSRange> separatorsLocationInString(NSString * str)
{
vector<NSRange> r {};
vector<NSRange> r;
if (str.length == 0)
return r;
@ -90,7 +91,7 @@ vector<NSRange> separatorsLocationInString(NSString * str)
while (searchRange.location < str.length)
{
searchRange.length = str.length - searchRange.location;
NSRange const foundRange = [str rangeOfString:kMWMCuisineSeparator options:NSCaseInsensitiveSearch range:searchRange];
NSRange const foundRange = [str rangeOfString:@(place_page::Info::kSubtitleSeparator) options:NSCaseInsensitiveSearch range:searchRange];
if (foundRange.location == NSNotFound)
break;
searchRange.location = foundRange.location + foundRange.length;

View file

@ -27,8 +27,6 @@ using MWMPlacePageCellTypeValueMap = map<MWMPlacePageCellType, string>;
@interface MWMPlacePageEntity : NSObject
+ (NSString *)makeMWMCuisineString:(NSSet<NSString *> *)cuisines;
@property (copy, nonatomic) NSString * title;
@property (copy, nonatomic) NSString * category;
@property (copy, nonatomic) NSString * address;
@ -37,7 +35,6 @@ using MWMPlacePageCellTypeValueMap = map<MWMPlacePageCellType, string>;
@property (copy, nonatomic) NSString * bookmarkDescription;
@property (nonatomic, readonly) BOOL isHTMLDescription;
@property (copy, nonatomic) NSString * bookmarkColor;
@property (copy, nonatomic) NSSet<NSString *> * cuisines;
@property (nonatomic) BookmarkAndCategory bac;
@property (weak, nonatomic) MWMPlacePageViewManager * manager;

View file

@ -9,27 +9,10 @@
using feature::Metadata;
extern NSString * const kUserDefaultsLatLonAsDMSKey = @"UserDefaultsLatLonAsDMS";
extern NSString * const kMWMCuisineSeparator = @" • ";
namespace
{
NSString * const kOSMCuisineSeparator = @";";
//TODO(Alex): If we can format cuisines in subtitle we won't need this function.
//NSString * makeOSMCuisineString(NSSet<NSString *> * cuisines)
//{
// NSMutableArray<NSString *> * osmCuisines = [NSMutableArray arrayWithCapacity:cuisines.count];
// for (NSString * cuisine in cuisines)
// [osmCuisines addObject:cuisine];
// [osmCuisines sortUsingComparator:^NSComparisonResult(NSString * s1, NSString * s2)
// {
// return [s1 compare:s2];
// }];
// return [osmCuisines componentsJoinedByString:kOSMCuisineSeparator];
//}
NSUInteger gMetaFieldsMap[MWMPlacePageCellTypeCount] = {};
void putFields(NSUInteger eTypeValue, NSUInteger ppValue)
@ -65,24 +48,6 @@ void initFieldsMap()
place_page::Info m_info;
}
+ (NSString *)makeMWMCuisineString:(NSSet<NSString *> *)cuisines
{
NSString * prefix = @"cuisine_";
NSMutableArray<NSString *> * localizedCuisines = [NSMutableArray arrayWithCapacity:cuisines.count];
for (NSString * cus in cuisines)
{
NSString * cuisine = [prefix stringByAppendingString:cus];
NSString * localizedCuisine = L(cuisine);
BOOL const noLocalization = [localizedCuisine isEqualToString:cuisine];
[localizedCuisines addObject:noLocalization ? cus : localizedCuisine];
}
[localizedCuisines sortUsingComparator:^NSComparisonResult(NSString * s1, NSString * s2)
{
return [s1 compare:s2 options:NSCaseInsensitiveSearch range:{0, s1.length} locale:[NSLocale currentLocale]];
}];
return [localizedCuisines componentsJoinedByString:kMWMCuisineSeparator];
}
- (instancetype)initWithInfo:(const place_page::Info &)info
{
self = [super init];
@ -176,11 +141,6 @@ void initFieldsMap()
[ud synchronize];
}
- (void)deserializeCuisine:(NSString *)cuisine
{
self.cuisines = [NSSet setWithArray:[cuisine componentsSeparatedByString:kOSMCuisineSeparator]];
}
#pragma mark - Getters
- (NSString *)getCellValue:(MWMPlacePageCellType)cellType
@ -254,25 +214,6 @@ void initFieldsMap()
: MeasurementUtils::FormatLatLonAsDMS(latlon.lat, latlon.lon, 2)).c_str());
}
#pragma mark - Properties
@synthesize cuisines = _cuisines;
- (NSSet<NSString *> *)cuisines
{
if (!_cuisines)
_cuisines = [NSSet set];
return _cuisines;
}
- (void)setCuisines:(NSSet<NSString *> *)cuisines
{
if ([_cuisines isEqualToSet:cuisines])
return;
_cuisines = cuisines;
[self setMetaField:MWMPlacePageCellTypeCuisine
value:[MWMPlacePageEntity makeMWMCuisineString:cuisines].UTF8String];
}
#pragma mark - Bookmark editing
- (void)setBac:(BookmarkAndCategory)bac