From 386ae0786445c06e1eabf074a4181614332f155f Mon Sep 17 00:00:00 2001 From: Ryan Nystrom Date: Mon, 7 Nov 2016 07:18:59 -0800 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + Source/IGListAdapter.h | 7 +++++++ Source/IGListAdapter.m | 19 +++++++++++++++++++ Tests/IGListAdapterTests.m | 12 ++++++++++++ 4 files changed, 39 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc8a92008..34b7b83bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ----- diff --git a/Source/IGListAdapter.h b/Source/IGListAdapter.h index 5e4f76c39..331e15d22 100644 --- a/Source/IGListAdapter.h +++ b/Source/IGListAdapter.h @@ -168,6 +168,13 @@ IGLK_SUBCLASSING_RESTRICTED */ - (NSArray *> *)visibleSectionControllers; +/** + An unordered array of the currently visible objects. + + @return An array of objects + */ +- (NSArray *)visibleObjects; + /** Scroll to an object in the list adapter. diff --git a/Source/IGListAdapter.m b/Source/IGListAdapter.m index c7841f0c0..650d596a9 100644 --- a/Source/IGListAdapter.m +++ b/Source/IGListAdapter.m @@ -342,6 +342,25 @@ - (NSArray *)objects { return [visibleSectionControllers allObjects]; } +- (NSArray *)visibleObjects { + IGAssertMainThread(); + NSArray *visibleCells = [self.collectionView visibleCells]; + NSMutableSet *visibleObjects = [NSMutableSet new]; + for (UICollectionViewCell *cell in visibleCells) { + IGListSectionController *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 diff --git a/Tests/IGListAdapterTests.m b/Tests/IGListAdapterTests.m index 1c186db36..6563b72d6 100644 --- a/Tests/IGListAdapterTests.m +++ b/Tests/IGListAdapterTests.m @@ -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