|
@@ -1,65 +1,69 @@
|
1
|
1
|
package com.reactnativenavigation.react;
|
2
|
2
|
|
|
3
|
+import android.util.Log;
|
|
4
|
+
|
3
|
5
|
import com.facebook.react.bridge.Arguments;
|
4
|
6
|
import com.facebook.react.bridge.ReactContext;
|
5
|
7
|
import com.facebook.react.bridge.WritableMap;
|
6
|
8
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
7
|
9
|
|
|
10
|
+import javax.annotation.Nullable;
|
|
11
|
+
|
8
|
12
|
import static com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
|
9
|
13
|
|
10
|
14
|
public class EventEmitter {
|
11
|
|
- private static final String AppLaunched = "RNN.AppLaunched";
|
12
|
|
- private static final String CommandCompleted = "RNN.CommandCompleted";
|
13
|
|
- private static final String BottomTabSelected = "RNN.BottomTabSelected";
|
14
|
|
- private static final String ComponentDidAppear = "RNN.ComponentDidAppear";
|
15
|
|
- private static final String ComponentDidDisappear = "RNN.ComponentDidDisappear";
|
16
|
|
- private static final String NavigationButtonPressed = "RNN.NavigationButtonPressed";
|
17
|
|
- private static final String ModalDismissed = "RNN.ModalDismissed";
|
18
|
|
-
|
19
|
|
- private final RCTDeviceEventEmitter emitter;
|
|
15
|
+ private static final String AppLaunched = "RNN.AppLaunched";
|
|
16
|
+ private static final String CommandCompleted = "RNN.CommandCompleted";
|
|
17
|
+ private static final String BottomTabSelected = "RNN.BottomTabSelected";
|
|
18
|
+ private static final String ComponentDidAppear = "RNN.ComponentDidAppear";
|
|
19
|
+ private static final String ComponentDidDisappear = "RNN.ComponentDidDisappear";
|
|
20
|
+ private static final String NavigationButtonPressed = "RNN.NavigationButtonPressed";
|
|
21
|
+ private static final String ModalDismissed = "RNN.ModalDismissed";
|
|
22
|
+ @Nullable
|
|
23
|
+ private ReactContext reactContext;
|
20
|
24
|
|
21
|
|
- public EventEmitter(ReactContext reactContext) {
|
22
|
|
- this.emitter = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
23
|
|
- }
|
|
25
|
+ public EventEmitter(@Nullable ReactContext reactContext) {
|
|
26
|
+ this.reactContext = reactContext;
|
|
27
|
+ }
|
24
|
28
|
|
25
|
|
- public void appLaunched() {
|
26
|
|
- emit(AppLaunched);
|
27
|
|
- }
|
|
29
|
+ public void appLaunched() {
|
|
30
|
+ emit(AppLaunched);
|
|
31
|
+ }
|
28
|
32
|
|
29
|
|
- public void componentDidDisappear(String id, String componentName) {
|
30
|
|
- WritableMap event = Arguments.createMap();
|
31
|
|
- event.putString("componentId", id);
|
32
|
|
- event.putString("componentName", componentName);
|
33
|
|
- emit(ComponentDidDisappear, event);
|
34
|
|
- }
|
|
33
|
+ public void emitComponentDidDisappear(String id, String componentName) {
|
|
34
|
+ WritableMap event = Arguments.createMap();
|
|
35
|
+ event.putString("componentId", id);
|
|
36
|
+ event.putString("componentName", componentName);
|
|
37
|
+ emit(ComponentDidDisappear, event);
|
|
38
|
+ }
|
35
|
39
|
|
36
|
|
- public void componentDidAppear(String id, String componentName) {
|
37
|
|
- WritableMap event = Arguments.createMap();
|
38
|
|
- event.putString("componentId", id);
|
39
|
|
- event.putString("componentName", componentName);
|
40
|
|
- emit(ComponentDidAppear, event);
|
41
|
|
- }
|
|
40
|
+ public void emitComponentDidAppear(String id, String componentName) {
|
|
41
|
+ WritableMap event = Arguments.createMap();
|
|
42
|
+ event.putString("componentId", id);
|
|
43
|
+ event.putString("componentName", componentName);
|
|
44
|
+ emit(ComponentDidAppear, event);
|
|
45
|
+ }
|
42
|
46
|
|
43
|
|
- public void emitOnNavigationButtonPressed(String id, String buttonId) {
|
44
|
|
- WritableMap event = Arguments.createMap();
|
45
|
|
- event.putString("componentId", id);
|
46
|
|
- event.putString("buttonId", buttonId);
|
47
|
|
- emit(NavigationButtonPressed, event);
|
48
|
|
- }
|
|
47
|
+ public void emitOnNavigationButtonPressed(String id, String buttonId) {
|
|
48
|
+ WritableMap event = Arguments.createMap();
|
|
49
|
+ event.putString("componentId", id);
|
|
50
|
+ event.putString("buttonId", buttonId);
|
|
51
|
+ emit(NavigationButtonPressed, event);
|
|
52
|
+ }
|
49
|
53
|
|
50
|
|
- public void emitBottomTabSelected(int unselectedTabIndex, int selectedTabIndex) {
|
51
|
|
- WritableMap event = Arguments.createMap();
|
52
|
|
- event.putInt("unselectedTabIndex", unselectedTabIndex);
|
53
|
|
- event.putInt("selectedTabIndex", selectedTabIndex);
|
54
|
|
- emit(BottomTabSelected, event);
|
55
|
|
- }
|
|
54
|
+ public void emitBottomTabSelected(int unselectedTabIndex, int selectedTabIndex) {
|
|
55
|
+ WritableMap event = Arguments.createMap();
|
|
56
|
+ event.putInt("unselectedTabIndex", unselectedTabIndex);
|
|
57
|
+ event.putInt("selectedTabIndex", selectedTabIndex);
|
|
58
|
+ emit(BottomTabSelected, event);
|
|
59
|
+ }
|
56
|
60
|
|
57
|
|
- public void emitCommandCompleted(String commandId, long completionTime) {
|
58
|
|
- WritableMap event = Arguments.createMap();
|
59
|
|
- event.putString("commandId", commandId);
|
60
|
|
- event.putDouble("completionTime", completionTime);
|
61
|
|
- emit(CommandCompleted, event);
|
62
|
|
- }
|
|
61
|
+ public void emitCommandCompleted(String commandId, long completionTime) {
|
|
62
|
+ WritableMap event = Arguments.createMap();
|
|
63
|
+ event.putString("commandId", commandId);
|
|
64
|
+ event.putDouble("completionTime", completionTime);
|
|
65
|
+ emit(CommandCompleted, event);
|
|
66
|
+ }
|
63
|
67
|
|
64
|
68
|
public void emitModalDismissed(String id, int modalsDismissed) {
|
65
|
69
|
WritableMap event = Arguments.createMap();
|
|
@@ -68,11 +72,16 @@ public class EventEmitter {
|
68
|
72
|
emit(ModalDismissed, event);
|
69
|
73
|
}
|
70
|
74
|
|
71
|
|
- private void emit(String eventName) {
|
72
|
|
- emit(eventName, Arguments.createMap());
|
73
|
|
- }
|
|
75
|
+ private void emit(String eventName) {
|
|
76
|
+ emit(eventName, Arguments.createMap());
|
|
77
|
+ }
|
74
|
78
|
|
75
|
|
- private void emit(String eventName, WritableMap data) {
|
76
|
|
- emitter.emit(eventName, data);
|
77
|
|
- }
|
|
79
|
+ private void emit(String eventName, WritableMap data) {
|
|
80
|
+ if (reactContext == null) {
|
|
81
|
+ Log.e("RNN", "Could not send event " + eventName + ". React context is null!");
|
|
82
|
+ return;
|
|
83
|
+ }
|
|
84
|
+ RCTDeviceEventEmitter emitter = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
|
85
|
+ emitter.emit(eventName, data);
|
|
86
|
+ }
|
78
|
87
|
}
|