Skip to content

Commit

Permalink
Fix visible section controllers bug (Instagram#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mani Ghasemlou committed Jul 27, 2017
1 parent 7d33065 commit fb6843d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The changelog for `IGListKit`. Also see the [releases](https://github.com/instag
### Fixes

- Prevent a crash when update queued immediately after item batch update. [Ryan Nystrom](https://github.com/rnystrom) (tbd)
- Return correct `-[IGListAdapter visibleSectionControllers]` when section has no items, but has supplementary views. [Mani Ghasemlou](https://github.com/manicakes) [(#643)](https://github.com/Instagram/IGListKit/issues/643)

### Enhancements

Expand Down
9 changes: 5 additions & 4 deletions Source/IGListAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,12 @@ - (NSArray *)objects {

- (NSArray<IGListSectionController *> *)visibleSectionControllers {
IGAssertMainThread();
NSArray<UICollectionViewCell *> *visibleCells = [self.collectionView visibleCells];
NSMutableSet *visibleSectionControllers = [NSMutableSet new];
for (UICollectionViewCell *cell in visibleCells) {
IGListSectionController *sectionController = [self sectionControllerForView:cell];
IGAssert(sectionController != nil, @"Section controller nil for cell %@", cell);
NSArray<UICollectionViewLayoutAttributes *> *attributes =
[self.collectionView.collectionViewLayout layoutAttributesForElementsInRect:self.collectionView.bounds];
for (UICollectionViewLayoutAttributes* attribute in attributes) {
IGListSectionController *sectionController = [self sectionControllerForSection:attribute.indexPath.section];
IGAssert(sectionController != nil, @"Section controller nil for cell in section %zd", attribute.indexPath.section);
if (sectionController) {
[visibleSectionControllers addObject:sectionController];
}
Expand Down
17 changes: 17 additions & 0 deletions Tests/IGListAdapterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,23 @@ - (void)test_whenCellsExtendBeyondBounds_thatVisibleSectionControllersAreLimited
XCTAssertTrue([visibleSectionControllers containsObject:[self.adapter sectionControllerForObject:@4]]);
}

- (void) test_withEmptySectionPlusFooter_thatVisibleSectionControllersAreCorrect {
self.dataSource.objects = @[@0];
[self.adapter reloadDataWithCompletion:nil];
IGTestSupplementarySource *supplementarySource = [IGTestSupplementarySource new];
supplementarySource.dequeueFromNib = YES;
supplementarySource.collectionContext = self.adapter;
supplementarySource.supportedElementKinds = @[UICollectionElementKindSectionFooter];
IGListSectionController *controller = [self.adapter sectionControllerForObject:@0];
controller.supplementaryViewSource = supplementarySource;
supplementarySource.sectionController = controller;
[self.adapter performUpdatesAnimated:NO completion:nil];
NSArray<IGListSectionController *> *visibleSectionControllers = [self.adapter visibleSectionControllers];

XCTAssertTrue([visibleSectionControllers count] == 1);
XCTAssertTrue(visibleSectionControllers.firstObject.supplementaryViewSource == supplementarySource);
}

- (void)test_whenCellsExtendBeyondBounds_thatVisibleCellsExistForSectionControllers {
self.dataSource.objects = @[@2, @3, @4, @5, @6];
[self.adapter reloadDataWithCompletion:nil];
Expand Down

0 comments on commit fb6843d

Please sign in to comment.