diff --git a/CHANGELOG.md b/CHANGELOG.md index d1f42e62..4b70d703 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ The changelog for `IGListKit`. Also see the [releases](https://github.com/instag - Removed `nibName` argument from `IGListReusableViewIdentifier`. [Trung Duc](https://github.com/trungducc) [(#1223)](https://github.com/Instagram/IGListKit/issues/1223) - Fixed crash when using `-[IGListCollectionContext dequeueReusableCellOfClass:withReuseIdentifier:forSectionController:atIndex:]` [Jeremy Lawrence](https://github.com/ziewvater) (tbd) +- Added missing method override to `IGListBindingSectionController` that updates the internal `viewModels` array after moving a cell. [Dennis Müller](https://github.com/d3mueller) (see [#1262](https://github.com/Instagram/IGListKit/issues/1262)) - Fixed logic flaw in `[IGListCollectionViewLayout shouldInvalidateLayoutForBoundsChange:]`. [Allen Hsu](https://github.com/allenhsu) (tbd) diff --git a/Source/IGListBindingSectionController.h b/Source/IGListBindingSectionController.h index 969f62de..17edd11b 100644 --- a/Source/IGListBindingSectionController.h +++ b/Source/IGListBindingSectionController.h @@ -77,6 +77,16 @@ NS_SWIFT_NAME(ListBindingSectionController) */ - (void)updateAnimated:(BOOL)animated completion:(nullable void (^)(BOOL updated))completion; +/** + Notifies the section that a list object should move within a section as the result of interactive reordering. + + @param sourceIndex The starting index of the object. + @param destinationIndex The ending index of the object. + + @note this method must be implemented if interactive reordering is enabled. To ensure updating the internal viewModels array, **calling super is required**, preferably before your own implementation. + */ +- (void)moveObjectFromIndex:(NSInteger)sourceIndex toIndex:(NSInteger)destinationIndex NS_REQUIRES_SUPER; + @end NS_ASSUME_NONNULL_END diff --git a/Source/IGListBindingSectionController.m b/Source/IGListBindingSectionController.m index 50149cbb..d18c5844 100644 --- a/Source/IGListBindingSectionController.m +++ b/Source/IGListBindingSectionController.m @@ -127,6 +127,16 @@ typedef NS_ENUM(NSInteger, IGListDiffingSectionState) { } } +- (void)moveObjectFromIndex:(NSInteger)sourceIndex toIndex:(NSInteger)destinationIndex { + NSMutableArray *viewModels = [self.viewModels mutableCopy]; + + id modelAtSource = [viewModels objectAtIndex:sourceIndex]; + [viewModels removeObjectAtIndex:sourceIndex]; + [viewModels insertObject:modelAtSource atIndex:destinationIndex]; + + self.viewModels = viewModels; +} + - (void)didSelectItemAtIndex:(NSInteger)index { [self.selectionDelegate sectionController:self didSelectItemAtIndex:index viewModel:self.viewModels[index]]; } diff --git a/Tests/IGListBindingSectionControllerTests.m b/Tests/IGListBindingSectionControllerTests.m index a11735bc..37c99975 100644 --- a/Tests/IGListBindingSectionControllerTests.m +++ b/Tests/IGListBindingSectionControllerTests.m @@ -465,5 +465,21 @@ [self waitForExpectationsWithTimeout:30 handler:nil]; } +- (void)test_viewModelsUpdate_afterCellHasBeenMoved { + [self setupWithObjects:@[ + [[IGTestDiffingObject alloc] initWithKey:@1 objects:@[@7, @"seven", @20]], + ]]; + + IGTestDiffingSectionController *section = [self.adapter sectionControllerForObject:self.dataSource.objects.firstObject]; + + [section moveObjectFromIndex:0 toIndex:2]; + XCTAssertEqual([section.viewModels firstObject], @"seven"); + XCTAssertEqual([section.viewModels lastObject], @7); + + [section moveObjectFromIndex:2 toIndex:1]; + XCTAssertEqual([section.viewModels objectAtIndex: 1], @7); + XCTAssertEqual([section.viewModels lastObject], @20); +} + @end