From a6e2f8e5ab06804ae3bb69e3455e1ee03e8304cc Mon Sep 17 00:00:00 2001 From: Adlai Holler Date: Tue, 1 Nov 2016 15:03:46 -0700 Subject: [PATCH] Finish Porting indexPath-Oriented Methods to ASTableNode/ASCollectionNode (#2527) * Finish porting over the indexPath-oriented properties from table/collection view to node * Make indexPathForSelectedRow a property --- AsyncDisplayKit/ASCollectionNode.h | 11 +- AsyncDisplayKit/ASCollectionNode.mm | 7 ++ AsyncDisplayKit/ASCollectionView.h | 13 +- AsyncDisplayKit/ASCollectionView.mm | 17 +++ AsyncDisplayKit/ASTableNode.h | 10 +- AsyncDisplayKit/ASTableNode.mm | 8 ++ AsyncDisplayKit/ASTableView.h | 117 ++---------------- .../Details/ASCollectionInternal.h | 7 ++ .../Private/ASCollectionView+Undeprecated.h | 6 + .../Private/ASTableView+Undeprecated.h | 12 ++ 10 files changed, 89 insertions(+), 119 deletions(-) diff --git a/AsyncDisplayKit/ASCollectionNode.h b/AsyncDisplayKit/ASCollectionNode.h index a1e0d74f49..ef6e5e9c2a 100644 --- a/AsyncDisplayKit/ASCollectionNode.h +++ b/AsyncDisplayKit/ASCollectionNode.h @@ -299,6 +299,11 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - Selection +/** + * The index paths of the selected items, or @c nil if no items are selected. + */ +@property (nonatomic, readonly, nullable) NSArray *indexPathsForSelectedItems; + /** * Selects the item at the specified index path and optionally scrolls it into view. * If the `allowsSelection` property is NO, calling this method has no effect. If there is an existing selection with a different index path and the `allowsMultipleSelection` property is NO, calling this method replaces the previous selection. @@ -348,7 +353,7 @@ NS_ASSUME_NONNULL_BEGIN * * @return an array containing the nodes being displayed on screen. This must be called on the main thread. */ -@property(readonly, copy) NSArray<__kindof ASCellNode *> *visibleNodes; +@property (nonatomic, readonly) NSArray<__kindof ASCellNode *> *visibleNodes; /** * Retrieves the node for the item at the given index path. @@ -357,7 +362,7 @@ NS_ASSUME_NONNULL_BEGIN * * @return The node for the given item, or @c nil if no item exists at the specified path. */ -- (nullable ASCellNode *)nodeForItemAtIndexPath:(NSIndexPath *)indexPath AS_WARN_UNUSED_RESULT; +- (nullable __kindof ASCellNode *)nodeForItemAtIndexPath:(NSIndexPath *)indexPath AS_WARN_UNUSED_RESULT; /** * Retrieve the index path for the item with the given node. @@ -373,7 +378,7 @@ NS_ASSUME_NONNULL_BEGIN * * @return an array containing the index paths of all visible items. This must be called on the main thread. */ -- (NSArray<__kindof NSIndexPath *> *)indexPathsForVisibleItems AS_WARN_UNUSED_RESULT; +@property (nonatomic, readonly) NSArray *indexPathsForVisibleItems; /** * Retrieve the index path of the item at the given point. diff --git a/AsyncDisplayKit/ASCollectionNode.mm b/AsyncDisplayKit/ASCollectionNode.mm index 4525e590fa..5d0dfe66cd 100644 --- a/AsyncDisplayKit/ASCollectionNode.mm +++ b/AsyncDisplayKit/ASCollectionNode.mm @@ -319,6 +319,13 @@ #pragma mark - Selection +- (NSArray *)indexPathsForSelectedItems +{ + ASDisplayNodeAssertMainThread(); + ASCollectionView *view = self.view; + return [view convertIndexPathsToCollectionNode:view.indexPathsForSelectedItems]; +} + - (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition { ASDisplayNodeAssertMainThread(); diff --git a/AsyncDisplayKit/ASCollectionView.h b/AsyncDisplayKit/ASCollectionView.h index b6c0b73e5f..88a1aaadfa 100644 --- a/AsyncDisplayKit/ASCollectionView.h +++ b/AsyncDisplayKit/ASCollectionView.h @@ -205,17 +205,16 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeMode:(ASLayoutRangeMode)rangeMode rangeType:(ASLayoutRangeType)rangeType ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode method instead."); -/** - * Scrolls the collection to the given item. - * - * @param indexPath The index path of the item. - * @param scrollPosition Where the row should end up after the scroll. - * @param animated Whether the scroll should be animated or not. - */ +- (nullable __kindof UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode method instead."); + - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode method instead."); - (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode method instead."); +@property (nonatomic, readonly) NSArray *indexPathsForVisibleItems ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode property instead."); + +@property (nonatomic, readonly, nullable) NSArray *indexPathsForSelectedItems ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode property instead."); + /** * Perform a batch of updates asynchronously, optionally disabling all animations in the batch. This method must be called from the main thread. * The asyncDataSource must be updated to reflect the changes before the update block completes. diff --git a/AsyncDisplayKit/ASCollectionView.mm b/AsyncDisplayKit/ASCollectionView.mm index 969402d7ce..5835ed5a43 100644 --- a/AsyncDisplayKit/ASCollectionView.mm +++ b/AsyncDisplayKit/ASCollectionView.mm @@ -582,6 +582,23 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell"; } } +- (NSArray *)convertIndexPathsToCollectionNode:(NSArray *)indexPaths +{ + if (indexPaths == nil) { + return nil; + } + + NSMutableArray *indexPathsArray = [NSMutableArray arrayWithCapacity:indexPaths.count]; + + for (NSIndexPath *indexPathInView in indexPaths) { + NSIndexPath *indexPath = [self convertIndexPathToCollectionNode:indexPathInView]; + if (indexPath != nil) { + [indexPathsArray addObject:indexPath]; + } + } + return indexPathsArray; +} + - (ASCellNode *)supplementaryNodeForElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath { return [_dataController supplementaryNodeOfKind:elementKind atIndexPath:indexPath]; diff --git a/AsyncDisplayKit/ASTableNode.h b/AsyncDisplayKit/ASTableNode.h index 195677ce59..019a910953 100644 --- a/AsyncDisplayKit/ASTableNode.h +++ b/AsyncDisplayKit/ASTableNode.h @@ -312,12 +312,12 @@ NS_ASSUME_NONNULL_BEGIN * * @return an array containing the nodes being displayed on screen. This must be called on the main thread. */ -@property(readonly, copy) NSArray<__kindof ASCellNode *> *visibleNodes; +@property (nonatomic, readonly) NSArray<__kindof ASCellNode *> *visibleNodes; /** * Retrieves the node for the row at the given index path. */ -- (nullable ASCellNode *)nodeForRowAtIndexPath:(NSIndexPath *)indexPath AS_WARN_UNUSED_RESULT; +- (nullable __kindof ASCellNode *)nodeForRowAtIndexPath:(NSIndexPath *)indexPath AS_WARN_UNUSED_RESULT; /** * Similar to -indexPathForCell:. @@ -356,7 +356,7 @@ NS_ASSUME_NONNULL_BEGIN - (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath AS_WARN_UNUSED_RESULT; /** - * Similar to -[UITableView indexPathForSelectedRow] + * Similar to UITableView.indexPathForSelectedRow * * @return The value of this property is an index path identifying the row and section * indexes of the selected row, or nil if the index path is invalid. If there are multiple selections, @@ -365,7 +365,9 @@ NS_ASSUME_NONNULL_BEGIN * * @discussion This method must be called from the main thread. */ -- (nullable NSIndexPath *)indexPathForSelectedRow AS_WARN_UNUSED_RESULT; +@property (nonatomic, readonly, nullable) NSIndexPath *indexPathForSelectedRow; + +@property (nonatomic, readonly, nullable) NSArray *indexPathsForSelectedRows; /** * Similar to -[UITableView indexPathForRowAtPoint:] diff --git a/AsyncDisplayKit/ASTableNode.mm b/AsyncDisplayKit/ASTableNode.mm index bd52b31f06..64696edd96 100644 --- a/AsyncDisplayKit/ASTableNode.mm +++ b/AsyncDisplayKit/ASTableNode.mm @@ -460,6 +460,14 @@ ASEnvironmentCollectionTableSetEnvironmentState(_environmentStateLock) return indexPath; } +- (NSArray *)indexPathsForSelectedRows +{ + ASDisplayNodeAssertMainThread(); + ASTableView *tableView = self.view; + + return [tableView convertIndexPathsToTableNode:tableView.indexPathsForSelectedRows]; +} + - (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point { ASDisplayNodeAssertMainThread(); diff --git a/AsyncDisplayKit/ASTableView.h b/AsyncDisplayKit/ASTableView.h index f12978c052..9213a865d7 100644 --- a/AsyncDisplayKit/ASTableView.h +++ b/AsyncDisplayKit/ASTableView.h @@ -124,17 +124,22 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeMode:(ASLayoutRangeMode)rangeMode rangeType:(ASLayoutRangeType)rangeType ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); -/** - * Scrolls the table to the given row. - * - * @param indexPath The index path of the row. - * @param scrollPosition Where the row should end up after the scroll. - * @param animated Whether the scroll should be animated or not. - */ +- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); + - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); +@property (nonatomic, readonly, nullable) NSIndexPath *indexPathForSelectedRow ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode property instead."); + +@property (nonatomic, readonly, nullable) NSArray *indexPathsForSelectedRows ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode property instead."); + +@property (nonatomic, readonly, nullable) NSArray *indexPathsForVisibleRows ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode property instead."); + +- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); + +- (nullable NSArray *)indexPathsForRowsInRect:(CGRect)rect ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); + /** * Similar to -visibleCells. * @@ -182,26 +187,8 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)relayoutItems ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); -/** - * Begins a series of method calls that insert, delete, select, or reload rows and sections of the table view, with animation enabled and no completion block. - * - * @discussion You call this method to bracket a series of method calls that ends with endUpdates and that consists of operations - * to insert, delete, select, and reload rows and sections of the table view. When you call endUpdates, ASTableView begins animating - * the operations simultaneously. It's important to remember that the ASTableView will be processing the updates asynchronously after this call is completed. - * - * @warning This method must be called from the main thread. - */ - (void)beginUpdates ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode's -performBatchUpdates:completion: instead."); -/** - * Concludes a series of method calls that insert, delete, select, or reload rows and sections of the table view, with animation enabled and no completion block. - * - * @discussion You call this method to bracket a series of method calls that begins with beginUpdates and that consists of operations - * to insert, delete, select, and reload rows and sections of the table view. When you call endUpdates, ASTableView begins animating - * the operations simultaneously. It's important to remember that the ASTableView will be processing the updates asynchronously after this call is completed. - * - * @warning This method is must be called from the main thread. - */ - (void)endUpdates ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode's -performBatchUpdates:completion: instead."); /** @@ -224,100 +211,20 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)waitUntilAllUpdatesAreCommitted ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); -/** - * Inserts one or more sections, with an option to animate the insertion. - * - * @param sections An index set that specifies the sections to insert. - * - * @param animation A constant that indicates how the insertion is to be animated. See UITableViewRowAnimation. - * - * @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes - * before this method is called. - */ - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); -/** - * Deletes one or more sections, with an option to animate the deletion. - * - * @param sections An index set that specifies the sections to delete. - * - * @param animation A constant that indicates how the deletion is to be animated. See UITableViewRowAnimation. - * - * @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes - * before this method is called. - */ - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); -/** - * Reloads the specified sections using a given animation effect. - * - * @param sections An index set that specifies the sections to reload. - * - * @param animation A constant that indicates how the reloading is to be animated. See UITableViewRowAnimation. - * - * @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes - * before this method is called. - */ - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); -/** - * Moves a section to a new location. - * - * @param section The index of the section to move. - * - * @param newSection The index that is the destination of the move for the section. - * - * @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes - * before this method is called. - */ - (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); -/** - * Inserts rows at the locations identified by an array of index paths, with an option to animate the insertion. - * - * @param indexPaths An array of NSIndexPath objects, each representing a row index and section index that together identify a row. - * - * @param animation A constant that indicates how the insertion is to be animated. See UITableViewRowAnimation. - * - * @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes - * before this method is called. - */ - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); -/** - * Deletes the rows specified by an array of index paths, with an option to animate the deletion. - * - * @param indexPaths An array of NSIndexPath objects identifying the rows to delete. - * - * @param animation A constant that indicates how the deletion is to be animated. See UITableViewRowAnimation. - * - * @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes - * before this method is called. - */ - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); -/** - * Reloads the specified rows using a given animation effect. - * - * @param indexPaths An array of NSIndexPath objects identifying the rows to reload. - * - * @param animation A constant that indicates how the reloading is to be animated. See UITableViewRowAnimation. - * - * @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes - * before this method is called. - */ - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); -/** - * Moves the row at a specified location to a destination location. - * - * @param indexPath The index path identifying the row to move. - * - * @param newIndexPath The index path that is the destination of the move for the row. - * - * @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes - * before this method is called. - */ - (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead."); /// Deprecated in 2.0. You should not call this method. diff --git a/AsyncDisplayKit/Details/ASCollectionInternal.h b/AsyncDisplayKit/Details/ASCollectionInternal.h index c17c8d689a..d471ebe5d8 100644 --- a/AsyncDisplayKit/Details/ASCollectionInternal.h +++ b/AsyncDisplayKit/Details/ASCollectionInternal.h @@ -41,6 +41,13 @@ NS_ASSUME_NONNULL_BEGIN */ - (NSIndexPath *)convertIndexPathToCollectionNode:(NSIndexPath *)indexPath; +/** + * Attempt to get the node index paths given the view-layer index paths. + * + * @param indexPaths An array of index paths in the view space + */ +- (nullable NSArray *)convertIndexPathsToCollectionNode:(nullable NSArray *)indexPaths; + @end NS_ASSUME_NONNULL_END diff --git a/AsyncDisplayKit/Private/ASCollectionView+Undeprecated.h b/AsyncDisplayKit/Private/ASCollectionView+Undeprecated.h index 4c1762a129..b633d65b68 100644 --- a/AsyncDisplayKit/Private/ASCollectionView+Undeprecated.h +++ b/AsyncDisplayKit/Private/ASCollectionView+Undeprecated.h @@ -86,6 +86,12 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeMode:(ASLayoutRangeMode)rangeMode rangeType:(ASLayoutRangeType)rangeType; +- (nullable __kindof UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath; + +@property (nonatomic, readonly) NSArray *indexPathsForVisibleItems; + +@property (nonatomic, readonly, nullable) NSArray *indexPathsForSelectedItems; + /** * Scrolls the collection to the given item. * diff --git a/AsyncDisplayKit/Private/ASTableView+Undeprecated.h b/AsyncDisplayKit/Private/ASTableView+Undeprecated.h index c6b99c56a1..d45a672601 100644 --- a/AsyncDisplayKit/Private/ASTableView+Undeprecated.h +++ b/AsyncDisplayKit/Private/ASTableView+Undeprecated.h @@ -77,6 +77,8 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeMode:(ASLayoutRangeMode)rangeMode rangeType:(ASLayoutRangeType)rangeType; +- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; + /** * Scrolls the table to the given row. * @@ -88,6 +90,16 @@ NS_ASSUME_NONNULL_BEGIN - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition; +@property (nonatomic, readonly, nullable) NSArray *indexPathsForVisibleRows; + +@property (nonatomic, readonly, nullable) NSArray *indexPathsForSelectedRows; + +@property (nonatomic, readonly, nullable) NSIndexPath *indexPathForSelectedRow; + +- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point; + +- (nullable NSArray *)indexPathsForRowsInRect:(CGRect)rect; + /** * Similar to -visibleCells. *