Skip to content

Commit

Permalink
[collections] Remove actions delegate forwarding.
Browse files Browse the repository at this point in the history
- Actions disable highlighting by default unless the object is actionable.
  • Loading branch information
jverkoey committed Apr 16, 2013
1 parent fcd36bb commit ca33f66
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 149 deletions.
54 changes: 3 additions & 51 deletions src/collections/src/NICollectionViewActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,9 @@ typedef BOOL (^NICollectionViewActionBlock)(id object, id target, NSIndexPath* i
* for each type of action and both will be executed, with the block being executed first. Blocks
* should be used for simple executions while selectors should be used when the action is complex.
*
* <h3>Delegate Forwarding</h3>
*
* NICollectionViewActions will intercept taps on a collection view using a mechanism known as
* <i>delegate chaining</i>. This effect is achieved by invoking
* @link NICollectionViewActions::forwardingTo: forwardingTo:@endlink on the
* NICollectionViewActions instance and providing the appropriate object to forward to (generally
* @c self).
*
@code
collectionView.delegate = [self.actions forwardingTo:self];
@endcode
*
* The dataSource property of the collection view must be an instance of NICollectionViewModel when
* using delegate chaining.
*
* @ingroup CollectionViewTools
*/
@interface NICollectionViewActions : NSObject <UICollectionViewDelegate>
@interface NICollectionViewActions : NSObject

// Designated initializer.
- (id)initWithTarget:(id)target;
Expand All @@ -78,10 +63,8 @@ collectionView.delegate = [self.actions forwardingTo:self];

- (BOOL)isObjectActionable:(id<NSObject>)object;

#pragma mark Forwarding

- (id<UICollectionViewDelegate>)forwardingTo:(id<UICollectionViewDelegate>)forwardDelegate;
- (void)removeForwarding:(id<UICollectionViewDelegate>)forwardDelegate;
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;

@end

Expand Down Expand Up @@ -179,34 +162,3 @@ collectionView.delegate = [self.actions forwardingTo:self];
*
* @fn NICollectionViewActions::isObjectActionable:
*/

/** @name Forwarding */

/**
* Sets the delegate that collection view methods should be forwarded to.
*
* This method allows you to insert the actions into the call chain for the collection view's
* delegate methods.
*
* Example:
*
@code
// Let the actions handle delegate methods and then forward them to whatever delegate was
// already assigned.
self.collectionView.delegate = [self.actions forwardingTo:self.collectionView.delegate];
@endcode
*
* @param forwardDelegate The delegate to forward invocations to.
* @returns self so that this method can be chained.
* @fn NICollectionViewActions::forwardingTo:
*/

/**
* Removes the delegate from the forwarding chain.
*
* If a forwared delegate is about to be released but this object may live on, you must remove the
* forwarding in order to avoid invalid access errors at runtime.
*
* @param forwardDelegate The delegate to stop forwarding invocations to.
* @fn NICollectionViewActions::removeForwarding:
*/
101 changes: 3 additions & 98 deletions src/collections/src/NICollectionViewActions.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ @interface NICollectionViewAction : NSObject
@interface NICollectionViewActions()

@property (nonatomic, NI_WEAK) id target;
@property (nonatomic, NI_STRONG) NSMutableSet* forwardDelegates;
@property (nonatomic, NI_STRONG) NSMutableDictionary* objectMap;
@property (nonatomic, NI_STRONG) NSMutableSet* objectSet;
@property (nonatomic, NI_STRONG) NSMutableDictionary* classMap;
Expand All @@ -47,7 +46,6 @@ @interface NICollectionViewActions()
@implementation NICollectionViewActions

@synthesize target = _target;
@synthesize forwardDelegates = _forwardDelegates;
@synthesize objectMap = _objectMap;
@synthesize objectSet = _objectSet;
@synthesize classMap = _classMap;
Expand All @@ -60,7 +58,6 @@ - (id)initWithTarget:(id)target {
_objectMap = [[NSMutableDictionary alloc] init];
_objectSet = [[NSMutableSet alloc] init];
_classMap = [[NSMutableDictionary alloc] init];
_forwardDelegates = NICreateNonRetainingMutableSet();
}
return self;
}
Expand Down Expand Up @@ -117,82 +114,6 @@ - (NICollectionViewAction *)actionForObjectOrClassOfObject:(id<NSObject>)object
}


///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Forward Invocations


///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldForwardSelector:(SEL)selector {
struct objc_method_description description;
description = protocol_getMethodDescription(@protocol(UICollectionViewDelegate), selector, NO, YES);
return (description.name != NULL && description.types != NULL);
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)respondsToSelector:(SEL)selector {
if ([super respondsToSelector:selector]) {
return YES;

} else if ([self shouldForwardSelector:selector]) {
for (id delegate in self.forwardDelegates) {
if ([delegate respondsToSelector:selector]) {
return YES;
}
}
}
return NO;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
NSMethodSignature *signature = [super methodSignatureForSelector:selector];
if (signature == nil) {
for (id delegate in self.forwardDelegates) {
if ([delegate respondsToSelector:selector]) {
signature = [delegate methodSignatureForSelector:selector];
}
}
}
return signature;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)forwardInvocation:(NSInvocation *)invocation {
BOOL didForward = NO;

if ([self shouldForwardSelector:invocation.selector]) {
for (id delegate in self.forwardDelegates) {
if ([delegate respondsToSelector:invocation.selector]) {
[invocation invokeWithTarget:delegate];
didForward = YES;
break;
}
}
}

if (!didForward) {
[super forwardInvocation:invocation];
}
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (id<UICollectionViewDelegate>)forwardingTo:(id<UICollectionViewDelegate>)forwardDelegate {
[self.forwardDelegates addObject:forwardDelegate];
return self;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)removeForwarding:(id<UICollectionViewDelegate>)forwardDelegate {
[self.forwardDelegates removeObject:forwardDelegate];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Public Methods
Expand Down Expand Up @@ -247,16 +168,7 @@ - (BOOL)isObjectActionable:(id<NSObject>)object {

///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
BOOL shouldHighlight = YES;

// Forward the invocation along.
for (id<UICollectionViewDelegate> delegate in self.forwardDelegates) {
if ([delegate respondsToSelector:_cmd]) {
if (![delegate collectionView:collectionView shouldHighlightItemAtIndexPath:indexPath]) {
shouldHighlight = NO;
}
}
}
BOOL shouldHighlight = NO;

NIDASSERT([collectionView.dataSource isKindOfClass:[NICollectionViewModel class]]);
if ([collectionView.dataSource isKindOfClass:[NICollectionViewModel class]]
Expand All @@ -268,8 +180,8 @@ - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtI
NICollectionViewAction* action = [self actionForObjectOrClassOfObject:object];

// If the cell is tappable, reflect that in the selection style.
if (!action.tapAction && !action.tapSelector) {
shouldHighlight = NO;
if (nil != action.tapAction || nil != action.tapSelector) {
shouldHighlight = YES;
}
}
}
Expand Down Expand Up @@ -324,13 +236,6 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa
}
}
}

// Forward the invocation along.
for (id<UICollectionViewDelegate> delegate in self.forwardDelegates) {
if ([delegate respondsToSelector:_cmd]) {
[delegate collectionView:collectionView didSelectItemAtIndexPath:indexPath];
}
}
}

@end
Expand Down

0 comments on commit ca33f66

Please sign in to comment.