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

Fix for #378 - emptyView not updated when items added #395

Closed
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The changelog for `IGListKit`. Also see the [releases](https://github.com/instagram/IGListKit/releases) on GitHub.

### Enhancements
Copy link
Contributor

Choose a reason for hiding this comment

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

Just for the sake of completion we should add a header for 2.2.0 with a link to the milestone

Also can you add a link to this pull request at the end of your contribution, following the same format as other changelog entries

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing, CHANGELOG.md updated.


### Fixes

- Fix bug where emptyView's hidden status is not updated after the number of items is changed with `insertInSectionController:atIndexes:` or related methods [Peter Edmonston](https://github.com/edmonston)

2.1.0
-----

Expand Down
31 changes: 27 additions & 4 deletions Source/IGListAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,13 @@ - (void)updateObjects:(NSArray *)objects dataSource:(id<IGListAdapterDataSource>
itemCount += [sectionController numberOfItems];
}

[self updateBackgroundViewWithItemCount:itemCount];
[self updateBackgroundViewShouldHide:itemCount > 0];
}

- (void)updateBackgroundViewWithItemCount:(NSUInteger)itemCount {
- (void)updateBackgroundViewShouldHide:(BOOL)shouldHide {
if (self.isInUpdateBlock) {
return; // will be called again when update block completes
}
UIView *backgroundView = [self.dataSource emptyViewForListAdapter:self];
// don't do anything if the client is using the same view
if (backgroundView != _collectionView.backgroundView) {
Expand All @@ -516,7 +519,18 @@ - (void)updateBackgroundViewWithItemCount:(NSUInteger)itemCount {
[_collectionView.backgroundView removeFromSuperview];
_collectionView.backgroundView = backgroundView;
}
_collectionView.backgroundView.hidden = itemCount > 0;
_collectionView.backgroundView.hidden = shouldHide;
}

- (BOOL)itemCountIsZero {
__block BOOL isZero = YES;
[self.sectionMap enumerateUsingBlock:^(id _Nonnull object, IGListSectionController<IGListSectionType> * _Nonnull sectionController, NSInteger section, BOOL * _Nonnull stop) {
Copy link
Contributor

Choose a reason for hiding this comment

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

minor nit: could we specify an explicit type for id __Nonnull object ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm, can we? it seems like id is the best we can do based on the signature of [IGListSectionMap enumerateUsingBlock:], but quite possibly I am missing something...

- (void)enumerateUsingBlock:(void (^)(id object, IGListSectionController<IGListSectionType> *sectionController, NSInteger section, BOOL *stop))block;

https://github.com/Instagram/IGListKit/blob/master/Source/Internal/IGListSectionMap.h#L103

Copy link
Contributor

Choose a reason for hiding this comment

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

oops! 😳 nevermind. 😄

if (sectionController.numberOfItems > 0) {
isZero = NO;
*stop = YES;
}
}];
return isZero;
}

- (IGListSectionMap *)sectionMapAdjustForUpdateBlock:(BOOL)adjustForUpdateBlock {
Expand Down Expand Up @@ -918,6 +932,7 @@ - (void)reloadInSectionController:(IGListSectionController<IGListSectionType> *)
} else {
NSArray *indexPaths = [self indexPathsFromSectionController:sectionController indexes:indexes adjustForUpdateBlock:YES];
[self.updater reloadItemsInCollectionView:collectionView indexPaths:indexPaths];
[self updateBackgroundViewShouldHide:![self itemCountIsZero]];
}
}

Expand All @@ -934,6 +949,7 @@ - (void)insertInSectionController:(IGListSectionController<IGListSectionType> *)

NSArray *indexPaths = [self indexPathsFromSectionController:sectionController indexes:indexes adjustForUpdateBlock:NO];
[self.updater insertItemsIntoCollectionView:collectionView indexPaths:indexPaths];
[self updateBackgroundViewShouldHide:![self itemCountIsZero]];
}

- (void)deleteInSectionController:(IGListSectionController<IGListSectionType> *)sectionController atIndexes:(NSIndexSet *)indexes {
Expand All @@ -949,6 +965,7 @@ - (void)deleteInSectionController:(IGListSectionController<IGListSectionType> *)

NSArray *indexPaths = [self indexPathsFromSectionController:sectionController indexes:indexes adjustForUpdateBlock:YES];
[self.updater deleteItemsFromCollectionView:collectionView indexPaths:indexPaths];
[self updateBackgroundViewShouldHide:![self itemCountIsZero]];
}

