-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added -[IGListAdapter visibleCellsForObject:]
API
#442
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
Turns out there's already a method for https://github.com/Instagram/IGListKit/blob/master/Source/IGListAdapter.m#L777-L788 Considering you can quite easily get the section controller from object using @rnystrom Suggestions? |
@Sherlouk doh. Ya, maybe we should just recommend people use that API instead... |
@rnystrom Was your API request, if you think the need is no longer necessary then close this and that issue 😄 |
Lol I know, forgot that API existed. Might still be valuable to cut out the middle-layer steps. Could also return early if |
Sorry, lost track here... We saying in the old API to return early? (Also pardon the test name 😂, only just realised I didn't change it) |
The old API will end up asserting if the section controller is nil. If you used this method by first getting the object, then the SC (in ObjC) you might assert: id controller = [self.adapter sectionControllerForObject:object]; // can be nil
NSArray *cells = [self.adapter visibleCellsForSectionController:controller]; // assert if nil Which means all your callsites have to look like: id controller = [self.adapter sectionControllerForObject:object]; // can be nil
if (controller != nil) {
NSArray *cells = [self.adapter visibleCellsForSectionController:controller]; // assert if nil
} Which is kind of gross. Swift will be just as bad. Options are:
We should also add a unit test that sends |
@Sherlouk ok thought about it more. Let's do option 2 and move forward w/ this for 2.2 if you think that's doable 😄 |
Option 2 is good, want to raise a new issue to track progress on amending old functionality? |
|
||
@return An array of collection view cells. | ||
*/ | ||
- (NSArray<UICollectionViewCell *> *)visibleCellsForObject:(id)item; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: object
as param name
IGAssert(section != NSNotFound, @"Section not found for object %@", item); | ||
|
||
NSPredicate *controllerPredicate = [NSPredicate predicateWithBlock:^BOOL(UICollectionViewCell* cell, NSDictionary* bindings) { | ||
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: calling self.collectionView
inside this block will execute an extra objc_msgSend(...)
every execution. Instead let's capture the collection view in a local variable that the block uses:
UICollectionView *collectionView = self.collectionView;
NSPredicate *predicate = [... ^{
NSIndexPath *path = [collectionView indexPathForCell:cell];
}];
|
||
NSArray<UICollectionViewCell *> *visibleCells = [self.collectionView visibleCells]; | ||
NSInteger section = [self.sectionMap sectionForObject:item]; | ||
IGAssert(section != NSNotFound, @"Section not found for object %@", item); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually let's remove this. A use might be holding a ref to an expired object. I think it's ok to return an empty array in that case.
IGParameterAssert(item != nil); | ||
|
||
NSArray<UICollectionViewCell *> *visibleCells = [self.collectionView visibleCells]; | ||
NSInteger section = [self.sectionMap sectionForObject:item]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: const
}]; | ||
|
||
NSArray<UICollectionViewCell *> *filteredCells = [visibleCells filteredArrayUsingPredicate:controllerPredicate]; | ||
return filteredCells; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: IMO just return [visibleCells filteredArrayUsingPredicate:controllerPredicate];
All valid comments, will work on it tonight! |
@Sherlouk updated the pull request - view changes |
@rnystrom Just updated, should address all of your points! Not sure if this is the best code to return an empty array, but it does seem to work? return [NSArray new]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Let's add a changelog for 2.2.0
@Sherlouk updated the pull request - view changes |
@jessesquires has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
1 similar comment
@jessesquires has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
Summary: - Added `-[IGListAdapter visibleCellsForObject:]` API 1. Get all visible cells 2. Get the section for the object 3. Filter cells where indexPath.section of cell is equal to the object 4. Return - [x] All tests pass. Demo project builds and runs. - [x] I added test(s), an experiment, or detailed why my change isn't tested. - [ ] 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) ------------- Purely additive, can go in 2.2.0 release? Hold off on changelog until agreed. Wanting to learn some more ObjC so gave this a shot, would appreciate some nit picking/feedback Closes #437 Closes #442 Differential Revision: D4450259 Pulled By: jessesquires fbshipit-source-id: 521583c669fc1160212a7c46a50d62d951cafa2e
Changes in this pull request
-[IGListAdapter visibleCellsForObject:]
APIApproach
Pull request checklist
CHANGELOG.md
for any breaking changes, enhancements, or bug fixes.Purely additive, can go in 2.2.0 release? Hold off on changelog until agreed.
Wanting to learn some more ObjC so gave this a shot, would appreciate some nit picking/feedback
Closes #437