|
@@ -25,161 +25,169 @@ import com.reactnativenavigation.utils.TypefaceLoader;
|
25
|
25
|
import com.reactnativenavigation.utils.UiThread;
|
26
|
26
|
import com.reactnativenavigation.utils.UiUtils;
|
27
|
27
|
import com.reactnativenavigation.viewcontrollers.ViewController;
|
28
|
|
-import com.reactnativenavigation.viewcontrollers.externalcomponent.ExternalComponentCreator;
|
29
|
28
|
import com.reactnativenavigation.viewcontrollers.navigator.Navigator;
|
30
|
29
|
|
31
|
30
|
import java.util.ArrayList;
|
32
|
|
-import java.util.Map;
|
33
|
31
|
|
34
|
32
|
public class NavigationModule extends ReactContextBaseJavaModule {
|
35
|
|
- private static final String NAME = "RNNBridgeModule";
|
|
33
|
+ private static final String NAME = "RNNBridgeModule";
|
36
|
34
|
|
37
|
35
|
private final Now now = new Now();
|
38
|
|
- private final ReactInstanceManager reactInstanceManager;
|
|
36
|
+ private final ReactInstanceManager reactInstanceManager;
|
|
37
|
+ private final JSONParser jsonParser;
|
|
38
|
+ private final LayoutFactory layoutFactory;
|
39
|
39
|
private EventEmitter eventEmitter;
|
40
|
40
|
|
41
|
41
|
@SuppressWarnings("WeakerAccess")
|
42
|
|
- public NavigationModule(ReactApplicationContext reactContext, ReactInstanceManager reactInstanceManager) {
|
43
|
|
- super(reactContext);
|
44
|
|
- this.reactInstanceManager = reactInstanceManager;
|
45
|
|
- reactInstanceManager.addReactInstanceEventListener(context -> eventEmitter = new EventEmitter(context));
|
|
42
|
+ public NavigationModule(ReactApplicationContext reactContext, ReactInstanceManager reactInstanceManager, LayoutFactory layoutFactory) {
|
|
43
|
+ this(reactContext, reactInstanceManager, new JSONParser(), layoutFactory);
|
46
|
44
|
}
|
47
|
45
|
|
48
|
|
- @Override
|
49
|
|
- public String getName() {
|
50
|
|
- return NAME;
|
51
|
|
- }
|
|
46
|
+ public NavigationModule(ReactApplicationContext reactContext, ReactInstanceManager reactInstanceManager, JSONParser jsonParser, LayoutFactory layoutFactory) {
|
|
47
|
+ super(reactContext);
|
|
48
|
+ this.reactInstanceManager = reactInstanceManager;
|
|
49
|
+ this.jsonParser = jsonParser;
|
|
50
|
+ this.layoutFactory = layoutFactory;
|
|
51
|
+ reactContext.addLifecycleEventListener(new LifecycleEventListenerAdapter() {
|
|
52
|
+ @Override
|
|
53
|
+ public void onHostResume() {
|
|
54
|
+ eventEmitter = new EventEmitter(reactContext);
|
|
55
|
+ navigator().setEventEmitter(eventEmitter);
|
|
56
|
+ layoutFactory.init(
|
|
57
|
+ activity(),
|
|
58
|
+ eventEmitter,
|
|
59
|
+ navigator().getChildRegistry(),
|
|
60
|
+ ((NavigationApplication) activity().getApplication()).getExternalComponents()
|
|
61
|
+ );
|
|
62
|
+ }
|
|
63
|
+ });
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ @NonNull
|
|
67
|
+ @Override
|
|
68
|
+ public String getName() {
|
|
69
|
+ return NAME;
|
|
70
|
+ }
|
52
|
71
|
|
53
|
|
- @ReactMethod
|
|
72
|
+ @ReactMethod
|
54
|
73
|
public void getConstants(Promise promise) {
|
55
|
74
|
ReactApplicationContext ctx = getReactApplicationContext();
|
56
|
75
|
WritableMap constants = Arguments.createMap();
|
57
|
|
- constants.putString(Constants.BACK_BUTTON_JS_KEY, Constants.BACK_BUTTON_ID);
|
58
|
|
- constants.putDouble(Constants.BOTTOM_TABS_HEIGHT_KEY, Constants.BOTTOM_TABS_HEIGHT);
|
|
76
|
+ constants.putString(Constants.BACK_BUTTON_JS_KEY, Constants.BACK_BUTTON_ID);
|
|
77
|
+ constants.putDouble(Constants.BOTTOM_TABS_HEIGHT_KEY, Constants.BOTTOM_TABS_HEIGHT);
|
59
|
78
|
constants.putDouble(Constants.STATUS_BAR_HEIGHT_KEY, UiUtils.pxToDp(ctx, UiUtils.getStatusBarHeight(ctx)));
|
60
|
|
- constants.putDouble(Constants.TOP_BAR_HEIGHT_KEY, UiUtils.pxToDp(ctx, UiUtils.getTopBarHeight(ctx)));
|
|
79
|
+ constants.putDouble(Constants.TOP_BAR_HEIGHT_KEY, UiUtils.pxToDp(ctx, UiUtils.getTopBarHeight(ctx)));
|
61
|
80
|
promise.resolve(constants);
|
62
|
81
|
}
|
63
|
82
|
|
64
|
|
- @ReactMethod
|
65
|
|
- public void setRoot(String commandId, ReadableMap rawLayoutTree, Promise promise) {
|
66
|
|
- final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree).optJSONObject("root"));
|
67
|
|
- handle(() -> {
|
68
|
|
- navigator().setEventEmitter(eventEmitter);
|
69
|
|
- final ViewController viewController = newLayoutFactory().create(layoutTree);
|
|
83
|
+ @ReactMethod
|
|
84
|
+ public void setRoot(String commandId, ReadableMap rawLayoutTree, Promise promise) {
|
|
85
|
+ final LayoutNode layoutTree = LayoutNodeParser.parse(jsonParser.parse(rawLayoutTree).optJSONObject("root"));
|
|
86
|
+ handle(() -> {
|
|
87
|
+ final ViewController viewController = layoutFactory.create(layoutTree);
|
70
|
88
|
navigator().setRoot(viewController, new NativeCommandListener(commandId, promise, eventEmitter, now), reactInstanceManager);
|
71
|
89
|
});
|
72
|
|
- }
|
|
90
|
+ }
|
73
|
91
|
|
74
|
|
- @ReactMethod
|
75
|
|
- public void setDefaultOptions(ReadableMap options) {
|
76
|
|
- handle(() -> navigator().setDefaultOptions(parse(options)));
|
|
92
|
+ @ReactMethod
|
|
93
|
+ public void setDefaultOptions(ReadableMap options) {
|
|
94
|
+ handle(() -> {
|
|
95
|
+ Options defaultOptions = parse(options);
|
|
96
|
+ layoutFactory.setDefaultOptions(defaultOptions);
|
|
97
|
+ navigator().setDefaultOptions(defaultOptions);
|
|
98
|
+ });
|
77
|
99
|
}
|
78
|
100
|
|
79
|
|
- @ReactMethod
|
80
|
|
- public void mergeOptions(String onComponentId, @Nullable ReadableMap options) {
|
81
|
|
- handle(() -> navigator().mergeOptions(onComponentId, parse(options)));
|
82
|
|
- }
|
|
101
|
+ @ReactMethod
|
|
102
|
+ public void mergeOptions(String onComponentId, @Nullable ReadableMap options) {
|
|
103
|
+ handle(() -> navigator().mergeOptions(onComponentId, parse(options)));
|
|
104
|
+ }
|
83
|
105
|
|
84
|
|
- @ReactMethod
|
85
|
|
- public void push(String commandId, String onComponentId, ReadableMap rawLayoutTree, Promise promise) {
|
86
|
|
- final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
|
87
|
|
- handle(() -> {
|
88
|
|
- final ViewController viewController = newLayoutFactory().create(layoutTree);
|
|
106
|
+ @ReactMethod
|
|
107
|
+ public void push(String commandId, String onComponentId, ReadableMap rawLayoutTree, Promise promise) {
|
|
108
|
+ final LayoutNode layoutTree = LayoutNodeParser.parse(jsonParser.parse(rawLayoutTree));
|
|
109
|
+ handle(() -> {
|
|
110
|
+ final ViewController viewController = layoutFactory.create(layoutTree);
|
89
|
111
|
navigator().push(onComponentId, viewController, new NativeCommandListener(commandId, promise, eventEmitter, now));
|
90
|
112
|
});
|
91
|
|
- }
|
|
113
|
+ }
|
92
|
114
|
|
93
|
115
|
@ReactMethod
|
94
|
116
|
public void setStackRoot(String commandId, String onComponentId, ReadableArray children, Promise promise) {
|
95
|
117
|
handle(() -> {
|
96
|
118
|
ArrayList<ViewController> _children = new ArrayList();
|
97
|
119
|
for (int i = 0; i < children.size(); i++) {
|
98
|
|
- final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(children.getMap(i)));
|
99
|
|
- _children.add(newLayoutFactory().create(layoutTree));
|
|
120
|
+ final LayoutNode layoutTree = LayoutNodeParser.parse(jsonParser.parse(children.getMap(i)));
|
|
121
|
+ _children.add(layoutFactory.create(layoutTree));
|
100
|
122
|
}
|
101
|
123
|
navigator().setStackRoot(onComponentId, _children, new NativeCommandListener(commandId, promise, eventEmitter, now));
|
102
|
124
|
});
|
103
|
125
|
}
|
104
|
126
|
|
105
|
|
- @ReactMethod
|
106
|
|
- public void pop(String commandId, String componentId, @Nullable ReadableMap mergeOptions, Promise promise) {
|
107
|
|
- handle(() -> navigator().pop(componentId, parse(mergeOptions), new NativeCommandListener(commandId, promise, eventEmitter, now)));
|
108
|
|
- }
|
|
127
|
+ @ReactMethod
|
|
128
|
+ public void pop(String commandId, String componentId, @Nullable ReadableMap mergeOptions, Promise promise) {
|
|
129
|
+ handle(() -> navigator().pop(componentId, parse(mergeOptions), new NativeCommandListener(commandId, promise, eventEmitter, now)));
|
|
130
|
+ }
|
109
|
131
|
|
110
|
|
- @ReactMethod
|
111
|
|
- public void popTo(String commandId, String componentId, @Nullable ReadableMap mergeOptions, Promise promise) {
|
112
|
|
- handle(() -> navigator().popTo(componentId, parse(mergeOptions), new NativeCommandListener(commandId, promise, eventEmitter, now)));
|
113
|
|
- }
|
|
132
|
+ @ReactMethod
|
|
133
|
+ public void popTo(String commandId, String componentId, @Nullable ReadableMap mergeOptions, Promise promise) {
|
|
134
|
+ handle(() -> navigator().popTo(componentId, parse(mergeOptions), new NativeCommandListener(commandId, promise, eventEmitter, now)));
|
|
135
|
+ }
|
114
|
136
|
|
115
|
|
- @ReactMethod
|
116
|
|
- public void popToRoot(String commandId, String componentId, @Nullable ReadableMap mergeOptions, Promise promise) {
|
117
|
|
- handle(() -> navigator().popToRoot(componentId, parse(mergeOptions), new NativeCommandListener(commandId, promise, eventEmitter, now)));
|
118
|
|
- }
|
|
137
|
+ @ReactMethod
|
|
138
|
+ public void popToRoot(String commandId, String componentId, @Nullable ReadableMap mergeOptions, Promise promise) {
|
|
139
|
+ handle(() -> navigator().popToRoot(componentId, parse(mergeOptions), new NativeCommandListener(commandId, promise, eventEmitter, now)));
|
|
140
|
+ }
|
119
|
141
|
|
120
|
|
- @ReactMethod
|
121
|
|
- public void showModal(String commandId, ReadableMap rawLayoutTree, Promise promise) {
|
122
|
|
- final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
|
123
|
|
- handle(() -> {
|
124
|
|
- final ViewController viewController = newLayoutFactory().create(layoutTree);
|
|
142
|
+ @ReactMethod
|
|
143
|
+ public void showModal(String commandId, ReadableMap rawLayoutTree, Promise promise) {
|
|
144
|
+ final LayoutNode layoutTree = LayoutNodeParser.parse(jsonParser.parse(rawLayoutTree));
|
|
145
|
+ handle(() -> {
|
|
146
|
+ final ViewController viewController = layoutFactory.create(layoutTree);
|
125
|
147
|
navigator().showModal(viewController, new NativeCommandListener(commandId, promise, eventEmitter, now));
|
126
|
148
|
});
|
127
|
|
- }
|
|
149
|
+ }
|
128
|
150
|
|
129
|
|
- @ReactMethod
|
130
|
|
- public void dismissModal(String commandId, String componentId, @Nullable ReadableMap mergeOptions, Promise promise) {
|
131
|
|
- handle(() -> {
|
|
151
|
+ @ReactMethod
|
|
152
|
+ public void dismissModal(String commandId, String componentId, @Nullable ReadableMap mergeOptions, Promise promise) {
|
|
153
|
+ handle(() -> {
|
132
|
154
|
navigator().mergeOptions(componentId, parse(mergeOptions));
|
133
|
155
|
navigator().dismissModal(componentId, new NativeCommandListener(commandId, promise, eventEmitter, now));
|
134
|
156
|
});
|
135
|
|
- }
|
|
157
|
+ }
|
136
|
158
|
|
137
|
159
|
@ReactMethod
|
138
|
|
- public void dismissAllModals(String commandId, @Nullable ReadableMap mergeOptions, Promise promise) {
|
139
|
|
- handle(() -> navigator().dismissAllModals(parse(mergeOptions), new NativeCommandListener(commandId, promise, eventEmitter, now)));
|
140
|
|
- }
|
|
160
|
+ public void dismissAllModals(String commandId, @Nullable ReadableMap mergeOptions, Promise promise) {
|
|
161
|
+ handle(() -> navigator().dismissAllModals(parse(mergeOptions), new NativeCommandListener(commandId, promise, eventEmitter, now)));
|
|
162
|
+ }
|
141
|
163
|
|
142
|
|
- @ReactMethod
|
143
|
|
- public void showOverlay(String commandId, ReadableMap rawLayoutTree, Promise promise) {
|
144
|
|
- final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
|
|
164
|
+ @ReactMethod
|
|
165
|
+ public void showOverlay(String commandId, ReadableMap rawLayoutTree, Promise promise) {
|
|
166
|
+ final LayoutNode layoutTree = LayoutNodeParser.parse(jsonParser.parse(rawLayoutTree));
|
145
|
167
|
handle(() -> {
|
146
|
|
- final ViewController viewController = newLayoutFactory().create(layoutTree);
|
|
168
|
+ final ViewController viewController = layoutFactory.create(layoutTree);
|
147
|
169
|
navigator().showOverlay(viewController, new NativeCommandListener(commandId, promise, eventEmitter, now));
|
148
|
170
|
});
|
149
|
|
- }
|
150
|
|
-
|
151
|
|
- @ReactMethod
|
152
|
|
- public void dismissOverlay(String commandId, String componentId, Promise promise) {
|
153
|
|
- handle(() -> navigator().dismissOverlay(componentId, new NativeCommandListener(commandId, promise, eventEmitter, now)));
|
154
|
|
- }
|
155
|
|
-
|
156
|
|
- private Navigator navigator() {
|
157
|
|
- return activity().getNavigator();
|
158
|
|
- }
|
|
171
|
+ }
|
159
|
172
|
|
160
|
|
- @NonNull
|
161
|
|
- private LayoutFactory newLayoutFactory() {
|
162
|
|
- return new LayoutFactory(activity(),
|
163
|
|
- navigator().getChildRegistry(),
|
164
|
|
- reactInstanceManager,
|
165
|
|
- eventEmitter,
|
166
|
|
- externalComponentCreator(),
|
167
|
|
- navigator().getDefaultOptions()
|
168
|
|
- );
|
169
|
|
- }
|
|
173
|
+ @ReactMethod
|
|
174
|
+ public void dismissOverlay(String commandId, String componentId, Promise promise) {
|
|
175
|
+ handle(() -> navigator().dismissOverlay(componentId, new NativeCommandListener(commandId, promise, eventEmitter, now)));
|
|
176
|
+ }
|
170
|
177
|
|
171
|
|
- private Options parse(@Nullable ReadableMap mergeOptions) {
|
172
|
|
- return mergeOptions == null ? Options.EMPTY : Options.parse(new TypefaceLoader(activity()), JSONParser.parse(mergeOptions));
|
|
178
|
+ private Navigator navigator() {
|
|
179
|
+ return activity().getNavigator();
|
173
|
180
|
}
|
174
|
181
|
|
175
|
|
- private Map<String, ExternalComponentCreator> externalComponentCreator() {
|
176
|
|
- return ((NavigationApplication) activity().getApplication()).getExternalComponents();
|
|
182
|
+ private Options parse(@Nullable ReadableMap mergeOptions) {
|
|
183
|
+ return mergeOptions ==
|
|
184
|
+ null ? Options.EMPTY : Options.parse(new TypefaceLoader(activity()), jsonParser.parse(mergeOptions));
|
177
|
185
|
}
|
178
|
186
|
|
179
|
|
- private void handle(Runnable task) {
|
180
|
|
- if (activity() == null || activity().isFinishing()) return;
|
181
|
|
- UiThread.post(task);
|
182
|
|
- }
|
|
187
|
+ private void handle(Runnable task) {
|
|
188
|
+ if (activity() == null || activity().isFinishing()) return;
|
|
189
|
+ UiThread.post(task);
|
|
190
|
+ }
|
183
|
191
|
|
184
|
192
|
private NavigationActivity activity() {
|
185
|
193
|
return (NavigationActivity) getCurrentActivity();
|