Skip to content

Commit

Permalink
Fix inconsistent state which can be caused in UICollectionViewLayout+…
Browse files Browse the repository at this point in the history
…InteractiveReordering.
  • Loading branch information
ofirgluzman committed Dec 18, 2018
1 parent 4387a48 commit 43d24ba
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ The changelog for `IGListKit`. Also see the [releases](https://github.com/instag

- Fixed binding section controllers failing to update their cells when the section controller's section changes. [Chrisna Aing](https://github.com/ccrazy88) [(#1144)](https://github.com/Instagram/IGListKit/pull/1144)

- Fixed a bug caused when applying interactive reordering on a single section item while dragging it through the last spot of the collection view and back to some (non-last) target position. [Ofir Gluzman](https://github.com/ofirgluzman) [#1289](https://github.com/Instagram/IGListKit/pull/1289)

3.2.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -114,6 +116,12 @@ - (nullable NSIndexPath *)updatedTargetForInteractivelyMovingItem:(NSIndexPath *
inSection:destinationSectionIndex];
return updatedTarget;
}
else {
adapter.isLastInteractiveMoveToLastSectionIndex = NO;
}
}
else {
adapter.isLastInteractiveMoveToLastSectionIndex = NO;
}

return nil;
Expand Down
30 changes: 30 additions & 0 deletions Tests/IGListAdapterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 43d24ba

Please sign in to comment.