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