Chromium Code Reviews| 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; |
| 16 const CGFloat kStandardSpacing = 8; | 18 const CGFloat kStandardSpacing = 8; |
| 17 } | 19 } |
| 18 | 20 |
| 19 @interface ContentSuggestionsArticleItem () | 21 @interface ContentSuggestionsArticleItem () |
| 20 | 22 |
| 21 @property(nonatomic, copy) NSString* subtitle; | 23 @property(nonatomic, copy) NSString* subtitle; |
| 22 @property(nonatomic, weak) id<ContentSuggestionsArticleItemDelegate> delegate; | 24 @property(nonatomic, weak) id<ContentSuggestionsArticleItemDelegate> delegate; |
| 23 | 25 |
| 24 @end | 26 @end |
| 25 | 27 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 51 _delegate = delegate; | 53 _delegate = delegate; |
| 52 } | 54 } |
| 53 return self; | 55 return self; |
| 54 } | 56 } |
| 55 | 57 |
| 56 - (void)configureCell:(ContentSuggestionsArticleCell*)cell { | 58 - (void)configureCell:(ContentSuggestionsArticleCell*)cell { |
| 57 [super configureCell:cell]; | 59 [super configureCell:cell]; |
| 58 if (!self.image && !self.imageBeingFetched) { | 60 if (!self.image && !self.imageBeingFetched) { |
| 59 [self.delegate loadImageForArticleItem:self]; | 61 [self.delegate loadImageForArticleItem:self]; |
| 60 } | 62 } |
| 61 cell.titleLabel.text = _title; | 63 cell.titleLabel.text = self.title; |
| 62 cell.subtitleLabel.text = _subtitle; | 64 cell.subtitleLabel.text = self.subtitle; |
| 63 cell.imageView.image = _image; | 65 cell.imageView.image = self.image; |
| 64 [cell setPublisherName:self.publisher date:self.publishDate]; | 66 [cell setPublisherName:self.publisher date:self.publishDate]; |
| 65 } | 67 } |
| 66 | 68 |
| 67 @end | 69 @end |
| 68 | 70 |
| 69 #pragma mark - ContentSuggestionsArticleCell | 71 #pragma mark - ContentSuggestionsArticleCell |
| 70 | 72 |
| 71 @interface ContentSuggestionsArticleCell () | 73 @interface ContentSuggestionsArticleCell () |
| 72 | 74 |
| 73 @property(nonatomic, strong) UILabel* publisherLabel; | 75 @property(nonatomic, strong) UILabel* publisherLabel; |
| 74 | 76 |
| 77 - (void)applyConstraints; | |
|
Olivier
2017/03/01 08:41:42
comment: Called from init.
gambard
2017/03/01 11:55:16
Done.
| |
| 78 | |
| 75 @end | 79 @end |
| 76 | 80 |
| 77 @implementation ContentSuggestionsArticleCell | 81 @implementation ContentSuggestionsArticleCell |
| 78 | 82 |
| 79 @synthesize titleLabel = _titleLabel; | 83 @synthesize titleLabel = _titleLabel; |
| 80 @synthesize subtitleLabel = _subtitleLabel; | 84 @synthesize subtitleLabel = _subtitleLabel; |
| 81 @synthesize imageView = _imageView; | 85 @synthesize imageView = _imageView; |
| 82 @synthesize publisherLabel = _publisherLabel; | 86 @synthesize publisherLabel = _publisherLabel; |
| 83 | 87 |
| 84 - (instancetype)initWithFrame:(CGRect)frame { | 88 - (instancetype)initWithFrame:(CGRect)frame { |
| 85 self = [super initWithFrame:frame]; | 89 self = [super initWithFrame:frame]; |
| 86 if (self) { | 90 if (self) { |
| 87 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | 91 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 88 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | 92 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 89 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; | 93 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; |
| 90 _publisherLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | 94 _publisherLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 91 | 95 |
| 96 _titleLabel.numberOfLines = 2; | |
| 92 _subtitleLabel.numberOfLines = 0; | 97 _subtitleLabel.numberOfLines = 0; |
| 93 [_subtitleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh | 98 [_subtitleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh |
| 94 forAxis:UILayoutConstraintAxisVertical]; | 99 forAxis:UILayoutConstraintAxisVertical]; |
| 95 [_titleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh | 100 [_titleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh |
| 96 forAxis:UILayoutConstraintAxisVertical]; | 101 forAxis:UILayoutConstraintAxisVertical]; |
| 97 _imageView.contentMode = UIViewContentModeScaleAspectFit; | 102 _imageView.contentMode = UIViewContentModeScaleAspectFill; |
| 103 _imageView.clipsToBounds = YES; | |
| 98 | 104 |
| 99 _imageView.translatesAutoresizingMaskIntoConstraints = NO; | 105 _imageView.translatesAutoresizingMaskIntoConstraints = NO; |
| 100 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; | 106 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 101 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; | 107 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 102 _publisherLabel.translatesAutoresizingMaskIntoConstraints = NO; | 108 _publisherLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 103 | 109 |
| 104 [self.contentView addSubview:_imageView]; | 110 [self.contentView addSubview:_imageView]; |
| 105 [self.contentView addSubview:_titleLabel]; | 111 [self.contentView addSubview:_titleLabel]; |
| 106 [self.contentView addSubview:_subtitleLabel]; | 112 [self.contentView addSubview:_subtitleLabel]; |
| 107 [self.contentView addSubview:_publisherLabel]; | 113 [self.contentView addSubview:_publisherLabel]; |
| 108 | 114 |
| 109 [NSLayoutConstraint activateConstraints:@[ | 115 _titleLabel.font = [MDCTypography subheadFont]; |
| 110 [_imageView.widthAnchor constraintLessThanOrEqualToConstant:kImageSize], | 116 _subtitleLabel.font = [MDCTypography body1Font]; |
| 111 [_imageView.heightAnchor constraintLessThanOrEqualToConstant:kImageSize], | 117 _publisherLabel.font = [MDCTypography captionFont]; |
| 112 [_publisherLabel.topAnchor | |
| 113 constraintGreaterThanOrEqualToAnchor:_imageView.bottomAnchor | |
| 114 constant:kStandardSpacing], | |
| 115 [_publisherLabel.topAnchor | |
| 116 constraintGreaterThanOrEqualToAnchor:_subtitleLabel.bottomAnchor | |
| 117 constant:kStandardSpacing], | |
| 118 ]]; | |
| 119 | 118 |
| 120 ApplyVisualConstraints( | 119 _subtitleLabel.textColor = [[MDCPalette greyPalette] tint700]; |
| 121 @[ | 120 _publisherLabel.textColor = [[MDCPalette greyPalette] tint700]; |
| 122 @"H:|-[title]-[image]-|", | 121 |
| 123 @"H:|-[text]-[image]", | 122 [self applyConstraints]; |
| 124 @"V:|-[title]-[text]", | |
| 125 @"V:|-[image]", | |
| 126 @"H:|-[publish]-|", | |
| 127 @"V:[publish]-|", | |
| 128 ], | |
| 129 @{ | |
| 130 @"image" : _imageView, | |
| 131 @"title" : _titleLabel, | |
| 132 @"text" : _subtitleLabel, | |
| 133 @"publish" : _publisherLabel, | |
| 134 }); | |
| 135 } | 123 } |
| 136 return self; | 124 return self; |
| 137 } | 125 } |
| 138 | 126 |
| 139 - (void)setPublisherName:(NSString*)publisherName date:(base::Time)publishDate { | 127 - (void)setPublisherName:(NSString*)publisherName date:(base::Time)publishDate { |
| 140 NSDate* date = [NSDate dateWithTimeIntervalSince1970:publishDate.ToDoubleT()]; | 128 NSDate* date = [NSDate dateWithTimeIntervalSince1970:publishDate.ToDoubleT()]; |
| 141 NSString* dateString = | 129 NSString* dateString = |
| 142 [NSDateFormatter localizedStringFromDate:date | 130 [NSDateFormatter localizedStringFromDate:date |
| 143 dateStyle:NSDateFormatterMediumStyle | 131 dateStyle:NSDateFormatterMediumStyle |
| 144 timeStyle:NSDateFormatterNoStyle]; | 132 timeStyle:NSDateFormatterNoStyle]; |
| 145 | 133 |
| 146 // TODO(crbug.com/694423): Make it RTL friendly. | 134 // TODO(crbug.com/694423): Make it RTL friendly. |
| 147 self.publisherLabel.text = | 135 self.publisherLabel.text = |
| 148 [NSString stringWithFormat:@"%@ - %@.", publisherName, dateString]; | 136 [NSString stringWithFormat:@"%@ - %@.", publisherName, dateString]; |
| 149 } | 137 } |
| 150 | 138 |
| 151 #pragma mark - UIView | 139 #pragma mark - UIView |
| 152 | 140 |
| 153 // Implements -layoutSubviews as per instructions in documentation for | 141 // Implements -layoutSubviews as per instructions in documentation for |
| 154 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | 142 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
| 155 - (void)layoutSubviews { | 143 - (void)layoutSubviews { |
| 156 [super layoutSubviews]; | 144 [super layoutSubviews]; |
| 157 | 145 |
| 158 // Adjust the text label preferredMaxLayoutWidth when the parent's width | 146 // Adjust the text label preferredMaxLayoutWidth when the parent's width |
| 159 // changes, for instance on screen rotation. | 147 // changes, for instance on screen rotation. |
| 160 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); | 148 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); |
| 149 | |
| 150 self.titleLabel.preferredMaxLayoutWidth = | |
| 151 parentWidth - self.imageView.bounds.size.width - 3 * 8; | |
|
Olivier
2017/03/01 08:41:42
what is 3 * 8 ?
gambard
2017/03/01 11:55:16
The margins. I have created a variable + a comment
| |
| 161 self.subtitleLabel.preferredMaxLayoutWidth = | 152 self.subtitleLabel.preferredMaxLayoutWidth = |
| 162 parentWidth - self.imageView.bounds.size.width - 3 * 8; | 153 parentWidth - self.imageView.bounds.size.width - 3 * 8; |
| 163 | 154 |
| 164 // Re-layout with the new preferred width to allow the label to adjust its | 155 // Re-layout with the new preferred width to allow the label to adjust its |
| 165 // height. | 156 // height. |
| 166 [super layoutSubviews]; | 157 [super layoutSubviews]; |
| 167 } | 158 } |
| 168 | 159 |
| 160 #pragma mark - Private | |
| 161 | |
| 162 - (void)applyConstraints { | |
| 163 [NSLayoutConstraint activateConstraints:@[ | |
| 164 [self.imageView.widthAnchor constraintEqualToConstant:kImageSize], | |
|
Olivier
2017/03/01 08:41:43
I don't think you should use getter from init.
gambard
2017/03/01 11:55:16
Done.
| |
| 165 [self.imageView.heightAnchor constraintEqualToConstant:kImageSize], | |
|
Olivier
2017/03/01 08:41:42
nit:
self.imageView.heightAnchor constraitEqualToA
gambard
2017/03/01 11:55:16
Done.
| |
| 166 [self.publisherLabel.topAnchor | |
| 167 constraintGreaterThanOrEqualToAnchor:self.imageView.bottomAnchor | |
| 168 constant:kStandardSpacing], | |
| 169 [self.publisherLabel.topAnchor | |
| 170 constraintGreaterThanOrEqualToAnchor:self.subtitleLabel.bottomAnchor | |
| 171 constant:kStandardSpacing], | |
| 172 ]]; | |
| 173 | |
| 174 ApplyVisualConstraints( | |
| 175 @[ | |
| 176 @"H:|-[title]-[image]-|", | |
| 177 @"H:|-[text]-[image]", | |
| 178 @"V:|-[title]-[text]", | |
| 179 @"V:|-[image]", | |
| 180 @"H:|-[publish]-|", | |
| 181 @"V:[publish]-|", | |
| 182 ], | |
| 183 @{ | |
| 184 @"image" : _imageView, | |
| 185 @"title" : _titleLabel, | |
| 186 @"text" : _subtitleLabel, | |
| 187 @"publish" : _publisherLabel, | |
| 188 }); | |
| 189 } | |
| 190 | |
| 169 @end | 191 @end |
| OLD | NEW |