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 #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 |
| 56 stringByTrimmingCharactersInSet:[NSCharacterSet | |
|
jif
2017/02/20 16:01:00
IMO stringByTrimmingCharactersInSet should be done
gambard
2017/02/21 08:49:46
This is a typo. I removed it.
| |
| 57 whitespaceAndNewlineCharacterSet]]; | |
| 53 cell.imageView.image = _image; | 58 cell.imageView.image = _image; |
| 59 [cell setPublisherName:self.publisher date:self.publishDate]; | |
| 54 } | 60 } |
| 55 | 61 |
| 56 @end | 62 @end |
| 57 | 63 |
| 58 #pragma mark - ContentSuggestionsArticleCell | 64 #pragma mark - ContentSuggestionsArticleCell |
| 59 | 65 |
| 66 @interface ContentSuggestionsArticleCell () | |
| 67 | |
| 68 @property(nonatomic, strong) UILabel* publisherLabel; | |
| 69 | |
| 70 @end | |
| 71 | |
| 60 @implementation ContentSuggestionsArticleCell | 72 @implementation ContentSuggestionsArticleCell |
| 61 | 73 |
| 62 @synthesize titleLabel = _titleLabel; | 74 @synthesize titleLabel = _titleLabel; |
| 63 @synthesize subtitleLabel = _subtitleLabel; | 75 @synthesize subtitleLabel = _subtitleLabel; |
| 64 @synthesize imageView = _imageView; | 76 @synthesize imageView = _imageView; |
| 77 @synthesize publisherLabel = _publisherLabel; | |
| 65 | 78 |
| 66 - (instancetype)initWithFrame:(CGRect)frame { | 79 - (instancetype)initWithFrame:(CGRect)frame { |
| 67 self = [super initWithFrame:frame]; | 80 self = [super initWithFrame:frame]; |
| 68 if (self) { | 81 if (self) { |
| 69 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | 82 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 70 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | 83 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 71 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; | 84 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; |
| 85 _publisherLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
| 72 | 86 |
| 73 _subtitleLabel.numberOfLines = 0; | 87 _subtitleLabel.numberOfLines = 0; |
| 88 [_subtitleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh | |
| 89 forAxis:UILayoutConstraintAxisVertical]; | |
| 90 [_titleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh | |
| 91 forAxis:UILayoutConstraintAxisVertical]; | |
| 74 _imageView.contentMode = UIViewContentModeScaleAspectFit; | 92 _imageView.contentMode = UIViewContentModeScaleAspectFit; |
| 75 | 93 |
| 76 _imageView.translatesAutoresizingMaskIntoConstraints = NO; | 94 _imageView.translatesAutoresizingMaskIntoConstraints = NO; |
| 77 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; | 95 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 78 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; | 96 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 97 _publisherLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
| 79 | 98 |
| 80 [self.contentView addSubview:_imageView]; | 99 [self.contentView addSubview:_imageView]; |
| 81 [self.contentView addSubview:_titleLabel]; | 100 [self.contentView addSubview:_titleLabel]; |
| 82 [self.contentView addSubview:_subtitleLabel]; | 101 [self.contentView addSubview:_subtitleLabel]; |
| 102 [self.contentView addSubview:_publisherLabel]; | |
| 83 | 103 |
| 84 [NSLayoutConstraint activateConstraints:@[ | 104 [NSLayoutConstraint activateConstraints:@[ |
| 85 [self.contentView.bottomAnchor | 105 [_imageView.widthAnchor constraintLessThanOrEqualToConstant:kImageSize], |
| 106 [_imageView.heightAnchor constraintLessThanOrEqualToConstant:kImageSize], | |
| 107 [_publisherLabel.topAnchor | |
| 86 constraintGreaterThanOrEqualToAnchor:_imageView.bottomAnchor | 108 constraintGreaterThanOrEqualToAnchor:_imageView.bottomAnchor |
| 87 constant:kStandardSpacing], | 109 constant:kStandardSpacing], |
| 88 [self.contentView.bottomAnchor | 110 [_publisherLabel.topAnchor |
| 89 constraintGreaterThanOrEqualToAnchor:_subtitleLabel.bottomAnchor | 111 constraintGreaterThanOrEqualToAnchor:_subtitleLabel.bottomAnchor |
| 90 constant:kStandardSpacing], | 112 constant:kStandardSpacing], |
| 91 [_imageView.widthAnchor constraintLessThanOrEqualToConstant:kImageSize], | |
| 92 [_imageView.heightAnchor constraintLessThanOrEqualToConstant:kImageSize] | |
| 93 ]]; | 113 ]]; |
| 94 | 114 |
| 95 ApplyVisualConstraints( | 115 ApplyVisualConstraints( |
| 96 @[ | 116 @[ |
| 97 @"H:|-[title]-[image]-|", | 117 @"H:|-[title]-[image]-|", |
| 98 @"H:|-[text]-[image]", | 118 @"H:|-[text]-[image]", |
| 99 @"V:|-[title]-[text]-|", | 119 @"V:|-[title]-[text]", |
| 100 @"V:|-[image]", | 120 @"V:|-[image]", |
| 121 @"H:|-[publish]-|", | |
| 122 @"V:[publish]-|", | |
| 101 ], | 123 ], |
| 102 @{ | 124 @{ |
| 103 @"image" : _imageView, | 125 @"image" : _imageView, |
| 104 @"title" : _titleLabel, | 126 @"title" : _titleLabel, |
| 105 @"text" : _subtitleLabel | 127 @"text" : _subtitleLabel, |
| 128 @"publish" : _publisherLabel, | |
| 106 }); | 129 }); |
| 107 } | 130 } |
| 108 return self; | 131 return self; |
| 109 } | 132 } |
| 110 | 133 |
| 134 - (void)setPublisherName:(NSString*)publisherName | |
| 135 date:(const base::Time&)publishDate { | |
| 136 NSDate* date = [NSDate dateWithTimeIntervalSince1970:publishDate.ToDoubleT()]; | |
| 137 NSString* dateString = | |
| 138 [NSDateFormatter localizedStringFromDate:date | |
| 139 dateStyle:NSDateFormatterMediumStyle | |
| 140 timeStyle:NSDateFormatterNoStyle]; | |
| 141 | |
| 142 self.publisherLabel.text = | |
| 143 [NSString stringWithFormat:@"%@ - %@.", publisherName, dateString]; | |
|
jif
2017/02/20 16:01:00
not RTL friendly :'(
gambard
2017/02/21 08:49:46
I added a TODO.
| |
| 144 } | |
| 145 | |
| 111 #pragma mark - UIView | 146 #pragma mark - UIView |
| 112 | 147 |
| 113 // Implements -layoutSubviews as per instructions in documentation for | 148 // Implements -layoutSubviews as per instructions in documentation for |
| 114 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | 149 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
| 115 - (void)layoutSubviews { | 150 - (void)layoutSubviews { |
| 116 [super layoutSubviews]; | 151 [super layoutSubviews]; |
| 117 | 152 |
| 118 // Adjust the text label preferredMaxLayoutWidth when the parent's width | 153 // Adjust the text label preferredMaxLayoutWidth when the parent's width |
| 119 // changes, for instance on screen rotation. | 154 // changes, for instance on screen rotation. |
| 120 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); | 155 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); |
| 121 self.subtitleLabel.preferredMaxLayoutWidth = parentWidth - kImageSize - 3 * 8; | 156 self.subtitleLabel.preferredMaxLayoutWidth = |
| 157 parentWidth - self.imageView.bounds.size.width - 3 * 8; | |
| 122 | 158 |
| 123 // Re-layout with the new preferred width to allow the label to adjust its | 159 // Re-layout with the new preferred width to allow the label to adjust its |
| 124 // height. | 160 // height. |
| 125 [super layoutSubviews]; | 161 [super layoutSubviews]; |
| 126 } | 162 } |
| 127 | 163 |
| 128 @end | 164 @end |
| OLD | NEW |