Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_most_visited_item.mm

Issue 2808433002: Create a MostVisited cell for ContentSuggestions (Closed)
Patch Set: Address comment Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_mos t_visited_item.h"
6
7 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_mos t_visited.h"
8 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_mos t_visited_tile.h"
9 #import "ios/chrome/browser/ui/favicon/favicon_attributes.h"
10 #import "ios/chrome/browser/ui/favicon/favicon_view.h"
11 #include "ios/chrome/browser/ui/ui_util.h"
12 #import "ios/chrome/browser/ui/uikit_ui_util.h"
13 #include "url/gurl.h"
14
15 #if !defined(__has_feature) || !__has_feature(objc_arc)
16 #error "This file requires ARC support."
17 #endif
18
19 namespace {
20 const CGFloat kSpacingIPhone = 16;
21 const CGFloat kSpacingIPad = 24;
22 }
23
24 #pragma mark - ContentSuggestionsMostVisitedItem
25
26 @implementation ContentSuggestionsMostVisitedItem
27
28 @synthesize suggestionIdentifier = _suggestionIdentifier;
29 @synthesize suggestions = _suggestions;
30
31 - (instancetype)initWithType:(NSInteger)type {
32 self = [super initWithType:type];
33 if (self) {
34 self.cellClass = [ContentSuggestionsMostVisitedCell class];
35 }
36 return self;
37 }
38
39 - (void)configureCell:(ContentSuggestionsMostVisitedCell*)cell {
40 [super configureCell:cell];
41 [cell setSuggestions:self.suggestions];
42 }
43
44 @end
45
46 #pragma mark - ContentSuggestionsMostVisitedCell
47
48 @interface ContentSuggestionsMostVisitedCell ()
49
50 // The Most Visited tiles to be displayed.
51 @property(nonatomic, strong)
52 NSMutableArray<ContentSuggestionsMostVisitedTile*>* tiles;
53
54 // The first line of Most Visited tiles.
55 @property(nonatomic, strong) UIStackView* firstLine;
56
57 // The second line of Most Visited tiles, displayed below the first one.
58 @property(nonatomic, strong) UIStackView* secondLine;
59
60 // Contains both stack views.
61 @property(nonatomic, copy) NSArray<UIStackView*>* stackViews;
62
63 // Superview for the stack views, used to center them.
64 @property(nonatomic, strong) UIView* stackContainer;
65
66 // Width of the |stackContainer|, allowing resizing.
67 @property(nonatomic, strong) NSLayoutConstraint* containerWidth;
68
69 // Number of tiles per line during the previous layout.
70 @property(nonatomic, assign) NSUInteger previousNumberOfTilesPerLine;
71
72 @end
73
74 @implementation ContentSuggestionsMostVisitedCell : MDCCollectionViewCell
75
76 @synthesize tiles = _tiles;
77 @synthesize firstLine = _firstLine;
78 @synthesize secondLine = _secondLine;
79 @synthesize stackViews = _stackViews;
80 @synthesize stackContainer = _stackContainer;
81 @synthesize containerWidth = _containerWidth;
82 @synthesize previousNumberOfTilesPerLine = _previousNumberOfTilesPerLine;
83
84 #pragma mark - Public
85
86 - (instancetype)initWithFrame:(CGRect)frame {
87 self = [super initWithFrame:frame];
88 if (self) {
89 _tiles = [NSMutableArray array];
90 _firstLine = [[UIStackView alloc] init];
91 _secondLine = [[UIStackView alloc] init];
92 _stackViews = @[ _firstLine, _secondLine ];
93 _stackContainer = [[UIView alloc] init];
94 _previousNumberOfTilesPerLine = 0;
95
96 for (UIStackView* row in self.stackViews) {
97 row.axis = UILayoutConstraintAxisHorizontal;
98 row.spacing = [self spacing];
99 row.translatesAutoresizingMaskIntoConstraints = NO;
100 [_stackContainer addSubview:row];
101 row.layoutMarginsRelativeArrangement = YES;
102 }
103
104 _stackContainer.translatesAutoresizingMaskIntoConstraints = NO;
105
106 [self.contentView addSubview:_stackContainer];
107
108 [_stackContainer.centerXAnchor
109 constraintEqualToAnchor:self.contentView.centerXAnchor]
110 .active = YES;
111 _containerWidth = [_stackContainer.widthAnchor constraintEqualToConstant:0];
112 _containerWidth.active = YES;
113
114 ApplyVisualConstraints(@[ @"V:|[container]|" ],
115 @{ @"container" : _stackContainer });
116 ApplyVisualConstraints(
117 @[
118 @"V:|[first][second]|", @"H:|[first]-(>=0)-|",
119 @"H:|[second]-(>=0)-|"
120 ],
121 @{ @"first" : _firstLine,
122 @"second" : _secondLine });
123 }
124 return self;
125 }
126
127 - (void)setSuggestions:(NSArray<ContentSuggestionsMostVisited*>*)suggestions {
128 [self.tiles removeAllObjects];
129 for (ContentSuggestionsMostVisited* suggestion in suggestions) {
130 [self.tiles addObject:[ContentSuggestionsMostVisitedTile
131 tileWithTitle:suggestion.title
132 attributes:suggestion.attributes]];
133 }
134 }
135
136 - (NSUInteger)numberOfTilesPerLine {
137 CGFloat availableWidth = self.contentView.bounds.size.width;
138
139 if (availableWidth > [self widthForNumberOfItem:4])
140 return 4;
141 if (availableWidth > [self widthForNumberOfItem:3])
142 return 3;
143 if (availableWidth > [self widthForNumberOfItem:2])
144 return 2;
145
146 NOTREACHED();
147 return 2;
148 }
149
150 #pragma mark - UIView
151
152 // Implements -layoutSubviews as per instructions in documentation for
153 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:].
154 - (void)layoutSubviews {
155 [super layoutSubviews];
156
157 [self applyLayout];
158
159 [super layoutSubviews];
160 }
161
162 #pragma mark - Private
163
164 // Layouts the tiles. The view is modified only if the number of tiles per line
165 // changes.
166 - (void)applyLayout {
167 NSUInteger numberOfTilesPerLine = [self numberOfTilesPerLine];
168
169 if (numberOfTilesPerLine == self.previousNumberOfTilesPerLine) {
170 return;
171 }
172 self.previousNumberOfTilesPerLine = numberOfTilesPerLine;
173
174 for (UIStackView* row in self.stackViews) {
175 while (row.arrangedSubviews.count > 0) {
176 UIView* view = row.arrangedSubviews.firstObject;
177 [row removeArrangedSubview:view];
178 [view removeFromSuperview];
179 }
180 }
181
182 NSUInteger numberOfTilesFirstLine =
183 MIN(numberOfTilesPerLine, self.tiles.count);
184 for (NSUInteger i = 0; i < numberOfTilesFirstLine; i++) {
185 [self.firstLine addArrangedSubview:self.tiles[i]];
186 }
187 if (self.tiles.count > numberOfTilesPerLine) {
188 NSUInteger totalNumberOfTiles =
189 MIN(2 * numberOfTilesPerLine, self.tiles.count);
190 for (NSUInteger i = numberOfTilesPerLine; i < totalNumberOfTiles; i++) {
191 [self.secondLine addArrangedSubview:self.tiles[i]];
192 }
193 }
194
195 self.containerWidth.constant =
196 [self widthForNumberOfItem:numberOfTilesPerLine];
197 }
198
199 // Returns the spacing between tiles, based on the device.
200 - (CGFloat)spacing {
201 return IsIPadIdiom() ? kSpacingIPad : kSpacingIPhone;
202 }
203
204 // Returns the width necessary to fit |numberOfItem| items.
205 - (CGFloat)widthForNumberOfItem:(NSUInteger)numberOfItem {
206 return (numberOfItem - 1) * [self spacing] +
207 numberOfItem * [ContentSuggestionsMostVisitedTile width];
208 }
209
210 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698