Skip to content

Commit

Permalink
Add visibleObjects API
Browse files Browse the repository at this point in the history
Summary: Adding this API to make querying visible objects a little easier. Fixes #164.

Reviewed By: dshahidehpour

Differential Revision: D4138472

fbshipit-source-id: 0136c39e17c72941b85284b7f3b5494b1ddabf68
  • Loading branch information
Ryan Nystrom authored and Facebook Github Bot committed Nov 7, 2016
1 parent 8fa4001 commit 386ae07
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This release closes the [2.0.0 milestone](https://github.com/Instagram/IGListKit
- Added support for cells created from storyboard. [Bofei Zhu](https://github.com/zhubofei) [(#92)](https://github.com/Instagram/IGListKit/pull/92)
- Added examples for Today & iMessage extensions. [Sherlouk](https://github.com/Sherlouk) [(#112)](https://github.com/Instagram/IGListKit/pull/112)
- Added `tvOS` support. [Jesse Squires](https://github.com/jessesquires) [(#137)](https://github.com/Instagram/IGListKit/pull/137)
- Added new `-[IGListAdapter visibleObjects]` API. [Ryan Nystrom](https://github.com/rnystrom)

1.0.0
-----
Expand Down
7 changes: 7 additions & 0 deletions Source/IGListAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ IGLK_SUBCLASSING_RESTRICTED
*/
- (NSArray<IGListSectionController<IGListSectionType> *> *)visibleSectionControllers;

/**
An unordered array of the currently visible objects.
@return An array of objects
*/
- (NSArray *)visibleObjects;

/**
Scroll to an object in the list adapter.
Expand Down
19 changes: 19 additions & 0 deletions Source/IGListAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,25 @@ - (NSArray *)objects {
return [visibleSectionControllers allObjects];
}

- (NSArray *)visibleObjects {
IGAssertMainThread();
NSArray<UICollectionViewCell *> *visibleCells = [self.collectionView visibleCells];
NSMutableSet *visibleObjects = [NSMutableSet new];
for (UICollectionViewCell *cell in visibleCells) {
IGListSectionController<IGListSectionType> *sectionController = [self sectionControllerForCell:cell];
IGAssert(sectionController != nil, @"Section controller nil for cell %@", cell);
if (sectionController != nil) {
const NSUInteger section = [self sectionForSectionController:sectionController];
id object = [self objectAtSection:section];
IGAssert(object != nil, @"Object not found for section controller %@ at section %zi", sectionController, section);
if (object != nil) {
[visibleObjects addObject:object];
}
}
}
return [visibleObjects allObjects];
}


#pragma mark - Layout

Expand Down
12 changes: 12 additions & 0 deletions Tests/IGListAdapterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -508,4 +508,16 @@ - (void)test_whenAdapterUpdatedTwice_withThreeSections_thatSectionsUpdatedFirstL
XCTAssertFalse([[self.adapter sectionControllerForObject:@2] isLastSection]);
}

- (void)test_whenAdapterUpdated_withObjectsOverflow_thatVisibleObjectsIsSubsetOfAllObjects {
// each section controller returns n items sized 100x10
self.dataSource.objects = @[@1, @2, @3, @4, @5, @6];
[self.adapter reloadDataWithCompletion:nil];
self.collectionView.contentOffset = CGPointMake(0, 30);
[self.collectionView layoutIfNeeded];

NSArray *visibleObjects = [[self.adapter visibleObjects] sortedArrayUsingSelector:@selector(compare:)];
NSArray *expectedObjects = @[@3, @4, @5];
XCTAssertEqualObjects(visibleObjects, expectedObjects);
}

@end

0 comments on commit 386ae07

Please sign in to comment.