Add support for collection view reordering as well

This commit is contained in:
Adlai Holler
2016-09-12 10:56:55 -07:00
parent af1baa6394
commit 1adbb3e5e0
3 changed files with 30 additions and 0 deletions

View File

@@ -153,6 +153,8 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
unsigned int asyncDataSourceNodeForItemAtIndexPath:1;
unsigned int asyncDataSourceNodeBlockForItemAtIndexPath:1;
unsigned int asyncDataSourceNumberOfSectionsInCollectionView:1;
unsigned int asyncDataSourceCanMoveItemAtIndexPath:1;
unsigned int asyncDataSourceMoveItemAtIndexPath:1;
} _asyncDataSourceFlags;
struct {
@@ -354,6 +356,8 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
_asyncDataSourceFlags.asyncDataSourceNodeForItemAtIndexPath = [_asyncDataSource respondsToSelector:@selector(collectionView:nodeForItemAtIndexPath:)];
_asyncDataSourceFlags.asyncDataSourceNodeBlockForItemAtIndexPath = [_asyncDataSource respondsToSelector:@selector(collectionView:nodeBlockForItemAtIndexPath:)];
_asyncDataSourceFlags.asyncDataSourceNumberOfSectionsInCollectionView = [_asyncDataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)];
_asyncDataSourceFlags.asyncDataSourceCanMoveItemAtIndexPath = [_asyncDataSource respondsToSelector:@selector(collectionView:canMoveItemAtIndexPath:)];
_asyncDataSourceFlags.asyncDataSourceMoveItemAtIndexPath = [_asyncDelegate respondsToSelector:@selector(collectionView:moveItemAtIndexPath:toIndexPath:)];
// Data-source must implement collectionView:nodeForItemAtIndexPath: or collectionView:nodeBlockForItemAtIndexPath:
ASDisplayNodeAssertTrue(_asyncDataSourceFlags.asyncDataSourceNodeBlockForItemAtIndexPath || _asyncDataSourceFlags.asyncDataSourceNodeForItemAtIndexPath);
@@ -636,6 +640,24 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
return [_dataController numberOfRowsInSection:section];
}
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
if (_asyncDataSourceFlags.asyncDataSourceCanMoveItemAtIndexPath) {
return [_asyncDataSource collectionView:self canMoveItemAtIndexPath:indexPath];
} else {
return NO;
}
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath
{
if (_asyncDataSourceFlags.asyncDataSourceMoveItemAtIndexPath) {
[_asyncDataSource collectionView:self moveItemAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
// Move node after informing data source in case they call nodeAtIndexPath:
[_dataController moveCompletedNodeAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return [[_dataController nodeAtIndexPath:indexPath] calculatedSize];

View File

@@ -25,6 +25,10 @@ NS_ASSUME_NONNULL_BEGIN
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(nonnull NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath;
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
@end

View File

@@ -61,6 +61,10 @@
// handled by ASRangeController
selector == @selector(numberOfSectionsInCollectionView:) ||
selector == @selector(collectionView:numberOfItemsInSection:) ||
// reordering support
selector == @selector(collectionView:canMoveItemAtIndexPath:) ||
selector == @selector(collectionView:moveItemAtIndexPath:toIndexPath:) ||
// used for ASRangeController visibility updates
selector == @selector(collectionView:willDisplayCell:forItemAtIndexPath:) ||