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

Layout invalidation API #499

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ This release closes the [3.0.0 milestone](https://github.com/Instagram/IGListKit

- You can now manually move items (cells) within a section controller, ex: `[self.collectionContext moveInSectionController:self fromIndex:0 toIndex:1]`. [Ryan Nystrom](https://github.com/rnystrom) [(#418)](https://github.com/Instagram/IGListKit/pull/418)

- Invalidate the layout of a section controller and control the transition with `UIView` animation APIs. [Ryan Nystrom](https://github.com/rnystrom) [(#499)](https://github.com/Instagram/IGListKit/pull/499)

### Fixes

- Gracefully handle a `nil` section controller returned by an `IGListAdapterDataSource`. [Ryan Nystrom](https://github.com/rnystrom) [(tbd)](https://github.com/Instagram/IGListKit/pull/tbd)
Expand Down
2 changes: 1 addition & 1 deletion Source/IGListAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ - (void)scrollToSectionController:(IGListSectionController<IGListSectionType> *)
}

- (void)invalidateLayoutForSectionController:(IGListSectionController<IGListSectionType> *)sectionController
completion:(void (^)(BOOL))completion{
completion:(void (^)(BOOL finished))completion{
const NSInteger section = [self sectionForSectionController:sectionController];
const NSInteger items = [_collectionView numberOfItemsInSection:section];

Expand Down
2 changes: 1 addition & 1 deletion Source/IGListCollectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ NS_ASSUME_NONNULL_BEGIN
without animations, or use springs.
*/
- (void)invalidateLayoutForSectionController:(IGListSectionController<IGListSectionType> *)sectionController
completion:(nullable void (^)(BOOL))completion;
completion:(nullable void (^)(BOOL finished))completion;

/**
Batches and performs many cell-level updates in a single transaction.
Expand Down
4 changes: 4 additions & 0 deletions Source/IGListStackedSectionController.m
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ - (void)scrollToSectionController:(IGListSectionController<IGListSectionType> *)
animated:animated];
}

- (void)invalidateLayoutForSectionController:(IGListSectionController<IGListSectionType> *)sectionController completion:(void (^)(BOOL))completion {
[self.collectionContext invalidateLayoutForSectionController:self completion:completion];
}

#pragma mark - IGListDisplayDelegate

- (void)listAdapter:(IGListAdapter *)listAdapter willDisplaySectionController:(IGListSectionController<IGListSectionType> *)sectionController cell:(UICollectionViewCell *)cell atIndex:(NSInteger)index {
Expand Down
28 changes: 28 additions & 0 deletions Tests/IGListAdapterE2ETests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1337,4 +1337,32 @@ - (void)test_whenMovingItems_withNoBatchUpdate_thatCollectionViewWorks {
XCTAssertEqualObjects(movedCell2.label.text, @"foo");
}

- (void)test_whenInvalidatingSectionController_withSizeChange_thatCellsAreSameInstance_thatCellsFrameChanged {
[self setupWithObjects:@[
genTestObject(@1, @2),
]];

NSIndexPath *path1 = [NSIndexPath indexPathForItem:0 inSection:0];
NSIndexPath *path2 = [NSIndexPath indexPathForItem:1 inSection:0];
IGTestCell *cell1 = (IGTestCell*)[self.collectionView cellForItemAtIndexPath:path1];
IGTestCell *cell2 = (IGTestCell*)[self.collectionView cellForItemAtIndexPath:path2];

XCTAssertEqual(cell1.frame.size.height, 10);
XCTAssertEqual(cell2.frame.size.height, 10);

IGTestDelegateController *section = [self.adapter sectionControllerForObject:self.dataSource.objects.lastObject];
section.height = 20.0;

XCTestExpectation *expectation = genExpectation;
[section.collectionContext invalidateLayoutForSectionController:section completion:^(BOOL finished) {
XCTAssertEqual(cell1, [self.collectionView cellForItemAtIndexPath:path1]);
XCTAssertEqual(cell2, [self.collectionView cellForItemAtIndexPath:path2]);
XCTAssertEqual(cell1.frame.size.height, 20);
XCTAssertEqual(cell2.frame.size.height, 20);
[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:15 handler:nil];
}

@end
2 changes: 2 additions & 0 deletions Tests/Objects/IGTestDelegateController.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

@property (nonatomic, strong, readonly) IGTestObject *item;

@property (nonatomic, assign) CGFloat height;

@property (nonatomic, copy) void (^itemUpdateBlock)();
@property (nonatomic, copy) void (^cellConfigureBlock)(IGTestDelegateController *);
@property (nonatomic, assign, readonly) NSInteger updateCount;
Expand Down
3 changes: 2 additions & 1 deletion Tests/Objects/IGTestDelegateController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ - (instancetype)init {
if (self = [super init]) {
_willDisplayCellIndexes = [NSCountedSet new];
_didEndDisplayCellIndexes = [NSCountedSet new];
_height = 10.0;
self.workingRangeDelegate = self;
}
return self;
Expand All @@ -31,7 +32,7 @@ - (NSInteger)numberOfItems {
}

- (CGSize)sizeForItemAtIndex:(NSInteger)index {
return CGSizeMake(self.collectionContext.containerSize.width, 10);
return CGSizeMake(self.collectionContext.containerSize.width, self.height);
}

- (UICollectionViewCell *)cellForItemAtIndex:(NSInteger)index {
Expand Down