Chromium Code Reviews| Index: ios/chrome/browser/ui/bookmarks/bookmark_folder_collection_view.mm |
| diff --git a/ios/chrome/browser/ui/bookmarks/bookmark_folder_collection_view.mm b/ios/chrome/browser/ui/bookmarks/bookmark_folder_collection_view.mm |
| index 4b2a16a4b81bd8dc1d8021e97856d84a8203b35a..f7d7b80f9e734ca01c1488c03e7c975052e7a639 100644 |
| --- a/ios/chrome/browser/ui/bookmarks/bookmark_folder_collection_view.mm |
| +++ b/ios/chrome/browser/ui/bookmarks/bookmark_folder_collection_view.mm |
| @@ -9,8 +9,13 @@ |
| #include "components/bookmarks/browser/bookmark_model.h" |
| #include "ios/chrome/browser/bookmarks/bookmarks_utils.h" |
| #include "ios/chrome/browser/experimental_flags.h" |
| +#import "ios/chrome/browser/ui/authentication/signin_promo_view.h" |
| +#import "ios/chrome/browser/ui/authentication/signin_promo_view_configurator.h" |
| +#import "ios/chrome/browser/ui/authentication/signin_promo_view_consumer.h" |
| +#import "ios/chrome/browser/ui/authentication/signin_promo_view_mediator.h" |
| #import "ios/chrome/browser/ui/bookmarks/bookmark_collection_cells.h" |
| #import "ios/chrome/browser/ui/bookmarks/bookmark_promo_cell.h" |
| +#import "ios/chrome/browser/ui/bookmarks/bookmark_signin_promo_cell.h" |
| #import "ios/chrome/browser/ui/bookmarks/bookmark_utils_ios.h" |
| #if !defined(__has_feature) || !__has_feature(objc_arc) |
| @@ -19,7 +24,8 @@ |
| using bookmarks::BookmarkNode; |
| -@interface BookmarkFolderCollectionView ()<BookmarkPromoCellDelegate> { |
| +@interface BookmarkFolderCollectionView ()<BookmarkPromoCellDelegate, |
| + SigninPromoViewConsumer> { |
| // A vector of folders to display in the collection view. |
| std::vector<const BookmarkNode*> _subFolders; |
| // A vector of bookmark urls to display in the collection view. |
| @@ -27,6 +33,9 @@ using bookmarks::BookmarkNode; |
| // True if the promo is visible. |
| BOOL _promoVisible; |
| + |
| + // Mediator, helper for the sign-in promo view. |
| + SigninPromoViewMediator* _signinPromoViewMediator; |
| } |
| @property(nonatomic, assign) const bookmarks::BookmarkNode* folder; |
| @@ -36,15 +45,11 @@ using bookmarks::BookmarkNode; |
| @property(nonatomic, readonly, assign) NSInteger itemsSection; |
| @property(nonatomic, readonly, assign) NSInteger sectionCount; |
| -// Keep a reference to the promo cell to deregister as delegate. |
| -@property(nonatomic, strong) BookmarkPromoCell* promoCell; |
| - |
| @end |
| @implementation BookmarkFolderCollectionView |
| @synthesize delegate = _delegate; |
| @synthesize folder = _folder; |
| -@synthesize promoCell = _promoCell; |
| - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState |
| frame:(CGRect)frame { |
| @@ -55,10 +60,6 @@ using bookmarks::BookmarkNode; |
| return self; |
| } |
| -- (void)dealloc { |
| - _promoCell.delegate = nil; |
| -} |
| - |
| - (void)setDelegate:(id<BookmarkFolderCollectionViewDelegate>)delegate { |
| _delegate = delegate; |
| [self promoStateChangedAnimated:NO]; |
| @@ -356,14 +357,66 @@ using bookmarks::BookmarkNode; |
| } |
| // Parent class override. |
| +- (CGSize)cellSizeForIndexPath:(NSIndexPath*)indexPath { |
| + if ([self isPromoSection:indexPath.section]) { |
| + UICollectionViewCell* cell = |
|
lpromero
2017/04/26 19:03:50
Is it possible to encapsulate this logic somewhere
jlebel
2017/04/27 09:54:54
I could copy this code into BookmarkSigninPromoCel
lpromero
2017/04/27 15:49:11
At some point, the old cell will be deprecated and
|
| + [self.collectionView cellForItemAtIndexPath:indexPath]; |
| + CGRect estimatedFrame = |
| + CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGFLOAT_MAX); |
| + CGPoint originalPosition = CGPointZero; |
| + if (cell) { |
| + originalPosition = cell.frame.origin; |
| + } else { |
| + // -[UICollectionView |
| + // dequeueReusableCellWithReuseIdentifier:forIndexPath:] cannot be used |
| + // here since this method is called by -[id<UICollectionViewDelegate> |
| + // collectionView:layout:sizeForItemAtIndexPath:]. This would generate |
| + // crash: SIGFPE, EXC_I386_DIV. |
| + if (experimental_flags::IsSigninPromoEnabled()) { |
| + DCHECK(_signinPromoViewMediator); |
| + BookmarkSigninPromoCell* signinPromoCell = |
| + [[BookmarkSigninPromoCell alloc] initWithFrame:estimatedFrame]; |
| + [[_signinPromoViewMediator createConfigurator] |
| + configureSigninPromoView:signinPromoCell.signinPromoView]; |
| + cell = signinPromoCell; |
| + } else { |
| + cell = [[BookmarkPromoCell alloc] initWithFrame:estimatedFrame]; |
| + } |
| + } |
| + cell.frame = estimatedFrame; |
| + [cell setNeedsLayout]; |
| + [cell layoutIfNeeded]; |
| + CGSize result = |
| + [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize |
| + withHorizontalFittingPriority:UILayoutPriorityRequired |
| + verticalFittingPriority:UILayoutPriorityDefaultLow]; |
| + cell.frame = CGRectMake(originalPosition.x, originalPosition.y, |
| + result.width, result.height); |
| + return result; |
| + } |
| + return [super cellSizeForIndexPath:indexPath]; |
| +} |
| + |
| +// Parent class override. |
| - (UICollectionViewCell*)cellAtIndexPath:(NSIndexPath*)indexPath { |
| if (indexPath.section == self.promoSection) { |
| - self.promoCell = [self.collectionView |
| - dequeueReusableCellWithReuseIdentifier:[BookmarkPromoCell |
| - reuseIdentifier] |
| - forIndexPath:indexPath]; |
| - self.promoCell.delegate = self; |
| - return self.promoCell; |
| + if (experimental_flags::IsSigninPromoEnabled()) { |
| + BookmarkSigninPromoCell* signinPromoCell = [self.collectionView |
| + dequeueReusableCellWithReuseIdentifier:[BookmarkSigninPromoCell |
| + reuseIdentifier] |
| + forIndexPath:indexPath]; |
| + signinPromoCell.signinPromoView.sendChromeCommand = YES; |
| + [[_signinPromoViewMediator createConfigurator] |
| + configureSigninPromoView:signinPromoCell.signinPromoView]; |
| + return signinPromoCell; |
| + } else { |
| + BookmarkPromoCell* promoCell = [self.collectionView |
| + dequeueReusableCellWithReuseIdentifier:[BookmarkPromoCell |
| + reuseIdentifier] |
| + forIndexPath:indexPath]; |
| + promoCell.delegate = self; |
| + return promoCell; |
| + } |
| } |
| const BookmarkNode* node = [self nodeAtIndexPath:indexPath]; |
| @@ -445,6 +498,14 @@ using bookmarks::BookmarkNode; |
| // in and out of edit mode is fixed, this is probably the cleanest thing to |
| // do. |
| _promoVisible = newPromoState; |
| + if (experimental_flags::IsSigninPromoEnabled()) { |
| + if (!_promoVisible) { |
| + _signinPromoViewMediator = nil; |
|
lpromero
2017/04/26 19:03:50
You might want to unset as consumer too.
jlebel
2017/04/27 09:54:54
Ok, but I'm the owner of this class.
lpromero
2017/04/27 15:49:11
You can't know if you are the only owner though :)
|
| + } else { |
| + _signinPromoViewMediator = [[SigninPromoViewMediator alloc] init]; |
| + _signinPromoViewMediator.consumer = self; |
| + } |
| + } |
| [self.collectionView reloadData]; |
| } |
| } |
| @@ -465,4 +526,26 @@ using bookmarks::BookmarkNode; |
| return _promoVisible; |
| } |
| +#pragma mark - SigninPromoViewConsumer |
| + |
| +- (void)configureSigninPromoViewWithNewIdentity:(BOOL)newIdentity |
| + configurator:(SigninPromoViewConfigurator*) |
| + configurator { |
| + DCHECK(_signinPromoViewMediator); |
| + if (newIdentity) { |
| + NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:self.promoSection]; |
| + [self.collectionView reloadSections:indexSet]; |
| + return; |
| + } |
| + NSIndexPath* indexPath = |
| + [NSIndexPath indexPathForRow:0 inSection:self.promoSection]; |
| + UICollectionViewCell* cell = |
| + [self.collectionView cellForItemAtIndexPath:indexPath]; |
| + BookmarkSigninPromoCell* signinPromoCell = |
| + static_cast<BookmarkSigninPromoCell*>(cell); |
| + if (!signinPromoCell) |
| + return; |
| + [configurator configureSigninPromoView:signinPromoCell.signinPromoView]; |
| +} |
| + |
| @end |