Add -[IGListAdapter objectForSectionController:] helper method -- iss…

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
This commit is contained in:
Ayush Saraswat 2016-11-16 08:18:23 -08:00 committed by Facebook Github Bot
parent 19280d27b3
commit 1f40dfcab7
4 changed files with 40 additions and 8 deletions

View file

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

View file

@ -141,6 +141,15 @@ IGLK_SUBCLASSING_RESTRICTED
*/
- (__kindof IGListSectionController <IGListSectionType> * _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 <IGListSectionType> *)sectionController;
/**
Returns the object corresponding to a section in the list. Constant time lookup.

View file

@ -304,6 +304,14 @@
return [self.sectionMap sectionControllerForObject:object];
}
- (id)objectForSectionController:(IGListSectionController <IGListSectionType> *)sectionController {
IGAssertMainThread();
IGParameterAssert(sectionController != nil);
const NSUInteger section = [self.sectionMap sectionForSectionController:sectionController];
return [self.sectionMap objectForSection:section];
}
- (id)objectAtSection:(NSUInteger)section {
IGAssertMainThread();

View file

@ -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 <IGListSectionType> * 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 <IGListSectionType> * 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 <IGListSectionType> * 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],