Skip to content

Commit

Permalink
[Collections] - Fix for possible infinite recursion (#1711)
Browse files Browse the repository at this point in the history
* [Collections] - Fix for possible infinite recursion in the case the UICOllectionViewDelegate methods call loadView if self.collectionView has not been set.

* [Collections] - Removed commented code, added comment as to why we shouldn’t call delegate method, updated passed argument in method

* [Collections] - Changed from self.collectionView to passed in collectionView param in delegate methods. Added check for nil _styler and divide by zero for column count
  • Loading branch information
ShepJGoogle authored and ianegordon committed Aug 3, 2017
1 parent be703bc commit abb81d9
Showing 1 changed file with 46 additions and 16 deletions.
62 changes: 46 additions & 16 deletions components/Collections/src/MDCCollectionViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ - (CGSize)collectionView:(UICollectionView *)collectionView
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attr =
[collectionViewLayout layoutAttributesForItemAtIndexPath:indexPath];
CGSize size = [self sizeWithAttribute:attr];
size = [self inlaidSizeAtIndexPath:indexPath withSize:size];
CGSize size = [self sizeWithAttribute:attr collectionView:collectionView];
size = [self inlaidSizeAtIndexPath:indexPath withSize:size collectionView:collectionView];
return size;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section {
return [self insetsAtSectionIndex:section];
return [self insetsAtSectionIndex:section collectionView:collectionView];
}

- (CGFloat)collectionView:(UICollectionView *)collectionView
Expand All @@ -199,23 +199,24 @@ - (CGFloat)collectionView:(UICollectionView *)collectionView
return 0;
}

- (CGSize)sizeWithAttribute:(UICollectionViewLayoutAttributes *)attr {
- (CGSize)sizeWithAttribute:(UICollectionViewLayoutAttributes *)attr
collectionView:(UICollectionView *)collectionView {
CGFloat height = MDCCellDefaultOneLineHeight;
if ([_styler.delegate respondsToSelector:@selector(collectionView:cellHeightAtIndexPath:)]) {
height =
[_styler.delegate collectionView:self.collectionView cellHeightAtIndexPath:attr.indexPath];
[_styler.delegate collectionView:collectionView cellHeightAtIndexPath:attr.indexPath];
}

CGFloat width = [self cellWidthAtSectionIndex:attr.indexPath.section];
CGFloat width = [self cellWidthAtSectionIndex:attr.indexPath.section
collectionView:collectionView];
return CGSizeMake(width, height);
}

- (CGFloat)cellWidthAtSectionIndex:(NSInteger)section {
- (CGFloat)cellWidthAtSectionIndex:(NSInteger)section
collectionView:(UICollectionView *)collectionView {
CGFloat bounds = CGRectGetWidth(
UIEdgeInsetsInsetRect(self.collectionView.bounds, self.collectionView.contentInset));
UIEdgeInsets sectionInsets = [self collectionView:self.collectionView
layout:self.collectionView.collectionViewLayout
insetForSectionAtIndex:section];
UIEdgeInsetsInsetRect(collectionView.bounds, collectionView.contentInset));
UIEdgeInsets sectionInsets = [self insetsAtSectionIndex:section collectionView:collectionView];

CGFloat insets = sectionInsets.left + sectionInsets.right;
if (_styler.cellLayoutType == MDCCollectionViewCellLayoutTypeGrid) {
Expand All @@ -225,11 +226,12 @@ - (CGFloat)cellWidthAtSectionIndex:(NSInteger)section {
return bounds - insets;
}

- (UIEdgeInsets)insetsAtSectionIndex:(NSInteger)section {
- (UIEdgeInsets)insetsAtSectionIndex:(NSInteger)section
collectionView:(UICollectionView *)collectionView {
// Determine insets based on cell style.
CGFloat inset = (CGFloat)floor(MDCCollectionViewCellStyleCardSectionInset);
UIEdgeInsets insets = UIEdgeInsetsZero;
NSInteger numberOfSections = self.collectionView.numberOfSections;
NSInteger numberOfSections = collectionView.numberOfSections;
BOOL isTop = (section == 0);
BOOL isBottom = (section == numberOfSections - 1);
MDCCollectionViewCellStyle cellStyle = [_styler cellStyleAtSectionIndex:section];
Expand All @@ -248,9 +250,10 @@ - (UIEdgeInsets)insetsAtSectionIndex:(NSInteger)section {
return insets;
}

- (CGSize)inlaidSizeAtIndexPath:(NSIndexPath *)indexPath withSize:(CGSize)size {
- (CGSize)inlaidSizeAtIndexPath:(NSIndexPath *)indexPath
withSize:(CGSize)size
collectionView:(UICollectionView *)collectionView {
// If object is inlaid, return its adjusted size.
UICollectionView *collectionView = self.collectionView;
if ([_styler isItemInlaidAtIndexPath:indexPath]) {
CGFloat inset = MDCCollectionViewCellStyleCardSectionInset;
UIEdgeInsets inlayInsets = UIEdgeInsetsZero;
Expand All @@ -261,7 +264,7 @@ - (CGSize)inlaidSizeAtIndexPath:(NSIndexPath *)indexPath withSize:(CGSize)size {
if ([self
respondsToSelector:@selector(collectionView:layout:referenceSizeForHeaderInSection:)]) {
CGSize headerSize = [self collectionView:collectionView
layout:_collectionViewLayout
layout:collectionView.collectionViewLayout
referenceSizeForHeaderInSection:indexPath.section];
hasSectionHeader = !CGSizeEqualToSize(headerSize, CGSizeZero);
}
Expand Down Expand Up @@ -298,6 +301,33 @@ - (CGSize)inlaidSizeAtIndexPath:(NSIndexPath *)indexPath withSize:(CGSize)size {
return size;
}

#pragma mark - Subclassing Methods

/*
The below method is solely used for subclasses to retrieve width information in order to
calculate cell height. Not meant to call method cellWidthAtSectionIndex:collectionView as
that method recalculates section insets which we don't want to do.
*/
- (CGFloat)cellWidthAtSectionIndex:(NSInteger)section {
CGFloat bounds = CGRectGetWidth(
UIEdgeInsetsInsetRect(self.collectionView.bounds,
self.collectionView.contentInset));
UIEdgeInsets sectionInsets = [self collectionView:self.collectionView
layout:self.collectionView.collectionViewLayout
insetForSectionAtIndex:section];

CGFloat insets = sectionInsets.left + sectionInsets.right;
if (_styler != nil) {
if (_styler.cellLayoutType == MDCCollectionViewCellLayoutTypeGrid) {
CGFloat cellWidth = bounds - insets - (_styler.gridPadding * (_styler.gridColumnCount - 1));
if (_styler.gridColumnCount > 0) {
return cellWidth / _styler.gridColumnCount;
}
}
}
return bounds - insets;
}

#pragma mark - <MDCInkTouchControllerDelegate>

- (BOOL)inkTouchController:(MDCInkTouchController *)inkTouchController
Expand Down

0 comments on commit abb81d9

Please sign in to comment.