Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit a6e2f8e

Browse files
author
Adlai Holler
authored
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
1 parent e1e5eb6 commit a6e2f8e

10 files changed

Lines changed: 89 additions & 119 deletions

AsyncDisplayKit/ASCollectionNode.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ NS_ASSUME_NONNULL_BEGIN
299299

300300
#pragma mark - Selection
301301

302+
/**
303+
* The index paths of the selected items, or @c nil if no items are selected.
304+
*/
305+
@property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForSelectedItems;
306+
302307
/**
303308
* Selects the item at the specified index path and optionally scrolls it into view.
304309
* 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
348353
*
349354
* @return an array containing the nodes being displayed on screen. This must be called on the main thread.
350355
*/
351-
@property(readonly, copy) NSArray<__kindof ASCellNode *> *visibleNodes;
356+
@property (nonatomic, readonly) NSArray<__kindof ASCellNode *> *visibleNodes;
352357

353358
/**
354359
* Retrieves the node for the item at the given index path.
@@ -357,7 +362,7 @@ NS_ASSUME_NONNULL_BEGIN
357362
*
358363
* @return The node for the given item, or @c nil if no item exists at the specified path.
359364
*/
360-
- (nullable ASCellNode *)nodeForItemAtIndexPath:(NSIndexPath *)indexPath AS_WARN_UNUSED_RESULT;
365+
- (nullable __kindof ASCellNode *)nodeForItemAtIndexPath:(NSIndexPath *)indexPath AS_WARN_UNUSED_RESULT;
361366

362367
/**
363368
* Retrieve the index path for the item with the given node.
@@ -373,7 +378,7 @@ NS_ASSUME_NONNULL_BEGIN
373378
*
374379
* @return an array containing the index paths of all visible items. This must be called on the main thread.
375380
*/
376-
- (NSArray<__kindof NSIndexPath *> *)indexPathsForVisibleItems AS_WARN_UNUSED_RESULT;
381+
@property (nonatomic, readonly) NSArray<NSIndexPath *> *indexPathsForVisibleItems;
377382

