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/suggestions/suggestions_favicon_item.h" | 5 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_favicon_i
tem.h" |
6 | 6 |
7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #import "ios/chrome/browser/ui/suggestions/suggestions_favicon_internal_cell.h" | 10 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_favicon_i
nternal_cell.h" |
11 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 11 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
12 | 12 |
13 #if !defined(__has_feature) || !__has_feature(objc_arc) | 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
14 #error "This file requires ARC support." | 14 #error "This file requires ARC support." |
15 #endif | 15 #endif |
16 | 16 |
17 namespace { | 17 namespace { |
18 // Space between two cells in the internal collection view. | 18 // Space between two cells in the internal collection view. |
19 const CGFloat kInternalCellSpacing = 40; | 19 const CGFloat kInternalCellSpacing = 40; |
20 // Width of the cells in the internal collection view. | 20 // Width of the cells in the internal collection view. |
21 const CGFloat kInternalCellWidth = 70; | 21 const CGFloat kInternalCellWidth = 70; |
22 // Height of the cells in the internal collection view. | 22 // Height of the cells in the internal collection view. |
23 const CGFloat kInternalCellHeight = 120; | 23 const CGFloat kInternalCellHeight = 120; |
24 // Leading inset of the internal collection view. | 24 // Leading inset of the internal collection view. |
25 const CGFloat kInternalLeadingSpacing = 16; | 25 const CGFloat kInternalLeadingSpacing = 16; |
26 } // namespace | 26 } // namespace |
27 | 27 |
28 #pragma mark - SugggestionsFaviconItem | 28 #pragma mark - SugggestionsFaviconItem |
29 | 29 |
30 // The item is the data source of the inner collection view. | 30 // The item is the data source of the inner collection view. |
31 @interface SuggestionsFaviconItem ()<UICollectionViewDataSource> { | 31 @interface ContentSuggestionsFaviconItem ()<UICollectionViewDataSource> { |
32 NSMutableArray<NSString*>* _faviconTitles; | 32 NSMutableArray<NSString*>* _faviconTitles; |
33 NSMutableArray<UIImage*>* _faviconImages; | 33 NSMutableArray<UIImage*>* _faviconImages; |
34 } | 34 } |
35 | 35 |
36 @end | 36 @end |
37 | 37 |
38 @implementation SuggestionsFaviconItem | 38 @implementation ContentSuggestionsFaviconItem |
39 | 39 |
40 @synthesize delegate = _delegate; | 40 @synthesize delegate = _delegate; |
41 | 41 |
42 - (instancetype)initWithType:(NSInteger)type { | 42 - (instancetype)initWithType:(NSInteger)type { |
43 self = [super initWithType:type]; | 43 self = [super initWithType:type]; |
44 if (self) { | 44 if (self) { |
45 self.cellClass = [SuggestionsFaviconCell class]; | 45 self.cellClass = [ContentSuggestionsFaviconCell class]; |
46 _faviconTitles = [NSMutableArray array]; | 46 _faviconTitles = [NSMutableArray array]; |
47 _faviconImages = [NSMutableArray array]; | 47 _faviconImages = [NSMutableArray array]; |
48 } | 48 } |
49 return self; | 49 return self; |
50 } | 50 } |
51 | 51 |
52 - (void)addFavicon:(UIImage*)favicon withTitle:(NSString*)title { | 52 - (void)addFavicon:(UIImage*)favicon withTitle:(NSString*)title { |
53 [_faviconImages addObject:favicon]; | 53 [_faviconImages addObject:favicon]; |
54 [_faviconTitles addObject:title]; | 54 [_faviconTitles addObject:title]; |
55 } | 55 } |
56 | 56 |
57 #pragma mark - UICollectionViewDataSource | 57 #pragma mark - UICollectionViewDataSource |
58 | 58 |
59 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView | 59 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView |
60 cellForItemAtIndexPath:(NSIndexPath*)indexPath { | 60 cellForItemAtIndexPath:(NSIndexPath*)indexPath { |
61 SuggestionsFaviconInternalCell* cell = [collectionView | 61 ContentSuggestionsFaviconInternalCell* cell = [collectionView |
62 dequeueReusableCellWithReuseIdentifier:[SuggestionsFaviconInternalCell | 62 dequeueReusableCellWithReuseIdentifier: |
63 reuseIdentifier] | 63 [ContentSuggestionsFaviconInternalCell reuseIdentifier] |
64 forIndexPath:indexPath]; | 64 forIndexPath:indexPath]; |
65 cell.faviconView.image = [_faviconImages objectAtIndex:indexPath.item]; | 65 cell.faviconView.image = [_faviconImages objectAtIndex:indexPath.item]; |
66 cell.titleLabel.text = [_faviconTitles objectAtIndex:indexPath.item]; | 66 cell.titleLabel.text = [_faviconTitles objectAtIndex:indexPath.item]; |
67 return cell; | 67 return cell; |
68 } | 68 } |
69 | 69 |
70 - (NSInteger)collectionView:(UICollectionView*)collectionView | 70 - (NSInteger)collectionView:(UICollectionView*)collectionView |
71 numberOfItemsInSection:(NSInteger)section { | 71 numberOfItemsInSection:(NSInteger)section { |
72 DCHECK([_faviconImages count] == [_faviconTitles count]); | 72 DCHECK([_faviconImages count] == [_faviconTitles count]); |
73 return [_faviconImages count]; | 73 return [_faviconImages count]; |
74 } | 74 } |
75 | 75 |
76 #pragma mark - CollectionViewItem | 76 #pragma mark - CollectionViewItem |
77 | 77 |
78 - (void)configureCell:(SuggestionsFaviconCell*)cell { | 78 - (void)configureCell:(ContentSuggestionsFaviconCell*)cell { |
79 [super configureCell:cell]; | 79 [super configureCell:cell]; |
80 cell.collectionView.dataSource = self; | 80 cell.collectionView.dataSource = self; |
81 cell.delegate = self.delegate; | 81 cell.delegate = self.delegate; |
82 } | 82 } |
83 | 83 |
84 @end | 84 @end |
85 | 85 |
86 #pragma mark - SuggestionsFaviconCell | 86 #pragma mark - ContentSuggestionsFaviconCell |
87 | 87 |
88 // The cell is the delegate of the inner collection view. | 88 // The cell is the delegate of the inner collection view. |
89 @interface SuggestionsFaviconCell ()<UICollectionViewDelegate> | 89 @interface ContentSuggestionsFaviconCell ()<UICollectionViewDelegate> |
90 | 90 |
91 @end | 91 @end |
92 | 92 |
93 @implementation SuggestionsFaviconCell | 93 @implementation ContentSuggestionsFaviconCell |
94 | 94 |
95 @synthesize collectionView = _collectionView; | 95 @synthesize collectionView = _collectionView; |
96 @synthesize delegate = _delegate; | 96 @synthesize delegate = _delegate; |
97 | 97 |
98 - (instancetype)initWithFrame:(CGRect)frame { | 98 - (instancetype)initWithFrame:(CGRect)frame { |
99 self = [super initWithFrame:frame]; | 99 self = [super initWithFrame:frame]; |
100 if (self) { | 100 if (self) { |
101 UICollectionViewFlowLayout* layout = | 101 UICollectionViewFlowLayout* layout = |
102 [[UICollectionViewFlowLayout alloc] init]; | 102 [[UICollectionViewFlowLayout alloc] init]; |
103 layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; | 103 layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; |
104 layout.minimumInteritemSpacing = kInternalCellSpacing; | 104 layout.minimumInteritemSpacing = kInternalCellSpacing; |
105 layout.itemSize = CGSizeMake(kInternalCellWidth, kInternalCellHeight); | 105 layout.itemSize = CGSizeMake(kInternalCellWidth, kInternalCellHeight); |
106 layout.sectionInset = UIEdgeInsetsMake(0, kInternalLeadingSpacing, 0, 0); | 106 layout.sectionInset = UIEdgeInsetsMake(0, kInternalLeadingSpacing, 0, 0); |
107 | 107 |
108 _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero | 108 _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero |
109 collectionViewLayout:layout]; | 109 collectionViewLayout:layout]; |
110 [_collectionView registerClass:[SuggestionsFaviconInternalCell class] | 110 [_collectionView registerClass:[ContentSuggestionsFaviconInternalCell class] |
111 forCellWithReuseIdentifier:[SuggestionsFaviconInternalCell | 111 forCellWithReuseIdentifier:[ContentSuggestionsFaviconInternalCell |
112 reuseIdentifier]]; | 112 reuseIdentifier]]; |
113 _collectionView.backgroundColor = [UIColor clearColor]; | 113 _collectionView.backgroundColor = [UIColor clearColor]; |
114 _collectionView.translatesAutoresizingMaskIntoConstraints = NO; | 114 _collectionView.translatesAutoresizingMaskIntoConstraints = NO; |
115 | 115 |
116 _collectionView.delegate = self; | 116 _collectionView.delegate = self; |
117 | 117 |
118 [self.contentView addSubview:_collectionView]; | 118 [self.contentView addSubview:_collectionView]; |
119 AddSameSizeConstraint(_collectionView, self.contentView); | 119 AddSameSizeConstraint(_collectionView, self.contentView); |
120 [_collectionView.heightAnchor constraintEqualToConstant:kInternalCellHeight] | 120 [_collectionView.heightAnchor constraintEqualToConstant:kInternalCellHeight] |
121 .active = YES; | 121 .active = YES; |
122 } | 122 } |
123 return self; | 123 return self; |
124 } | 124 } |
125 | 125 |
126 - (void)prepareForReuse { | 126 - (void)prepareForReuse { |
127 [super prepareForReuse]; | 127 [super prepareForReuse]; |
128 self.collectionView.dataSource = nil; | 128 self.collectionView.dataSource = nil; |
129 self.delegate = nil; | 129 self.delegate = nil; |
130 } | 130 } |
131 | 131 |
132 #pragma mark - UICollectionViewDelegate | 132 #pragma mark - UICollectionViewDelegate |
133 | 133 |
134 - (void)collectionView:(UICollectionView*)collectionView | 134 - (void)collectionView:(UICollectionView*)collectionView |
135 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { | 135 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
136 [self.delegate openFaviconAtIndexPath:indexPath]; | 136 [self.delegate openFaviconAtIndexPath:indexPath]; |
137 } | 137 } |
138 | 138 |
139 @end | 139 @end |
OLD | NEW |