From 67da3b4f5f74ceb97ad2de6855ebfdbdb5392bc4 Mon Sep 17 00:00:00 2001 From: Bofei Zhu Date: Wed, 25 Jul 2018 14:15:28 -0700 Subject: [PATCH] Add missed delegate call #trivial (#1211) Summary: Issue fixed: # - [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) Pull Request resolved: https://github.com/Instagram/IGListKit/pull/1211 Differential Revision: D9003392 Pulled By: rnystrom fbshipit-source-id: 73ef837300f8fdb6d9a6005e86f8e9e842827979 --- CHANGELOG.md | 3 ++- Source/IGListAdapterUpdater.m | 1 + Tests/IGListAdapterUpdaterTests.m | 40 +++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1f52e968..c82063b4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,11 +16,12 @@ The changelog for `IGListKit`. Also see the [releases](https://github.com/instag - Experimental fix to get the `UICollectionView` for batch updating immediately before applying the update. [Ryan Nystrom](https://github.com/rnystrom) (tbd) - - `[IGListAdapterUpdater performBatchUpdatesWithCollectionViewBlock:]` and `[IGListAdapterUpdater performReloadDataWithCollectionViewBlock:]` clean state and run completion blocks if their `UICollectionView` is nil. [Brandon Darin](https://github.com/jbd1030) (tbd) - Ensuring view models with duplicate diff identifiers are removed when view models are first requested by `IGListBindingSectionController` [Adam Stern](https://github.com/adamastern) (tbd) +- Fixed `[IGListAdapterUpdater reloadItemInCollectionView:fromIndexPath:toIndexPath:]` does not call delegate when not inside a batch update. [Bofei Zhu] (https://github.com/zhubofei) [(#1211)](https://github.com/Instagram/IGListKit/pull/1211) + - Log instead of assert for duplicate diff identifiers to make code testable. [Adam Stern](https://github.com/adamastern) (tbd) 3.4.0 diff --git a/Source/IGListAdapterUpdater.m b/Source/IGListAdapterUpdater.m index 19c42afaf..90e8be4ea 100644 --- a/Source/IGListAdapterUpdater.m +++ b/Source/IGListAdapterUpdater.m @@ -534,6 +534,7 @@ - (void)reloadItemInCollectionView:(UICollectionView *)collectionView IGListReloadIndexPath *reload = [[IGListReloadIndexPath alloc] initWithFromIndexPath:fromIndexPath toIndexPath:toIndexPath]; [self.batchUpdates.itemReloads addObject:reload]; } else { + [self.delegate listAdapterUpdater:self willReloadIndexPaths:@[fromIndexPath] collectionView:collectionView]; [collectionView reloadItemsAtIndexPaths:@[fromIndexPath]]; } } diff --git a/Tests/IGListAdapterUpdaterTests.m b/Tests/IGListAdapterUpdaterTests.m index b32659e84..3e4c476d0 100644 --- a/Tests/IGListAdapterUpdaterTests.m +++ b/Tests/IGListAdapterUpdaterTests.m @@ -602,6 +602,46 @@ - (void)test_whenNotInViewHierarchy_thatUpdatesStillExecuteBlocks { [self waitForExpectationsWithTimeout:30 handler:nil]; } +- (void)test_whenNotBatchUpdate_thatDelegateEventsSent { + IGSectionObject *object = [IGSectionObject sectionWithObjects:@[@0, @1, @2]]; + self.dataSource.sections = @[object]; + [self.collectionView reloadData]; + + id mockDelegate = [OCMockObject niceMockForProtocol:@protocol(IGListAdapterUpdaterDelegate)]; + self.updater.delegate = mockDelegate; + [mockDelegate setExpectationOrderMatters:YES]; + [[mockDelegate expect] listAdapterUpdater:self.updater willDeleteIndexPaths:OCMOCK_ANY collectionView:self.collectionView]; + [[mockDelegate expect] listAdapterUpdater:self.updater willInsertIndexPaths:OCMOCK_ANY collectionView:self.collectionView]; + [[mockDelegate expect] listAdapterUpdater:self.updater + willMoveFromIndexPath:OCMOCK_ANY + toIndexPath:OCMOCK_ANY + collectionView:self.collectionView]; + [[mockDelegate expect] listAdapterUpdater:self.updater willReloadIndexPaths:OCMOCK_ANY collectionView:self.collectionView]; + + // This code is of no use, but it will let UICollectionView synchronize number of items, + // so it will not crash in following updates. https://stackoverflow.com/a/46751421/2977647 + [self.collectionView numberOfItemsInSection:0]; + + object.objects = @[@1, @2]; + [self.updater deleteItemsFromCollectionView:self.collectionView indexPaths:@[ + [NSIndexPath indexPathForItem:0 inSection:0], + ]]; + object.objects = @[@1, @2, @4, @5]; + [self.updater insertItemsIntoCollectionView:self.collectionView indexPaths:@[ + [NSIndexPath indexPathForItem:2 inSection:0], + [NSIndexPath indexPathForItem:3 inSection:0], + ]]; + object.objects = @[@2, @1, @4, @5]; + [self.updater moveItemInCollectionView:self.collectionView + fromIndexPath:[NSIndexPath indexPathForItem:2 inSection:0] + toIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; + + [self.updater reloadItemInCollectionView:self.collectionView + fromIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] + toIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; + [mockDelegate verify]; +} + - (void)test_whenObjectIdentifiersCollide_withDifferentTypes_thatLookupReturnsNil { id testObject = [[IGTestObject alloc] initWithKey:@"foo" value:@"bar"]; id collision = @"foo";