Add visibleObjects API

Summary: Adding this API to make querying visible objects a little easier. Fixes #164.

Reviewed By: dshahidehpour

Differential Revision: D4138472

fbshipit-source-id: 0136c39e17c72941b85284b7f3b5494b1ddabf68
This commit is contained in:
Ryan Nystrom 2016-11-07 07:18:59 -08:00 committed by Facebook Github Bot
parent 8fa4001d15
commit 386ae07864
4 changed files with 39 additions and 0 deletions

View file

@ -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
-----

View file

@ -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.

View file

@ -342,6 +342,25 @@
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

View file

@ -508,4 +508,16 @@
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