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

Added selection API to ASTableNode and ASCollectionNode #2453

Merged
merged 4 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions AsyncDisplayKit/ASCollectionNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (weak, nonatomic) id <ASCollectionDataSource> dataSource;

/**
* A Boolean value that indicates whether users can select items in the collection node.
* If the value of this property is YES (the default), users can select items. If you want more fine-grained control over the selection of items, you must provide a delegate object and implement the appropriate methods of the UICollectionNodeDelegate protocol.
*/
@property (nonatomic, assign) BOOL allowsSelection;

/**
* A Boolean value that determines whether users can select more than one item in the collection node.
* This property controls whether multiple items can be selected simultaneously. The default value of this property is NO.
* When the value of this property is YES, tapping a cell adds it to the current selection (assuming the delegate permits the cell to be selected). Tapping the cell again removes it from the selection.
*/
@property (nonatomic, assign) BOOL allowsMultipleSelection;


/**
* Tuning parameters for a range type in full mode.
*
Expand Down Expand Up @@ -272,6 +286,36 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)relayoutItems;

#pragma mark - Selection

/**
* 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.
* This method does not cause any selection-related delegate methods to be called.
*
* @param indexPath The index path of the item to select. Specifying nil for this parameter clears the current selection.
*
* @param animated Specify YES to animate the change in the selection or NO to make the change without animating it.
*
* @param scrollPosition An option that specifies where the item should be positioned when scrolling finishes. For a list of possible values, see `UICollectionViewScrollPosition`.
*
* @discussion This method must be called from the main thread.
*/
- (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition;

/**
* Deselects the item at the specified index.
* If the allowsSelection property is NO, calling this method has no effect.
* This method does not cause any selection-related delegate methods to be called.
*
* @param indexPath The index path of the item to select. Specifying nil for this parameter clears the current selection.
*
* @param animated Specify YES to animate the change in the selection or NO to make the change without animating it.
*
* @discussion This method must be called from the main thread.
*/
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

#pragma mark - Querying Data

/**
Expand Down
70 changes: 66 additions & 4 deletions AsyncDisplayKit/ASCollectionNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
@interface _ASCollectionPendingState : NSObject
@property (weak, nonatomic) id <ASCollectionDelegate> delegate;
@property (weak, nonatomic) id <ASCollectionDataSource> dataSource;
@property (assign, nonatomic) ASLayoutRangeMode rangeMode;
@property (nonatomic, assign) ASLayoutRangeMode rangeMode;
@property (nonatomic, assign) BOOL allowsSelection; // default is YES
@property (nonatomic, assign) BOOL allowsMultipleSelection; // default is NO
@end

@implementation _ASCollectionPendingState
Expand All @@ -37,6 +39,8 @@ - (instancetype)init
self = [super init];
if (self) {
_rangeMode = ASLayoutRangeModeCount;
_allowsSelection = YES;
_allowsMultipleSelection = NO;
}
return self;
}
Expand Down Expand Up @@ -143,9 +147,11 @@ - (void)didLoad

if (_pendingState) {
_ASCollectionPendingState *pendingState = _pendingState;
self.pendingState = nil;
view.asyncDelegate = pendingState.delegate;
view.asyncDataSource = pendingState.dataSource;
self.pendingState = nil;
view.asyncDelegate = pendingState.delegate;
view.asyncDataSource = pendingState.dataSource;
view.allowsSelection = pendingState.allowsSelection;
view.allowsMultipleSelection = pendingState.allowsMultipleSelection;

if (pendingState.rangeMode != ASLayoutRangeModeCount) {
[view.rangeController updateCurrentRangeWithMode:pendingState.rangeMode];
Expand Down Expand Up @@ -251,6 +257,44 @@ - (void)setDataSource:(id <ASCollectionDataSource>)dataSource
}
}

- (void)setAllowsSelection:(BOOL)allowsSelection
{
if ([self pendingState]) {
_pendingState.allowsSelection = allowsSelection;
} else {
ASDisplayNodeAssert([self isNodeLoaded], @"ASCollectionNode should be loaded if pendingState doesn't exist");
self.view.allowsSelection = allowsSelection;
}
}

- (BOOL)allowsSelection
{
if ([self pendingState]) {
return _pendingState.allowsSelection;
} else {
return self.view.allowsSelection;
}
}

- (void)setAllowsMultipleSelection:(BOOL)allowsMultipleSelection
{
if ([self pendingState]) {
_pendingState.allowsMultipleSelection = allowsMultipleSelection;
} else {
ASDisplayNodeAssert([self isNodeLoaded], @"ASCollectionNode should be loaded if pendingState doesn't exist");
self.view.allowsMultipleSelection = allowsMultipleSelection;
}
}

- (BOOL)allowsMultipleSelection
{
if ([self pendingState]) {
return _pendingState.allowsMultipleSelection;
} else {
return self.view.allowsMultipleSelection;
}
}

#pragma mark - Range Tuning

- (ASRangeTuningParameters)tuningParametersForRangeType:(ASLayoutRangeType)rangeType
Expand All @@ -273,6 +317,24 @@ - (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeMo
return [self.rangeController setTuningParameters:tuningParameters forRangeMode:rangeMode rangeType:rangeType];
}

#pragma mark - Selection

- (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition
{
ASDisplayNodeAssertMainThread();
// TODO: Solve this in a way to be able to remove this restriction (https://github.com/facebook/AsyncDisplayKit/pull/2453#discussion_r84515457)
ASDisplayNodeAssert([self isNodeLoaded], @"ASCollectionNode should be loaded before calling selectItemAtIndexPath");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future it will be nice to remove this restriction. It would require that we have a clean method for ensuring that the view is loaded, that the initial reload has been called, and that all updates are committed. Today we could cobble together all that into a method but it would be ugly and there are other potential hazards (what if we're inside willDisplayCell:forItemAtIndexPath: and waiting is unsafe, what if we're zero-size, etc.) That will come later and I think that this is plenty for the medium-term.

Also, would you be willing to add ASDisplayNodeAssertMainThread() in these methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I added a todo referring to your discussion post.

I added the ASDisplayNodeAssertMainThread(), I didn't add it before because the code that this method calls checks for that assertion. But I suppose it's better to have it in case the implementation changes later on.

[self.view selectItemAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
}

- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
{
ASDisplayNodeAssertMainThread();
// TODO: Solve this in a way to be able to remove this restriction (https://github.com/facebook/AsyncDisplayKit/pull/2453#discussion_r84515457)
ASDisplayNodeAssert([self isNodeLoaded], @"ASCollectionNode should be loaded before calling deselectItemAtIndexPath");
[self.view deselectItemAtIndexPath:indexPath animated:animated];
}

#pragma mark - Querying Data

- (NSInteger)numberOfItemsInSection:(NSInteger)section
Expand Down
53 changes: 53 additions & 0 deletions AsyncDisplayKit/ASTableNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ NS_ASSUME_NONNULL_BEGIN
@property (weak, nonatomic) id <ASTableDelegate> delegate;
@property (weak, nonatomic) id <ASTableDataSource> dataSource;

/*
* A Boolean value that determines whether users can select a row.
* If the value of this property is YES (the default), users can select rows. If you set it to NO, they cannot select rows. Setting this property affects cell selection only when the table view is not in editing mode. If you want to restrict selection of cells in editing mode, use `allowsSelectionDuringEditing`.
*/
@property (nonatomic, assign) BOOL allowsSelection;
/*
* A Boolean value that determines whether users can select cells while the table view is in editing mode.
* If the value of this property is YES, users can select rows during editing. The default value is NO. If you want to restrict selection of cells regardless of mode, use allowsSelection.
*/
@property (nonatomic, assign) BOOL allowsSelectionDuringEditing;
/*
* A Boolean value that determines whether users can select more than one row outside of editing mode.
* This property controls whether multiple rows can be selected simultaneously outside of editing mode. When the value of this property is YES, each row that is tapped acquires a selected appearance. Tapping the row again removes the selected appearance. If you access indexPathsForSelectedRows, you can get the index paths that identify the selected rows.
*/
@property (nonatomic, assign) BOOL allowsMultipleSelection;
/*
* A Boolean value that controls whether users can select more than one cell simultaneously in editing mode.
* The default value of this property is NO. If you set it to YES, check marks appear next to selected rows in editing mode. In addition, UITableView does not query for editing styles when it goes into editing mode. If you access indexPathsForSelectedRows, you can get the index paths that identify the selected rows.
*/
@property (nonatomic, assign) BOOL allowsMultipleSelectionDuringEditing;

/**
* Tuning parameters for a range type in full mode.
*
Expand Down Expand Up @@ -230,6 +251,38 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;

#pragma mark - Selection

/**
* Selects a row in the table view identified by index path, optionally scrolling the row to a location in the table view.
* This method does not cause any selection-related delegate methods to be called.
*
* @param indexPath An index path identifying a row in the table view.
*
* @param animated Specify YES to animate the change in the selection or NO to make the change without animating it.
*
* @param scrollPosition A constant that identifies a relative position in the table view (top, middle, bottom) for the row when scrolling concludes. See `UITableViewScrollPosition` for descriptions of valid constants.
*
* @discussion This method must be called from the main thread.
*/
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;

/*
* Deselects a given row identified by index path, with an option to animate the deselection.
* This method does not cause any selection-related delegate methods to be called.
* Calling this method does not cause any scrolling to the deselected row.
*
* @param indexPath An index path identifying a row in the table view.
*
* @param animated Specify YES to animate the change in the selection or NO to make the change without animating it.
*
* @discussion This method must be called from the main thread.
*/
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;


#pragma mark - Querying Data

/**
* Retrieves the number of rows in the given section.
*
Expand Down
108 changes: 107 additions & 1 deletion AsyncDisplayKit/ASTableNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
@interface _ASTablePendingState : NSObject
@property (weak, nonatomic) id <ASTableDelegate> delegate;
@property (weak, nonatomic) id <ASTableDataSource> dataSource;
@property (assign, nonatomic) ASLayoutRangeMode rangeMode;
@property (nonatomic, assign) ASLayoutRangeMode rangeMode;
@property (nonatomic, assign) BOOL allowsSelection;
@property (nonatomic, assign) BOOL allowsSelectionDuringEditing;
@property (nonatomic, assign) BOOL allowsMultipleSelection;
@property (nonatomic, assign) BOOL allowsMultipleSelectionDuringEditing;
@end

@implementation _ASTablePendingState
Expand All @@ -33,6 +37,10 @@ - (instancetype)init
self = [super init];
if (self) {
_rangeMode = ASLayoutRangeModeCount;
_allowsSelection = YES;
_allowsSelectionDuringEditing = NO;
_allowsMultipleSelection = NO;
_allowsMultipleSelectionDuringEditing = NO;
}
return self;
}
Expand Down Expand Up @@ -104,6 +112,10 @@ - (void)didLoad
self.pendingState = nil;
view.asyncDelegate = pendingState.delegate;
view.asyncDataSource = pendingState.dataSource;
view.allowsSelection = pendingState.allowsSelection;
view.allowsSelectionDuringEditing = pendingState.allowsSelectionDuringEditing;
view.allowsMultipleSelection = pendingState.allowsMultipleSelection;
view.allowsMultipleSelectionDuringEditing = pendingState.allowsMultipleSelectionDuringEditing;
if (pendingState.rangeMode != ASLayoutRangeModeCount) {
[view.rangeController updateCurrentRangeWithMode:pendingState.rangeMode];
}
Expand Down Expand Up @@ -215,6 +227,82 @@ - (void)setDataSource:(id <ASTableDataSource>)dataSource
}
}

- (void)setAllowsSelection:(BOOL)allowsSelection
{
if ([self pendingState]) {
_pendingState.allowsSelection = allowsSelection;
} else {
ASDisplayNodeAssert([self isNodeLoaded], @"ASTableNode should be loaded if pendingState doesn't exist");
self.view.allowsSelection = allowsSelection;
}
}

- (BOOL)allowsSelection
{
if ([self pendingState]) {
return _pendingState.allowsSelection;
} else {
return self.view.allowsSelection;
}
}

- (void)setAllowsSelectionDuringEditing:(BOOL)allowsSelectionDuringEditing
{
if ([self pendingState]) {
_pendingState.allowsSelectionDuringEditing = allowsSelectionDuringEditing;
} else {
ASDisplayNodeAssert([self isNodeLoaded], @"ASTableNode should be loaded if pendingState doesn't exist");
self.view.allowsSelectionDuringEditing = allowsSelectionDuringEditing;
}
}

- (BOOL)allowsSelectionDuringEditing
{
if ([self pendingState]) {
return _pendingState.allowsSelectionDuringEditing;
} else {
return self.view.allowsSelectionDuringEditing;
}
}

- (void)setAllowsMultipleSelection:(BOOL)allowsMultipleSelection
{
if ([self pendingState]) {
_pendingState.allowsMultipleSelection = allowsMultipleSelection;
} else {
ASDisplayNodeAssert([self isNodeLoaded], @"ASTableNode should be loaded if pendingState doesn't exist");
self.view.allowsMultipleSelection = allowsMultipleSelection;
}
}

- (BOOL)allowsMultipleSelection
{
if ([self pendingState]) {
return _pendingState.allowsMultipleSelection;
} else {
return self.view.allowsMultipleSelection;
}
}

- (void)setAllowsMultipleSelectionDuringEditing:(BOOL)allowsMultipleSelectionDuringEditing
{
if ([self pendingState]) {
_pendingState.allowsMultipleSelectionDuringEditing = allowsMultipleSelectionDuringEditing;
} else {
ASDisplayNodeAssert([self isNodeLoaded], @"ASTableNode should be loaded if pendingState doesn't exist");
self.view.allowsMultipleSelectionDuringEditing = allowsMultipleSelectionDuringEditing;
}
}

- (BOOL)allowsMultipleSelectionDuringEditing
{
if ([self pendingState]) {
return _pendingState.allowsMultipleSelectionDuringEditing;
} else {
return self.view.allowsMultipleSelectionDuringEditing;
}
}

#pragma mark ASRangeControllerUpdateRangeProtocol

- (void)updateCurrentRangeWithMode:(ASLayoutRangeMode)rangeMode
Expand Down Expand Up @@ -253,6 +341,24 @@ - (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeMo
return [self.rangeController setTuningParameters:tuningParameters forRangeMode:rangeMode rangeType:rangeType];
}

#pragma mark - Selection

- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
{
ASDisplayNodeAssertMainThread();
// TODO: Solve this in a way to be able to remove this restriction (https://github.com/facebook/AsyncDisplayKit/pull/2453#discussion_r84515457)
ASDisplayNodeAssert([self isNodeLoaded], @"ASTableNode should be loaded before calling selectRowAtIndexPath");
[self.view selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
}

- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
{
ASDisplayNodeAssertMainThread();
// TODO: Solve this in a way to be able to remove this restriction (https://github.com/facebook/AsyncDisplayKit/pull/2453#discussion_r84515457)
ASDisplayNodeAssert([self isNodeLoaded], @"ASTableNode should be loaded before calling deselectRowAtIndexPath");
[self.view deselectRowAtIndexPath:indexPath animated:animated];
}

#pragma mark - Querying Data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add white space below


- (NSInteger)numberOfRowsInSection:(NSInteger)section
Expand Down
Loading