-
Notifications
You must be signed in to change notification settings - Fork 10
/
RNTGlobalEventEmitter.m
101 lines (85 loc) · 4.43 KB
/
RNTGlobalEventEmitter.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// RNTGlobalEventEmitter.m
// RNTGlobalEventEmitter
//
// Created by Param Aggarwal on 27/08/15.
// Copyright (c) 2015 Facebook. All rights reserved.
//
#import "RNTGlobalEventEmitter.h"
#import "RCTEventDispatcher.h"
@interface RNTGlobalEventEmitter ()
- (void)bridgeNotification:(NSNotification *)notification;
@end
@implementation RNTGlobalEventEmitter
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(addObserver:(NSString *)notificationName)
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(bridgeNotification:)
name:notificationName
object:nil];
}
RCT_EXPORT_METHOD(postNotification:(NSString *)notificationName userInfo:(NSDictionary *)userInfo)
{
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName
object:nil
userInfo:userInfo];
}
RCT_EXPORT_METHOD(removeObserver:(NSString *)notificationName)
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:notificationName
object:nil];
}
- (void)bridgeNotification:(NSNotification *)notification
{
[self.bridge.eventDispatcher sendDeviceEventWithName:@"onNotification"
body:@{
@"name": notification.name,
@"userInfo": notification.userInfo ?: [NSNull null],
}];
}
- (NSDictionary *)constantsToExport
{
return @{
@"UIApplicationNotifications": @{
@"UIApplicationDidEnterBackgroundNotification": UIApplicationDidEnterBackgroundNotification,
@"UIApplicationWillEnterForegroundNotification": UIApplicationWillEnterForegroundNotification,
@"UIApplicationDidFinishLaunchingNotification": UIApplicationDidFinishLaunchingNotification,
@"UIApplicationDidBecomeActiveNotification": UIApplicationDidBecomeActiveNotification,
@"UIApplicationWillResignActiveNotification": UIApplicationWillResignActiveNotification,
@"UIApplicationDidReceiveMemoryWarningNotification": UIApplicationDidReceiveMemoryWarningNotification,
@"UIApplicationWillTerminateNotification": UIApplicationWillTerminateNotification,
@"UIApplicationSignificantTimeChangeNotification": UIApplicationSignificantTimeChangeNotification,
@"UIApplicationWillChangeStatusBarOrientationNotification": UIApplicationWillChangeStatusBarOrientationNotification,
@"UIApplicationDidChangeStatusBarOrientationNotification": UIApplicationDidChangeStatusBarOrientationNotification,
@"UIApplicationStatusBarOrientationUserInfoKey": UIApplicationStatusBarOrientationUserInfoKey,
@"UIApplicationWillChangeStatusBarFrameNotification": UIApplicationWillChangeStatusBarFrameNotification,
@"UIApplicationDidChangeStatusBarFrameNotification": UIApplicationDidChangeStatusBarFrameNotification,
},
@"UIWindowNotifications": @{
@"UIWindowDidBecomeVisibleNotification": UIWindowDidBecomeVisibleNotification,
@"UIWindowDidBecomeHiddenNotification": UIWindowDidBecomeHiddenNotification,
@"UIWindowDidBecomeKeyNotification": UIWindowDidBecomeKeyNotification,
@"UIWindowDidResignKeyNotification": UIWindowDidResignKeyNotification,
},
@"UIKeyboardNotifications": @{
@"UIKeyboardWillShowNotification": UIKeyboardWillShowNotification,
@"UIKeyboardDidShowNotification": UIKeyboardDidShowNotification,
@"UIKeyboardWillHideNotification": UIKeyboardWillHideNotification,
@"UIKeyboardDidHideNotification": UIKeyboardDidHideNotification,
@"UIKeyboardWillChangeFrameNotification": UIKeyboardWillChangeFrameNotification,
@"UIKeyboardDidChangeFrameNotification": UIKeyboardDidChangeFrameNotification,
},
};
}
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end