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 = | |
363 [self.collectionView cellForItemAtIndexPath:indexPath]; | |
364 CGRect estimatedFrame = | |
msarda
2017/04/27 11:58:24
I think this is not the estimated frame, but rathe
jlebel
2017/04/27 14:01:28
Done.
| |
365 CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGFLOAT_MAX); | |
366 CGPoint originalPosition = CGPointZero; | |
msarda
2017/04/27 11:58:24
Optional nit:
s/originalPosition/initialCellOrigin
jlebel
2017/04/27 14:01:28
Done.
| |
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()) { | |
msarda
2017/04/27 11:58:24
I think this code would be easier to read if we ha
jlebel
2017/04/27 14:01:28
Done.
| |
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; | |
msarda
2017/04/27 11:58:24
Nit: Move this inside the if(cell) as on the else
jlebel
2017/04/27 14:01:28
Done.
| |
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.consumer = nil; | |
504 _signinPromoViewMediator = nil; | |
505 } else { | |
506 _signinPromoViewMediator = [[SigninPromoViewMediator alloc] init]; | |
507 _signinPromoViewMediator.consumer = self; | |
508 } | |
509 } | |
448 [self.collectionView reloadData]; | 510 [self.collectionView reloadData]; |
449 } | 511 } |
450 } | 512 } |
451 | 513 |
452 #pragma mark - BookmarkPromoCellDelegate | 514 #pragma mark - BookmarkPromoCellDelegate |
453 | 515 |
454 - (void)bookmarkPromoCellDidTapSignIn:(BookmarkPromoCell*)bookmarkPromoCell { | 516 - (void)bookmarkPromoCellDidTapSignIn:(BookmarkPromoCell*)bookmarkPromoCell { |
455 [self.delegate bookmarkCollectionViewShowSignIn:self]; | 517 [self.delegate bookmarkCollectionViewShowSignIn:self]; |
456 } | 518 } |
457 | 519 |
458 - (void)bookmarkPromoCellDidTapDismiss:(BookmarkPromoCell*)bookmarkPromoCell { | 520 - (void)bookmarkPromoCellDidTapDismiss:(BookmarkPromoCell*)bookmarkPromoCell { |
459 [self.delegate bookmarkCollectionViewDismissPromo:self]; | 521 [self.delegate bookmarkCollectionViewDismissPromo:self]; |
460 } | 522 } |
461 | 523 |
462 #pragma mark - Promo Cell | 524 #pragma mark - Promo Cell |
463 | 525 |
464 - (BOOL)shouldShowPromoCell { | 526 - (BOOL)shouldShowPromoCell { |
465 return _promoVisible; | 527 return _promoVisible; |
466 } | 528 } |
467 | 529 |
530 #pragma mark - SigninPromoViewConsumer | |
531 | |
532 - (void)configureSigninPromoViewWithNewIdentity:(BOOL)newIdentity | |
533 configurator:(SigninPromoViewConfigurator*) | |
534 configurator { | |
535 DCHECK(_signinPromoViewMediator); | |
536 if (newIdentity) { | |
537 NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:self.promoSection]; | |
538 [self.collectionView reloadSections:indexSet]; | |
539 return; | |
540 } | |
541 NSIndexPath* indexPath = | |
542 [NSIndexPath indexPathForRow:0 inSection:self.promoSection]; | |
543 UICollectionViewCell* cell = | |
544 [self.collectionView cellForItemAtIndexPath:indexPath]; | |
545 BookmarkSigninPromoCell* signinPromoCell = | |
546 static_cast<BookmarkSigninPromoCell*>(cell); | |
547 if (!signinPromoCell) | |
548 return; | |
549 [configurator configureSigninPromoView:signinPromoCell.signinPromoView]; | |
550 } | |
551 | |
468 @end | 552 @end |
OLD | NEW |