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