Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/bookmarks/bookmark_folder_collection_view.h" | 5 #import "ios/chrome/browser/ui/bookmarks/bookmark_folder_collection_view.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
| 9 #include "components/bookmarks/browser/bookmark_model.h" | 9 #include "components/bookmarks/browser/bookmark_model.h" |
| 10 #include "ios/chrome/browser/bookmarks/bookmarks_utils.h" | 10 #include "ios/chrome/browser/bookmarks/bookmarks_utils.h" |
| 11 #include "ios/chrome/browser/experimental_flags.h" | 11 #include "ios/chrome/browser/experimental_flags.h" |
| 12 #import "ios/chrome/browser/ui/authentication/signin_promo_view.h" | |
| 13 #import "ios/chrome/browser/ui/authentication/signin_promo_view_configurator.h" | |
| 14 #import "ios/chrome/browser/ui/authentication/signin_promo_view_consumer.h" | |
| 15 #import "ios/chrome/browser/ui/authentication/signin_promo_view_mediator.h" | |
| 12 #import "ios/chrome/browser/ui/bookmarks/bookmark_collection_cells.h" | 16 #import "ios/chrome/browser/ui/bookmarks/bookmark_collection_cells.h" |
| 13 #import "ios/chrome/browser/ui/bookmarks/bookmark_promo_cell.h" | 17 #import "ios/chrome/browser/ui/bookmarks/bookmark_promo_cell.h" |
| 18 #import "ios/chrome/browser/ui/bookmarks/bookmark_signin_promo_cell.h" | |
| 14 #import "ios/chrome/browser/ui/bookmarks/bookmark_utils_ios.h" | 19 #import "ios/chrome/browser/ui/bookmarks/bookmark_utils_ios.h" |
| 15 | 20 |
| 16 #if !defined(__has_feature) || !__has_feature(objc_arc) | 21 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 17 #error "This file requires ARC support." | 22 #error "This file requires ARC support." |
| 18 #endif | 23 #endif |
| 19 | 24 |
| 20 using bookmarks::BookmarkNode; | 25 using bookmarks::BookmarkNode; |
| 21 | 26 |
| 22 @interface BookmarkFolderCollectionView ()<BookmarkPromoCellDelegate> { | 27 @interface BookmarkFolderCollectionView ()<BookmarkPromoCellDelegate, |
| 28 SigninPromoViewConsumer> { | |
| 23 // A vector of folders to display in the collection view. | 29 // A vector of folders to display in the collection view. |
| 24 std::vector<const BookmarkNode*> _subFolders; | 30 std::vector<const BookmarkNode*> _subFolders; |
| 25 // A vector of bookmark urls to display in the collection view. | 31 // A vector of bookmark urls to display in the collection view. |
| 26 std::vector<const BookmarkNode*> _subItems; | 32 std::vector<const BookmarkNode*> _subItems; |
| 27 | 33 |
| 28 // True if the promo is visible. | 34 // True if the promo is visible. |
| 29 BOOL _promoVisible; | 35 BOOL _promoVisible; |
| 36 | |
| 37 // Mediator, helper for the sign-in promo view. | |
| 38 SigninPromoViewMediator* _signinPromoViewMediator; | |
| 30 } | 39 } |
| 31 @property(nonatomic, assign) const bookmarks::BookmarkNode* folder; | 40 @property(nonatomic, assign) const bookmarks::BookmarkNode* folder; |
| 32 | 41 |
| 33 // Section indices. | 42 // Section indices. |
| 34 @property(nonatomic, readonly, assign) NSInteger promoSection; | 43 @property(nonatomic, readonly, assign) NSInteger promoSection; |
| 35 @property(nonatomic, readonly, assign) NSInteger folderSection; | 44 @property(nonatomic, readonly, assign) NSInteger folderSection; |
| 36 @property(nonatomic, readonly, assign) NSInteger itemsSection; | 45 @property(nonatomic, readonly, assign) NSInteger itemsSection; |
| 37 @property(nonatomic, readonly, assign) NSInteger sectionCount; | 46 @property(nonatomic, readonly, assign) NSInteger sectionCount; |
| 38 | 47 |
| 39 // Keep a reference to the promo cell to deregister as delegate. | |
| 40 @property(nonatomic, strong) BookmarkPromoCell* promoCell; | |
| 41 | |
| 42 @end | 48 @end |
| 43 | 49 |
| 44 @implementation BookmarkFolderCollectionView | 50 @implementation BookmarkFolderCollectionView |
| 45 @synthesize delegate = _delegate; | 51 @synthesize delegate = _delegate; |
| 46 @synthesize folder = _folder; | 52 @synthesize folder = _folder; |
| 47 @synthesize promoCell = _promoCell; | |
| 48 | 53 |
| 49 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState | 54 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState |
| 50 frame:(CGRect)frame { | 55 frame:(CGRect)frame { |
| 51 self = [super initWithBrowserState:browserState frame:frame]; | 56 self = [super initWithBrowserState:browserState frame:frame]; |
| 52 if (self) { | 57 if (self) { |
| 53 [self updateCollectionView]; | 58 [self updateCollectionView]; |
| 54 } | 59 } |
| 55 return self; | 60 return self; |
| 56 } | 61 } |
| 57 | 62 |
| 58 - (void)dealloc { | |
| 59 _promoCell.delegate = nil; | |
| 60 } | |
| 61 | |
| 62 - (void)setDelegate:(id<BookmarkFolderCollectionViewDelegate>)delegate { | 63 - (void)setDelegate:(id<BookmarkFolderCollectionViewDelegate>)delegate { |
| 63 _delegate = delegate; | 64 _delegate = delegate; |
| 64 [self promoStateChangedAnimated:NO]; | 65 [self promoStateChangedAnimated:NO]; |
| 65 } | 66 } |
| 66 | 67 |
| 67 - (NSInteger)promoSection { | 68 - (NSInteger)promoSection { |
| 68 return [self shouldShowPromoCell] ? 0 : -1; | 69 return [self shouldShowPromoCell] ? 0 : -1; |
| 69 } | 70 } |
| 70 | 71 |
| 71 - (NSInteger)folderSection { | 72 - (NSInteger)folderSection { |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 349 else if (section == self.itemsSection) | 350 else if (section == self.itemsSection) |
| 350 insets.top = [self interitemSpacingForSectionAtIndex:section] / 2.; | 351 insets.top = [self interitemSpacingForSectionAtIndex:section] / 2.; |
| 351 else if (section == self.promoSection) | 352 else if (section == self.promoSection) |
| 352 (void)0; // No insets to update. | 353 (void)0; // No insets to update. |
| 353 else | 354 else |
| 354 NOTREACHED(); | 355 NOTREACHED(); |
| 355 return insets; | 356 return insets; |
| 356 } | 357 } |
| 357 | 358 |
| 358 // Parent class override. | 359 // Parent class override. |
| 360 - (CGSize)cellSizeForIndexPath:(NSIndexPath*)indexPath { | |
| 361 if ([self isPromoSection:indexPath.section]) { | |
| 362 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
| |
| 363 [self.collectionView cellForItemAtIndexPath:indexPath]; | |
| 364 CGRect estimatedFrame = | |
| 365 CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGFLOAT_MAX); | |
| 366 CGPoint originalPosition = CGPointZero; | |
| 367 if (cell) { | |
| 368 originalPosition = cell.frame.origin; | |
| 369 } else { | |
| 370 // -[UICollectionView | |
| 371 // dequeueReusableCellWithReuseIdentifier:forIndexPath:] cannot be used | |
| 372 // here since this method is called by -[id<UICollectionViewDelegate> | |
| 373 // collectionView:layout:sizeForItemAtIndexPath:]. This would generate | |
| 374 // crash: SIGFPE, EXC_I386_DIV. | |
| 375 if (experimental_flags::IsSigninPromoEnabled()) { | |
| 376 DCHECK(_signinPromoViewMediator); | |
| 377 BookmarkSigninPromoCell* signinPromoCell = | |
| 378 [[BookmarkSigninPromoCell alloc] initWithFrame:estimatedFrame]; | |
| 379 [[_signinPromoViewMediator createConfigurator] | |
| 380 configureSigninPromoView:signinPromoCell.signinPromoView]; | |
| 381 cell = signinPromoCell; | |
| 382 } else { | |
| 383 cell = [[BookmarkPromoCell alloc] initWithFrame:estimatedFrame]; | |
| 384 } | |
| 385 } | |
| 386 cell.frame = estimatedFrame; | |
| 387 [cell setNeedsLayout]; | |
| 388 [cell layoutIfNeeded]; | |
| 389 CGSize result = | |
| 390 [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize | |
| 391 withHorizontalFittingPriority:UILayoutPriorityRequired | |
| 392 verticalFittingPriority:UILayoutPriorityDefaultLow]; | |
| 393 cell.frame = CGRectMake(originalPosition.x, originalPosition.y, | |
| 394 result.width, result.height); | |
| 395 return result; | |
| 396 } | |
| 397 return [super cellSizeForIndexPath:indexPath]; | |
| 398 } | |
| 399 | |
| 400 // Parent class override. | |
| 359 - (UICollectionViewCell*)cellAtIndexPath:(NSIndexPath*)indexPath { | 401 - (UICollectionViewCell*)cellAtIndexPath:(NSIndexPath*)indexPath { |
| 360 if (indexPath.section == self.promoSection) { | 402 if (indexPath.section == self.promoSection) { |
| 361 self.promoCell = [self.collectionView | 403 if (experimental_flags::IsSigninPromoEnabled()) { |
| 362 dequeueReusableCellWithReuseIdentifier:[BookmarkPromoCell | 404 BookmarkSigninPromoCell* signinPromoCell = [self.collectionView |
| 363 reuseIdentifier] | 405 dequeueReusableCellWithReuseIdentifier:[BookmarkSigninPromoCell |
| 364 forIndexPath:indexPath]; | 406 reuseIdentifier] |
| 365 self.promoCell.delegate = self; | 407 forIndexPath:indexPath]; |
| 366 return self.promoCell; | 408 signinPromoCell.signinPromoView.sendChromeCommand = YES; |
| 409 [[_signinPromoViewMediator createConfigurator] | |
| 410 configureSigninPromoView:signinPromoCell.signinPromoView]; | |
| 411 return signinPromoCell; | |
| 412 } else { | |
| 413 BookmarkPromoCell* promoCell = [self.collectionView | |
| 414 dequeueReusableCellWithReuseIdentifier:[BookmarkPromoCell | |
| 415 reuseIdentifier] | |
| 416 forIndexPath:indexPath]; | |
| 417 promoCell.delegate = self; | |
| 418 return promoCell; | |
| 419 } | |
| 367 } | 420 } |
| 368 const BookmarkNode* node = [self nodeAtIndexPath:indexPath]; | 421 const BookmarkNode* node = [self nodeAtIndexPath:indexPath]; |
| 369 | 422 |
| 370 if (indexPath.section == self.folderSection) | 423 if (indexPath.section == self.folderSection) |
| 371 return [self cellForFolder:node indexPath:indexPath]; | 424 return [self cellForFolder:node indexPath:indexPath]; |
| 372 | 425 |
| 373 BookmarkItemCell* cell = [self cellForBookmark:node indexPath:indexPath]; | 426 BookmarkItemCell* cell = [self cellForBookmark:node indexPath:indexPath]; |
| 374 return cell; | 427 return cell; |
| 375 } | 428 } |
| 376 | 429 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 438 - (void)promoStateChangedAnimated:(BOOL)animate { | 491 - (void)promoStateChangedAnimated:(BOOL)animate { |
| 439 BOOL newPromoState = | 492 BOOL newPromoState = |
| 440 !self.editing && self.folder && | 493 !self.editing && self.folder && |
| 441 self.folder->type() == BookmarkNode::MOBILE && | 494 self.folder->type() == BookmarkNode::MOBILE && |
| 442 [self.delegate bookmarkCollectionViewShouldShowPromoCell:self]; | 495 [self.delegate bookmarkCollectionViewShouldShowPromoCell:self]; |
| 443 if (newPromoState != _promoVisible) { | 496 if (newPromoState != _promoVisible) { |
| 444 // This is awful, but until the old code to do the refresh when switching | 497 // This is awful, but until the old code to do the refresh when switching |
| 445 // in and out of edit mode is fixed, this is probably the cleanest thing to | 498 // in and out of edit mode is fixed, this is probably the cleanest thing to |
| 446 // do. | 499 // do. |
| 447 _promoVisible = newPromoState; | 500 _promoVisible = newPromoState; |
| 501 if (experimental_flags::IsSigninPromoEnabled()) { | |
| 502 if (!_promoVisible) { | |
| 503 _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 :)
| |
| 504 } else { | |
| 505 _signinPromoViewMediator = [[SigninPromoViewMediator alloc] init]; | |
| 506 _signinPromoViewMediator.consumer = self; | |
| 507 } | |
| 508 } | |
| 448 [self.collectionView reloadData]; | 509 [self.collectionView reloadData]; |
| 449 } | 510 } |
| 450 } | 511 } |
| 451 | 512 |
| 452 #pragma mark - BookmarkPromoCellDelegate | 513 #pragma mark - BookmarkPromoCellDelegate |
| 453 | 514 |
| 454 - (void)bookmarkPromoCellDidTapSignIn:(BookmarkPromoCell*)bookmarkPromoCell { | 515 - (void)bookmarkPromoCellDidTapSignIn:(BookmarkPromoCell*)bookmarkPromoCell { |
| 455 [self.delegate bookmarkCollectionViewShowSignIn:self]; | 516 [self.delegate bookmarkCollectionViewShowSignIn:self]; |
| 456 } | 517 } |
| 457 | 518 |
| 458 - (void)bookmarkPromoCellDidTapDismiss:(BookmarkPromoCell*)bookmarkPromoCell { | 519 - (void)bookmarkPromoCellDidTapDismiss:(BookmarkPromoCell*)bookmarkPromoCell { |
| 459 [self.delegate bookmarkCollectionViewDismissPromo:self]; | 520 [self.delegate bookmarkCollectionViewDismissPromo:self]; |
| 460 } | 521 } |
| 461 | 522 |
| 462 #pragma mark - Promo Cell | 523 #pragma mark - Promo Cell |
| 463 | 524 |
| 464 - (BOOL)shouldShowPromoCell { | 525 - (BOOL)shouldShowPromoCell { |
| 465 return _promoVisible; | 526 return _promoVisible; |
| 466 } | 527 } |
| 467 | 528 |
| 529 #pragma mark - SigninPromoViewConsumer | |
| 530 | |
| 531 - (void)configureSigninPromoViewWithNewIdentity:(BOOL)newIdentity | |
| 532 configurator:(SigninPromoViewConfigurator*) | |
| 533 configurator { | |
| 534 DCHECK(_signinPromoViewMediator); | |
| 535 if (newIdentity) { | |
| 536 NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:self.promoSection]; | |
| 537 [self.collectionView reloadSections:indexSet]; | |
| 538 return; | |
| 539 } | |
| 540 NSIndexPath* indexPath = | |
| 541 [NSIndexPath indexPathForRow:0 inSection:self.promoSection]; | |
| 542 UICollectionViewCell* cell = | |
| 543 [self.collectionView cellForItemAtIndexPath:indexPath]; | |
| 544 BookmarkSigninPromoCell* signinPromoCell = | |
| 545 static_cast<BookmarkSigninPromoCell*>(cell); | |
| 546 if (!signinPromoCell) | |
| 547 return; | |
| 548 [configurator configureSigninPromoView:signinPromoCell.signinPromoView]; | |
| 549 } | |
| 550 | |
| 468 @end | 551 @end |
| OLD | NEW |