From 1f40dfcab7c383c4c566ae50d60ab1b63c69dc21 Mon Sep 17 00:00:00 2001 From: Ayush Saraswat Date: Wed, 16 Nov 2016 08:18:23 -0800 Subject: [PATCH] =?UTF-8?q?Add=20-[IGListAdapter=20objectForSectionControl?= =?UTF-8?q?ler:]=20helper=20method=20--=20iss=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Add -[IGListAdapter objectForSectionController:] helper method Fixes #201 - [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 have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/master/CONTRIBUTING.md) Closes https://github.com/Instagram/IGListKit/pull/204 Differential Revision: D4189648 Pulled By: rnystrom fbshipit-source-id: 75963dafef1a2905311e8edf6e98e99e5ab90061 --- CHANGELOG.md | 1 + Source/IGListAdapter.h | 9 +++++++++ Source/IGListAdapter.m | 8 ++++++++ Tests/IGListAdapterTests.m | 30 ++++++++++++++++++++++-------- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75091c04..d1a49095 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ This release closes the [2.0.0 milestone](https://github.com/Instagram/IGListKit - Added `tvOS` support. [Jesse Squires](https://github.com/jessesquires) [(#137)](https://github.com/Instagram/IGListKit/pull/137) - Added `tvOS` example pack. [Sherlouk](https://github.com/Sherlouk) [(#141)](https://github.com/Instagram/IGListKit/pull/141) - Added new `-[IGListAdapter visibleObjects]` API. [Ryan Nystrom](https://github.com/rnystrom) [(386ae07)](https://github.com/Instagram/IGListKit/commit/386ae0786445c06e1eabf074a4181614332f155f) +- Added new `-[IGListAdapter objectForSectionController:]` API. [Ayush Saraswat](https://github.com/saraswatayu) [(#204)](https://github.com/Instagram/IGListKit/pull/204) 1.0.0 ----- diff --git a/Source/IGListAdapter.h b/Source/IGListAdapter.h index 0b67be7e..13608018 100644 --- a/Source/IGListAdapter.h +++ b/Source/IGListAdapter.h @@ -141,6 +141,15 @@ IGLK_SUBCLASSING_RESTRICTED */ - (__kindof IGListSectionController * _Nullable)sectionControllerForObject:(id)object; +/** + Returns the object corresponding to the specified section controller in the list. Constant time lookup. + + @param sectionController A section controller in the list. + + @return The object for the specified section controller, or nil if not found. + */ +- (nullable id)objectForSectionController:(IGListSectionController *)sectionController; + /** Returns the object corresponding to a section in the list. Constant time lookup. diff --git a/Source/IGListAdapter.m b/Source/IGListAdapter.m index 650d596a..2d975085 100644 --- a/Source/IGListAdapter.m +++ b/Source/IGListAdapter.m @@ -304,6 +304,14 @@ return [self.sectionMap sectionControllerForObject:object]; } +- (id)objectForSectionController:(IGListSectionController *)sectionController { + IGAssertMainThread(); + IGParameterAssert(sectionController != nil); + + const NSUInteger section = [self.sectionMap sectionForSectionController:sectionController]; + return [self.sectionMap objectForSection:section]; +} + - (id)objectAtSection:(NSUInteger)section { IGAssertMainThread(); diff --git a/Tests/IGListAdapterTests.m b/Tests/IGListAdapterTests.m index 7ae1b17d..cfc19535 100644 --- a/Tests/IGListAdapterTests.m +++ b/Tests/IGListAdapterTests.m @@ -55,8 +55,8 @@ XCTAssertEqual(CGPointEqualToPoint(point, p), YES); \ self.dataSource = [[IGListTestAdapterDataSource alloc] init]; self.adapter = [[IGListAdapter alloc] initWithUpdater:updater - viewController:nil - workingRangeSize:0]; + viewController:nil + workingRangeSize:0]; self.adapter.collectionView = self.collectionView; self.adapter.dataSource = self.dataSource; } @@ -113,13 +113,27 @@ XCTAssertEqual(CGPointEqualToPoint(point, p), YES); \ XCTAssertNil([self.adapter sectionControllerForObject:@3]); } +- (void)test_whenAdapterUpdated_thatSectionControllerHasCorrectObject { + self.dataSource.objects = @[@0, @1, @2]; + [self.adapter performUpdatesAnimated:YES completion:nil]; + IGListSectionController * list = [self.adapter sectionControllerForObject:@1]; + XCTAssertEqual([self.adapter objectForSectionController:list], @1); +} + +- (void)test_whenQueryingAdapter_withUnknownItem_thatObjectForSectionControllerIsNil { + self.dataSource.objects = @[@0, @1, @2]; + [self.adapter performUpdatesAnimated:YES completion:nil]; + IGListSectionController * randomList = [[IGListTestSection alloc] init]; + XCTAssertNil([self.adapter objectForSectionController:randomList]); +} + - (void)test_whenQueryingIndexPaths_withSectionController_thatPathsAreEqual { self.dataSource.objects = @[@0, @1, @2]; [self.adapter performUpdatesAnimated:YES completion:nil]; IGListSectionController * second = [self.adapter sectionControllerForObject:@1]; - NSArray *paths0 = [self.adapter indexPathsFromSectionController:second - indexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 4)] - adjustForUpdateBlock:NO]; + NSArray *paths0 = [self.adapter indexPathsFromSectionController:second + indexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 4)] + adjustForUpdateBlock:NO]; NSArray *expected = @[ [NSIndexPath indexPathForItem:2 inSection:1], [NSIndexPath indexPathForItem:3 inSection:1], @@ -136,9 +150,9 @@ XCTAssertEqual(CGPointEqualToPoint(point, p), YES); \ __block BOOL executed = NO; [self.adapter performBatchAnimated:YES updates:^{ - NSArray *paths = [self.adapter indexPathsFromSectionController:second - indexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 2)] - adjustForUpdateBlock:YES]; + NSArray *paths = [self.adapter indexPathsFromSectionController:second + indexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 2)] + adjustForUpdateBlock:YES]; NSArray *expected = @[ [NSIndexPath indexPathForItem:2 inSection:1], [NSIndexPath indexPathForItem:3 inSection:1],