| 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 #include "base/time/time.h" | 7 #include "base/time/time.h" |
| 8 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| 8 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 9 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 10 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
| 9 | 11 |
| 10 #if !defined(__has_feature) || !__has_feature(objc_arc) | 12 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 11 #error "This file requires ARC support." | 13 #error "This file requires ARC support." |
| 12 #endif | 14 #endif |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 const CGFloat kImageSize = 100; | 17 const CGFloat kImageSize = 72; |
| 18 // When updating this, make sure to update |layoutSubviews|. |
| 16 const CGFloat kStandardSpacing = 8; | 19 const CGFloat kStandardSpacing = 8; |
| 17 } | 20 } |
| 18 | 21 |
| 19 @interface ContentSuggestionsArticleItem () | 22 @interface ContentSuggestionsArticleItem () |
| 20 | 23 |
| 21 @property(nonatomic, copy) NSString* subtitle; | 24 @property(nonatomic, copy) NSString* subtitle; |
| 22 // Used to check if the image has already been fetched. There is no way to | 25 // Used to check if the image has already been fetched. There is no way to |
| 23 // discriminate between failed image download and nonexitent image. The article | 26 // discriminate between failed image download and nonexitent image. The article |
| 24 // tries to download the image only once. | 27 // tries to download the image only once. |
| 25 @property(nonatomic, assign) BOOL imageFetched; | 28 @property(nonatomic, assign) BOOL imageFetched; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 } | 60 } |
| 58 | 61 |
| 59 - (void)configureCell:(ContentSuggestionsArticleCell*)cell { | 62 - (void)configureCell:(ContentSuggestionsArticleCell*)cell { |
| 60 [super configureCell:cell]; | 63 [super configureCell:cell]; |
| 61 if (!self.image && !self.imageFetched) { | 64 if (!self.image && !self.imageFetched) { |
| 62 self.imageFetched = YES; | 65 self.imageFetched = YES; |
| 63 // Fetch the image. During the fetch the cell's image should still be set to | 66 // Fetch the image. During the fetch the cell's image should still be set to |
| 64 // nil. | 67 // nil. |
| 65 [self.delegate loadImageForArticleItem:self]; | 68 [self.delegate loadImageForArticleItem:self]; |
| 66 } | 69 } |
| 67 cell.titleLabel.text = _title; | 70 cell.titleLabel.text = self.title; |
| 68 cell.subtitleLabel.text = _subtitle; | 71 cell.subtitleLabel.text = self.subtitle; |
| 69 cell.imageView.image = _image; | 72 cell.imageView.image = self.image; |
| 70 [cell setPublisherName:self.publisher date:self.publishDate]; | 73 [cell setPublisherName:self.publisher date:self.publishDate]; |
| 71 } | 74 } |
| 72 | 75 |
| 73 @end | 76 @end |
| 74 | 77 |
| 75 #pragma mark - ContentSuggestionsArticleCell | 78 #pragma mark - ContentSuggestionsArticleCell |
| 76 | 79 |
| 77 @interface ContentSuggestionsArticleCell () | 80 @interface ContentSuggestionsArticleCell () |
| 78 | 81 |
| 79 @property(nonatomic, strong) UILabel* publisherLabel; | 82 @property(nonatomic, strong) UILabel* publisherLabel; |
| 80 | 83 |
| 84 // Applies the constraints on the elements. Called in the init. |
| 85 - (void)applyConstraints; |
| 86 |
| 81 @end | 87 @end |
| 82 | 88 |
| 83 @implementation ContentSuggestionsArticleCell | 89 @implementation ContentSuggestionsArticleCell |
| 84 | 90 |
| 85 @synthesize titleLabel = _titleLabel; | 91 @synthesize titleLabel = _titleLabel; |
| 86 @synthesize subtitleLabel = _subtitleLabel; | 92 @synthesize subtitleLabel = _subtitleLabel; |
| 87 @synthesize imageView = _imageView; | 93 @synthesize imageView = _imageView; |
| 88 @synthesize publisherLabel = _publisherLabel; | 94 @synthesize publisherLabel = _publisherLabel; |
| 89 | 95 |
| 90 - (instancetype)initWithFrame:(CGRect)frame { | 96 - (instancetype)initWithFrame:(CGRect)frame { |
| 91 self = [super initWithFrame:frame]; | 97 self = [super initWithFrame:frame]; |
| 92 if (self) { | 98 if (self) { |
| 93 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | 99 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 94 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | 100 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 95 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; | 101 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; |
| 96 _publisherLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | 102 _publisherLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 97 | 103 |
| 104 _titleLabel.numberOfLines = 2; |
| 98 _subtitleLabel.numberOfLines = 0; | 105 _subtitleLabel.numberOfLines = 0; |
| 99 [_subtitleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh | 106 [_subtitleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh |
| 100 forAxis:UILayoutConstraintAxisVertical]; | 107 forAxis:UILayoutConstraintAxisVertical]; |
| 101 [_titleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh | 108 [_titleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh |
| 102 forAxis:UILayoutConstraintAxisVertical]; | 109 forAxis:UILayoutConstraintAxisVertical]; |
| 103 _imageView.contentMode = UIViewContentModeScaleAspectFit; | 110 _imageView.contentMode = UIViewContentModeScaleAspectFill; |
| 111 _imageView.clipsToBounds = YES; |
| 104 | 112 |
| 105 _imageView.translatesAutoresizingMaskIntoConstraints = NO; | 113 _imageView.translatesAutoresizingMaskIntoConstraints = NO; |
| 106 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; | 114 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 107 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; | 115 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 108 _publisherLabel.translatesAutoresizingMaskIntoConstraints = NO; | 116 _publisherLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 109 | 117 |
| 110 [self.contentView addSubview:_imageView]; | 118 [self.contentView addSubview:_imageView]; |
| 111 [self.contentView addSubview:_titleLabel]; | 119 [self.contentView addSubview:_titleLabel]; |
| 112 [self.contentView addSubview:_subtitleLabel]; | 120 [self.contentView addSubview:_subtitleLabel]; |
| 113 [self.contentView addSubview:_publisherLabel]; | 121 [self.contentView addSubview:_publisherLabel]; |
| 114 | 122 |
| 115 [NSLayoutConstraint activateConstraints:@[ | 123 _titleLabel.font = [MDCTypography subheadFont]; |
| 116 [_imageView.widthAnchor constraintLessThanOrEqualToConstant:kImageSize], | 124 _subtitleLabel.font = [MDCTypography body1Font]; |
| 117 [_imageView.heightAnchor constraintLessThanOrEqualToConstant:kImageSize], | 125 _publisherLabel.font = [MDCTypography captionFont]; |
| 118 [_publisherLabel.topAnchor | |
| 119 constraintGreaterThanOrEqualToAnchor:_imageView.bottomAnchor | |
| 120 constant:kStandardSpacing], | |
| 121 [_publisherLabel.topAnchor | |
| 122 constraintGreaterThanOrEqualToAnchor:_subtitleLabel.bottomAnchor | |
| 123 constant:kStandardSpacing], | |
| 124 ]]; | |
| 125 | 126 |
| 126 ApplyVisualConstraints( | 127 _subtitleLabel.textColor = [[MDCPalette greyPalette] tint700]; |
| 127 @[ | 128 _publisherLabel.textColor = [[MDCPalette greyPalette] tint700]; |
| 128 @"H:|-[title]-[image]-|", | 129 |
| 129 @"H:|-[text]-[image]", | 130 [self applyConstraints]; |
| 130 @"V:|-[title]-[text]", | |
| 131 @"V:|-[image]", | |
| 132 @"H:|-[publish]-|", | |
| 133 @"V:[publish]-|", | |
| 134 ], | |
| 135 @{ | |
| 136 @"image" : _imageView, | |
| 137 @"title" : _titleLabel, | |
| 138 @"text" : _subtitleLabel, | |
| 139 @"publish" : _publisherLabel, | |
| 140 }); | |
| 141 } | 131 } |
| 142 return self; | 132 return self; |
| 143 } | 133 } |
| 144 | 134 |
| 145 - (void)setPublisherName:(NSString*)publisherName date:(base::Time)publishDate { | 135 - (void)setPublisherName:(NSString*)publisherName date:(base::Time)publishDate { |
| 146 NSDate* date = [NSDate dateWithTimeIntervalSince1970:publishDate.ToDoubleT()]; | 136 NSDate* date = [NSDate dateWithTimeIntervalSince1970:publishDate.ToDoubleT()]; |
| 147 NSString* dateString = | 137 NSString* dateString = |
| 148 [NSDateFormatter localizedStringFromDate:date | 138 [NSDateFormatter localizedStringFromDate:date |
| 149 dateStyle:NSDateFormatterMediumStyle | 139 dateStyle:NSDateFormatterMediumStyle |
| 150 timeStyle:NSDateFormatterNoStyle]; | 140 timeStyle:NSDateFormatterNoStyle]; |
| 151 | 141 |
| 152 // TODO(crbug.com/694423): Make it RTL friendly. | 142 // TODO(crbug.com/694423): Make it RTL friendly. |
| 153 self.publisherLabel.text = | 143 self.publisherLabel.text = |
| 154 [NSString stringWithFormat:@"%@ - %@.", publisherName, dateString]; | 144 [NSString stringWithFormat:@"%@ - %@.", publisherName, dateString]; |
| 155 } | 145 } |
| 156 | 146 |
| 157 #pragma mark - UIView | 147 #pragma mark - UIView |
| 158 | 148 |
| 159 // Implements -layoutSubviews as per instructions in documentation for | 149 // Implements -layoutSubviews as per instructions in documentation for |
| 160 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | 150 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
| 161 - (void)layoutSubviews { | 151 - (void)layoutSubviews { |
| 162 [super layoutSubviews]; | 152 [super layoutSubviews]; |
| 163 | 153 |
| 164 // Adjust the text label preferredMaxLayoutWidth when the parent's width | 154 // Adjust the text label preferredMaxLayoutWidth when the parent's width |
| 165 // changes, for instance on screen rotation. | 155 // changes, for instance on screen rotation. |
| 166 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); | 156 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); |
| 157 |
| 158 self.titleLabel.preferredMaxLayoutWidth = |
| 159 parentWidth - self.imageView.bounds.size.width - 3 * kStandardSpacing; |
| 167 self.subtitleLabel.preferredMaxLayoutWidth = | 160 self.subtitleLabel.preferredMaxLayoutWidth = |
| 168 parentWidth - self.imageView.bounds.size.width - 3 * 8; | 161 parentWidth - self.imageView.bounds.size.width - 3 * kStandardSpacing; |
| 169 | 162 |
| 170 // Re-layout with the new preferred width to allow the label to adjust its | 163 // Re-layout with the new preferred width to allow the label to adjust its |
| 171 // height. | 164 // height. |
| 172 [super layoutSubviews]; | 165 [super layoutSubviews]; |
| 173 } | 166 } |
| 174 | 167 |
| 168 #pragma mark - Private |
| 169 |
| 170 - (void)applyConstraints { |
| 171 [NSLayoutConstraint activateConstraints:@[ |
| 172 [_imageView.widthAnchor constraintEqualToConstant:kImageSize], |
| 173 [_imageView.heightAnchor constraintEqualToAnchor:_imageView.widthAnchor], |
| 174 [_publisherLabel.topAnchor |
| 175 constraintGreaterThanOrEqualToAnchor:_imageView.bottomAnchor |
| 176 constant:kStandardSpacing], |
| 177 [_publisherLabel.topAnchor |
| 178 constraintGreaterThanOrEqualToAnchor:_subtitleLabel.bottomAnchor |
| 179 constant:kStandardSpacing], |
| 180 ]]; |
| 181 |
| 182 ApplyVisualConstraintsWithMetrics( |
| 183 @[ |
| 184 @"H:|-(space)-[title]-(space)-[image]-(space)-|", |
| 185 @"H:|-(space)-[text]-(space)-[image]", |
| 186 @"V:|-[title]-[text]", |
| 187 @"V:|-[image]", |
| 188 @"H:|-[publish]-|", |
| 189 @"V:[publish]-|", |
| 190 ], |
| 191 @{ |
| 192 @"image" : _imageView, |
| 193 @"title" : _titleLabel, |
| 194 @"text" : _subtitleLabel, |
| 195 @"publish" : _publisherLabel, |
| 196 }, |
| 197 @{ @"space" : @(kStandardSpacing) }); |
| 198 } |
| 199 |
| 175 @end | 200 @end |
| OLD | NEW |