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