Select an item through IGListCollectionContext

Summary:
We already had a API to deselect an item through `IGListCollectionContext` this is the support for selecting an item.

- [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 added an entry to the `CHANGELOG.md` for any breaking changes, enhancements, or bug fixes.
- [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/master/.github/CONTRIBUTING.md)
Closes https://github.com/Instagram/IGListKit/pull/874

Reviewed By: ryanolsonk

Differential Revision: D5562902

Pulled By: rnystrom

fbshipit-source-id: 756a154607d7d758faddc70ac784e544739f5a93
This commit is contained in:
Marvin Nazari 2017-08-04 15:16:11 -07:00 committed by Facebook Github Bot
parent 7fb228728a
commit 5bede7b78e
6 changed files with 55 additions and 0 deletions

View file

@ -13,6 +13,7 @@ The changelog for `IGListKit`. Also see the [releases](https://github.com/instag
### Enhancements
- Added `-[IGListSectionController didDeselectItemAtIndex:]` API to support default `UICollectionView` cell deselection. [Ryan Nystrom](https://github.com/rnystrom) (tbd)
- Added `-[IGListCollectionContext selectItemAtIndex:]` Select an item through IGListCollectionContext like `-[IGListCollectionContext deselectItemAtIndex:]`. [Marvin Nazari](https://github.com/MarvinNazari) (tbd)
3.0.0
-----

View file

@ -807,6 +807,16 @@
[self.collectionView deselectItemAtIndexPath:indexPath animated:animated];
}
- (void)selectItemAtIndex:(NSInteger)index
sectionController:(IGListSectionController *)sectionController
animated:(BOOL)animated
scrollPosition:(UICollectionViewScrollPosition)scrollPosition {
IGAssertMainThread();
IGParameterAssert(sectionController != nil);
NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousIfInUpdateBlock:NO];
[self.collectionView selectItemAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
}
- (__kindof UICollectionViewCell *)dequeueReusableCellOfClass:(Class)cellClass
forSectionController:(IGListSectionController *)sectionController
atIndex:(NSInteger)index {

View file

@ -99,6 +99,19 @@ NS_SWIFT_NAME(ListCollectionContext)
sectionController:(IGListSectionController *)sectionController
animated:(BOOL)animated;
/**
Selects a cell in the collection.
@param index The index of the item to select.
@param sectionController The section controller requesting this information.
@param animated Pass `YES` to animate the change, `NO` otherwise.
@param scrollPosition An option that specifies where the item should be positioned when scrolling finishes.
*/
- (void)selectItemAtIndex:(NSInteger)index
sectionController:(IGListSectionController *)sectionController
animated:(BOOL)animated
scrollPosition:(UICollectionViewScrollPosition)scrollPosition;
/**
Dequeues a cell from the collection view reuse pool.

View file

@ -225,6 +225,14 @@ static void * kStackedSectionControllerIndexKey = &kStackedSectionControllerInde
[self.collectionContext deselectItemAtIndex:offsetIndex sectionController:self animated:animated];
}
- (void)selectItemAtIndex:(NSInteger)index
sectionController:(IGListSectionController *)sectionController
animated:(BOOL)animated
scrollPosition:(UICollectionViewScrollPosition)scrollPosition {
const NSInteger offsetIndex = [self relativeIndexForSectionController:sectionController fromLocalIndex:index];
[self.collectionContext selectItemAtIndex:offsetIndex sectionController:self animated:animated scrollPosition:scrollPosition];
}
- (UICollectionViewCell *)dequeueReusableCellOfClass:(Class)cellClass
forSectionController:(IGListSectionController *)sectionController
atIndex:(NSInteger)index {

View file

@ -986,6 +986,15 @@
XCTAssertFalse([[self.collectionView cellForItemAtIndexPath:path] isSelected]);
}
- (void)test_whenSelectingThroughContext_thatCellSelected {
self.dataSource.objects = @[@1, @2, @3];
[self.adapter reloadDataWithCompletion:nil];
NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];
[self.adapter selectItemAtIndex:0 sectionController:[self.adapter sectionControllerForObject:@1] animated:NO scrollPosition:UICollectionViewScrollPositionTop];
XCTAssertTrue([[self.collectionView cellForItemAtIndexPath:path] isSelected]);
}
- (void)test_whenScrollingToIndex_withSectionController_thatPositionCorrect {
self.dataSource.objects = @[@1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19];
[self.adapter reloadDataWithCompletion:nil];

View file

@ -826,6 +826,20 @@ static const CGRect kStackTestFrame = (CGRect){{0.0, 0.0}, {100.0, 100.0}};
XCTAssertFalse([[self.collectionView cellForItemAtIndexPath:path] isSelected]);
}
- (void)test_whenSelectingChildSectionControllerIndex_thatCorrectCellSelected {
[self setupWithObjects:@[
[[IGTestObject alloc] initWithKey:@0 value:@[@1, @2, @3]],
[[IGTestObject alloc] initWithKey:@1 value:@[@1, @1]]
]];
NSIndexPath *path = [NSIndexPath indexPathForItem:1 inSection:1];
IGListStackedSectionController *stack = [self.adapter sectionControllerForObject:self.dataSource.objects.lastObject];
IGListSectionController *section = stack.sectionControllers.lastObject;
[section.collectionContext selectItemAtIndex:0 sectionController:section animated:NO scrollPosition:UICollectionViewScrollPositionTop];
XCTAssertTrue([[self.collectionView cellForItemAtIndexPath:path] isSelected]);
}
- (void)test_whenRemovingSection_withWorkingRange_thatChildSectionControllersReceiveEvents {
[self setupWithObjects:@[
[[IGTestObject alloc] initWithKey:@0 value:@[@1, @2, @3]],