| 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 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 8 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 8 | 9 |
| 9 #if !defined(__has_feature) || !__has_feature(objc_arc) | 10 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 10 #error "This file requires ARC support." | 11 #error "This file requires ARC support." |
| 11 #endif | 12 #endif |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 const CGFloat kImageSize = 100; | 15 const CGFloat kImageSize = 100; |
| 15 const CGFloat kStandardSpacing = 8; | 16 const CGFloat kStandardSpacing = 8; |
| 16 } | 17 } |
| 17 | 18 |
| 18 @interface ContentSuggestionsArticleItem () | 19 @interface ContentSuggestionsArticleItem () |
| 19 | 20 |
| 20 @property(nonatomic, copy) NSString* subtitle; | 21 @property(nonatomic, copy) NSString* subtitle; |
| 21 | 22 |
| 22 @end | 23 @end |
| 23 | 24 |
| 24 #pragma mark - ContentSuggestionsArticleItem | 25 #pragma mark - ContentSuggestionsArticleItem |
| 25 | 26 |
| 26 @implementation ContentSuggestionsArticleItem | 27 @implementation ContentSuggestionsArticleItem |
| 27 | 28 |
| 28 @synthesize title = _title; | 29 @synthesize title = _title; |
| 29 @synthesize subtitle = _subtitle; | 30 @synthesize subtitle = _subtitle; |
| 30 @synthesize image = _image; | 31 @synthesize image = _image; |
| 31 @synthesize articleURL = _articleURL; | 32 @synthesize articleURL = _articleURL; |
| 33 @synthesize publisher = _publisher; |
| 34 @synthesize publishDate = _publishDate; |
| 32 | 35 |
| 33 - (instancetype)initWithType:(NSInteger)type | 36 - (instancetype)initWithType:(NSInteger)type |
| 34 title:(NSString*)title | 37 title:(NSString*)title |
| 35 subtitle:(NSString*)subtitle | 38 subtitle:(NSString*)subtitle |
| 36 image:(UIImage*)image | 39 image:(UIImage*)image |
| 37 url:(const GURL&)url { | 40 url:(const GURL&)url { |
| 38 self = [super initWithType:type]; | 41 self = [super initWithType:type]; |
| 39 if (self) { | 42 if (self) { |
| 40 self.cellClass = [ContentSuggestionsArticleCell class]; | 43 self.cellClass = [ContentSuggestionsArticleCell class]; |
| 41 _title = [title copy]; | 44 _title = [title copy]; |
| 42 _subtitle = [subtitle copy]; | 45 _subtitle = [subtitle copy]; |
| 43 _image = image; | 46 _image = image; |
| 44 _articleURL = url; | 47 _articleURL = url; |
| 45 } | 48 } |
| 46 return self; | 49 return self; |
| 47 } | 50 } |
| 48 | 51 |
| 49 - (void)configureCell:(ContentSuggestionsArticleCell*)cell { | 52 - (void)configureCell:(ContentSuggestionsArticleCell*)cell { |
| 50 [super configureCell:cell]; | 53 [super configureCell:cell]; |
| 51 cell.titleLabel.text = _title; | 54 cell.titleLabel.text = _title; |
| 52 cell.subtitleLabel.text = _subtitle; | 55 cell.subtitleLabel.text = _subtitle; |
| 53 cell.imageView.image = _image; | 56 cell.imageView.image = _image; |
| 57 [cell setPublisherName:self.publisher date:self.publishDate]; |
| 54 } | 58 } |
| 55 | 59 |
| 56 @end | 60 @end |
| 57 | 61 |
| 58 #pragma mark - ContentSuggestionsArticleCell | 62 #pragma mark - ContentSuggestionsArticleCell |
| 59 | 63 |
| 64 @interface ContentSuggestionsArticleCell () |
| 65 |
| 66 @property(nonatomic, strong) UILabel* publisherLabel; |
| 67 |
| 68 @end |
| 69 |
| 60 @implementation ContentSuggestionsArticleCell | 70 @implementation ContentSuggestionsArticleCell |
| 61 | 71 |
| 62 @synthesize titleLabel = _titleLabel; | 72 @synthesize titleLabel = _titleLabel; |
| 63 @synthesize subtitleLabel = _subtitleLabel; | 73 @synthesize subtitleLabel = _subtitleLabel; |
| 64 @synthesize imageView = _imageView; | 74 @synthesize imageView = _imageView; |
| 75 @synthesize publisherLabel = _publisherLabel; |
| 65 | 76 |
| 66 - (instancetype)initWithFrame:(CGRect)frame { | 77 - (instancetype)initWithFrame:(CGRect)frame { |
| 67 self = [super initWithFrame:frame]; | 78 self = [super initWithFrame:frame]; |
| 68 if (self) { | 79 if (self) { |
| 69 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | 80 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 70 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | 81 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 71 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; | 82 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; |
| 83 _publisherLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 72 | 84 |
| 73 _subtitleLabel.numberOfLines = 0; | 85 _subtitleLabel.numberOfLines = 0; |
| 86 [_subtitleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh |
| 87 forAxis:UILayoutConstraintAxisVertical]; |
| 88 [_titleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh |
| 89 forAxis:UILayoutConstraintAxisVertical]; |
| 74 _imageView.contentMode = UIViewContentModeScaleAspectFit; | 90 _imageView.contentMode = UIViewContentModeScaleAspectFit; |
| 75 | 91 |
| 76 _imageView.translatesAutoresizingMaskIntoConstraints = NO; | 92 _imageView.translatesAutoresizingMaskIntoConstraints = NO; |
| 77 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; | 93 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 78 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; | 94 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 95 _publisherLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 79 | 96 |
| 80 [self.contentView addSubview:_imageView]; | 97 [self.contentView addSubview:_imageView]; |
| 81 [self.contentView addSubview:_titleLabel]; | 98 [self.contentView addSubview:_titleLabel]; |
| 82 [self.contentView addSubview:_subtitleLabel]; | 99 [self.contentView addSubview:_subtitleLabel]; |
| 100 [self.contentView addSubview:_publisherLabel]; |
| 83 | 101 |
| 84 [NSLayoutConstraint activateConstraints:@[ | 102 [NSLayoutConstraint activateConstraints:@[ |
| 85 [self.contentView.bottomAnchor | 103 [_imageView.widthAnchor constraintLessThanOrEqualToConstant:kImageSize], |
| 104 [_imageView.heightAnchor constraintLessThanOrEqualToConstant:kImageSize], |
| 105 [_publisherLabel.topAnchor |
| 86 constraintGreaterThanOrEqualToAnchor:_imageView.bottomAnchor | 106 constraintGreaterThanOrEqualToAnchor:_imageView.bottomAnchor |
| 87 constant:kStandardSpacing], | 107 constant:kStandardSpacing], |
| 88 [self.contentView.bottomAnchor | 108 [_publisherLabel.topAnchor |
| 89 constraintGreaterThanOrEqualToAnchor:_subtitleLabel.bottomAnchor | 109 constraintGreaterThanOrEqualToAnchor:_subtitleLabel.bottomAnchor |
| 90 constant:kStandardSpacing], | 110 constant:kStandardSpacing], |
| 91 [_imageView.widthAnchor constraintLessThanOrEqualToConstant:kImageSize], | |
| 92 [_imageView.heightAnchor constraintLessThanOrEqualToConstant:kImageSize] | |
| 93 ]]; | 111 ]]; |
| 94 | 112 |
| 95 ApplyVisualConstraints( | 113 ApplyVisualConstraints( |
| 96 @[ | 114 @[ |
| 97 @"H:|-[title]-[image]-|", | 115 @"H:|-[title]-[image]-|", |
| 98 @"H:|-[text]-[image]", | 116 @"H:|-[text]-[image]", |
| 99 @"V:|-[title]-[text]-|", | 117 @"V:|-[title]-[text]", |
| 100 @"V:|-[image]", | 118 @"V:|-[image]", |
| 119 @"H:|-[publish]-|", |
| 120 @"V:[publish]-|", |
| 101 ], | 121 ], |
| 102 @{ | 122 @{ |
| 103 @"image" : _imageView, | 123 @"image" : _imageView, |
| 104 @"title" : _titleLabel, | 124 @"title" : _titleLabel, |
| 105 @"text" : _subtitleLabel | 125 @"text" : _subtitleLabel, |
| 126 @"publish" : _publisherLabel, |
| 106 }); | 127 }); |
| 107 } | 128 } |
| 108 return self; | 129 return self; |
| 109 } | 130 } |
| 110 | 131 |
| 132 - (void)setPublisherName:(NSString*)publisherName date:(base::Time)publishDate { |
| 133 NSDate* date = [NSDate dateWithTimeIntervalSince1970:publishDate.ToDoubleT()]; |
| 134 NSString* dateString = |
| 135 [NSDateFormatter localizedStringFromDate:date |
| 136 dateStyle:NSDateFormatterMediumStyle |
| 137 timeStyle:NSDateFormatterNoStyle]; |
| 138 |
| 139 // TODO(crbug.com/694423): Make it RTL friendly. |
| 140 self.publisherLabel.text = |
| 141 [NSString stringWithFormat:@"%@ - %@.", publisherName, dateString]; |
| 142 } |
| 143 |
| 111 #pragma mark - UIView | 144 #pragma mark - UIView |
| 112 | 145 |
| 113 // Implements -layoutSubviews as per instructions in documentation for | 146 // Implements -layoutSubviews as per instructions in documentation for |
| 114 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | 147 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
| 115 - (void)layoutSubviews { | 148 - (void)layoutSubviews { |
| 116 [super layoutSubviews]; | 149 [super layoutSubviews]; |
| 117 | 150 |
| 118 // Adjust the text label preferredMaxLayoutWidth when the parent's width | 151 // Adjust the text label preferredMaxLayoutWidth when the parent's width |
| 119 // changes, for instance on screen rotation. | 152 // changes, for instance on screen rotation. |
| 120 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); | 153 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); |
| 121 self.subtitleLabel.preferredMaxLayoutWidth = parentWidth - kImageSize - 3 * 8; | 154 self.subtitleLabel.preferredMaxLayoutWidth = |
| 155 parentWidth - self.imageView.bounds.size.width - 3 * 8; |
| 122 | 156 |
| 123 // 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 |
| 124 // height. | 158 // height. |
| 125 [super layoutSubviews]; | 159 [super layoutSubviews]; |
| 126 } | 160 } |
| 127 | 161 |
| 128 @end | 162 @end |
| OLD | NEW |