378383
/**
379384
* Retrieve the index path of the item at the given point.

AsyncDisplayKit/ASCollectionNode.mm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ - (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeMo
319319

320320
#pragma mark - Selection
321321

322+
- (NSArray<NSIndexPath *> *)indexPathsForSelectedItems
323+
{
324+
ASDisplayNodeAssertMainThread();
325+
ASCollectionView *view = self.view;
326+
return [view convertIndexPathsToCollectionNode:view.indexPathsForSelectedItems];
327+
}
328+
322329
- (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition
323330
{
324331
ASDisplayNodeAssertMainThread();

AsyncDisplayKit/ASCollectionView.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,16 @@ NS_ASSUME_NONNULL_BEGIN
205205
*/
206206
- (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeMode:(ASLayoutRangeMode)rangeMode rangeType:(ASLayoutRangeType)rangeType ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode method instead.");
207207

208-
/**
209-
* Scrolls the collection to the given item.
210-
*
211-
* @param indexPath The index path of the item.
212-
* @param scrollPosition Where the row should end up after the scroll.
213-
* @param animated Whether the scroll should be animated or not.
214-
*/
208+
- (nullable __kindof UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode method instead.");
209+
215210
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode method instead.");
216211

217212
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode method instead.");
218213

214+
@property (nonatomic, readonly) NSArray<NSIndexPath *> *indexPathsForVisibleItems ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode property instead.");
215+
216+
@property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForSelectedItems ASDISPLAYNODE_DEPRECATED_MSG("Use ASCollectionNode property instead.");
217+
219218
/**
220219
* Perform a batch of updates asynchronously, optionally disabling all animations in the batch. This method must be called from the main thread.
221220
* The asyncDataSource must be updated to reflect the changes before the update block completes.

AsyncDisplayKit/ASCollectionView.mm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,23 @@ - (NSIndexPath *)convertIndexPathToCollectionNode:(NSIndexPath *)indexPath
582582
}
583583
}
584584

585+
- (NSArray<NSIndexPath *> *)convertIndexPathsToCollectionNode:(NSArray<NSIndexPath *> *)indexPaths
586+
{
587+
if (indexPaths == nil) {
588+
return nil;
589+
}
590+
591+
NSMutableArray<NSIndexPath *> *indexPathsArray = [NSMutableArray arrayWithCapacity:indexPaths.count];
592+
593+
for (NSIndexPath *indexPathInView in indexPaths) {
594+
NSIndexPath *indexPath = [self convertIndexPathToCollectionNode:indexPathInView];
595+
if (indexPath != nil) {
596+
[indexPathsArray addObject:indexPath];
597+
}
598+
}
599+
return indexPathsArray;
600+
}
601+
585602
- (ASCellNode *)supplementaryNodeForElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
586603
{
587604
return [_dataController supplementaryNodeOfKind:elementKind atIndexPath:indexPath];

AsyncDisplayKit/ASTableNode.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,12 @@ NS_ASSUME_NONNULL_BEGIN
312312
*
313313
* @return an array containing the nodes being displayed on screen. This must be called on the main thread.
314314
*/
315-
@property(readonly, copy) NSArray<__kindof ASCellNode *> *visibleNodes;
315+
@property (nonatomic, readonly) NSArray<__kindof ASCellNode *> *visibleNodes;
316316

317317
/**
318318
* Retrieves the node for the row at the given index path.
319319
*/
320-
- (nullable ASCellNode *)nodeForRowAtIndexPath:(NSIndexPath *)indexPath AS_WARN_UNUSED_RESULT;
320+
- (nullable __kindof ASCellNode *)nodeForRowAtIndexPath:(NSIndexPath *)indexPath AS_WARN_UNUSED_RESULT;
321321

322322
/**
323323
* Similar to -indexPathForCell:.
@@ -356,7 +356,7 @@ NS_ASSUME_NONNULL_BEGIN
356356
- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath AS_WARN_UNUSED_RESULT;
357357

358358
/**
359-
* Similar to -[UITableView indexPathForSelectedRow]
359+
* Similar to UITableView.indexPathForSelectedRow
360360
*
361361
* @return The value of this property is an index path identifying the row and section
362362
* 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
365365
*
366366
* @discussion This method must be called from the main thread.
367367
*/
368-
- (nullable NSIndexPath *)indexPathForSelectedRow AS_WARN_UNUSED_RESULT;
368+
@property (nonatomic, readonly, nullable) NSIndexPath *indexPathForSelectedRow;
369+
370+
@property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForSelectedRows;
369371

370372
/**
371373
* Similar to -[UITableView indexPathForRowAtPoint:]

AsyncDisplayKit/ASTableNode.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,14 @@ - (nullable NSIndexPath *)indexPathForSelectedRow
460460
return indexPath;
461461
}
462462

463+
- (NSArray<NSIndexPath *> *)indexPathsForSelectedRows
464+
{
465+
ASDisplayNodeAssertMainThread();
466+
ASTableView *tableView = self.view;
467+
468+
return [tableView convertIndexPathsToTableNode:tableView.indexPathsForSelectedRows];
469+
}
470+
463471
- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
464472
{
465473
ASDisplayNodeAssertMainThread();

AsyncDisplayKit/ASTableView.h

Lines changed: 12 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,22 @@ NS_ASSUME_NONNULL_BEGIN
124124
*/
125125
- (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeMode:(ASLayoutRangeMode)rangeMode rangeType:(ASLayoutRangeType)rangeType ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
126126

127-
/**
128-
* Scrolls the table to the given row.
129-
*
130-
* @param indexPath The index path of the row.
131-
* @param scrollPosition Where the row should end up after the scroll.
132-
* @param animated Whether the scroll should be animated or not.
133-
*/
127+
- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
128+
134129
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
135130

136131
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
137132

133+
@property (nonatomic, readonly, nullable) NSIndexPath *indexPathForSelectedRow ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode property instead.");
134+
135+
@property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForSelectedRows ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode property instead.");
136+
137+
@property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForVisibleRows ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode property instead.");
138+
139+
- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
140+
141+
- (nullable NSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
142+
138143
/**
139144
* Similar to -visibleCells.
140145
*
@@ -182,26 +187,8 @@ NS_ASSUME_NONNULL_BEGIN
182187
*/
183188
- (void)relayoutItems ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
184189

185-
/**
186-
* 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.
187-
*
188-
* @discussion You call this method to bracket a series of method calls that ends with endUpdates and that consists of operations
189-
* to insert, delete, select, and reload rows and sections of the table view. When you call endUpdates, ASTableView begins animating
190-
* the operations simultaneously. It's important to remember that the ASTableView will be processing the updates asynchronously after this call is completed.
191-
*
192-
* @warning This method must be called from the main thread.
193-
*/
194190
- (void)beginUpdates ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode's -performBatchUpdates:completion: instead.");
195191

196-
/**
197-
* 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.
198-
*
199-
* @discussion You call this method to bracket a series of method calls that begins with beginUpdates and that consists of operations
200-
* to insert, delete, select, and reload rows and sections of the table view. When you call endUpdates, ASTableView begins animating
201-
* the operations simultaneously. It's important to remember that the ASTableView will be processing the updates asynchronously after this call is completed.
202-
*
203-
* @warning This method is must be called from the main thread.
204-
*/
205192
- (void)endUpdates ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode's -performBatchUpdates:completion: instead.");
206193

207194
/**
@@ -224,100 +211,20 @@ NS_ASSUME_NONNULL_BEGIN
224211
*/
225212
- (void)waitUntilAllUpdatesAreCommitted ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
226213

227-
/**
228-
* Inserts one or more sections, with an option to animate the insertion.
229-
*
230-
* @param sections An index set that specifies the sections to insert.
231-
*
232-
* @param animation A constant that indicates how the insertion is to be animated. See UITableViewRowAnimation.
233-
*
234-
* @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes
235-
* before this method is called.
236-
*/
237214
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
238215

239-
/**
240-
* Deletes one or more sections, with an option to animate the deletion.
241-
*
242-
* @param sections An index set that specifies the sections to delete.
243-
*
244-
* @param animation A constant that indicates how the deletion is to be animated. See UITableViewRowAnimation.
245-
*
246-
* @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes
247-
* before this method is called.
248-
*/
249216
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
250217

251-
/**
252-
* Reloads the specified sections using a given animation effect.
253-
*
254-
* @param sections An index set that specifies the sections to reload.
255-
*
256-
* @param animation A constant that indicates how the reloading is to be animated. See UITableViewRowAnimation.
257-
*
258-
* @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes
259-
* before this method is called.
260-
*/
261218
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
262219

263-
/**
264-
* Moves a section to a new location.
265-
*
266-
* @param section The index of the section to move.
267-
*
268-
* @param newSection The index that is the destination of the move for the section.
269-
*
270-
* @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes
271-
* before this method is called.
272-
*/
273220
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
274221

275-
/**
276-
* Inserts rows at the locations identified by an array of index paths, with an option to animate the insertion.
277-
*
278-
* @param indexPaths An array of NSIndexPath objects, each representing a row index and section index that together identify a row.
279-
*
280-
* @param animation A constant that indicates how the insertion is to be animated. See UITableViewRowAnimation.
281-
*
282-
* @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes
283-
* before this method is called.
284-
*/
285222
- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
286223

287-
/**
288-
* Deletes the rows specified by an array of index paths, with an option to animate the deletion.
289-
*
290-
* @param indexPaths An array of NSIndexPath objects identifying the rows to delete.
291-
*
292-
* @param animation A constant that indicates how the deletion is to be animated. See UITableViewRowAnimation.
293-
*
294-
* @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes
295-
* before this method is called.
296-
*/
297224
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
298225

299-
/**
300-
* Reloads the specified rows using a given animation effect.
301-
*
302-
* @param indexPaths An array of NSIndexPath objects identifying the rows to reload.
303-
*
304-
* @param animation A constant that indicates how the reloading is to be animated. See UITableViewRowAnimation.
305-
*
306-
* @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes
307-
* before this method is called.
308-
*/
309226
- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
310227

311-
/**
312-
* Moves the row at a specified location to a destination location.
313-
*
314-
* @param indexPath The index path identifying the row to move.
315-
*
316-
* @param newIndexPath The index path that is the destination of the move for the row.
317-
*
318-
* @discussion This method must be called from the main thread. The asyncDataSource must be updated to reflect the changes
319-
* before this method is called.
320-
*/
321228
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath ASDISPLAYNODE_DEPRECATED_MSG("Use ASTableNode method instead.");
322229

323230
/// Deprecated in 2.0. You should not call this method.

AsyncDisplayKit/Details/ASCollectionInternal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ NS_ASSUME_NONNULL_BEGIN
4141
*/
4242
- (NSIndexPath *)convertIndexPathToCollectionNode:(NSIndexPath *)indexPath;
4343

44+
/**
45+
* Attempt to get the node index paths given the view-layer index paths.
46+
*
47+
* @param indexPaths An array of index paths in the view space
48+
*/
49+
- (nullable NSArray<NSIndexPath *> *)convertIndexPathsToCollectionNode:(nullable NSArray<NSIndexPath *> *)indexPaths;
50+
4451
@end
4552

4653
NS_ASSUME_NONNULL_END

AsyncDisplayKit/Private/ASCollectionView+Undeprecated.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ NS_ASSUME_NONNULL_BEGIN
8686
*/
8787
- (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeMode:(ASLayoutRangeMode)rangeMode rangeType:(ASLayoutRangeType)rangeType;
8888

89+
- (nullable __kindof UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath;
90+
91+
@property (nonatomic, readonly) NSArray<NSIndexPath *> *indexPathsForVisibleItems;
92+
93+
@property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForSelectedItems;
94+
8995
/**
9096
* Scrolls the collection to the given item.
9197
*

AsyncDisplayKit/Private/ASTableView+Undeprecated.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ NS_ASSUME_NONNULL_BEGIN
7777
*/
7878
- (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeMode:(ASLayoutRangeMode)rangeMode rangeType:(ASLayoutRangeType)rangeType;
7979

80+
- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
81+
8082
/**
8183
* Scrolls the table to the given row.
8284
*
@@ -88,6 +90,16 @@ NS_ASSUME_NONNULL_BEGIN
8890

8991
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
9092

93+
@property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForVisibleRows;
94+
95+
@property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForSelectedRows;
96+
97+
@property (nonatomic, readonly, nullable) NSIndexPath *indexPathForSelectedRow;
98+
99+
- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
100+
101+
- (nullable NSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect;
102+
91103
/**
92104
* Similar to -visibleCells.
93105
*

0 commit comments

Comments
 (0)