Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Unified Diff: ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_most_visited_item.mm

Issue 2873503005: Create individual cells for MostVisited tiles (Closed)
Patch Set: Address comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_most_visited_item.mm
diff --git a/ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_most_visited_item.mm b/ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_most_visited_item.mm
index bdad43a9fa824293aed16c58d67db906c9964bb8..0e121d4667f1a184b4b6b872fa4d80ec212784d4 100644
--- a/ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_most_visited_item.mm
+++ b/ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_most_visited_item.mm
@@ -4,21 +4,23 @@
#import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_most_visited_item.h"
-#import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_most_visited.h"
-#import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_most_visited_tile.h"
#import "ios/chrome/browser/ui/favicon/favicon_attributes.h"
#import "ios/chrome/browser/ui/favicon/favicon_view.h"
-#include "ios/chrome/browser/ui/ui_util.h"
#import "ios/chrome/browser/ui/uikit_ui_util.h"
-#include "url/gurl.h"
+#import "ios/third_party/material_components_ios/src/components/Typography/src/MaterialTypography.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
-const CGFloat kSpacingIPhone = 16;
-const CGFloat kSpacingIPad = 24;
+const CGFloat kLabelTextColor = 0.314;
+const NSInteger kLabelNumLines = 2;
+const CGFloat kFaviconSize = 48;
+const CGFloat kSpaceFaviconTitle = 10;
+
+// Size of a Most Visited cell.
+const CGSize kCellSize = {73, 100};
}
#pragma mark - ContentSuggestionsMostVisitedItem
@@ -26,7 +28,8 @@ const CGFloat kSpacingIPad = 24;
@implementation ContentSuggestionsMostVisitedItem
@synthesize suggestionIdentifier = _suggestionIdentifier;
-@synthesize suggestions = _suggestions;
+@synthesize attributes = _attributes;
+@synthesize title = _title;
- (instancetype)initWithType:(NSInteger)type {
self = [super initWithType:type];
@@ -38,173 +41,66 @@ const CGFloat kSpacingIPad = 24;
- (void)configureCell:(ContentSuggestionsMostVisitedCell*)cell {
[super configureCell:cell];
- [cell setSuggestions:self.suggestions];
+ cell.titleLabel.text = self.title;
+ cell.accessibilityLabel = self.title;
+ [cell.faviconView configureWithAttributes:self.attributes];
}
@end
#pragma mark - ContentSuggestionsMostVisitedCell
-@interface ContentSuggestionsMostVisitedCell ()
-
-// The Most Visited tiles to be displayed.
-@property(nonatomic, strong)
- NSMutableArray<ContentSuggestionsMostVisitedTile*>* tiles;
-
-// The first line of Most Visited tiles.
-@property(nonatomic, strong) UIStackView* firstLine;
-
-// The second line of Most Visited tiles, displayed below the first one.
-@property(nonatomic, strong) UIStackView* secondLine;
-
-// Contains both stack views.
-@property(nonatomic, copy) NSArray<UIStackView*>* stackViews;
-
-// Superview for the stack views, used to center them.
-@property(nonatomic, strong) UIView* stackContainer;
-
-// Width of the |stackContainer|, allowing resizing.
-@property(nonatomic, strong) NSLayoutConstraint* containerWidth;
-
-// Number of tiles per line during the previous layout.
-@property(nonatomic, assign) NSUInteger previousNumberOfTilesPerLine;
-
-@end
-
@implementation ContentSuggestionsMostVisitedCell : MDCCollectionViewCell
-@synthesize tiles = _tiles;
-@synthesize firstLine = _firstLine;
-@synthesize secondLine = _secondLine;
-@synthesize stackViews = _stackViews;
-@synthesize stackContainer = _stackContainer;
-@synthesize containerWidth = _containerWidth;
-@synthesize previousNumberOfTilesPerLine = _previousNumberOfTilesPerLine;
+@synthesize faviconView = _faviconView;
+@synthesize titleLabel = _titleLabel;
#pragma mark - Public
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
- _tiles = [NSMutableArray array];
- _firstLine = [[UIStackView alloc] init];
- _secondLine = [[UIStackView alloc] init];
- _stackViews = @[ _firstLine, _secondLine ];
- _stackContainer = [[UIView alloc] init];
- _previousNumberOfTilesPerLine = 0;
-
- for (UIStackView* row in self.stackViews) {
- row.axis = UILayoutConstraintAxisHorizontal;
- row.spacing = [self spacing];
- row.translatesAutoresizingMaskIntoConstraints = NO;
- [_stackContainer addSubview:row];
- row.layoutMarginsRelativeArrangement = YES;
- }
-
- _stackContainer.translatesAutoresizingMaskIntoConstraints = NO;
-
- [self.contentView addSubview:_stackContainer];
-
- [_stackContainer.centerXAnchor
- constraintEqualToAnchor:self.contentView.centerXAnchor]
- .active = YES;
- _containerWidth = [_stackContainer.widthAnchor constraintEqualToConstant:0];
- _containerWidth.active = YES;
-
- ApplyVisualConstraints(@[ @"V:|[container]|" ],
- @{ @"container" : _stackContainer });
- ApplyVisualConstraints(
- @[
- @"V:|[first][second]|", @"H:|[first]-(>=0)-|",
- @"H:|[second]-(>=0)-|"
- ],
- @{ @"first" : _firstLine,
- @"second" : _secondLine });
+ _titleLabel = [[UILabel alloc] init];
+ _titleLabel.textColor = [UIColor colorWithWhite:kLabelTextColor alpha:1.0];
+ _titleLabel.font = [MDCTypography captionFont];
+ _titleLabel.textAlignment = NSTextAlignmentCenter;
+ _titleLabel.preferredMaxLayoutWidth = [[self class] defaultSize].width;
+ _titleLabel.numberOfLines = kLabelNumLines;
+
+ _faviconView = [[FaviconViewNew alloc] init];
+ _faviconView.font = [MDCTypography headlineFont];
+
+ _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
+ _faviconView.translatesAutoresizingMaskIntoConstraints = NO;
+
+ [self addSubview:_titleLabel];
+ [self addSubview:_faviconView];
+
+ [NSLayoutConstraint activateConstraints:@[
+ [_faviconView.widthAnchor constraintEqualToConstant:kFaviconSize],
+ [_faviconView.heightAnchor
+ constraintEqualToAnchor:_faviconView.widthAnchor],
+ [_faviconView.centerXAnchor
+ constraintEqualToAnchor:_titleLabel.centerXAnchor],
+ ]];
+
+ ApplyVisualConstraintsWithMetrics(
+ @[ @"V:|[favicon]-(space)-[title]", @"H:|[title]|" ],
+ @{ @"favicon" : _faviconView,
+ @"title" : _titleLabel },
+ @{ @"space" : @(kSpaceFaviconTitle) });
+
+ self.isAccessibilityElement = YES;
}
return self;
}
-- (void)setSuggestions:(NSArray<ContentSuggestionsMostVisited*>*)suggestions {
- [self.tiles removeAllObjects];
- for (ContentSuggestionsMostVisited* suggestion in suggestions) {
- [self.tiles addObject:[ContentSuggestionsMostVisitedTile
- tileWithTitle:suggestion.title
- attributes:suggestion.attributes]];
- }
-}
-
-- (NSUInteger)numberOfTilesPerLine {
- CGFloat availableWidth = self.contentView.bounds.size.width;
-
- if (availableWidth > [self widthForNumberOfItem:4])
- return 4;
- if (availableWidth > [self widthForNumberOfItem:3])
- return 3;
- if (availableWidth > [self widthForNumberOfItem:2])
- return 2;
-
- NOTREACHED();
- return 2;
-}
-
-#pragma mark - UIView
-
-// Implements -layoutSubviews as per instructions in documentation for
-// +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:].
-- (void)layoutSubviews {
- [super layoutSubviews];
-
- [self applyLayout];
-
- [super layoutSubviews];
-}
-
-#pragma mark - Private
-
-// Layouts the tiles. The view is modified only if the number of tiles per line
-// changes.
-- (void)applyLayout {
- NSUInteger numberOfTilesPerLine = [self numberOfTilesPerLine];
-
- if (numberOfTilesPerLine == self.previousNumberOfTilesPerLine) {
- return;
- }
- self.previousNumberOfTilesPerLine = numberOfTilesPerLine;
-
- for (UIStackView* row in self.stackViews) {
- while (row.arrangedSubviews.count > 0) {
- UIView* view = row.arrangedSubviews.firstObject;
- [row removeArrangedSubview:view];
- [view removeFromSuperview];
- }
- }
-
- NSUInteger numberOfTilesFirstLine =
- MIN(numberOfTilesPerLine, self.tiles.count);
- for (NSUInteger i = 0; i < numberOfTilesFirstLine; i++) {
- [self.firstLine addArrangedSubview:self.tiles[i]];
- }
- if (self.tiles.count > numberOfTilesPerLine) {
- NSUInteger totalNumberOfTiles =
- MIN(2 * numberOfTilesPerLine, self.tiles.count);
- for (NSUInteger i = numberOfTilesPerLine; i < totalNumberOfTiles; i++) {
- [self.secondLine addArrangedSubview:self.tiles[i]];
- }
- }
-
- self.containerWidth.constant =
- [self widthForNumberOfItem:numberOfTilesPerLine];
-}
-
-// Returns the spacing between tiles, based on the device.
-- (CGFloat)spacing {
- return IsIPadIdiom() ? kSpacingIPad : kSpacingIPhone;
++ (CGSize)defaultSize {
+ return kCellSize;
}
-// Returns the width necessary to fit |numberOfItem| items.
-- (CGFloat)widthForNumberOfItem:(NSUInteger)numberOfItem {
- return (numberOfItem - 1) * [self spacing] +
- numberOfItem * [ContentSuggestionsMostVisitedTile width];
+- (CGSize)intrinsicContentSize {
+ return [[self class] defaultSize];
}
@end

Powered by Google App Engine
This is Rietveld 408576698