Skip to content

Commit

Permalink
Fix visible section controllers bug (#643)
Browse files Browse the repository at this point in the history
Summary:
Issue fixed: #643

- [x] All tests pass. Demo project builds and runs.
- [x] I added tests, an experiment, or detailed why my change isn't tested.
- [x] I added an entry to the `CHANGELOG.md` for any breaking changes, enhancements, or bug fixes.
- [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/master/.github/CONTRIBUTING.md)
Closes #868

Reviewed By: manicakes

Differential Revision: D5553538

Pulled By: rnystrom

fbshipit-source-id: 25d98a28d47a15d494ba530c850e0335d87d758c
  • Loading branch information
Mani Ghasemlou authored and facebook-github-bot committed Aug 3, 2017
1 parent 85afdfa commit bb9b037
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 bb9b037

Please sign in to comment.