| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_article_i
tem.h" | 5 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_article_i
tem.h" |
| 6 | 6 |
| 7 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 7 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 8 | 8 |
| 9 #if !defined(__has_feature) || !__has_feature(objc_arc) | 9 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 10 #error "This file requires ARC support." | 10 #error "This file requires ARC support." |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 const CGFloat kImageSize = 100; | 14 const CGFloat kImageSize = 100; |
| 15 const CGFloat kStandardSpacing = 8; |
| 15 } | 16 } |
| 16 | 17 |
| 17 @interface ContentSuggestionsArticleItem () | 18 @interface ContentSuggestionsArticleItem () |
| 18 | 19 |
| 19 @property(nonatomic, copy) NSString* title; | 20 @property(nonatomic, copy) NSString* title; |
| 20 @property(nonatomic, copy) NSString* subtitle; | 21 @property(nonatomic, copy) NSString* subtitle; |
| 21 @property(nonatomic, strong) UIImage* image; | |
| 22 | 22 |
| 23 @end | 23 @end |
| 24 | 24 |
| 25 @implementation ContentSuggestionsArticleItem | 25 @implementation ContentSuggestionsArticleItem |
| 26 | 26 |
| 27 @synthesize title = _title; | 27 @synthesize title = _title; |
| 28 @synthesize subtitle = _subtitle; | 28 @synthesize subtitle = _subtitle; |
| 29 @synthesize image = _image; | 29 @synthesize image = _image; |
| 30 @synthesize articleURL = _articleURL; |
| 30 | 31 |
| 31 - (instancetype)initWithType:(NSInteger)type | 32 - (instancetype)initWithType:(NSInteger)type |
| 32 title:(NSString*)title | 33 title:(NSString*)title |
| 33 subtitle:(NSString*)subtitle | 34 subtitle:(NSString*)subtitle |
| 34 image:(UIImage*)image { | 35 image:(UIImage*)image |
| 36 url:(const GURL&)url { |
| 35 self = [super initWithType:type]; | 37 self = [super initWithType:type]; |
| 36 if (self) { | 38 if (self) { |
| 37 self.cellClass = [ContentSuggestionsArticleCell class]; | 39 self.cellClass = [ContentSuggestionsArticleCell class]; |
| 38 _title = [title copy]; | 40 _title = [title copy]; |
| 39 _subtitle = [subtitle copy]; | 41 _subtitle = [subtitle copy]; |
| 40 _image = image; | 42 _image = image; |
| 43 _articleURL = url; |
| 41 } | 44 } |
| 42 return self; | 45 return self; |
| 43 } | 46 } |
| 44 | 47 |
| 45 - (void)configureCell:(ContentSuggestionsArticleCell*)cell { | 48 - (void)configureCell:(ContentSuggestionsArticleCell*)cell { |
| 46 [super configureCell:cell]; | 49 [super configureCell:cell]; |
| 47 cell.titleLabel.text = _title; | 50 cell.titleLabel.text = _title; |
| 48 cell.subtitleLabel.text = _subtitle; | 51 cell.subtitleLabel.text = _subtitle; |
| 49 cell.imageView.image = _image; | 52 cell.imageView.image = _image; |
| 50 } | 53 } |
| 51 | 54 |
| 52 @end | 55 @end |
| 53 | 56 |
| 54 @implementation ContentSuggestionsArticleCell | 57 @implementation ContentSuggestionsArticleCell |
| 55 | 58 |
| 56 @synthesize titleLabel = _titleLabel; | 59 @synthesize titleLabel = _titleLabel; |
| 57 @synthesize subtitleLabel = _subtitleLabel; | 60 @synthesize subtitleLabel = _subtitleLabel; |
| 58 @synthesize imageView = _imageView; | 61 @synthesize imageView = _imageView; |
| 59 | 62 |
| 60 - (instancetype)initWithFrame:(CGRect)frame { | 63 - (instancetype)initWithFrame:(CGRect)frame { |
| 61 self = [super initWithFrame:frame]; | 64 self = [super initWithFrame:frame]; |
| 62 if (self) { | 65 if (self) { |
| 63 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | 66 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 64 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | 67 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 65 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; | 68 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; |
| 66 UIView* imageContainer = [[UIView alloc] initWithFrame:CGRectZero]; | |
| 67 | 69 |
| 68 _subtitleLabel.numberOfLines = 0; | 70 _subtitleLabel.numberOfLines = 0; |
| 71 _imageView.contentMode = UIViewContentModeScaleAspectFit; |
| 69 | 72 |
| 70 imageContainer.translatesAutoresizingMaskIntoConstraints = NO; | |
| 71 _imageView.translatesAutoresizingMaskIntoConstraints = NO; | 73 _imageView.translatesAutoresizingMaskIntoConstraints = NO; |
| 72 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; | 74 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 73 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; | 75 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 74 | 76 |
| 75 [imageContainer addSubview:_imageView]; | 77 [self.contentView addSubview:_imageView]; |
| 76 [self.contentView addSubview:imageContainer]; | |
| 77 [self.contentView addSubview:_titleLabel]; | 78 [self.contentView addSubview:_titleLabel]; |
| 78 [self.contentView addSubview:_subtitleLabel]; | 79 [self.contentView addSubview:_subtitleLabel]; |
| 79 | 80 |
| 80 ApplyVisualConstraintsWithMetrics( | 81 [NSLayoutConstraint activateConstraints:@[ |
| 82 [self.contentView.bottomAnchor |
| 83 constraintGreaterThanOrEqualToAnchor:_imageView.bottomAnchor |
| 84 constant:kStandardSpacing], |
| 85 [self.contentView.bottomAnchor |
| 86 constraintGreaterThanOrEqualToAnchor:_subtitleLabel.bottomAnchor |
| 87 constant:kStandardSpacing], |
| 88 [_imageView.widthAnchor constraintLessThanOrEqualToConstant:kImageSize], |
| 89 [_imageView.heightAnchor constraintLessThanOrEqualToConstant:kImageSize] |
| 90 ]]; |
| 91 |
| 92 ApplyVisualConstraints( |
| 81 @[ | 93 @[ |
| 82 @"H:|-[title]-[imageContainer(imageSize)]-|", | 94 @"H:|-[title]-[image]-|", |
| 83 @"H:|[image(imageSize)]", @"H:|-[text]-[imageContainer]", | 95 @"H:|-[text]-[image]", |
| 84 @"V:|[image(imageSize)]", @"V:|-[title]-[text]-|", | 96 @"V:|-[title]-[text]-|", |
| 85 @"V:|-[imageContainer(>=imageSize)]-|" | 97 @"V:|-[image]", |
| 86 ], | 98 ], |
| 87 @{ | 99 @{ |
| 88 @"image" : _imageView, | 100 @"image" : _imageView, |
| 89 @"imageContainer" : imageContainer, | |
| 90 @"title" : _titleLabel, | 101 @"title" : _titleLabel, |
| 91 @"text" : _subtitleLabel | 102 @"text" : _subtitleLabel |
| 92 }, | 103 }); |
| 93 @{ @"imageSize" : @(kImageSize) }); | |
| 94 } | 104 } |
| 95 return self; | 105 return self; |
| 96 } | 106 } |
| 97 | 107 |
| 98 #pragma mark - UIView | 108 #pragma mark - UIView |
| 99 | 109 |
| 100 // Implements -layoutSubviews as per instructions in documentation for | 110 // Implements -layoutSubviews as per instructions in documentation for |
| 101 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | 111 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
| 102 - (void)layoutSubviews { | 112 - (void)layoutSubviews { |
| 103 [super layoutSubviews]; | 113 [super layoutSubviews]; |
| 104 | 114 |
| 105 // Adjust the text label preferredMaxLayoutWidth when the parent's width | 115 // Adjust the text label preferredMaxLayoutWidth when the parent's width |
| 106 // changes, for instance on screen rotation. | 116 // changes, for instance on screen rotation. |
| 107 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); | 117 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); |
| 108 self.subtitleLabel.preferredMaxLayoutWidth = parentWidth - kImageSize - 3 * 8; | 118 self.subtitleLabel.preferredMaxLayoutWidth = parentWidth - kImageSize - 3 * 8; |
| 109 | 119 |
| 110 // Re-layout with the new preferred width to allow the label to adjust its | 120 // Re-layout with the new preferred width to allow the label to adjust its |
| 111 // height. | 121 // height. |
| 112 [super layoutSubviews]; | 122 [super layoutSubviews]; |
| 113 } | 123 } |
| 114 | 124 |
| 115 @end | 125 @end |
| OLD | NEW |