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