Created
June 21, 2016 22:07
-
-
Save thefotes/3f6e1d8871fb33d466a24750865d0a7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PJNotificationCenter.m | |
// NotificationSample | |
// | |
// Created by Peter Foti on 6/21/16. | |
// Copyright © 2016 Peter Foti. All rights reserved. | |
// | |
#import "PJNotificationCenter.h" | |
#import "PJNotificationKey.h" | |
@interface PJNotificationCenter () | |
@property (strong, nonatomic) NSMutableDictionary *observerDict; | |
@end | |
@implementation PJNotificationCenter | |
- (NSMutableDictionary *)observerDict | |
{ | |
return _observerDict = _observerDict ?: [NSMutableDictionary new]; | |
} | |
- (void)pj_addObserverForName:(NSString *)name object:(id)obj usingBlock:(void (^)(NSNotification * _Nonnull))block | |
{ | |
PJNotificationKey *key = [PJNotificationKey keyForName:name object:obj]; | |
NSMutableSet *observerBlocks = self.observerDict[key]; | |
if (!observerBlocks) { | |
observerBlocks = [NSMutableSet new]; | |
self.observerDict[key] = observerBlocks; | |
} | |
[observerBlocks addObject:[block copy]]; | |
} | |
- (void)pj_postNotificationName:(NSString *)aName object:(id)anObject | |
{ | |
PJNotificationKey *key = [PJNotificationKey keyForName:aName object:anObject]; | |
NSMutableSet *observerBlocks = self.observerDict[key]; | |
NSNotification *note = [NSNotification notificationWithName:aName object:anObject]; | |
for (void (^block)(NSNotification *) in observerBlocks) { | |
block(note); | |
} | |
} | |
@end | |
// | |
// PJNotificationKey.m | |
// NotificationSample | |
// | |
// Created by Peter Foti on 6/21/16. | |
// Copyright © 2016 Peter Foti. All rights reserved. | |
// | |
#import "PJNotificationKey.h" | |
@interface PJNotificationKey () | |
@property (copy, nonatomic) NSString *name; | |
@property (weak, nonatomic) id object; | |
@end | |
@implementation PJNotificationKey | |
+ (PJNotificationKey *)keyForName:(NSString *)name object:(id)object | |
{ | |
return [[self alloc] initWithName:name object:object]; | |
} | |
- (id)copyWithZone:(NSZone *)zone | |
{ | |
return self; | |
} | |
- (NSUInteger)hash | |
{ | |
return [self.name hash] ^ [self.object hash]; | |
} | |
- (BOOL)isEqual:(id)object | |
{ | |
if ([object isKindOfClass:[self class]]) { | |
PJNotificationKey *key = (PJNotificationKey *)object; | |
if ([key.name isEqualToString:self.name] && [key.object isEqual:self.object]) { | |
return YES; | |
} else { | |
return [object isEqual:self]; | |
} | |
} else { | |
return NO; | |
} | |
} | |
- (id)initWithName:(NSString *)name object:(id)obj | |
{ | |
if (self = [self init]) { | |
_name = name; | |
_object = obj; | |
} | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment