From 01d2ec0d41ffdd31d37f2179b6ae372148af856c Mon Sep 17 00:00:00 2001 From: VladiMihaylenko Date: Wed, 27 Jun 2018 14:42:56 +0300 Subject: [PATCH] [ios] Using enums instead of raw int. --- iphone/Maps/Bookmarks/BookmarksVC.mm | 335 ++++++++++--------- iphone/Maps/Bookmarks/MWMCategoryInfoCell.h | 6 +- iphone/Maps/Bookmarks/MWMCategoryInfoCell.mm | 12 +- 3 files changed, 191 insertions(+), 162 deletions(-) diff --git a/iphone/Maps/Bookmarks/BookmarksVC.mm b/iphone/Maps/Bookmarks/BookmarksVC.mm index 7c39587fb4..bf1eb47303 100644 --- a/iphone/Maps/Bookmarks/BookmarksVC.mm +++ b/iphone/Maps/Bookmarks/BookmarksVC.mm @@ -15,17 +15,29 @@ #include "coding/zip_creator.hpp" #include "coding/internal/file_data.hpp" -#define PINDIAMETER 18 +#include +#include +#include -#define EMPTY_SECTION -666 +using namespace std; + +namespace +{ +enum class Section +{ + Info, + Track, + Bookmark +}; + +CGFloat const kPinDiameter = 18.0f; +} // namespace @interface BookmarksVC() { - int m_infoSection; - int m_trackSection; - int m_bookmarkSection; - int m_numberOfSections; + vector
m_sections; } + @end @implementation BookmarksVC @@ -36,8 +48,8 @@ if (self) { m_categoryId = index; - auto const & bmManager = GetFramework().GetBookmarkManager(); - self.title = @(bmManager.GetCategoryName(m_categoryId).c_str()); + auto const & bm = GetFramework().GetBookmarkManager(); + self.title = @(bm.GetCategoryName(m_categoryId).c_str()); [self calculateSections]; } return self; @@ -45,76 +57,82 @@ - (kml::MarkId)getBookmarkIdByRow:(NSInteger)row { - auto const & bmManager = GetFramework().GetBookmarkManager(); - auto const & bookmarkIds = bmManager.GetUserMarkIds(m_categoryId); + auto const & bm = GetFramework().GetBookmarkManager(); + auto const & bookmarkIds = bm.GetUserMarkIds(m_categoryId); ASSERT_LESS(row, bookmarkIds.size(), ()); auto it = bookmarkIds.begin(); - std::advance(it, row); + advance(it, row); return *it; } - (kml::TrackId)getTrackIdByRow:(NSInteger)row { - auto const & bmManager = GetFramework().GetBookmarkManager(); - auto const & trackIds = bmManager.GetTrackIds(m_categoryId); + auto const & bm = GetFramework().GetBookmarkManager(); + auto const & trackIds = bm.GetTrackIds(m_categoryId); ASSERT_LESS(row, trackIds.size(), ()); auto it = trackIds.begin(); - std::advance(it, row); + advance(it, row); return *it; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { - return m_numberOfSections; + return m_sections.size(); } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - if (section == m_infoSection) + auto const & bm = GetFramework().GetBookmarkManager(); + switch (m_sections.at(section)) + { + case Section::Info: return 1; - else if (section == m_trackSection) - return GetFramework().GetBookmarkManager().GetTrackIds(m_categoryId).size(); - else if (section == m_bookmarkSection) - return GetFramework().GetBookmarkManager().GetUserMarkIds(m_categoryId).size(); - else - return 0; + case Section::Track: + return bm.GetTrackIds(m_categoryId).size(); + case Section::Bookmark: + return bm.GetUserMarkIds(m_categoryId).size(); + } } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { - if (section == m_infoSection) + switch (m_sections.at(section)) + { + case Section::Info: return L(@"placepage_place_description"); - if (section == m_trackSection) + case Section::Track: return L(@"tracks"); - if (section == m_bookmarkSection) + case Section::Bookmark: return L(@"bookmarks"); - return nil; + } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - Framework & fr = GetFramework(); - - auto & bmManager = fr.GetBookmarkManager(); - if (!bmManager.HasBmCategory(m_categoryId)) + auto & f = GetFramework(); + auto const & bm = f.GetBookmarkManager(); + if (!bm.HasBmCategory(m_categoryId)) return nil; UITableViewCell * cell = nil; - // First section, contains info about current set - if (indexPath.section == m_infoSection) + switch (m_sections.at(indexPath.row)) + { + case Section::Info: { cell = [tableView dequeueReusableCellWithCellClass:MWMCategoryInfoCell.class indexPath:indexPath]; auto infoCell = (MWMCategoryInfoCell *)cell; - auto const & categoryData = bmManager.GetCategoryData(m_categoryId); + auto const & categoryData = bm.GetCategoryData(m_categoryId); [infoCell updateWithCategoryData:categoryData delegate:self]; + break; } - else if (indexPath.section == m_trackSection) + case Section::Track: { cell = [tableView dequeueReusableCellWithIdentifier:@"TrackCell"]; if (!cell) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"TrackCell"]; + kml::TrackId const trackId = [self getTrackIdByRow:indexPath.row]; - Track const * tr = bmManager.GetTrack(trackId); + Track const * tr = bm.GetTrack(trackId); cell.textLabel.text = @(tr->GetName().c_str()); string dist; if (measurement_utils::FormatDistance(tr->GetLengthMeters(), dist)) @@ -122,41 +140,46 @@ cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", L(@"length"), @(dist.c_str())]; else cell.detailTextLabel.text = nil; - const dp::Color c = tr->GetColor(0); - cell.imageView.image = [CircleView createCircleImageWith:PINDIAMETER andColor:[UIColor colorWithRed:c.GetRed()/255.f green:c.GetGreen()/255.f - blue:c.GetBlue()/255.f alpha:1.f]]; + dp::Color const c = tr->GetColor(0); + cell.imageView.image = [CircleView createCircleImageWith:kPinDiameter + andColor:[UIColor colorWithRed:c.GetRed()/255.f + green:c.GetGreen()/255.f + blue:c.GetBlue()/255.f + alpha:1.f]]; + break; } - // Contains bookmarks list - else if (indexPath.section == m_bookmarkSection) + case Section::Bookmark: { - UITableViewCell * bmCell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"BookmarksVCBookmarkItemCell"]; - if (!bmCell) - bmCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"BookmarksVCBookmarkItemCell"]; + cell = [tableView dequeueReusableCellWithIdentifier:@"BookmarksVCBookmarkItemCell"]; + if (!cell) + cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle + reuseIdentifier:@"BookmarksVCBookmarkItemCell"]; + kml::MarkId const bmId = [self getBookmarkIdByRow:indexPath.row]; - Bookmark const * bm = bmManager.GetBookmark(bmId); - if (bm) + Bookmark const * bookmark = bm.GetBookmark(bmId); + CHECK(cell, ("NULL bookmark")); + cell.textLabel.text = @(bookmark->GetPreferredName().c_str()); + cell.imageView.image = [CircleView createCircleImageWith:kPinDiameter + andColor:[ColorPickerView getUIColor:bookmark->GetColor()]]; + + CLLocation * lastLocation = [MWMLocationManager lastLocation]; + if (lastLocation) { - bmCell.textLabel.text = @(bm->GetPreferredName().c_str()); - bmCell.imageView.image = [CircleView createCircleImageWith:PINDIAMETER andColor:[ColorPickerView getUIColor:bm->GetColor()]]; + double north = location_helpers::headingToNorthRad([MWMLocationManager lastHeading]); + string distance; + double azimut = -1.0; + f.GetDistanceAndAzimut(bookmark->GetPivot(), lastLocation.coordinate.latitude, + lastLocation.coordinate.longitude, north, distance, azimut); - CLLocation * lastLocation = [MWMLocationManager lastLocation]; - if (lastLocation) - { - double north = location_helpers::headingToNorthRad([MWMLocationManager lastHeading]); - string distance; - double azimut = -1.0; - fr.GetDistanceAndAzimut(bm->GetPivot(), lastLocation.coordinate.latitude, - lastLocation.coordinate.longitude, north, distance, azimut); - - bmCell.detailTextLabel.text = @(distance.c_str()); - } - else - bmCell.detailTextLabel.text = nil; + cell.detailTextLabel.text = @(distance.c_str()); } else - ASSERT(false, ("NULL bookmark")); + { + cell.detailTextLabel.text = nil; + } - cell = bmCell; + break; + } } cell.backgroundColor = [UIColor white]; @@ -169,91 +192,87 @@ { // Remove cell selection [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; - - Framework & f = GetFramework(); - auto & bmManager = f.GetBookmarkManager(); - bool const categoryExists = bmManager.HasBmCategory(m_categoryId); - ASSERT(categoryExists, ("Nonexistent category")); - if (indexPath.section == m_trackSection) + auto & f = GetFramework(); + auto & bm = f.GetBookmarkManager(); + bool const categoryExists = bm.HasBmCategory(m_categoryId); + CHECK(categoryExists, ("Nonexistent category")); + switch (m_sections.at(indexPath.section)) { - if (categoryExists) - { - kml::TrackId const trackId = [self getTrackIdByRow:indexPath.row]; - Track const * tr = bmManager.GetTrack(trackId); - ASSERT(tr, ("NULL track")); - if (tr) - { - f.ShowTrack(*tr); - [self.navigationController popToRootViewControllerAnimated:YES]; - } - } + case Section::Info: + break; + case Section::Track: + { + kml::TrackId const trackId = [self getTrackIdByRow:indexPath.row]; + Track const * tr = bm.GetTrack(trackId); + CHECK(tr, ("NULL track")); + f.ShowTrack(*tr); + [self.navigationController popToRootViewControllerAnimated:YES]; + break; } - else if (indexPath.section == m_bookmarkSection) + case Section::Bookmark: { - if (categoryExists) - { - kml::MarkId const bmId = [self getBookmarkIdByRow:indexPath.row]; - Bookmark const * bm = bmManager.GetBookmark(bmId); - ASSERT(bm, ("NULL bookmark")); - if (bm) - { - [Statistics logEvent:kStatEventName(kStatBookmarks, kStatShowOnMap)]; - // Same as "Close". - [MWMSearchManager manager].state = MWMSearchManagerStateHidden; - f.ShowBookmark(bm); - [self.navigationController popToRootViewControllerAnimated:YES]; - } - } + kml::MarkId const bmId = [self getBookmarkIdByRow:indexPath.row]; + Bookmark const * bookmark = bm.GetBookmark(bmId); + CHECK(bookmark, ("NULL bookmark")); + [Statistics logEvent:kStatEventName(kStatBookmarks, kStatShowOnMap)]; + // Same as "Close". + [MWMSearchManager manager].state = MWMSearchManagerStateHidden; + f.ShowBookmark(bookmark); + [self.navigationController popToRootViewControllerAnimated:YES]; + break; + } } } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { - if (!GetFramework().GetBookmarkManager().IsCategoryFromCatalog(m_categoryId) && - (indexPath.section == m_trackSection || indexPath.section == m_bookmarkSection)) - return YES; - return NO; + auto const s = m_sections.at(indexPath.section); + return GetFramework().GetBookmarkManager().IsCategoryFromCatalog(m_categoryId) && s != Section::Info; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { - if (indexPath.section == m_trackSection || indexPath.section == m_bookmarkSection) + auto const s = m_sections.at(indexPath.section); + if (s == Section::Info) + return; + + auto & bm = GetFramework().GetBookmarkManager(); + if (!bm.HasBmCategory(m_categoryId)) + return; + + if (editingStyle == UITableViewCellEditingStyleDelete) { - auto & bmManager = GetFramework().GetBookmarkManager(); - if (bmManager.HasBmCategory(m_categoryId)) + if (s == Section::Track) { - if (editingStyle == UITableViewCellEditingStyleDelete) - { - if (indexPath.section == m_trackSection) - { - kml::TrackId const trackId = [self getTrackIdByRow:indexPath.row]; - bmManager.GetEditSession().DeleteTrack(trackId); - } - else - { - kml::MarkId const bmId = [self getBookmarkIdByRow:indexPath.row]; - [MWMBookmarksManager deleteBookmark:bmId]; - } - } - size_t previousNumberOfSections = m_numberOfSections; - [self calculateSections]; - //We can delete the row with animation, if number of sections stay the same. - if (previousNumberOfSections == m_numberOfSections) - [self.tableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationFade]; - else - [self.tableView reloadData]; - if (bmManager.GetUserMarkIds(m_categoryId).size() + bmManager.GetTrackIds(m_categoryId).size() == 0) - { - self.navigationItem.rightBarButtonItem = nil; - [self setEditing:NO animated:YES]; - } + kml::TrackId const trackId = [self getTrackIdByRow:indexPath.row]; + bm.GetEditSession().DeleteTrack(trackId); } + else + { + kml::MarkId const bmId = [self getBookmarkIdByRow:indexPath.row]; + [MWMBookmarksManager deleteBookmark:bmId]; + } + } + + auto const previousNumberOfSections = m_sections.size(); + [self calculateSections]; + + //We can delete the row with animation, if number of sections stay the same. + if (previousNumberOfSections == m_sections.size()) + [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; + else + [self.tableView reloadData]; + + if (bm.GetUserMarkIds(m_categoryId).size() + bm.GetTrackIds(m_categoryId).size() == 0) + { + self.navigationItem.rightBarButtonItem = nil; + [self setEditing:NO animated:YES]; } } - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { - UITableViewHeaderFooterView * header = (UITableViewHeaderFooterView *)view; + auto header = (UITableViewHeaderFooterView *)view; header.textLabel.textColor = [UIColor blackSecondaryText]; header.textLabel.font = [UIFont medium14]; } @@ -272,27 +291,28 @@ - (void)onLocationUpdate:(location::GpsInfo const &)info { // Refresh distance - auto & bmManager = GetFramework().GetBookmarkManager(); - if (bmManager.HasBmCategory(m_categoryId)) + auto const & bm = GetFramework().GetBookmarkManager(); + if (!bm.HasBmCategory(m_categoryId)) + return; + + auto table = (UITableView *)self.view; + [table.visibleCells enumerateObjectsUsingBlock:^(UITableViewCell * cell, NSUInteger idx, BOOL * stop) { - UITableView * table = (UITableView *)self.view; - [table.visibleCells enumerateObjectsUsingBlock:^(UITableViewCell * cell, NSUInteger idx, BOOL * stop) - { - NSIndexPath * indexPath = [table indexPathForCell:cell]; - if (indexPath.section == self->m_bookmarkSection) - { - kml::MarkId const bmId = [self getBookmarkIdByRow:indexPath.row]; - Bookmark const * bm = bmManager.GetBookmark(bmId); - if (bm) - { - m2::PointD const center = bm->GetPivot(); - double const metres = ms::DistanceOnEarth(info.m_latitude, info.m_longitude, - MercatorBounds::YToLat(center.y), MercatorBounds::XToLon(center.x)); - cell.detailTextLabel.text = location_helpers::formattedDistance(metres); - } - } - }]; - } + auto indexPath = [table indexPathForCell:cell]; + auto const s = self->m_sections.at(indexPath.section); + if (s != Section::Bookmark) + return; + + kml::MarkId const bmId = [self getBookmarkIdByRow:indexPath.row]; + Bookmark const * bookmark = bm.GetBookmark(bmId); + if (!bookmark) + return; + + m2::PointD const center = bookmark->GetPivot(); + double const metres = ms::DistanceOnEarth(info.m_latitude, info.m_longitude, + MercatorBounds::YToLat(center.y), MercatorBounds::XToLon(center.x)); + cell.detailTextLabel.text = location_helpers::formattedDistance(metres); + }]; } //*********** End of Location manager callbacks ******************** @@ -309,9 +329,9 @@ [MWMLocationManager addObserver:self]; // Display Edit button only if table is not empty - auto & bmManager = GetFramework().GetBookmarkManager(); - if (bmManager.HasBmCategory(m_categoryId) && !bmManager.IsCategoryFromCatalog(m_categoryId) - && (bmManager.GetUserMarkIds(m_categoryId).size() + bmManager.GetTrackIds(m_categoryId).size())) + auto & bm = GetFramework().GetBookmarkManager(); + if (bm.HasBmCategory(m_categoryId) && !bm.IsCategoryFromCatalog(m_categoryId) + && (bm.GetUserMarkIds(m_categoryId).size() + bm.GetTrackIds(m_categoryId).size())) self.navigationItem.rightBarButtonItem = self.editButtonItem; else self.navigationItem.rightBarButtonItem = nil; @@ -352,12 +372,15 @@ - (void)calculateSections { - int index = 0; - auto & bmManager = GetFramework().GetBookmarkManager(); - m_infoSection = bmManager.IsCategoryFromCatalog(m_categoryId) ? index++ : EMPTY_SECTION; - m_trackSection = bmManager.GetTrackIds(m_categoryId).size() ? index++ : EMPTY_SECTION; - m_bookmarkSection = bmManager.GetUserMarkIds(m_categoryId).size() ? index ++ : EMPTY_SECTION; - m_numberOfSections = index; + auto const & bm = GetFramework().GetBookmarkManager(); + if (bm.IsCategoryFromCatalog(m_categoryId)) + m_sections.emplace_back(Section::Info); + + if (bm.GetTrackIds(m_categoryId).size() > 0) + m_sections.emplace_back(Section::Track); + + if (bm.GetUserMarkIds(m_categoryId).size() > 0) + m_sections.emplace_back(Section::Bookmark); } @end diff --git a/iphone/Maps/Bookmarks/MWMCategoryInfoCell.h b/iphone/Maps/Bookmarks/MWMCategoryInfoCell.h index 59e3c97c07..5a82cb9fe9 100644 --- a/iphone/Maps/Bookmarks/MWMCategoryInfoCell.h +++ b/iphone/Maps/Bookmarks/MWMCategoryInfoCell.h @@ -1,6 +1,9 @@ #import "MWMTableViewCell.h" -#include "Framework.h" +namespace kml +{ +struct CategoryData; +} @class MWMCategoryInfoCell; @@ -13,7 +16,6 @@ @interface MWMCategoryInfoCell : MWMTableViewCell @property (nonatomic) BOOL expanded; -@property (weak, nonatomic) id delegate; - (void)updateWithCategoryData:(kml::CategoryData const &)data delegate:(id)delegate; diff --git a/iphone/Maps/Bookmarks/MWMCategoryInfoCell.mm b/iphone/Maps/Bookmarks/MWMCategoryInfoCell.mm index 87317b8a38..7fc28356ff 100644 --- a/iphone/Maps/Bookmarks/MWMCategoryInfoCell.mm +++ b/iphone/Maps/Bookmarks/MWMCategoryInfoCell.mm @@ -1,6 +1,9 @@ #import "MWMCategoryInfoCell.h" #import "SwiftBridge.h" +#include "kml/types.hpp" +#include "kml/type_utils.hpp" + @interface MWMCategoryInfoCell() @property (weak, nonatomic) IBOutlet UILabel * titleLabel; @@ -11,6 +14,7 @@ @property (copy, nonatomic) NSString * info; @property (copy, nonatomic) NSString * shortInfo; +@property (weak, nonatomic) id delegate; @end @@ -36,15 +40,15 @@ self.moreButton.hidden = expanded; } -- (void)updateWithCategoryData:(const kml::CategoryData &)data +- (void)updateWithCategoryData:(kml::CategoryData const &)data delegate:(id)delegate { self.delegate = delegate; self.titleLabel.text = @(kml::GetDefaultStr(data.m_name).c_str()); self.authorLabel.text = [NSString stringWithCoreFormat:L(@"author_name_by_prefix") - arguments:@[@(data.m_authorName.c_str())]]; - NSString * info = @(kml::GetDefaultStr(data.m_description).c_str()); - NSString * shortInfo = @(kml::GetDefaultStr(data.m_annotation).c_str()); + arguments:@[@(data.m_authorName.c_str())]]; + auto info = @(kml::GetDefaultStr(data.m_description).c_str()); + auto shortInfo = @(kml::GetDefaultStr(data.m_annotation).c_str()); if (info.length > 0 && shortInfo.length > 0) { self.info = info;