- (void)reloadSectionController:(IGListSectionController <IGListSectionType> *)sectionController {
Expand All @@ -965,6 +982,7 @@ - (void)reloadSectionController:(IGListSectionController <IGListSectionType> *)s

NSIndexSet *sections = [NSIndexSet indexSetWithIndex:section];
[self.updater reloadCollectionView:collectionView sections:sections];
[self updateBackgroundViewShouldHide:![self itemCountIsZero]];
}

- (void)performBatchAnimated:(BOOL)animated updates:(void (^)())updates completion:(void (^)(BOOL))completion {
Expand All @@ -978,7 +996,12 @@ - (void)performBatchAnimated:(BOOL)animated updates:(void (^)())updates completi
weakSelf.isInUpdateBlock = YES;
updates();
weakSelf.isInUpdateBlock = NO;
} completion:completion];
} completion: ^(BOOL finished) {
[weakSelf updateBackgroundViewShouldHide:![weakSelf itemCountIsZero]];
if (completion) {
completion(finished);
}
}];
}

- (void)scrollToSectionController:(IGListSectionController<IGListSectionType> *)sectionController
Expand Down
54 changes: 54 additions & 0 deletions Tests/IGListAdapterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,60 @@ - (void)test_whenDataSourceAddsItems_thatEmptyViewBecomesVisible {
XCTAssertTrue(self.collectionView.backgroundView.hidden);
}

- (void)test_whenInsertingIntoEmptySection_thatEmptyViewBecomesHidden {
self.dataSource.objects = @[@0];
self.dataSource.backgroundView = [UIView new];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertFalse(self.collectionView.backgroundView.hidden);
IGListTestSection *sectionController = [self.adapter sectionControllerForObject:@(0)];
sectionController.items = 1;
[self.adapter insertInSectionController:sectionController atIndexes:[NSIndexSet indexSetWithIndex:0]];
XCTAssertTrue(self.collectionView.backgroundView.hidden);
}

- (void)test_whenDeletingAllItemsFromSection_thatEmptyViewBecomesVisible {
self.dataSource.objects = @[@1];
self.dataSource.backgroundView = [UIView new];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertTrue(self.collectionView.backgroundView.hidden);
IGListTestSection *sectionController = [self.adapter sectionControllerForObject:@(1)];
sectionController.items = 0;
[self.adapter deleteInSectionController:sectionController atIndexes:[NSIndexSet indexSetWithIndex:0]];
XCTAssertFalse(self.collectionView.backgroundView.hidden);
}

- (void)test_whenEmptySectionAddsItems_thatEmptyViewBecomesHidden {
self.dataSource.objects = @[@0];
self.dataSource.backgroundView = [UIView new];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertFalse(self.collectionView.backgroundView.hidden);
IGListTestSection *sectionController = [self.adapter sectionControllerForObject:@(0)];
sectionController.items = 2;
[self.adapter reloadSectionController:sectionController];
XCTAssertTrue(self.collectionView.backgroundView.hidden);
}

- (void)test_whenSectionItemsAreDeletedAsBatch_thatEmptyViewBecomesVisible {
self.dataSource.objects = @[@1, @2];
self.dataSource.backgroundView = [UIView new];
[self.adapter reloadDataWithCompletion:nil];
XCTAssertTrue(self.collectionView.backgroundView.hidden);
IGListTestSection *firstSectionController = [self.adapter sectionControllerForObject:@(1)];
IGListTestSection *secondSectionController = [self.adapter sectionControllerForObject:@(2)];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.adapter performBatchAnimated:YES updates:^{
firstSectionController.items = 0;
[self.adapter deleteInSectionController:firstSectionController atIndexes:[NSIndexSet indexSetWithIndex:0]];
secondSectionController.items = 0;
NSIndexSet *indexesToDelete = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)];
[self.adapter deleteInSectionController:secondSectionController atIndexes:indexesToDelete];
} completion:^(BOOL finished) {
XCTAssertFalse(self.collectionView.backgroundView.hidden);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:15 handler:nil];
}

- (void)test_whenScrollViewDelegateSet_thatDelegateReceivesEvents {
id mockDelegate = [OCMockObject mockForProtocol:@protocol(UIScrollViewDelegate)];

Expand Down