Skip to content

Commit

Permalink
[Collections] Caches decoration views used in grid layout. Cleans up …
Browse files Browse the repository at this point in the history
…layout invalidation logic.

Summary: Closes https://github.com/google/material-components-ios/issues/479

Reviewers: junius, #mdc_ios_owners

Reviewed By: junius, #mdc_ios_owners

Projects: #material_components_ios

Differential Revision: http://codereview.cc/D808
  • Loading branch information
chriscox committed May 3, 2016
1 parent 5ca5458 commit 9e37e3a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
7 changes: 4 additions & 3 deletions components/Collections/src/MDCCollectionViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ - (void)viewDidLoad {
_inkTouchController.delaysInkSpread = YES;
}

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_styler.shouldInvalidateLayout = NO;
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[_collectionViewLayout invalidateLayout];
}

- (UICollectionViewLayout *)collectionViewLayout {
Expand Down
27 changes: 20 additions & 7 deletions components/Collections/src/MDCCollectionViewFlowLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ @implementation MDCCollectionViewFlowLayout {
NSMutableIndexSet *_insertedSections;
NSMutableIndexSet *_headerSections;
NSMutableIndexSet *_footerSections;
NSMutableDictionary *_decorationViewAttributeCache;
}

- (instancetype)init {
Expand All @@ -54,6 +55,7 @@ - (instancetype)init {
self.sectionInset = UIEdgeInsetsZero;

// Register decoration view for grid background.
_decorationViewAttributeCache = [NSMutableDictionary dictionary];
[self registerClass:[MDCCollectionGridBackgroundView class]
forDecorationViewOfKind:kCollectionDecorationView];
}
Expand Down Expand Up @@ -109,13 +111,17 @@ - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
if (!CGSizeEqualToSize(self.collectionView.bounds.size, newBounds.size) ||
self.editor.isEditing ||
self.styler.cellLayoutType == MDCCollectionViewCellLayoutTypeGrid) {
[self invalidateLayout];
if (!CGSizeEqualToSize(self.collectionView.bounds.size, newBounds.size)) {
return YES;
}
return [super shouldInvalidateLayoutForBoundsChange:newBounds];
return NO;
}

- (void)invalidateLayout {
[super invalidateLayout];

// Clear decoration attribute cache.
[_decorationViewAttributeCache removeAllObjects];
}

#pragma mark - UICollectionViewLayout (UISubclassingHooks)
Expand Down Expand Up @@ -176,9 +182,16 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSInde
- (UICollectionViewLayoutAttributes *)
layoutAttributesForDecorationViewOfKind:(NSString *)elementKind
atIndexPath:(NSIndexPath *)indexPath {
// Check cache for decoration view attributes, and add to cache if they don't exist.
MDCCollectionViewLayoutAttributes *decorationAttr =
[MDCCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:elementKind
withIndexPath:indexPath];
[_decorationViewAttributeCache objectForKey:indexPath];
if (!decorationAttr) {
decorationAttr =
[MDCCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:elementKind
withIndexPath:indexPath];
[_decorationViewAttributeCache setObject:decorationAttr forKey:indexPath];
}

// Determine section frame by summing all of its item frames.
CGRect sectionFrame = CGRectNull;
for (NSInteger i = 0; i < [self numberOfItemsInSection:indexPath.section]; ++i) {
Expand Down

0 comments on commit 9e37e3a

Please sign in to comment.