Chromium Code Reviews| Index: ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_article_item.mm |
| diff --git a/ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_article_item.mm b/ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_article_item.mm |
| index bdb04fd3d5d7fe07b17a609f7133d216b3e83dd3..67e167524daa8021cb6e4839ac16ab61cc71509e 100644 |
| --- a/ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_article_item.mm |
| +++ b/ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_article_item.mm |
| @@ -8,6 +8,7 @@ |
| #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| #import "ios/chrome/browser/ui/util/i18n_string.h" |
| +#import "ios/third_party/material_components_ios/src/components/Palettes/src/MaterialPalettes.h" |
| #import "ios/third_party/material_components_ios/src/components/Typography/src/MaterialTypography.h" |
| #include "url/gurl.h" |
| @@ -19,6 +20,15 @@ namespace { |
| const CGFloat kImageSize = 72; |
| // When updating this, make sure to update |layoutSubviews|. |
| const CGFloat kStandardSpacing = 16; |
| +// Size of the icon displayed when there is not image. |
| +const CGFloat kIconSize = 24; |
| +// Name of the image. |
| +NSString* const kImageName = @"content_suggestions_no_image"; |
|
jif
2017/03/28 09:04:58
kImageName can be made more precise. Possible repl
gambard
2017/03/28 09:40:29
The backend service use "fetchSuggestionImage". I
|
| +// No image icon percentage of white. |
| +const CGFloat kImageWhite = 0.38; |
| +// No image background percentage of white. |
| +const CGFloat kBackgroundWhite = 0.95; |
| +const CGFloat kAnimationDuration = 0.3; |
| } |
| @interface ContentSuggestionsArticleItem () |
| @@ -57,7 +67,6 @@ const CGFloat kStandardSpacing = 16; |
| _subtitle = [subtitle copy]; |
| _articleURL = url; |
| _delegate = delegate; |
| - _image = [self emptyImageBackground]; |
| } |
| return self; |
| } |
| @@ -71,25 +80,11 @@ const CGFloat kStandardSpacing = 16; |
| } |
| cell.titleLabel.text = self.title; |
| cell.subtitleLabel.text = self.subtitle; |
| - cell.imageView.image = self.image; |
| + if (self.image) |
|
jif
2017/03/28 09:04:58
we are already checking in |setContentImage| if it
gambard
2017/03/28 09:40:29
Done.
|
| + [cell setContentImage:self.image]; |
| [cell setPublisherName:self.publisher date:self.publishDate]; |
| } |
| -#pragma mark - Private |
| - |
| -- (UIImage*)emptyImageBackground { |
| - // TODO(crbug.com/698171): Remove this function once we have real background |
| - // image. |
| - UIColor* color = [UIColor lightGrayColor]; |
| - CGRect rect = CGRectMake(0, 0, 1, 1); |
| - UIGraphicsBeginImageContext(rect.size); |
| - [color setFill]; |
| - UIRectFill(rect); |
| - UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); |
| - UIGraphicsEndImageContext(); |
| - return image; |
| -} |
| - |
| @end |
| #pragma mark - ContentSuggestionsArticleCell |
| @@ -97,6 +92,9 @@ const CGFloat kStandardSpacing = 16; |
| @interface ContentSuggestionsArticleCell () |
| @property(nonatomic, strong) UILabel* publisherLabel; |
| +@property(nonatomic, strong) UIView* imageContainer; |
| +@property(nonatomic, strong) UIImageView* noImageIcon; |
| +@property(nonatomic, strong) UIImageView* contentImageView; |
| // Applies the constraints on the elements. Called in the init. |
| - (void)applyConstraints; |
| @@ -107,15 +105,18 @@ const CGFloat kStandardSpacing = 16; |
| @synthesize titleLabel = _titleLabel; |
| @synthesize subtitleLabel = _subtitleLabel; |
| -@synthesize imageView = _imageView; |
| +@synthesize imageContainer = _imageContainer; |
| +@synthesize noImageIcon = _noImageIcon; |
| @synthesize publisherLabel = _publisherLabel; |
| +@synthesize contentImageView = _contentImageView; |
| - (instancetype)initWithFrame:(CGRect)frame { |
| self = [super initWithFrame:frame]; |
| if (self) { |
| _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| - _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; |
| + _imageContainer = [[UIView alloc] initWithFrame:CGRectZero]; |
| + _noImageIcon = [[UIImageView alloc] initWithFrame:CGRectZero]; |
| _publisherLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| _titleLabel.numberOfLines = 2; |
| @@ -124,19 +125,26 @@ const CGFloat kStandardSpacing = 16; |
| forAxis:UILayoutConstraintAxisVertical]; |
| [_titleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh |
| forAxis:UILayoutConstraintAxisVertical]; |
| - _imageView.contentMode = UIViewContentModeScaleAspectFill; |
| - _imageView.clipsToBounds = YES; |
| - _imageView.translatesAutoresizingMaskIntoConstraints = NO; |
| + _imageContainer.translatesAutoresizingMaskIntoConstraints = NO; |
| + _noImageIcon.translatesAutoresizingMaskIntoConstraints = NO; |
| _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| _publisherLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| - [self.contentView addSubview:_imageView]; |
| + [self.contentView addSubview:_imageContainer]; |
| [self.contentView addSubview:_titleLabel]; |
| [self.contentView addSubview:_subtitleLabel]; |
| [self.contentView addSubview:_publisherLabel]; |
| + [_imageContainer addSubview:_noImageIcon]; |
| + |
| + _imageContainer.backgroundColor = |
| + [UIColor colorWithWhite:kBackgroundWhite alpha:1]; |
| + _noImageIcon.image = [[UIImage imageNamed:kImageName] |
| + imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; |
| + [_noImageIcon setTintColor:[UIColor colorWithWhite:kImageWhite alpha:1]]; |
| + |
| _titleLabel.font = [MDCTypography subheadFont]; |
| _subtitleLabel.font = [MDCTypography body1Font]; |
| _publisherLabel.font = [MDCTypography captionFont]; |
| @@ -149,6 +157,25 @@ const CGFloat kStandardSpacing = 16; |
| return self; |
| } |
| +- (void)setContentImage:(UIImage*)image { |
| + if (!image) |
| + return; |
| + self.contentImageView = [[UIImageView alloc] initWithFrame:CGRectZero]; |
| + self.contentImageView.translatesAutoresizingMaskIntoConstraints = NO; |
| + self.contentImageView.contentMode = UIViewContentModeScaleAspectFill; |
| + self.contentImageView.clipsToBounds = YES; |
| + self.contentImageView.image = image; |
| + |
| + self.contentImageView.alpha = 0; |
| + [self.imageContainer addSubview:self.contentImageView]; |
| + AddSameSizeConstraint(self.contentImageView, self.imageContainer); |
| + |
| + [UIView animateWithDuration:kAnimationDuration |
| + animations:^{ |
| + self.contentImageView.alpha = 1; |
| + }]; |
| +} |
| + |
| - (void)setPublisherName:(NSString*)publisherName date:(base::Time)publishDate { |
| NSDate* date = [NSDate dateWithTimeIntervalSince1970:publishDate.ToDoubleT()]; |
| NSString* dateString = |
| @@ -160,6 +187,10 @@ const CGFloat kStandardSpacing = 16; |
| [NSString stringWithFormat:@"%@ - %@.", publisherName, dateString]); |
| } |
| +- (void)prepareForReuse { |
| + [self.contentImageView removeFromSuperview]; |
| +} |
| + |
| #pragma mark - UIView |
| // Implements -layoutSubviews as per instructions in documentation for |
| @@ -185,15 +216,24 @@ const CGFloat kStandardSpacing = 16; |
| - (void)applyConstraints { |
| [NSLayoutConstraint activateConstraints:@[ |
| - [_imageView.widthAnchor constraintEqualToConstant:kImageSize], |
| - [_imageView.heightAnchor constraintEqualToAnchor:_imageView.widthAnchor], |
| - [_imageView.topAnchor constraintEqualToAnchor:_titleLabel.topAnchor], |
| + [_imageContainer.widthAnchor constraintEqualToConstant:kImageSize], |
| + [_imageContainer.heightAnchor |
| + constraintEqualToAnchor:_imageContainer.widthAnchor], |
| + [_imageContainer.topAnchor constraintEqualToAnchor:_titleLabel.topAnchor], |
| [_publisherLabel.topAnchor |
| - constraintGreaterThanOrEqualToAnchor:_imageView.bottomAnchor |
| + constraintGreaterThanOrEqualToAnchor:_imageContainer.bottomAnchor |
| constant:kStandardSpacing], |
| [_publisherLabel.topAnchor |
| constraintGreaterThanOrEqualToAnchor:_subtitleLabel.bottomAnchor |
| constant:kStandardSpacing], |
| + |
| + // No image icon. |
| + [_noImageIcon.centerXAnchor |
| + constraintEqualToAnchor:_imageContainer.centerXAnchor], |
| + [_noImageIcon.centerYAnchor |
| + constraintEqualToAnchor:_imageContainer.centerYAnchor], |
| + [_noImageIcon.widthAnchor constraintEqualToConstant:kIconSize], |
| + [_noImageIcon.heightAnchor constraintEqualToAnchor:_noImageIcon.widthAnchor] |
| ]]; |
| ApplyVisualConstraintsWithMetrics( |
| @@ -205,7 +245,7 @@ const CGFloat kStandardSpacing = 16; |
| @"V:[publish]-|", |
| ], |
| @{ |
| - @"image" : _imageView, |
| + @"image" : _imageContainer, |
| @"title" : _titleLabel, |
| @"text" : _subtitleLabel, |
| @"publish" : _publisherLabel, |