Index: ios/chrome/browser/ui/suggestions/suggestions_favicon_item.mm |
diff --git a/ios/chrome/browser/ui/suggestions/suggestions_favicon_item.mm b/ios/chrome/browser/ui/suggestions/suggestions_favicon_item.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0a26ba4185e14935410f9a127eebb11cafd4f6e0 |
--- /dev/null |
+++ b/ios/chrome/browser/ui/suggestions/suggestions_favicon_item.mm |
@@ -0,0 +1,139 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#import "ios/chrome/browser/ui/suggestions/suggestions_favicon_item.h" |
+ |
+#import <UIKit/UIKit.h> |
+ |
+#include "base/logging.h" |
+#import "ios/chrome/browser/ui/suggestions/suggestions_favicon_internal_cell.h" |
+#import "ios/chrome/browser/ui/uikit_ui_util.h" |
+ |
+#if !defined(__has_feature) || !__has_feature(objc_arc) |
+#error "This file requires ARC support." |
+#endif |
+ |
+namespace { |
+// Space between two cells in the internal collection view. |
+const CGFloat kInternalCellSpacing = 40; |
+// Width of the cells in the internal collection view. |
+const CGFloat kInternalCellWidth = 70; |
+// Height of the cells in the internal collection view. |
+const CGFloat kInternalCellHeight = 120; |
+// Leading inset of the internal collection view. |
+const CGFloat kInternalLeadingSpacing = 16; |
+} // namespace |
+ |
+#pragma mark - SugggestionsFaviconItem |
+ |
+// The item is the data source of the inner collection view. |
+@interface SuggestionsFaviconItem ()<UICollectionViewDataSource> { |
+ NSMutableArray<NSString*>* _faviconTitles; |
+ NSMutableArray<UIImage*>* _faviconImages; |
+} |
+ |
+@end |
+ |
+@implementation SuggestionsFaviconItem |
+ |
+@synthesize delegate = _delegate; |
+ |
+- (instancetype)initWithType:(NSInteger)type { |
+ self = [super initWithType:type]; |
+ if (self) { |
+ self.cellClass = [SuggestionsFaviconCell class]; |
+ _faviconTitles = [NSMutableArray array]; |
+ _faviconImages = [NSMutableArray array]; |
+ } |
+ return self; |
+} |
+ |
+- (void)addFavicon:(UIImage*)favicon withTitle:(NSString*)title { |
+ [_faviconImages addObject:favicon]; |
+ [_faviconTitles addObject:title]; |
+} |
+ |
+#pragma mark - UICollectionViewDataSource |
+ |
+- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView |
+ cellForItemAtIndexPath:(NSIndexPath*)indexPath { |
+ SuggestionsFaviconInternalCell* cell = [collectionView |
+ dequeueReusableCellWithReuseIdentifier:[SuggestionsFaviconInternalCell |
+ reuseIdentifier] |
+ forIndexPath:indexPath]; |
+ cell.faviconView.image = [_faviconImages objectAtIndex:indexPath.item]; |
+ cell.titleLabel.text = [_faviconTitles objectAtIndex:indexPath.item]; |
+ return cell; |
+} |
+ |
+- (NSInteger)collectionView:(UICollectionView*)collectionView |
+ numberOfItemsInSection:(NSInteger)section { |
+ DCHECK([_faviconImages count] == [_faviconTitles count]); |
+ return [_faviconImages count]; |
+} |
+ |
+#pragma mark - CollectionViewItem |
+ |
+- (void)configureCell:(SuggestionsFaviconCell*)cell { |
+ [super configureCell:cell]; |
+ cell.collectionView.dataSource = self; |
+ cell.delegate = self.delegate; |
+} |
+ |
+@end |
+ |
+#pragma mark - SuggestionsFaviconCell |
+ |
+// The cell is the delegate of the inner collection view. |
+@interface SuggestionsFaviconCell ()<UICollectionViewDelegate> |
+ |
+@end |
+ |
+@implementation SuggestionsFaviconCell |
+ |
+@synthesize collectionView = _collectionView; |
+@synthesize delegate = _delegate; |
+ |
+- (instancetype)initWithFrame:(CGRect)frame { |
+ self = [super initWithFrame:frame]; |
+ if (self) { |
+ UICollectionViewFlowLayout* layout = |
+ [[UICollectionViewFlowLayout alloc] init]; |
+ layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; |
+ layout.minimumInteritemSpacing = kInternalCellSpacing; |
+ layout.itemSize = CGSizeMake(kInternalCellWidth, kInternalCellHeight); |
+ layout.sectionInset = UIEdgeInsetsMake(0, kInternalLeadingSpacing, 0, 0); |
+ |
+ _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero |
+ collectionViewLayout:layout]; |
+ [_collectionView registerClass:[SuggestionsFaviconInternalCell class] |
+ forCellWithReuseIdentifier:[SuggestionsFaviconInternalCell |
+ reuseIdentifier]]; |
+ _collectionView.backgroundColor = [UIColor clearColor]; |
+ _collectionView.translatesAutoresizingMaskIntoConstraints = NO; |
+ |
+ _collectionView.delegate = self; |
+ |
+ [self.contentView addSubview:_collectionView]; |
+ AddSameSizeConstraint(_collectionView, self.contentView); |
+ [_collectionView.heightAnchor constraintEqualToConstant:kInternalCellHeight] |
+ .active = YES; |
+ } |
+ return self; |
+} |
+ |
+- (void)prepareForReuse { |
+ [super prepareForReuse]; |
+ self.collectionView.dataSource = nil; |
+ self.delegate = nil; |
+} |
+ |
+#pragma mark - UICollectionViewDelegate |
+ |
+- (void)collectionView:(UICollectionView*)collectionView |
+ didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
+ [self.delegate openFaviconAtIndexPath:indexPath]; |
+} |
+ |
+@end |