Skip to content

Commit

Permalink
Select an item through IGListCollectionContext
Browse files Browse the repository at this point in the history
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 #874

Reviewed By: ryanolsonk

Differential Revision: D5562902

Pulled By: rnystrom

fbshipit-source-id: 756a154607d7d758faddc70ac784e544739f5a93
  • Loading branch information
MarvinNazari authored and facebook-github-bot committed Aug 4, 2017
1 parent 7fb2287 commit 5bede7b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
10 changes: 10 additions & 0 deletions Source/IGListAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,16 @@ - (void)deselectItemAtIndex:(NSInteger)index
[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 {
Expand Down
13 changes: 13 additions & 0 deletions Source/IGListCollectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions Source/IGListStackedSectionController.m
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ - (void)deselectItemAtIndex:(NSInteger)index sectionController:(IGListSectionCon
[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 {
Expand Down
9 changes: 9 additions & 0 deletions Tests/IGListAdapterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,15 @@ - (void)test_whenDeselectingThroughContext_thatCellDeselected {
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];
Expand Down
14 changes: 14 additions & 0 deletions Tests/IGListStackSectionControllerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,20 @@ - (void)test_whenDeselectingChildSectionControllerIndex_thatCorrectCellDeselecte
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]],
Expand Down

0 comments on commit 5bede7b

Please sign in to comment.