-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
RNNEventEmitter.m
164 lines (140 loc) · 5.66 KB
/
RNNEventEmitter.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#import "RNNEventEmitter.h"
#import "RNNUtils.h"
@implementation RNNEventEmitter {
NSInteger _appLaunchedListenerCount;
BOOL _appLaunchedEventDeferred;
}
RCT_EXPORT_MODULE();
static NSString *const AppLaunched = @"RNN.AppLaunched";
static NSString *const CommandCompleted = @"RNN.CommandCompleted";
static NSString *const BottomTabSelected = @"RNN.BottomTabSelected";
static NSString *const BottomTabLongPressed = @"RNN.BottomTabLongPressed";
static NSString *const ComponentWillAppear = @"RNN.ComponentWillAppear";
static NSString *const ComponentDidAppear = @"RNN.ComponentDidAppear";
static NSString *const ComponentDidDisappear = @"RNN.ComponentDidDisappear";
static NSString *const NavigationButtonPressed = @"RNN.NavigationButtonPressed";
static NSString *const ModalDismissed = @"RNN.ModalDismissed";
static NSString *const ModalAttemptedToDismiss = @"RNN.ModalAttemptedToDismiss";
static NSString *const SearchBarUpdated = @"RNN.SearchBarUpdated";
static NSString *const SearchBarCancelPressed = @"RNN.SearchBarCancelPressed";
static NSString *const PreviewCompleted = @"RNN.PreviewCompleted";
static NSString *const ScreenPopped = @"RNN.ScreenPopped";
static NSString *const BottomTabPressed = @"RNN.BottomTabPressed";
- (NSArray<NSString *> *)supportedEvents {
return @[
AppLaunched, CommandCompleted, BottomTabSelected, BottomTabLongPressed, BottomTabPressed,
ComponentWillAppear, ComponentDidAppear, ComponentDidDisappear, NavigationButtonPressed,
ModalDismissed, SearchBarUpdated, SearchBarCancelPressed, PreviewCompleted, ScreenPopped,
ModalAttemptedToDismiss
];
}
#pragma mark public
- (void)sendOnAppLaunched {
if (_appLaunchedListenerCount > 0) {
[self send:AppLaunched body:nil];
} else {
_appLaunchedEventDeferred = TRUE;
}
}
- (void)sendComponentWillAppear:(NSString *)componentId
componentName:(NSString *)componentName
componentType:(NSString *)componentType {
[self send:ComponentWillAppear
body:@{
@"componentId" : componentId,
@"componentName" : componentName,
@"componentType" : componentType
}];
}
- (void)sendComponentDidAppear:(NSString *)componentId
componentName:(NSString *)componentName
componentType:(NSString *)componentType {
[self send:ComponentDidAppear
body:@{
@"componentId" : componentId,
@"componentName" : componentName,
@"componentType" : componentType
}];
}
- (void)sendComponentDidDisappear:(NSString *)componentId
componentName:(NSString *)componentName
componentType:(NSString *)componentType {
[self send:ComponentDidDisappear
body:@{
@"componentId" : componentId,
@"componentName" : componentName,
@"componentType" : componentType
}];
}
- (void)sendOnNavigationButtonPressed:(NSString *)componentId buttonId:(NSString *)buttonId {
[self send:NavigationButtonPressed
body:@{@"componentId" : componentId, @"buttonId" : buttonId}];
}
- (void)sendBottomTabSelected:(NSNumber *)selectedTabIndex
unselected:(NSNumber *)unselectedTabIndex {
[self
send:BottomTabSelected
body:@{@"selectedTabIndex" : selectedTabIndex, @"unselectedTabIndex" : unselectedTabIndex}];
}
- (void)sendBottomTabLongPressed:(NSNumber *)selectedTabIndex {
[self send:BottomTabLongPressed body:@{@"selectedTabIndex" : selectedTabIndex}];
}
- (void)sendBottomTabPressed:(NSNumber *)tabIndex {
[self send:BottomTabPressed body:@{@"tabIndex" : tabIndex}];
}
- (void)sendOnNavigationCommandCompletion:(NSString *)commandName commandId:(NSString *)commandId {
[self send:CommandCompleted
body:@{
@"commandId" : commandId,
@"commandName" : commandName,
@"completionTime" : [RNNUtils getCurrentTimestamp]
}];
}
- (void)sendOnSearchBarUpdated:(NSString *)componentId
text:(NSString *)text
isFocused:(BOOL)isFocused {
[self send:SearchBarUpdated
body:@{@"componentId" : componentId, @"text" : text, @"isFocused" : @(isFocused)}];
}
- (void)sendOnSearchBarCancelPressed:(NSString *)componentId {
[self send:SearchBarCancelPressed body:@{@"componentId" : componentId}];
}
- (void)sendOnPreviewCompleted:(NSString *)componentId
previewComponentId:(NSString *)previewComponentId {
[self send:PreviewCompleted
body:@{@"componentId" : componentId, @"previewComponentId" : previewComponentId}];
}
- (void)sendModalsDismissedEvent:(NSString *)componentId
numberOfModalsDismissed:(NSNumber *)modalsDismissed {
if (componentId) {
[self send:ModalDismissed
body:@{@"componentId" : componentId, @"modalsDismissed" : modalsDismissed}];
}
}
- (void)sendModalAttemptedToDismissEvent:(NSString *)componentId {
[self send:ModalAttemptedToDismiss
body:@{
@"componentId" : componentId,
}];
}
- (void)sendScreenPoppedEvent:(NSString *)componentId {
[self send:ScreenPopped body:@{@"componentId" : componentId}];
}
- (void)addListener:(NSString *)eventName {
[super addListener:eventName];
if ([eventName isEqualToString:AppLaunched]) {
_appLaunchedListenerCount++;
if (_appLaunchedEventDeferred) {
_appLaunchedEventDeferred = FALSE;
[self sendOnAppLaunched];
}
}
}
#pragma mark private
- (void)send:(NSString *)eventName body:(id)body {
if (self.bridge == nil) {
return;
}
[self sendEventWithName:eventName body:body];
}
@end