Skip to content
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

Add APIs for invalidating single and many items in section controller #443

Closed
wants to merge 4 commits into from

Conversation

Sherlouk
Copy link
Contributor

Changes in this pull request

  • Added -[IGListCollectionContext invalidateLayoutForIndexPaths]
  • Added -[IGListCollectionContext invalidateLayoutForSectionController]

Unsure the best way to test these new methods, so a suggestion would be welcomed!

Closes #360

Pull request checklist

  • All tests pass. Demo project builds and runs.
  • I added tests, 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.
  • I have reviewed the contributing guide

@facebook-github-bot
Copy link
Contributor

@Sherlouk updated the pull request - view changes

@facebook-github-bot
Copy link
Contributor

@Sherlouk updated the pull request - view changes

@Sherlouk
Copy link
Contributor Author

Was building against macOS scheme so Xcode wasn't flagging errors, should be fixed now.

Copy link
Contributor

@rnystrom rnystrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sherlouk super cool! This is going to be so useful.

Unsure the best way to test these new methods, so a suggestion would be welcomed!

These should probably go in IGListAdapterTests and IGListStackSectionControllerTests.

For the tests, we'll want to:

  • Be able to control what is returned in a test section controller's sizeForItem:
  • Lay everything out and assert the size is correct
  • Get the section controller and update the size
  • Call invalidate
  • Assert the sizes are correct

Something along these lines:

self.dataSource.objects = @[@1, @2];
[self.adapter reloadDataWithCompletion:nil];

UICollectionViewCell *cell = [self.colectionView cellForItemAtIndexpath:[NSIndexPath indexPathForItem:0 section:0]];
XCTAssertEqual(cell.frame.size.width, 100);
XCTAssertEqual(cell.frame.size.height, 10);

IGListSectionController<IGListSectionType> *controller = [self.adapter sectionControllerForObject:@1];
controller.customHeight = 30;

[controller.collectionContext invalidateLayoutForSectionController:controller];

UICollectionViewCell *cell = [self.colectionView cellForItemAtIndexpath:[NSIndexPath indexPathForItem:0 section:0]];
XCTAssertEqual(cell.frame.size.width, 100);
XCTAssertEqual(cell.frame.size.height, 30);

We don't have any test section controller's w/ variable sizes though, so it might require some work. We could do w/ more centralized test objects, section controllers, etc.


@param indexPaths The index path array to invalidate.
*/
- (void)invalidateLayoutForIndexPaths:(NSArray<NSIndexPath *> *) indexPaths;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since section controllers shouldn't know about index paths, can we change this to:

- (void)invalidateLayoutForSectionController:(IGListSectionController<IGListSectionType> *)sectionController indexes:(NSIndexSet *)indexes;

I wonder, maybe we should just have a single API for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will reduce down to just a single API, if a user wants to reset the whole section then it's not that difficult to create an index set for the entire range

IGAssertMainThread();
IGParameterAssert(sectionController != nil);

NSInteger itemCount = [sectionController numberOfItems];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: const

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't mind adding this, but for the sake of learning -- Is putting const here the same as using 'let' in Swift? "A non mutable version"

Copy link
Contributor

@rnystrom rnystrom Jan 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! Though its only going to stop from writing these primitives. It's not like you can do

const UILabel *label = [UILabel new];
label.text = @"foo";

Swift works basically the same w/ class.

const also works w/ structs in ObjC too, so you can't do

const CGRect frame = CGRectMake(0, 0, 100, 10);
frame.size.height = 20; // compiler error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the need for this variable by having a single API, though thanks for explaining it to me!


UICollectionViewLayoutInvalidationContext *context = [UICollectionViewLayoutInvalidationContext new];
[context invalidateItemsAtIndexPaths:indexPaths];
[self.collectionView.collectionViewLayout invalidateLayoutWithContext:context];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting to myself that this is an iOS 8+ API, so we're covered w/ our min API

@facebook-github-bot
Copy link
Contributor

@Sherlouk updated the pull request - view changes

@Sherlouk
Copy link
Contributor Author

Will work on the tests ASAP, as you say I think a bit more work is needed just to create a different sort of test object that is usable for more complicated tests.

Amateur hour over here, but fun nonetheless!

@rnystrom
Copy link
Contributor

@Sherlouk no hurry on this 😄 we don't need to bundle it w/ 2.2, take your time.

fun nonetheless!

🙌

@Sherlouk
Copy link
Contributor Author

Going to close this PR for the time being until I get a chance to work on this piece, will reopen then!

@Sherlouk Sherlouk closed this Jan 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IGListCollectionContext API to invalidate layout of a section controller
3 participants