From 38a33869695dc17d43c0808175db9171bffb7057 Mon Sep 17 00:00:00 2001 From: VladiMihaylenko Date: Wed, 17 Feb 2016 14:44:49 +0300 Subject: [PATCH] [ios] Refactored place page. --- iphone/Maps/Classes/MWMBasePlacePageView.mm | 117 ++++++++++++++++-- .../Maps/Classes/MWMPlacePageButtonCell.xib | 21 +--- iphone/Maps/Classes/MWMPlacePageEntity.h | 6 +- iphone/Maps/Classes/MWMPlacePageEntity.mm | 114 ++++------------- iphone/Maps/Classes/PlacePageActionBar.xib | 15 +++ iphone/Maps/Classes/PlacePageBookmarkCell.xib | 73 +++++------ iphone/Maps/Classes/PlacePageView.xib | 5 +- 7 files changed, 180 insertions(+), 171 deletions(-) diff --git a/iphone/Maps/Classes/MWMBasePlacePageView.mm b/iphone/Maps/Classes/MWMBasePlacePageView.mm index 2ad75b31c5..df3e4f01f5 100644 --- a/iphone/Maps/Classes/MWMBasePlacePageView.mm +++ b/iphone/Maps/Classes/MWMBasePlacePageView.mm @@ -18,13 +18,42 @@ extern NSString * const kMWMCuisineSeparator; namespace { CGFloat const kLeftOffset = 16.; +CGFloat const kDefaultHeaderHeight = 16.; CGFloat const kLabelsPadding = kLeftOffset * 2; CGFloat const kDirectionArrowSide = 20.; CGFloat const kOffsetFromTitleToDistance = 8.; CGFloat const kOffsetFromDistanceToArrow = 5.; CGFloat const kMaximumWidth = 360.; -MWMPlacePageCellTypeValueMap const gCellType2ReuseIdentifier{ +enum class PlacePageSection +{ + Bookmark, + Metadata, + Editing +}; + +vector const kSectionBookmarkCellTypes { + MWMPlacePageCellTypeBookmark +}; + +vector const kSectionMetadataCellTypes { + MWMPlacePageCellTypePostcode, MWMPlacePageCellTypePhoneNumber, MWMPlacePageCellTypeWebsite, MWMPlacePageCellTypeURL, + MWMPlacePageCellTypeEmail, MWMPlacePageCellTypeOpenHours, MWMPlacePageCellTypeWiFi, MWMPlacePageCellTypeCoordinate +}; + +vector const kSectionEditingCellTypes { + MWMPlacePageCellTypeEditButton +}; + +using TCellTypesSectionMap = pair, PlacePageSection>; + +vector const kCellTypesSectionMap { + {kSectionBookmarkCellTypes, PlacePageSection::Bookmark}, + {kSectionMetadataCellTypes, PlacePageSection::Metadata}, + {kSectionEditingCellTypes, PlacePageSection::Editing} +}; + +MWMPlacePageCellTypeValueMap const kCellType2ReuseIdentifier{ {MWMPlacePageCellTypeWiFi, "PlacePageInfoCell"}, {MWMPlacePageCellTypeCoordinate, "PlacePageInfoCell"}, {MWMPlacePageCellTypePostcode, "PlacePageInfoCell"}, @@ -38,8 +67,8 @@ MWMPlacePageCellTypeValueMap const gCellType2ReuseIdentifier{ NSString * reuseIdentifier(MWMPlacePageCellType cellType) { - auto const it = gCellType2ReuseIdentifier.find(cellType); - BOOL const haveCell = (it != gCellType2ReuseIdentifier.end()); + auto const it = kCellType2ReuseIdentifier.find(cellType); + BOOL const haveCell = (it != kCellType2ReuseIdentifier.end()); ASSERT(haveCell, ()); return haveCell ? @(it->second.c_str()) : @""; } @@ -59,6 +88,10 @@ enum class AttributePosition } // namespace @interface MWMBasePlacePageView () +{ + vector m_sections; + map> m_cells; +} @property (weak, nonatomic) MWMPlacePageEntity * entity; @property (weak, nonatomic) IBOutlet MWMPlacePage * ownerPlacePage; @@ -80,7 +113,7 @@ enum class AttributePosition self.featureTable.delegate = self; self.featureTable.dataSource = self; self.featureTable.separatorColor = [UIColor blackDividers]; - for (auto const & type : gCellType2ReuseIdentifier) + for (auto const & type : kCellType2ReuseIdentifier) { NSString * identifier = @(type.second.c_str()); [self.featureTable registerNib:[UINib nibWithNibName:identifier bundle:nil] @@ -92,9 +125,29 @@ enum class AttributePosition - (void)configureWithEntity:(MWMPlacePageEntity *)entity { self.entity = entity; + [self configTable]; [self configure]; } +- (void)configTable +{ + m_sections.clear(); + m_cells.clear(); + for (auto const cellSection : kCellTypesSectionMap) + { + for (auto const cellType : cellSection.first) + { + if (![self.entity getCellValue:cellType]) + continue; + m_sections.push_back(cellSection.second); + m_cells[cellSection.second].push_back(cellType); + } + } + + sort(m_sections.begin(), m_sections.end()); + m_sections.erase(unique(m_sections.begin(), m_sections.end()), m_sections.end()); +} + - (void)configure { MWMPlacePageEntity * entity = self.entity; @@ -110,7 +163,7 @@ enum class AttributePosition self.titleLabel.text = entity.title; NSString * typeString = entity.category.capitalizedString; NSRange const range = [typeString rangeOfString:kMWMCuisineSeparator]; - if (range.location != NSNotFound) + if (range.location != NSNotFound && typeString.length > 0) { NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:typeString]; [str addAttributes:@{NSForegroundColorAttributeName : [UIColor blackHintText]} range:range]; @@ -277,7 +330,11 @@ enum class AttributePosition [self.typeDescriptionView removeFromSuperview]; self.typeDescriptionView = nil; [self.typeLabel sizeToFit]; - [self.entity addBookmarkField]; + + m_sections.push_back(PlacePageSection::Bookmark); + m_cells[PlacePageSection::Bookmark].push_back(MWMPlacePageCellTypeBookmark); + sort(m_sections.begin(), m_sections.end()); + [self configure]; } @@ -286,7 +343,14 @@ enum class AttributePosition [[Statistics instance] logEvent:kStatEventName(kStatPlacePage, kStatToggleBookmark) withParameters:@{kStatValue : kStatRemove}]; self.entity.type = MWMPlacePageEntityTypeRegular; - [self.entity removeBookmarkField]; + + auto const it = find(m_sections.begin(), m_sections.end(), PlacePageSection::Bookmark); + if (it != m_sections.end()) + { + m_sections.erase(it); + m_cells.erase(PlacePageSection::Bookmark); + } + [self configure]; } @@ -366,8 +430,7 @@ enum class AttributePosition - (MWMPlacePageCellType)cellTypeForIndexPath:(NSIndexPath *)indexPath { - MWMPlacePageCellType const cellType = [self.entity getCellType:indexPath.row]; - return cellType; + return [self cellsForSection:indexPath.section][indexPath.row]; } - (NSString *)cellIdentifierForIndexPath:(NSIndexPath *)indexPath @@ -433,7 +496,12 @@ enum class AttributePosition - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return [self.entity getCellsCount]; + return [self cellsForSection:section].size(); +} + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView +{ + return m_sections.size(); } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath @@ -442,4 +510,33 @@ enum class AttributePosition return [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; } +- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section +{ + return section == m_sections.size() - 1 ? kDefaultHeaderHeight : 0.; +} + +- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section +{ + return kDefaultHeaderHeight; +} + +- (vector)cellsForSection:(NSInteger)section +{ + switch (m_sections.size()) + { + case 1: + return m_cells[PlacePageSection::Metadata]; + case 2: + if (self.entity.canEditObject) + return section == 0 ? m_cells[PlacePageSection::Metadata] : m_cells[PlacePageSection::Editing]; + else + return section == 0 ? m_cells[PlacePageSection::Bookmark] : m_cells[PlacePageSection::Metadata]; + case 3: + return m_cells[static_cast(section)]; + default: + NSAssert(false, @"Invalid m_sections size"); + return {}; + } +} + @end diff --git a/iphone/Maps/Classes/MWMPlacePageButtonCell.xib b/iphone/Maps/Classes/MWMPlacePageButtonCell.xib index fc113a3e31..09607a5ca7 100644 --- a/iphone/Maps/Classes/MWMPlacePageButtonCell.xib +++ b/iphone/Maps/Classes/MWMPlacePageButtonCell.xib @@ -1,5 +1,5 @@ - + @@ -11,10 +11,10 @@ - + - - - - - - - - - - - - - @@ -62,7 +50,4 @@ - - - diff --git a/iphone/Maps/Classes/MWMPlacePageEntity.h b/iphone/Maps/Classes/MWMPlacePageEntity.h index 75ae718267..6a0ccc215d 100644 --- a/iphone/Maps/Classes/MWMPlacePageEntity.h +++ b/iphone/Maps/Classes/MWMPlacePageEntity.h @@ -56,6 +56,7 @@ using MWMPlacePageCellTypeValueMap = map; @property (copy, nonatomic) NSString * bookmarkColor; @property (copy, nonatomic) NSSet * cuisines; @property (copy, nonatomic) NSArray * nearbyStreets; +@property (nonatomic, readonly) BOOL canEditObject; @property (nonatomic) MWMPlacePageEntityType type; @@ -66,16 +67,11 @@ using MWMPlacePageCellTypeValueMap = map; @property (nonatomic, readonly) ms::LatLon latlon; -- (void)addBookmarkField; -- (void)removeBookmarkField; - - (instancetype)initWithDelegate:(id)delegate; - (void)synchronize; - (void)toggleCoordinateSystem; -- (NSUInteger)getCellsCount; -- (MWMPlacePageCellType)getCellType:(NSUInteger)index; - (NSString *)getCellValue:(MWMPlacePageCellType)cellType; - (BOOL)isCellEditable:(MWMPlacePageCellType)cellType; - (void)saveEditedCells:(MWMPlacePageCellTypeValueMap const &)cells; diff --git a/iphone/Maps/Classes/MWMPlacePageEntity.mm b/iphone/Maps/Classes/MWMPlacePageEntity.mm index f1916b089f..6c02e3dc94 100644 --- a/iphone/Maps/Classes/MWMPlacePageEntity.mm +++ b/iphone/Maps/Classes/MWMPlacePageEntity.mm @@ -27,18 +27,12 @@ NSString * makeOSMCuisineString(NSSet * cuisines) return [osmCuisines componentsJoinedByString:kOSMCuisineSeparator]; } -array const gMetaFieldsMap{ - {MWMPlacePageCellTypePostcode, MWMPlacePageCellTypePhoneNumber, MWMPlacePageCellTypeWebsite, - MWMPlacePageCellTypeURL, MWMPlacePageCellTypeEmail, MWMPlacePageCellTypeOpenHours, - MWMPlacePageCellTypeWiFi, MWMPlacePageCellTypeCoordinate, MWMPlacePageCellTypeBookmark, - MWMPlacePageCellTypeEditButton}}; - -NSUInteger kMetaFieldsMap[MWMPlacePageCellTypeCount] = {}; +NSUInteger gMetaFieldsMap[MWMPlacePageCellTypeCount] = {}; void putFields(NSUInteger eTypeValue, NSUInteger ppValue) { - kMetaFieldsMap[eTypeValue] = ppValue; - kMetaFieldsMap[ppValue] = eTypeValue; + gMetaFieldsMap[eTypeValue] = ppValue; + gMetaFieldsMap[ppValue] = eTypeValue; } void initFieldsMap() @@ -52,25 +46,25 @@ void initFieldsMap() putFields(Metadata::FMD_INTERNET, MWMPlacePageCellTypeWiFi); putFields(Metadata::FMD_CUISINE, MWMPlacePageCellTypeCuisine); - ASSERT_EQUAL(kMetaFieldsMap[Metadata::FMD_URL], MWMPlacePageCellTypeURL, ()); - ASSERT_EQUAL(kMetaFieldsMap[MWMPlacePageCellTypeURL], Metadata::FMD_URL, ()); - ASSERT_EQUAL(kMetaFieldsMap[Metadata::FMD_WEBSITE], MWMPlacePageCellTypeWebsite, ()); - ASSERT_EQUAL(kMetaFieldsMap[MWMPlacePageCellTypeWebsite], Metadata::FMD_WEBSITE, ()); - ASSERT_EQUAL(kMetaFieldsMap[Metadata::FMD_POSTCODE], MWMPlacePageCellTypePostcode, ()); - ASSERT_EQUAL(kMetaFieldsMap[MWMPlacePageCellTypePostcode], Metadata::FMD_POSTCODE, ()); - ASSERT_EQUAL(kMetaFieldsMap[Metadata::FMD_MAXSPEED], 0, ()); + ASSERT_EQUAL(gMetaFieldsMap[Metadata::FMD_URL], MWMPlacePageCellTypeURL, ()); + ASSERT_EQUAL(gMetaFieldsMap[MWMPlacePageCellTypeURL], Metadata::FMD_URL, ()); + ASSERT_EQUAL(gMetaFieldsMap[Metadata::FMD_WEBSITE], MWMPlacePageCellTypeWebsite, ()); + ASSERT_EQUAL(gMetaFieldsMap[MWMPlacePageCellTypeWebsite], Metadata::FMD_WEBSITE, ()); + ASSERT_EQUAL(gMetaFieldsMap[Metadata::FMD_POSTCODE], MWMPlacePageCellTypePostcode, ()); + ASSERT_EQUAL(gMetaFieldsMap[MWMPlacePageCellTypePostcode], Metadata::FMD_POSTCODE, ()); + ASSERT_EQUAL(gMetaFieldsMap[Metadata::FMD_MAXSPEED], 0, ()); } } // namespace @interface MWMPlacePageEntity () @property (weak, nonatomic) id delegate; +@property (nonatomic, readwrite) BOOL canEditObject; @end @implementation MWMPlacePageEntity { - vector m_fields; set m_editableFields; MWMPlacePageCellTypeValueMap m_values; } @@ -133,35 +127,6 @@ void initFieldsMap() break; } [self setEditableTypes]; - [self sortMetaFields]; -} - -- (void)sortMetaFields -{ - auto const begin = gMetaFieldsMap.begin(); - auto const end = gMetaFieldsMap.end(); - sort(m_fields.begin(), m_fields.end(), [&](MWMPlacePageCellType a, MWMPlacePageCellType b) - { - return find(begin, end, a) < find(begin, end, b); - }); -} - -- (void)addMetaField:(NSUInteger)value -{ - NSAssert(value >= Metadata::FMD_COUNT, @"Incorrect enum value"); - MWMPlacePageCellType const field = static_cast(value); - if (find(gMetaFieldsMap.begin(), gMetaFieldsMap.end(), field) == gMetaFieldsMap.end()) - return; - if (find(m_fields.begin(), m_fields.end(), field) == m_fields.end()) - m_fields.emplace_back(field); -} - -- (void)removeMetaField:(NSUInteger)value -{ - NSAssert(value >= Metadata::FMD_COUNT, @"Incorrect enum value"); - auto const it = find(m_fields.begin(), m_fields.end(), value); - if (it != m_fields.end()) - m_fields.erase(it); } - (void)setMetaField:(NSUInteger)key value:(string const &)value @@ -169,15 +134,9 @@ void initFieldsMap() NSAssert(key >= Metadata::FMD_COUNT, @"Incorrect enum value"); MWMPlacePageCellType const cellType = static_cast(key); if (value.empty()) - { - [self removeMetaField:key]; m_values.erase(cellType); - } else - { - [self addMetaField:key]; m_values[cellType] = value; - } } - (void)configureForBookmark:(UserMark const *)bookmark @@ -197,7 +156,6 @@ void initFieldsMap() self.bookmarkColor = @(data.GetType().c_str()); [self configureWithFeature:bookmark->GetFeature() andCustomName:nil]; - [self addBookmarkField]; } - (void)configureForMyPosition:(MyPositionMarkPoint const *)myPositionMark @@ -205,7 +163,6 @@ void initFieldsMap() // TODO: There is need to get address info which store feature address. self.title = L(@"my_position"); self.type = MWMPlacePageEntityTypeMyPosition; - [self addMetaField:MWMPlacePageCellTypeCoordinate]; } - (void)configureForApi:(ApiMarkPoint const *)apiMark @@ -214,7 +171,6 @@ void initFieldsMap() self.type = MWMPlacePageEntityTypeAPI; self.title = @(apiMark->GetName().c_str()); self.category = @(GetFramework().GetApiDataHolder().GetAppTitle().c_str()); - [self addMetaField:MWMPlacePageCellTypeCoordinate]; } - (void)configureWithFeature:(FeatureType *)feature andCustomName:(NSString *)customName @@ -280,10 +236,10 @@ void initFieldsMap() case Metadata::FMD_OPEN_HOURS: case Metadata::FMD_EMAIL: case Metadata::FMD_POSTCODE: - [self setMetaField:kMetaFieldsMap[type] value:metadata.Get(type)]; + [self setMetaField:gMetaFieldsMap[type] value:metadata.Get(type)]; break; case Metadata::FMD_INTERNET: - [self setMetaField:kMetaFieldsMap[type] value:L(@"WiFi_available").UTF8String]; + [self setMetaField:gMetaFieldsMap[type] value:L(@"WiFi_available").UTF8String]; break; default: break; @@ -292,24 +248,6 @@ void initFieldsMap() [self processStreets]; } - - [self addMetaField:MWMPlacePageCellTypeCoordinate]; -} - -- (void)addEditField -{ - [self addMetaField:MWMPlacePageCellTypeEditButton]; -} - -- (void)addBookmarkField -{ - [self addMetaField:MWMPlacePageCellTypeBookmark]; - [self sortMetaFields]; -} - -- (void)removeBookmarkField -{ - [self removeMetaField:MWMPlacePageCellTypeBookmark]; } - (void)toggleCoordinateSystem @@ -350,8 +288,7 @@ void initFieldsMap() return; osm::EditableProperties const editable = osm::Editor::Instance().GetEditableProperties(*feature); - if (editable.IsEditable()) - [self addEditField]; + self.canEditObject = editable.IsEditable(); if (editable.m_name) m_editableFields.insert(MWMPlacePageCellTypeName); if (editable.m_address) @@ -361,8 +298,8 @@ void initFieldsMap() } for (feature::Metadata::EType const type : editable.m_metadata) { - NSAssert(kMetaFieldsMap[type] >= Metadata::FMD_COUNT || kMetaFieldsMap[type] == 0, @"Incorrect enum value"); - MWMPlacePageCellType const field = static_cast(kMetaFieldsMap[type]); + NSAssert(gMetaFieldsMap[type] >= Metadata::FMD_COUNT || gMetaFieldsMap[type] == 0, @"Incorrect enum value"); + MWMPlacePageCellType const field = static_cast(gMetaFieldsMap[type]); m_editableFields.insert(field); } } @@ -394,14 +331,14 @@ void initFieldsMap() case MWMPlacePageCellTypeEmail: case MWMPlacePageCellTypeWiFi: { - Metadata::EType const fmdType = static_cast(kMetaFieldsMap[cell.first]); + Metadata::EType const fmdType = static_cast(gMetaFieldsMap[cell.first]); NSAssert(fmdType > 0 && fmdType < Metadata::FMD_COUNT, @"Incorrect enum value"); metadata.Set(fmdType, cell.second); break; } case MWMPlacePageCellTypeCuisine: { - Metadata::EType const fmdType = static_cast(kMetaFieldsMap[cell.first]); + Metadata::EType const fmdType = static_cast(gMetaFieldsMap[cell.first]); NSAssert(fmdType > 0 && fmdType < Metadata::FMD_COUNT, @"Incorrect enum value"); NSString * osmCuisineStr = makeOSMCuisineString(self.cuisines); metadata.Set(fmdType, osmCuisineStr.UTF8String); @@ -435,17 +372,6 @@ void initFieldsMap() #pragma mark - Getters -- (NSUInteger)getCellsCount -{ - return m_fields.size(); -} - -- (MWMPlacePageCellType)getCellType:(NSUInteger)index -{ - NSAssert(index < [self getCellsCount], @"Invalid meta index"); - return m_fields[index]; -} - - (NSString *)getCellValue:(MWMPlacePageCellType)cellType { switch (cellType) @@ -454,6 +380,10 @@ void initFieldsMap() return self.title; case MWMPlacePageCellTypeCoordinate: return [self coordinate]; + case MWMPlacePageCellTypeBookmark: + return self.type == MWMPlacePageEntityTypeBookmark ? @"haveValue" : nil; + case MWMPlacePageCellTypeEditButton: + return self.canEditObject ? @"haveValue" : nil; default: { auto const it = m_values.find(cellType); diff --git a/iphone/Maps/Classes/PlacePageActionBar.xib b/iphone/Maps/Classes/PlacePageActionBar.xib index 7003d98c12..2a31d3b720 100644 --- a/iphone/Maps/Classes/PlacePageActionBar.xib +++ b/iphone/Maps/Classes/PlacePageActionBar.xib @@ -11,6 +11,13 @@ + + + + + + + - + - - + - + - +