From 90f2f77ee2cf8d78dfb5c7943bad89a7bab82ad9 Mon Sep 17 00:00:00 2001 From: Ofir Gluzman Date: Tue, 18 Dec 2018 16:29:28 +0200 Subject: [PATCH] Fix inconsistent state which can be caused in UICollectionViewLayout+InteractiveReordering. --- ...llectionViewLayout+InteractiveReordering.m | 8 +++++ Tests/IGListAdapterTests.m | 30 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/Source/Internal/UICollectionViewLayout+InteractiveReordering.m b/Source/Internal/UICollectionViewLayout+InteractiveReordering.m index 5dbbb7235..d868d09b6 100644 --- a/Source/Internal/UICollectionViewLayout+InteractiveReordering.m +++ b/Source/Internal/UICollectionViewLayout+InteractiveReordering.m @@ -104,6 +104,8 @@ - (nullable NSIndexPath *)updatedTargetForInteractivelyMovingItem:(NSIndexPath * if (destinationSectionIndex < [[adapter objects] count] - 1) { destinationSectionIndex += 1; destinationItemIndex = 0; + + adapter.isLastInteractiveMoveToLastSectionIndex = NO; } else { // if we're moving an item to the last spot, our index would exceed the number of sections available @@ -114,6 +116,12 @@ - (nullable NSIndexPath *)updatedTargetForInteractivelyMovingItem:(NSIndexPath * inSection:destinationSectionIndex]; return updatedTarget; } + else { + adapter.isLastInteractiveMoveToLastSectionIndex = NO; + } + } + else { + adapter.isLastInteractiveMoveToLastSectionIndex = NO; } return nil; diff --git a/Tests/IGListAdapterTests.m b/Tests/IGListAdapterTests.m index 18a38e354..bb65d7bc9 100644 --- a/Tests/IGListAdapterTests.m +++ b/Tests/IGListAdapterTests.m @@ -1699,4 +1699,34 @@ - (void)test_whenItemsAreInteractivelyReorderedAcrossSections_thatIndexesRevertT XCTAssertEqual(section1Objects[2], section1.sectionObject.objects[2]); } +- (void)test_whenSingleItemInSectionIsInteractivelyReorderedThorughLastSpot_indexesUpdateCorrectly { + IGListTestAdapterReorderingDataSource *dataSource = [IGListTestAdapterReorderingDataSource new]; + dataSource.objects = @[@0, @1, @2]; + self.adapter.dataSource = dataSource; + self.adapter.moveDelegate = dataSource; + + IGTestReorderableSection *section0 = (IGTestReorderableSection *)[self.adapter sectionControllerForSection:0]; + IGTestReorderableSection *section1 = (IGTestReorderableSection *)[self.adapter sectionControllerForSection:1]; + IGTestReorderableSection *section2 = (IGTestReorderableSection *)[self.adapter sectionControllerForSection:2]; + section0.sectionObject = [IGTestReorderableSectionObject sectionWithObjects:@[@0]]; + section0.isReorderable = YES; + section1.sectionObject = [IGTestReorderableSectionObject sectionWithObjects:@[@0]]; + section2.sectionObject = [IGTestReorderableSectionObject sectionWithObjects:@[@0]]; + + [self.adapter performUpdatesAnimated:NO completion:nil]; + + NSIndexPath *fromIndexPath = [NSIndexPath indexPathForItem:0 inSection:0]; + NSIndexPath *lastSpotIndexPath = [NSIndexPath indexPathForItem:1 inSection:2]; + NSIndexPath *toIndexPath = [NSIndexPath indexPathForItem:1 inSection:1]; + + // move the first section item to the middle while simulating dragging to the last spot and back. + NSIndexPath *interpretedPath = [self interpretedIndexPathFromIndexPath:fromIndexPath toIndexPath:lastSpotIndexPath]; + interpretedPath = [self interpretedIndexPathFromIndexPath:interpretedPath toIndexPath:toIndexPath]; + [self.adapter collectionView:self.collectionView moveItemAtIndexPath:fromIndexPath toIndexPath:interpretedPath]; + + XCTAssertEqual(section0, [self.adapter sectionControllerForSection:1]); + XCTAssertEqual(section1, [self.adapter sectionControllerForSection:0]); + XCTAssertEqual(section2, [self.adapter sectionControllerForSection:2]); +} + @end