OLD | NEW |
---|---|
(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 mostVisiteds = _mostVisiteds; | |
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 setMostVisiteds:self.mostVisiteds]; | |
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; | |
lpromero
2017/04/07 12:46:57
Maybe add new lines between the property, as it is
gambard
2017/04/10 11:49:16
Done.
| |
53 // The first line of Most Visited tiles. | |
54 @property(nonatomic, strong) UIStackView* firstLine; | |
55 // The second line of Most Visited tiles, displayed below the first one. | |
56 @property(nonatomic, strong) UIStackView* secondLine; | |
57 // Contains both stack views. | |
58 @property(nonatomic, copy) NSArray<UIStackView*>* stackViews; | |
59 // Superview for the stack views, used to center them. | |
60 @property(nonatomic, strong) UIView* stackContainer; | |
61 // Width of the |stackContainer|, allowing resizing. | |
62 @property(nonatomic, strong) NSLayoutConstraint* containerWidth; | |
63 // Number of tiles per line during the previous layout. | |
64 @property(nonatomic, assign) NSUInteger oldNumberOfTilesPerLine; | |
lpromero
2017/04/07 12:46:57
Optional nit: s/old/previous
gambard
2017/04/10 11:49:16
Done.
| |
65 | |
66 @end | |
67 | |
68 @implementation ContentSuggestionsMostVisitedCell : MDCCollectionViewCell | |
69 | |
70 @synthesize tiles = _tiles; | |
71 @synthesize firstLine = _firstLine; | |
72 @synthesize secondLine = _secondLine; | |
73 @synthesize stackViews = _stackViews; | |
74 @synthesize stackContainer = _stackContainer; | |
75 @synthesize containerWidth = _containerWidth; | |
76 @synthesize oldNumberOfTilesPerLine = _oldNumberOfTilesPerLine; | |
77 | |
78 #pragma mark - Public | |
79 | |
80 - (instancetype)initWithFrame:(CGRect)frame { | |
stkhapugin
2017/04/07 12:17:58
optional: add a comment here or above @implementat
gambard
2017/04/10 11:49:16
Done.
| |
81 self = [super initWithFrame:frame]; | |
82 if (self) { | |
83 _tiles = [NSMutableArray array]; | |
84 _firstLine = [[UIStackView alloc] init]; | |
85 _secondLine = [[UIStackView alloc] init]; | |
86 _stackViews = @[ _firstLine, _secondLine ]; | |
87 _stackContainer = [[UIView alloc] init]; | |
88 _oldNumberOfTilesPerLine = 0; | |
89 | |
90 for (UIStackView* row in self.stackViews) { | |
91 row.axis = UILayoutConstraintAxisHorizontal; | |
92 row.spacing = [self spacing]; | |
93 row.translatesAutoresizingMaskIntoConstraints = NO; | |
94 [_stackContainer addSubview:row]; | |
95 row.layoutMarginsRelativeArrangement = YES; | |
96 } | |
97 | |
98 _stackContainer.translatesAutoresizingMaskIntoConstraints = NO; | |
99 | |
100 [self.contentView addSubview:_stackContainer]; | |
101 | |
102 [_stackContainer.centerXAnchor | |
103 constraintEqualToAnchor:self.contentView.centerXAnchor] | |
104 .active = YES; | |
105 _containerWidth = [_stackContainer.widthAnchor constraintEqualToConstant:0]; | |
106 _containerWidth.active = YES; | |
107 | |
108 ApplyVisualConstraints(@[ @"V:|[container]|" ], | |
109 @{ @"container" : _stackContainer }); | |
110 ApplyVisualConstraints( | |
111 @[ | |
112 @"V:|[first][second]|", @"H:|[first]-(>=0)-|", | |
113 @"H:|[second]-(>=0)-|" | |
114 ], | |
115 @{ @"first" : _firstLine, | |
116 @"second" : _secondLine }); | |
117 } | |
118 return self; | |
119 } | |
120 | |
121 - (void)setMostVisiteds:(NSArray<ContentSuggestionsMostVisited*>*)mostVisiteds { | |
122 [self.tiles removeAllObjects]; | |
123 for (ContentSuggestionsMostVisited* mostVisited : mostVisiteds) { | |
lpromero
2017/04/07 12:46:57
s/:/in
gambard
2017/04/10 11:49:16
Done.
| |
124 [self.tiles addObject:[ContentSuggestionsMostVisitedTile | |
125 tileWithTitle:mostVisited.title | |
126 attributes:mostVisited.attributes]]; | |
127 } | |
128 } | |
129 | |
130 #pragma mark - UIView | |
131 | |
132 - (void)layoutSubviews { | |
lpromero
2017/04/07 12:46:57
Please add the comment we use usually to describe
gambard
2017/04/10 11:49:16
Done.
| |
133 [super layoutSubviews]; | |
134 | |
135 [self applyLayout]; | |
136 | |
137 [super layoutSubviews]; | |
138 } | |
139 | |
140 #pragma mark - Private | |
141 | |
142 // Layouts the tiles. The view is modified only if the number of tiles per line | |
143 // changes. | |
144 - (void)applyLayout { | |
145 NSUInteger numberOfTilesPerLine = [self numberOfTilesPerLine]; | |
146 | |
147 if (numberOfTilesPerLine == self.oldNumberOfTilesPerLine) { | |
148 return; | |
149 } | |
150 self.oldNumberOfTilesPerLine = numberOfTilesPerLine; | |
151 | |
152 for (UIStackView* row in self.stackViews) { | |
153 while (row.arrangedSubviews.count > 0) { | |
154 UIView* view = row.arrangedSubviews.firstObject; | |
155 [row removeArrangedSubview:view]; | |
156 [view removeFromSuperview]; | |
157 } | |
158 } | |
159 | |
160 NSUInteger numberOfTilesfirstLine = | |
lpromero
2017/04/07 12:46:57
s/numberOfTilesfirstLine/numberOfTilesFirstLine
gambard
2017/04/10 11:49:16
Done.
| |
161 MIN(numberOfTilesPerLine, self.tiles.count); | |
162 for (NSUInteger i = 0; i < numberOfTilesfirstLine; i++) { | |
163 [self.firstLine addArrangedSubview:self.tiles[i]]; | |
164 } | |
165 if (self.tiles.count > numberOfTilesPerLine) { | |
166 NSUInteger totalNumberOfTiles = | |
167 MIN(2 * numberOfTilesPerLine, self.tiles.count); | |
168 for (NSUInteger i = numberOfTilesPerLine; i < totalNumberOfTiles; i++) { | |
169 [self.secondLine addArrangedSubview:self.tiles[i]]; | |
170 } | |
171 } | |
172 | |
173 self.containerWidth.constant = | |
174 [self widthForNumberOfItem:numberOfTilesPerLine]; | |
175 } | |
176 | |
177 // Returns the maximum number of tiles per line, limited to 4. | |
178 - (NSUInteger)numberOfTilesPerLine { | |
stkhapugin
2017/04/07 12:17:58
optional: it'd be nice to have a few tests for thi
gambard
2017/04/10 11:49:16
Done.
| |
179 CGFloat availableWidth = self.contentView.bounds.size.width; | |
180 | |
181 if (availableWidth > [self widthForNumberOfItem:4]) | |
182 return 4; | |
183 if (availableWidth > [self widthForNumberOfItem:3]) | |
184 return 3; | |
185 if (availableWidth > [self widthForNumberOfItem:2]) | |
186 return 2; | |
187 | |
188 NOTREACHED(); | |
189 return 2; | |
190 } | |
191 | |
192 // Returns the spacing of between tiles, based on the device. | |
lpromero
2017/04/07 12:46:57
s/between/middle or interior or inner?
Or maybe i
gambard
2017/04/10 11:49:16
Done.
| |
193 - (CGFloat)spacing { | |
194 return IsIPadIdiom() ? kSpacingIPad : kSpacingIPhone; | |
195 } | |
196 | |
197 // Returns the width necessary to fit |numberOfItem| items. | |
198 - (CGFloat)widthForNumberOfItem:(NSUInteger)numberOfItem { | |
199 return (numberOfItem - 1) * [self spacing] + | |
200 numberOfItem * [ContentSuggestionsMostVisitedTile width]; | |
201 } | |
202 | |
203 @end | |
OLD | NEW |