From 13cc393e39071cf09f025a44ee5c74b7182e100e Mon Sep 17 00:00:00 2001 From: VladiMihaylenko Date: Wed, 16 Mar 2016 14:53:53 +0300 Subject: [PATCH] [ios] Fixed cuisines editing with untranslated keys. --- indexer/cuisines.hpp | 2 +- .../Cuisine/MWMCuisineEditorViewController.mm | 53 ++++++++++++++++--- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/indexer/cuisines.hpp b/indexer/cuisines.hpp index 2d745e302c..049f545372 100644 --- a/indexer/cuisines.hpp +++ b/indexer/cuisines.hpp @@ -23,7 +23,7 @@ public: void ParseAndLocalize(string const & osmRawCuisinesTagValue, vector & outCuisines, string const & lang = languages::GetCurrentTwine()); /// @param[in] lang should be in our twine strings.txt/cuisines.txt format. - /// @returns translated cuisine. + /// @returns translated cuisine (can be empty, if we can't translate key). string Translate(string const & singleOsmCuisine, string const & lang = languages::GetCurrentTwine()); /// @returns list of osm cuisines in cuisines.txt (not localized). diff --git a/iphone/Maps/Classes/Editor/Cuisine/MWMCuisineEditorViewController.mm b/iphone/Maps/Classes/Editor/Cuisine/MWMCuisineEditorViewController.mm index 50e24e9cab..a2eceda025 100644 --- a/iphone/Maps/Classes/Editor/Cuisine/MWMCuisineEditorViewController.mm +++ b/iphone/Maps/Classes/Editor/Cuisine/MWMCuisineEditorViewController.mm @@ -24,9 +24,11 @@ vector SliceKeys(vector> const & v) osm::TAllCuisines m_allCuisines; vector m_selectedCuisines; vector m_displayedKeys; + vector m_untranslatedKeys; } @property (weak, nonatomic) IBOutlet UISearchBar * searchBar; +@property (nonatomic) BOOL isSearch; @end @@ -47,6 +49,7 @@ vector SliceKeys(vector> const & v) m_displayedKeys.clear(); if (searchText.length) { + self.isSearch = YES; string const st = searchText.UTF8String; for (auto const & kv : m_allCuisines) if (search::ContainsNormalized(kv.second, st)) @@ -54,6 +57,7 @@ vector SliceKeys(vector> const & v) } else { + self.isSearch = NO; m_displayedKeys = SliceKeys(m_allCuisines); } [self.tableView reloadData]; @@ -61,6 +65,7 @@ vector SliceKeys(vector> const & v) - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { + self.isSearch = NO; [self searchBar:searchBar setActiveState:YES]; return YES; } @@ -68,12 +73,16 @@ vector SliceKeys(vector> const & v) - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar { if (!searchBar.text.length) + { + self.isSearch = NO; [self searchBar:searchBar setActiveState:NO]; + } return YES; } - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { + self.isSearch = NO; [searchBar resignFirstResponder]; searchBar.text = @""; [self searchBar:searchBar setActiveState:NO]; @@ -115,6 +124,7 @@ vector SliceKeys(vector> const & v) - (void)configSearchBar { + self.isSearch = NO; self.searchBar.backgroundImage = [UIImage imageWithColor:[UIColor primary]]; self.searchBar.placeholder = L(@"search_in_cuisine"); UITextField * textFiled = [self.searchBar valueForKey:@"searchField"]; @@ -124,9 +134,16 @@ vector SliceKeys(vector> const & v) - (void)configData { - m_allCuisines = osm::Cuisines::Instance().AllSupportedCuisines(); + using namespace osm; + m_allCuisines = Cuisines::Instance().AllSupportedCuisines(); m_displayedKeys = SliceKeys(m_allCuisines); m_selectedCuisines = [self.delegate getSelectedCuisines]; + for (auto const & s : m_selectedCuisines) + { + string const translated = Cuisines::Instance().Translate(s); + if (translated.empty()) + m_untranslatedKeys.push_back(s); + } } #pragma mark - Actions @@ -158,21 +175,41 @@ vector SliceKeys(vector> const & v) { UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:[UITableViewCell className]]; NSInteger const index = indexPath.row; - string const & key = m_displayedKeys[index]; - string translated; - if (!osm::Cuisines::Instance().Translate(m_displayedKeys[index], translated)) - NSAssert(false, @"There is only transled keys in m_displayedKeys!"); + auto const & dataSource = [self dataSourceForSection:indexPath.section]; + string const & key = dataSource[index]; + if (dataSource == m_displayedKeys) + { + string const translated = osm::Cuisines::Instance().Translate(m_displayedKeys[index]); + NSAssert(!translated.empty(), @"There are only localizable keys in m_displayedKeys!"); + cell.textLabel.text = @(translated.c_str()); + } + else + { + cell.textLabel.text = @(key.c_str()); + } - cell.textLabel.text = @(translated.c_str()); BOOL const selected = find(m_selectedCuisines.begin(), m_selectedCuisines.end(), key) != m_selectedCuisines.end(); cell.accessoryType = selected ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; return cell; } +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView +{ + return self.isSearch ? 1 : !m_untranslatedKeys.empty() + !m_displayedKeys.empty(); +} + - (NSInteger)tableView:(UITableView * _Nonnull)tableView numberOfRowsInSection:(NSInteger)section { - return m_displayedKeys.size(); + return [self dataSourceForSection:section].size(); +} + +- (vector const &)dataSourceForSection:(NSInteger)section +{ + if (m_untranslatedKeys.empty()) + return m_displayedKeys; + else + return self.isSearch ? m_displayedKeys : (section == 0 ? m_untranslatedKeys : m_displayedKeys); } #pragma mark - UITableViewDelegate @@ -183,7 +220,7 @@ vector SliceKeys(vector> const & v) [cell setSelected:NO animated:YES]; BOOL const isAlreadySelected = cell.accessoryType == UITableViewCellAccessoryCheckmark; cell.accessoryType = isAlreadySelected ? UITableViewCellAccessoryNone : UITableViewCellAccessoryCheckmark; - [self change:m_displayedKeys[indexPath.row] selected:!isAlreadySelected]; + [self change:[self dataSourceForSection:indexPath.section][indexPath.row] selected:!isAlreadySelected]; } @end