-
Notifications
You must be signed in to change notification settings - Fork 2
/
AIMNotificationObserver.m
62 lines (51 loc) · 1.75 KB
/
AIMNotificationObserver.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// AIMNotificationObserver.m
//
// Created by Maciej Gad on 05.08.2015.
// Copyright (c) 2015 All in Mobile. All rights reserved.
//
#import "AIMNotificationObserver.h"
/**
Typedef block for ChangeBlock
*/
typedef void (^ChangeBlock)(NSNotification *);
@interface AIMNotificationObserver ()
@property (strong, nonatomic) NSString *name;
@property (copy, nonatomic) void (^onChange)(NSNotification *notification);
@property (weak, nonatomic) id sender;
@end
@implementation AIMNotificationObserver
+ (instancetype)observeName:(NSString *)name onChange:(ChangeBlock)onChange {
AIMNotificationObserver *observer = [[self alloc] initWithNotificationName:name sender:nil onChange:onChange];
return observer;
}
+ (instancetype)observeName:(NSString *)name sender:(id)sender onChange:(ChangeBlock)onChange {
AIMNotificationObserver *observer = [[self alloc] initWithNotificationName:name sender:sender onChange:onChange];
return observer;
}
- (instancetype)initWithNotificationName:(NSString *)name sender:(id)sender onChange:(ChangeBlock)onChange {
self = [super init];
if (self) {
self.name = name;
self.sender = sender;
self.onChange = onChange;
[self startObserve];
}
return self;
}
- (instancetype)init {
[NSException raise:@"Invalid method" format:@"use [AIMNotificationObserver observeName:sender:onChange:] instead"];
return nil;
}
- (void)startObserve {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification:) name:self.name object:self.sender];
}
- (void)getNotification:(NSNotification *)notification {
if (self.onChange) {
self.onChange(notification);
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end