mirror of
https://github.com/Instagram/IGListKit
synced 2026-05-23 09:18:29 +00:00
Fix - Cell Reordering in IGListBindingSectionController (viewModels array not being updated) (#1274)
Summary: - See #1262 (cell reordering using a binding section controller caused problems) ## Changes in this pull request - Added method override in `IGListBindingSectionController` to update the internal `viewModels` array after a cell has been moved - Subclasses of IGListBindingSectionController require to call super Issue fixed: #1262 I've added a test for this change. Is this enough? Or should I add more corner cases or something? ### Checklist Some tests fail, but they do seem unrelated. I don't know if this is normal. I haven't changed anything regarding those failing tests. - [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/1274 Reviewed By: candance Differential Revision: D15592822 Pulled By: lorixx fbshipit-source-id: d81c18bec0ad0a6a6613089245352bb182a92f26
This commit is contained in:
parent
78cca5bc43
commit
0ac855df99
4 changed files with 37 additions and 0 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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]];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue