hybrid-navigation 4.0.8 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HybridNavigation.podspec +9 -1
- package/README.md +12 -4
- package/android/build.gradle +5 -4
- package/android/proguard-rules.pro +3 -0
- package/android/src/main/java/com/navigation/androidx/AwesomeActivity.java +114 -23
- package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +116 -13
- package/android/src/main/java/com/navigation/androidx/DrawableUtils.java +83 -18
- package/android/src/main/java/com/navigation/androidx/DrawerFragment.java +192 -28
- package/android/src/main/java/com/navigation/androidx/FragmentHelper.java +11 -0
- package/android/src/main/java/com/navigation/androidx/HybridDrawerLayout.java +563 -143
- package/android/src/main/java/com/navigation/androidx/LifecycleDelegate.java +47 -15
- package/android/src/main/java/com/navigation/androidx/PresentableActivity.java +3 -0
- package/android/src/main/java/com/navigation/androidx/PresentationDelegate.java +8 -3
- package/android/src/main/java/com/navigation/androidx/StackDelegate.java +29 -9
- package/android/src/main/java/com/navigation/androidx/StackFragment.java +28 -3
- package/android/src/main/java/com/navigation/androidx/SystemUI.java +59 -90
- package/android/src/main/java/com/navigation/androidx/TabBar.java +21 -2
- package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +24 -7
- package/android/src/main/java/com/navigation/androidx/TabView.java +11 -2
- package/android/src/main/java/com/reactnative/hybridnavigation/DispatchResult.java +59 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/Garden.java +18 -10
- package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +15 -2
- package/android/src/main/java/com/reactnative/hybridnavigation/HBDRootView.java +3 -8
- package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +113 -6
- package/android/src/main/java/com/reactnative/hybridnavigation/HybridNavigationPackage.java +13 -24
- package/android/src/main/java/com/reactnative/hybridnavigation/NativeEvent.java +5 -1
- package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +196 -194
- package/android/src/main/java/com/reactnative/hybridnavigation/NavigationCommandQueue.java +499 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/Navigator.java +4 -1
- package/android/src/main/java/com/reactnative/hybridnavigation/OrientationCoordinator.java +162 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactAppCompatActivity.java +103 -66
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +50 -87
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +156 -77
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactStackFragment.java +6 -6
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactSurfaceCleanup.java +3 -1
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarProvider.java +8 -1
- package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleState.java +21 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleStateMachine.java +116 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/ScreenOrientation.java +42 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/Utils.java +17 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/navigator/DrawerNavigator.java +14 -6
- package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +62 -22
- package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +35 -16
- package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +16 -13
- package/dist/DeepLink.d.ts +3 -2
- package/dist/DeepLink.js +37 -14
- package/dist/NativeEvent.d.ts +4 -12
- package/dist/NativeNavigation.d.ts +17 -19
- package/dist/Navigation.d.ts +21 -19
- package/dist/Navigation.js +83 -89
- package/dist/NavigationComponentWrap.d.ts +4 -3
- package/dist/NavigationComponentWrap.js +3 -6
- package/dist/Navigator.d.ts +31 -42
- package/dist/Navigator.js +97 -90
- package/dist/Options.d.ts +3 -1
- package/dist/Route.d.ts +85 -18
- package/dist/handler/DispatchCommandHandler.d.ts +10 -6
- package/dist/handler/DispatchCommandHandler.js +41 -17
- package/dist/handler/LayoutCommandHandler.d.ts +7 -7
- package/dist/handler/LayoutCommandHandler.js +21 -15
- package/dist/handler/LifecycleEventHandler.d.ts +26 -0
- package/dist/handler/LifecycleEventHandler.js +136 -0
- package/dist/handler/ResultEventHandler.d.ts +14 -9
- package/dist/handler/ResultEventHandler.js +88 -13
- package/dist/hooks.d.ts +5 -2
- package/dist/hooks.js +26 -4
- package/dist/index.d.ts +13 -0
- package/dist/index.js +0 -2
- package/dist/routeConfigRegistry.d.ts +6 -0
- package/dist/routeConfigRegistry.js +19 -0
- package/dist/router/drawer.d.ts +2 -2
- package/dist/router/drawer.js +6 -3
- package/dist/router/index.d.ts +8 -5
- package/dist/router/index.js +127 -43
- package/dist/router/screen.d.ts +2 -2
- package/dist/router/screen.js +1 -1
- package/dist/router/stack.d.ts +2 -2
- package/dist/router/stack.js +59 -30
- package/dist/router/tabs.d.ts +2 -2
- package/dist/router/tabs.js +4 -1
- package/dist/store.d.ts +2 -2
- package/ios/HBDGlobalStyle.h +26 -0
- package/ios/{GlobalStyle.mm → HBDGlobalStyle.mm} +30 -27
- package/ios/HybridNavigation.h +4 -1
- package/ios/Navigator/HBDDrawerNavigator.mm +24 -8
- package/ios/Navigator/HBDNavigatorRegistry.mm +1 -0
- package/ios/Navigator/HBDScreenNavigator.mm +106 -52
- package/ios/Navigator/HBDStackNavigator.mm +109 -61
- package/ios/Navigator/HBDTabNavigator.mm +32 -14
- package/ios/React/HBDNativeNavigation.mm +109 -131
- package/ios/React/HBDNavigator.h +29 -5
- package/ios/React/HBDOrientationCoordinator.h +27 -0
- package/ios/React/HBDOrientationCoordinator.mm +282 -0
- package/ios/React/HBDReactBridgeManager.h +37 -19
- package/ios/React/HBDReactBridgeManager.mm +490 -96
- package/ios/React/HBDSceneLifecycleStateMachine.h +38 -0
- package/ios/React/HBDSceneLifecycleStateMachine.mm +180 -0
- package/ios/View/HBDRootView.h +2 -0
- package/ios/View/HBDRootView.mm +148 -0
- package/ios/View/UIImage+WithBadge.h +3 -3
- package/ios/View/UIImage+WithBadge.mm +10 -15
- package/ios/ViewController/HBDDrawerController.mm +127 -6
- package/ios/ViewController/HBDNavigationController.mm +17 -4
- package/ios/ViewController/HBDReactViewController.mm +59 -121
- package/ios/ViewController/HBDTabBarController.h +4 -0
- package/ios/ViewController/HBDTabBarController.mm +59 -44
- package/ios/ViewController/HBDViewController.h +10 -3
- package/ios/ViewController/HBDViewController.mm +161 -147
- package/ios/ViewController/UINavigationController+HBD.h +2 -2
- package/ios/ViewController/UINavigationController+HBD.mm +3 -3
- package/ios/ViewController/UIViewController+HBD.h +17 -10
- package/ios/ViewController/UIViewController+HBD.mm +85 -69
- package/package.json +1 -1
- package/src/DeepLink.ts +41 -14
- package/src/NativeEvent.ts +4 -14
- package/src/NativeNavigation.ts +22 -33
- package/src/Navigation.ts +162 -121
- package/src/NavigationComponentWrap.tsx +9 -9
- package/src/Navigator.ts +191 -153
- package/src/Options.ts +3 -1
- package/src/Route.ts +134 -19
- package/src/handler/DispatchCommandHandler.ts +55 -21
- package/src/handler/LayoutCommandHandler.ts +36 -17
- package/src/handler/LifecycleEventHandler.ts +172 -0
- package/src/handler/ResultEventHandler.ts +112 -19
- package/src/hooks.ts +39 -7
- package/src/index.ts +26 -2
- package/src/routeConfigRegistry.ts +26 -0
- package/src/router/drawer.ts +8 -5
- package/src/router/index.ts +161 -50
- package/src/router/screen.ts +3 -3
- package/src/router/stack.ts +80 -29
- package/src/router/tabs.ts +15 -7
- package/src/store.ts +1 -1
- package/android/src/main/java/androidx/fragment/app/InternalFragment.java +0 -22
- package/android/src/main/java/com/navigation/androidx/SystemUI30.java +0 -87
- package/android/src/main/java/com/reactnative/hybridnavigation/NativeGarden.java +0 -176
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBar.java +0 -130
- package/android/src/main/res/layout/nav_tab_bar_react.xml +0 -32
- package/android/src/main/res/layout/nav_tab_bar_react_indeterminate.xml +0 -32
- package/dist/NativeGarden.d.ts +0 -13
- package/dist/NativeGarden.js +0 -2
- package/dist/handler/VisibilityEventHandler.d.ts +0 -13
- package/dist/handler/VisibilityEventHandler.js +0 -39
- package/ios/GlobalStyle.h +0 -15
- package/ios/React/HBDNativeGarden.h +0 -10
- package/ios/React/HBDNativeGarden.mm +0 -111
- package/src/NativeGarden.ts +0 -16
- package/src/handler/VisibilityEventHandler.ts +0 -52
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
package com.reactnative.hybridnavigation;
|
|
2
|
+
|
|
3
|
+
import android.os.Handler;
|
|
4
|
+
import android.os.Looper;
|
|
5
|
+
|
|
6
|
+
import androidx.annotation.NonNull;
|
|
7
|
+
import androidx.annotation.Nullable;
|
|
8
|
+
import androidx.fragment.app.FragmentManager;
|
|
9
|
+
|
|
10
|
+
import com.facebook.common.logging.FLog;
|
|
11
|
+
import com.facebook.react.bridge.ReactContext;
|
|
12
|
+
import com.facebook.react.bridge.ReadableMap;
|
|
13
|
+
import com.navigation.androidx.AwesomeFragment;
|
|
14
|
+
import com.navigation.androidx.FragmentHelper;
|
|
15
|
+
|
|
16
|
+
import java.util.ArrayDeque;
|
|
17
|
+
import java.util.ArrayList;
|
|
18
|
+
import java.util.Iterator;
|
|
19
|
+
import java.util.List;
|
|
20
|
+
import java.util.concurrent.atomic.AtomicBoolean;
|
|
21
|
+
|
|
22
|
+
final class NavigationCommandQueue {
|
|
23
|
+
|
|
24
|
+
interface ReadyTask {
|
|
25
|
+
void execute(@NonNull ReactAppCompatActivity activity);
|
|
26
|
+
|
|
27
|
+
void onCancelled();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private enum CommandType {
|
|
31
|
+
DISPATCH,
|
|
32
|
+
SET_ROOT
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
private static final class Command {
|
|
36
|
+
final CommandType type;
|
|
37
|
+
@Nullable
|
|
38
|
+
final String sceneId;
|
|
39
|
+
@Nullable
|
|
40
|
+
final String action;
|
|
41
|
+
@Nullable
|
|
42
|
+
final ReadableMap extras;
|
|
43
|
+
@Nullable
|
|
44
|
+
final ReadableMap layout;
|
|
45
|
+
final boolean sticky;
|
|
46
|
+
@Nullable
|
|
47
|
+
Navigator.Callback callback;
|
|
48
|
+
|
|
49
|
+
private Command(
|
|
50
|
+
@NonNull CommandType type,
|
|
51
|
+
@Nullable String sceneId,
|
|
52
|
+
@Nullable String action,
|
|
53
|
+
@Nullable ReadableMap extras,
|
|
54
|
+
@Nullable ReadableMap layout,
|
|
55
|
+
boolean sticky,
|
|
56
|
+
@Nullable Navigator.Callback callback
|
|
57
|
+
) {
|
|
58
|
+
this.type = type;
|
|
59
|
+
this.sceneId = sceneId;
|
|
60
|
+
this.action = action;
|
|
61
|
+
this.extras = extras;
|
|
62
|
+
this.layout = layout;
|
|
63
|
+
this.sticky = sticky;
|
|
64
|
+
this.callback = callback;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static Command dispatch(
|
|
68
|
+
@NonNull String sceneId,
|
|
69
|
+
@NonNull String action,
|
|
70
|
+
@NonNull ReadableMap extras,
|
|
71
|
+
@NonNull Navigator.Callback callback
|
|
72
|
+
) {
|
|
73
|
+
return new Command(CommandType.DISPATCH, sceneId, action, extras, null, false, callback);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static Command setRoot(
|
|
77
|
+
@NonNull ReadableMap layout,
|
|
78
|
+
boolean sticky,
|
|
79
|
+
@NonNull Navigator.Callback callback
|
|
80
|
+
) {
|
|
81
|
+
return new Command(CommandType.SET_ROOT, null, null, null, layout, sticky, callback);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private static final String TAG = "Navigation";
|
|
86
|
+
private static final long COMMAND_TIMEOUT_MS = 3000L;
|
|
87
|
+
|
|
88
|
+
private final ReactManager reactManager;
|
|
89
|
+
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
90
|
+
private final ArrayDeque<Command> commands = new ArrayDeque<>();
|
|
91
|
+
private final ArrayDeque<ReadyTask> readyTasks = new ArrayDeque<>();
|
|
92
|
+
|
|
93
|
+
@Nullable
|
|
94
|
+
private ReactAppCompatActivity currentActivity;
|
|
95
|
+
private boolean viewHierarchyReady = false;
|
|
96
|
+
private boolean flushingReadyTasks = false;
|
|
97
|
+
private boolean executingCommand = false;
|
|
98
|
+
private int commandGeneration = 0;
|
|
99
|
+
private long rootGeneration = 0;
|
|
100
|
+
@Nullable
|
|
101
|
+
private Command activeCommand;
|
|
102
|
+
@Nullable
|
|
103
|
+
private Runnable activeTimeoutRunnable;
|
|
104
|
+
@Nullable
|
|
105
|
+
private Navigator.Callback pendingInstallSetRootCallback;
|
|
106
|
+
@Nullable
|
|
107
|
+
private Navigator.Callback activeSetRootCallback;
|
|
108
|
+
|
|
109
|
+
NavigationCommandQueue(@NonNull ReactManager reactManager) {
|
|
110
|
+
this.reactManager = reactManager;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
void attachActivity(@NonNull ReactAppCompatActivity activity) {
|
|
114
|
+
this.currentActivity = activity;
|
|
115
|
+
this.viewHierarchyReady = false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
boolean detachActivity(@NonNull ReactAppCompatActivity activity) {
|
|
119
|
+
if (this.currentActivity == activity) {
|
|
120
|
+
this.currentActivity = null;
|
|
121
|
+
invalidate("Navigation host was destroyed.");
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
@Nullable
|
|
128
|
+
ReactAppCompatActivity getActiveActivity() {
|
|
129
|
+
if (currentActivity != null && !currentActivity.isFinishingOrDestroyed()) {
|
|
130
|
+
return currentActivity;
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
void onActivityResumed(@NonNull ReactAppCompatActivity activity) {
|
|
136
|
+
if (currentActivity != activity) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
drain();
|
|
140
|
+
if (viewHierarchyReady) {
|
|
141
|
+
flushReadyTasks();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
boolean isViewHierarchyReady() {
|
|
146
|
+
return viewHierarchyReady && getActiveActivity() != null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
void setViewHierarchyReady(boolean ready) {
|
|
150
|
+
viewHierarchyReady = ready;
|
|
151
|
+
if (ready) {
|
|
152
|
+
flushReadyTasks();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
boolean hasQueuedSetRoot() {
|
|
157
|
+
if (activeCommand != null && activeCommand.type == CommandType.SET_ROOT) {
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
for (Command command : commands) {
|
|
161
|
+
if (command.type == CommandType.SET_ROOT) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
void runWhenViewHierarchyReady(@NonNull ReadyTask task) {
|
|
169
|
+
readyTasks.add(task);
|
|
170
|
+
if (isViewHierarchyReady()) {
|
|
171
|
+
flushReadyTasks();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private void flushReadyTasks() {
|
|
176
|
+
ReactAppCompatActivity activity = getActiveActivity();
|
|
177
|
+
if (!viewHierarchyReady || flushingReadyTasks || activity == null) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
flushingReadyTasks = true;
|
|
182
|
+
try {
|
|
183
|
+
while (viewHierarchyReady && !readyTasks.isEmpty()) {
|
|
184
|
+
ReactAppCompatActivity current = getActiveActivity();
|
|
185
|
+
if (current == null) {
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
ReadyTask task = readyTasks.poll();
|
|
189
|
+
if (task != null) {
|
|
190
|
+
task.execute(current);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
} finally {
|
|
194
|
+
flushingReadyTasks = false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
void enqueueNavigation(
|
|
199
|
+
@NonNull String sceneId,
|
|
200
|
+
@NonNull String action,
|
|
201
|
+
@NonNull ReadableMap extras,
|
|
202
|
+
@NonNull Navigator.Callback callback
|
|
203
|
+
) {
|
|
204
|
+
if (getActiveActivity() == null) {
|
|
205
|
+
callback.invoke(DispatchResult.fail(DispatchResult.CODE_HOST_UNAVAILABLE, "Navigation host is unavailable."));
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
commands.add(Command.dispatch(sceneId, action, extras, callback));
|
|
209
|
+
drain();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
void enqueueSetRoot(
|
|
213
|
+
@NonNull ReadableMap layout,
|
|
214
|
+
boolean sticky,
|
|
215
|
+
@NonNull Navigator.Callback callback
|
|
216
|
+
) {
|
|
217
|
+
supersedeQueuedCommands(false, "Navigation command was superseded by a newer setRoot call.");
|
|
218
|
+
commands.add(Command.setRoot(layout, sticky, callback));
|
|
219
|
+
drain();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
void drain() {
|
|
223
|
+
ReactAppCompatActivity activity = getActiveActivity();
|
|
224
|
+
if (executingCommand || commands.isEmpty() || activity == null) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
Command next = commands.peek();
|
|
229
|
+
if (next == null) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if (next.type == CommandType.SET_ROOT) {
|
|
233
|
+
if (!reactManager.isReactModuleRegisterCompleted() || activity.getSupportFragmentManager().isStateSaved()) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
Command command = commands.poll();
|
|
239
|
+
if (command == null) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
executingCommand = true;
|
|
244
|
+
activeCommand = command;
|
|
245
|
+
final int generation = commandGeneration;
|
|
246
|
+
final AtomicBoolean completed = new AtomicBoolean(false);
|
|
247
|
+
|
|
248
|
+
final Navigator.Callback finishCommand = result -> {
|
|
249
|
+
if (!completed.compareAndSet(false, true)) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (activeTimeoutRunnable != null) {
|
|
253
|
+
mainHandler.removeCallbacks(activeTimeoutRunnable);
|
|
254
|
+
activeTimeoutRunnable = null;
|
|
255
|
+
}
|
|
256
|
+
Navigator.Callback cb = command.callback;
|
|
257
|
+
command.callback = null;
|
|
258
|
+
if (activeCommand == command) {
|
|
259
|
+
activeCommand = null;
|
|
260
|
+
}
|
|
261
|
+
if (generation != commandGeneration) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
if (cb != null) {
|
|
265
|
+
cb.invoke(result);
|
|
266
|
+
}
|
|
267
|
+
executingCommand = false;
|
|
268
|
+
mainHandler.post(this::drain);
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
Runnable timeoutRunnable = () -> {
|
|
272
|
+
if (!completed.get() && generation == commandGeneration) {
|
|
273
|
+
FLog.w(TAG, "Navigation command timed out, unlocking command queue. action=" + (command.action != null ? command.action : "setRoot"));
|
|
274
|
+
if (command.type == CommandType.SET_ROOT) {
|
|
275
|
+
rootGeneration++;
|
|
276
|
+
}
|
|
277
|
+
if (pendingInstallSetRootCallback == finishCommand) {
|
|
278
|
+
pendingInstallSetRootCallback = null;
|
|
279
|
+
}
|
|
280
|
+
if (activeSetRootCallback == finishCommand) {
|
|
281
|
+
activeSetRootCallback = null;
|
|
282
|
+
}
|
|
283
|
+
finishCommand.invoke(DispatchResult.fail(DispatchResult.CODE_NOT_READY, "Navigation command timed out."));
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
activeTimeoutRunnable = timeoutRunnable;
|
|
287
|
+
mainHandler.postDelayed(timeoutRunnable, COMMAND_TIMEOUT_MS);
|
|
288
|
+
|
|
289
|
+
if (command.type == CommandType.SET_ROOT) {
|
|
290
|
+
executeSetRoot(activity, command, finishCommand);
|
|
291
|
+
} else {
|
|
292
|
+
executeDispatch(activity, command, generation, completed, finishCommand);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private void executeSetRoot(
|
|
297
|
+
@NonNull ReactAppCompatActivity activity,
|
|
298
|
+
@NonNull Command command,
|
|
299
|
+
@NonNull Navigator.Callback finishCommand
|
|
300
|
+
) {
|
|
301
|
+
ReactContext reactContext = reactManager.getCurrentReactContext();
|
|
302
|
+
if (reactContext == null || !reactContext.hasActiveReactInstance()) {
|
|
303
|
+
FLog.w(TAG, "ReactContext hasn't active ReactInstance, skip action `setRoot`.");
|
|
304
|
+
finishCommand.invoke(DispatchResult.fail(DispatchResult.CODE_NOT_READY, "ReactContext has no active ReactInstance."));
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
AwesomeFragment fragment;
|
|
309
|
+
try {
|
|
310
|
+
fragment = reactManager.createFragment(command.layout);
|
|
311
|
+
} catch (RuntimeException e) {
|
|
312
|
+
FLog.e(TAG, "Failed to create root fragment", e);
|
|
313
|
+
finishCommand.invoke(DispatchResult.fail(DispatchResult.CODE_INVALID_ACTION, e.getMessage()));
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (fragment == null) {
|
|
317
|
+
finishCommand.invoke(DispatchResult.fail(DispatchResult.CODE_INVALID_ACTION, "Unable to create root fragment."));
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (command.layout != null) {
|
|
322
|
+
reactManager.recordRootLayout(command.layout, command.sticky);
|
|
323
|
+
}
|
|
324
|
+
pendingInstallSetRootCallback = finishCommand;
|
|
325
|
+
FLog.i(TAG, "Have active Activity and React module was registered, set root Fragment immediately.");
|
|
326
|
+
activity.setActivityRootFragment(fragment);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
private void executeDispatch(
|
|
330
|
+
@NonNull ReactAppCompatActivity activity,
|
|
331
|
+
@NonNull Command command,
|
|
332
|
+
int generation,
|
|
333
|
+
@NonNull AtomicBoolean completed,
|
|
334
|
+
@NonNull Navigator.Callback finishCommand
|
|
335
|
+
) {
|
|
336
|
+
activity.scheduleTaskAtStarted(() -> {
|
|
337
|
+
if (completed.get() || generation != commandGeneration) {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if (activity.isFinishingOrDestroyed()) {
|
|
341
|
+
finishCommand.invoke(DispatchResult.fail(DispatchResult.CODE_HOST_UNAVAILABLE, "Navigation host is unavailable."));
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
if (!isViewHierarchyReady()) {
|
|
345
|
+
finishCommand.invoke(DispatchResult.fail(DispatchResult.CODE_NOT_READY, "View hierarchy is not ready."));
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
String sceneId = command.sceneId;
|
|
350
|
+
String action = command.action;
|
|
351
|
+
ReadableMap extras = command.extras;
|
|
352
|
+
if (sceneId == null || action == null || extras == null) {
|
|
353
|
+
finishCommand.invoke(DispatchResult.fail(DispatchResult.CODE_INVALID_ACTION, "Invalid navigation command."));
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
AwesomeFragment target = FragmentHelper.findAwesomeFragment(activity.getSupportFragmentManager(), sceneId);
|
|
358
|
+
if (target == null) {
|
|
359
|
+
FLog.w(TAG, "Can't find target scene for action:" + action + ", maybe the scene is gone.\nparams: " + extras);
|
|
360
|
+
finishCommand.invoke(DispatchResult.fail(DispatchResult.CODE_SCENE_NOT_FOUND, "Can't find target scene for action: " + action));
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (!target.isAdded() || FragmentHelper.isRemoving(target)) {
|
|
365
|
+
finishCommand.invoke(DispatchResult.fail(DispatchResult.CODE_SCENE_NOT_FOUND, "Target scene is detached or being removed."));
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
reactManager.handleNavigation(target, action, extras, finishCommand);
|
|
370
|
+
}, () -> {
|
|
371
|
+
if (!completed.get() && generation == commandGeneration) {
|
|
372
|
+
finishCommand.invoke(DispatchResult.fail(DispatchResult.CODE_HOST_UNAVAILABLE, "Navigation host was destroyed."));
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
void installRootFragment(
|
|
378
|
+
@NonNull ReactAppCompatActivity activity,
|
|
379
|
+
@NonNull Runnable installAction
|
|
380
|
+
) {
|
|
381
|
+
long generation = ++rootGeneration;
|
|
382
|
+
boolean directNativeInstall = pendingInstallSetRootCallback == null;
|
|
383
|
+
Navigator.Callback previousCallback = activeSetRootCallback;
|
|
384
|
+
activeSetRootCallback = pendingInstallSetRootCallback;
|
|
385
|
+
pendingInstallSetRootCallback = null;
|
|
386
|
+
if (previousCallback != null && previousCallback != activeSetRootCallback) {
|
|
387
|
+
previousCallback.invoke(DispatchResult.fail(DispatchResult.CODE_SUPERSEDED, "setRoot was superseded by a newer call."));
|
|
388
|
+
}
|
|
389
|
+
if (directNativeInstall) {
|
|
390
|
+
supersedeQueuedCommands(true, "Navigation command was superseded by a root replacement.");
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
setViewHierarchyReady(false);
|
|
394
|
+
NativeEvent willEvent = NativeEvent.getInstance();
|
|
395
|
+
if (willEvent != null) {
|
|
396
|
+
willEvent.emitWillSetRoot();
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
installAction.run();
|
|
400
|
+
|
|
401
|
+
final Navigator.Callback capturedCallback = activeSetRootCallback;
|
|
402
|
+
activity.scheduleTaskAtStarted(() -> {
|
|
403
|
+
if (generation != rootGeneration || activity.isFinishingOrDestroyed()) {
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
FragmentManager fragmentManager = activity.getSupportFragmentManager();
|
|
407
|
+
if (!fragmentManager.isStateSaved()) {
|
|
408
|
+
fragmentManager.executePendingTransactions();
|
|
409
|
+
}
|
|
410
|
+
activity.updateBackPressedCallbackState();
|
|
411
|
+
setViewHierarchyReady(true);
|
|
412
|
+
if (activeSetRootCallback == capturedCallback) {
|
|
413
|
+
activeSetRootCallback = null;
|
|
414
|
+
}
|
|
415
|
+
if (capturedCallback != null) {
|
|
416
|
+
capturedCallback.invoke(DispatchResult.ok());
|
|
417
|
+
}
|
|
418
|
+
NativeEvent didEvent = NativeEvent.getInstance();
|
|
419
|
+
if (didEvent != null) {
|
|
420
|
+
didEvent.emitDidSetRoot();
|
|
421
|
+
}
|
|
422
|
+
}, () -> {
|
|
423
|
+
if (activeSetRootCallback == capturedCallback) {
|
|
424
|
+
activeSetRootCallback = null;
|
|
425
|
+
}
|
|
426
|
+
if (capturedCallback != null) {
|
|
427
|
+
capturedCallback.invoke(DispatchResult.fail(DispatchResult.CODE_HOST_UNAVAILABLE, "Navigation host was destroyed."));
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
void invalidate(@NonNull String reason) {
|
|
433
|
+
commandGeneration++;
|
|
434
|
+
rootGeneration++;
|
|
435
|
+
if (activeTimeoutRunnable != null) {
|
|
436
|
+
mainHandler.removeCallbacks(activeTimeoutRunnable);
|
|
437
|
+
activeTimeoutRunnable = null;
|
|
438
|
+
}
|
|
439
|
+
executingCommand = false;
|
|
440
|
+
|
|
441
|
+
Command running = activeCommand;
|
|
442
|
+
activeCommand = null;
|
|
443
|
+
List<Command> pendingCommands = new ArrayList<>(commands);
|
|
444
|
+
commands.clear();
|
|
445
|
+
|
|
446
|
+
Navigator.Callback pendingInstall = pendingInstallSetRootCallback;
|
|
447
|
+
pendingInstallSetRootCallback = null;
|
|
448
|
+
Navigator.Callback activeSetRoot = activeSetRootCallback;
|
|
449
|
+
activeSetRootCallback = null;
|
|
450
|
+
|
|
451
|
+
List<ReadyTask> cancelledReadyTasks = new ArrayList<>(readyTasks);
|
|
452
|
+
readyTasks.clear();
|
|
453
|
+
viewHierarchyReady = false;
|
|
454
|
+
|
|
455
|
+
DispatchResult unavailable = DispatchResult.fail(DispatchResult.CODE_HOST_UNAVAILABLE, reason);
|
|
456
|
+
if (running != null && running.callback != null) {
|
|
457
|
+
Navigator.Callback cb = running.callback;
|
|
458
|
+
running.callback = null;
|
|
459
|
+
cb.invoke(unavailable);
|
|
460
|
+
}
|
|
461
|
+
if (pendingInstall != null) {
|
|
462
|
+
pendingInstall.invoke(unavailable);
|
|
463
|
+
}
|
|
464
|
+
if (activeSetRoot != null && activeSetRoot != pendingInstall) {
|
|
465
|
+
activeSetRoot.invoke(unavailable);
|
|
466
|
+
}
|
|
467
|
+
for (Command command : pendingCommands) {
|
|
468
|
+
if (command.callback != null) {
|
|
469
|
+
Navigator.Callback cb = command.callback;
|
|
470
|
+
command.callback = null;
|
|
471
|
+
cb.invoke(unavailable);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
for (ReadyTask task : cancelledReadyTasks) {
|
|
475
|
+
task.onCancelled();
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
private void supersedeQueuedCommands(boolean dispatchOnly, @NonNull String message) {
|
|
480
|
+
if (commands.isEmpty()) {
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
List<Navigator.Callback> supersededCallbacks = new ArrayList<>();
|
|
484
|
+
Iterator<Command> iterator = commands.iterator();
|
|
485
|
+
while (iterator.hasNext()) {
|
|
486
|
+
Command queued = iterator.next();
|
|
487
|
+
if (!dispatchOnly || queued.type == CommandType.DISPATCH) {
|
|
488
|
+
if (queued.callback != null) {
|
|
489
|
+
supersededCallbacks.add(queued.callback);
|
|
490
|
+
queued.callback = null;
|
|
491
|
+
}
|
|
492
|
+
iterator.remove();
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
for (Navigator.Callback superseded : supersededCallbacks) {
|
|
496
|
+
superseded.invoke(DispatchResult.fail(DispatchResult.CODE_SUPERSEDED, message));
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
@@ -5,7 +5,6 @@ import android.os.Bundle;
|
|
|
5
5
|
import androidx.annotation.NonNull;
|
|
6
6
|
import androidx.annotation.Nullable;
|
|
7
7
|
|
|
8
|
-
import com.facebook.react.bridge.Callback;
|
|
9
8
|
import com.facebook.react.bridge.ReadableMap;
|
|
10
9
|
import com.navigation.androidx.AwesomeFragment;
|
|
11
10
|
import com.navigation.androidx.PresentationStyle;
|
|
@@ -14,6 +13,10 @@ import java.util.List;
|
|
|
14
13
|
|
|
15
14
|
public interface Navigator {
|
|
16
15
|
|
|
16
|
+
interface Callback {
|
|
17
|
+
void invoke(@NonNull DispatchResult result);
|
|
18
|
+
}
|
|
19
|
+
|
|
17
20
|
String MODE_NORMAL = "normal";
|
|
18
21
|
String MODE_MODAL = "modal";
|
|
19
22
|
String MODE_PRESENT = "present";
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
package com.reactnative.hybridnavigation;
|
|
2
|
+
|
|
3
|
+
import android.annotation.SuppressLint;
|
|
4
|
+
import android.content.pm.ActivityInfo;
|
|
5
|
+
import android.view.View;
|
|
6
|
+
|
|
7
|
+
import androidx.annotation.NonNull;
|
|
8
|
+
import androidx.annotation.Nullable;
|
|
9
|
+
import androidx.fragment.app.FragmentActivity;
|
|
10
|
+
|
|
11
|
+
import com.navigation.androidx.AwesomeFragment;
|
|
12
|
+
import com.navigation.androidx.FragmentHelper;
|
|
13
|
+
import com.navigation.androidx.PresentableActivity;
|
|
14
|
+
import com.navigation.androidx.PresentationStyle;
|
|
15
|
+
|
|
16
|
+
import java.util.HashSet;
|
|
17
|
+
import java.util.Set;
|
|
18
|
+
|
|
19
|
+
public class OrientationCoordinator {
|
|
20
|
+
|
|
21
|
+
private final FragmentActivity activity;
|
|
22
|
+
private final ReactManager reactManager;
|
|
23
|
+
@Nullable
|
|
24
|
+
private ScreenOrientation currentOrientation;
|
|
25
|
+
|
|
26
|
+
public OrientationCoordinator(@NonNull FragmentActivity activity, @NonNull ReactManager reactManager) {
|
|
27
|
+
this.activity = activity;
|
|
28
|
+
this.reactManager = reactManager;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private boolean isActivityUnavailable() {
|
|
32
|
+
return activity.isFinishing() || activity.isDestroyed();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@NonNull
|
|
36
|
+
public ScreenOrientation resolveOrientation(@NonNull HybridFragment fragment) {
|
|
37
|
+
return resolveOrientation(fragment, new HashSet<>());
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@NonNull
|
|
41
|
+
private ScreenOrientation resolveOrientation(@NonNull HybridFragment fragment, @NonNull Set<AwesomeFragment> visited) {
|
|
42
|
+
if (!visited.add(fragment)) {
|
|
43
|
+
return reactManager.getGlobalStyle().getDefaultOrientation();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
ScreenOrientation explicit = fragment.getExplicitOrientation();
|
|
47
|
+
if (explicit != null) {
|
|
48
|
+
return explicit;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
AwesomeFragment modalContainer = findModalContainer(fragment);
|
|
52
|
+
if (modalContainer != null) {
|
|
53
|
+
ScreenOrientation inherited = resolveModalInheritedOrientation(fragment, modalContainer, visited);
|
|
54
|
+
if (inherited != null) {
|
|
55
|
+
return inherited;
|
|
56
|
+
}
|
|
57
|
+
if (currentOrientation != null) {
|
|
58
|
+
return currentOrientation;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return reactManager.getGlobalStyle().getDefaultOrientation();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public boolean requiresOrientationChange(@NonNull HybridFragment fragment) {
|
|
66
|
+
ScreenOrientation target = resolveOrientation(fragment);
|
|
67
|
+
ScreenOrientation active = currentOrientation != null ? currentOrientation : reactManager.getGlobalStyle().getDefaultOrientation();
|
|
68
|
+
return target != active;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@SuppressLint("SourceLockedOrientationActivity")
|
|
72
|
+
public void applyForScene(@NonNull HybridFragment fragment) {
|
|
73
|
+
if (isActivityUnavailable() || !fragment.isAdded() || fragment.isResumingForRedirect()) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
ScreenOrientation target = resolveOrientation(fragment);
|
|
78
|
+
applyOrientation(target, fragment.getView());
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public void onSceneOptionsUpdated(@NonNull HybridFragment fragment) {
|
|
82
|
+
if (isActivityUnavailable() || !fragment.isAdded()) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (!fragment.isResumed() || fragment.isResumingForRedirect()) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
applyForScene(fragment);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
@SuppressLint("SourceLockedOrientationActivity")
|
|
92
|
+
private void applyOrientation(@NonNull ScreenOrientation target, @Nullable View sceneView) {
|
|
93
|
+
int requested = activity.getRequestedOrientation();
|
|
94
|
+
int desired = target.getActivityInfoOrientation();
|
|
95
|
+
currentOrientation = target;
|
|
96
|
+
|
|
97
|
+
if (target == ScreenOrientation.LANDSCAPE && isRequestedOrientationLandscape(requested)) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (requested == desired) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (target == ScreenOrientation.PORTRAIT && isRequestedOrientationLandscape(requested) && sceneView != null) {
|
|
106
|
+
sceneView.clearAnimation();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
activity.setRequestedOrientation(desired);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
@Nullable
|
|
113
|
+
private AwesomeFragment findModalContainer(@NonNull AwesomeFragment fragment) {
|
|
114
|
+
AwesomeFragment current = fragment;
|
|
115
|
+
while (current != null) {
|
|
116
|
+
if (current.getPresentationStyle() == PresentationStyle.OverFullScreen) {
|
|
117
|
+
return current;
|
|
118
|
+
}
|
|
119
|
+
current = current.getParentAwesomeFragment();
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
@Nullable
|
|
125
|
+
private ScreenOrientation resolveModalInheritedOrientation(@NonNull HybridFragment modalFragment, @NonNull AwesomeFragment modalContainer, @NonNull Set<AwesomeFragment> visited) {
|
|
126
|
+
String presentingSceneId = modalFragment.getPresentingSceneId();
|
|
127
|
+
if (presentingSceneId == null && modalContainer != modalFragment) {
|
|
128
|
+
presentingSceneId = FragmentHelper.getArguments(modalContainer).getString(PresentableActivity.ARG_PRESENTING_SCENE_ID);
|
|
129
|
+
}
|
|
130
|
+
if (presentingSceneId != null) {
|
|
131
|
+
AwesomeFragment presenting = FragmentHelper.findAwesomeFragment(activity.getSupportFragmentManager(), presentingSceneId);
|
|
132
|
+
if (presenting instanceof HybridFragment hybridPresenting && hybridPresenting != modalFragment && !visited.contains(hybridPresenting)) {
|
|
133
|
+
return resolveOrientation(hybridPresenting, visited);
|
|
134
|
+
}
|
|
135
|
+
if (presenting != null && presenting != modalFragment && presenting != modalContainer) {
|
|
136
|
+
HybridFragment primary = reactManager.primaryFragment(presenting);
|
|
137
|
+
if (primary != null && primary != modalFragment && !visited.contains(primary)) {
|
|
138
|
+
return resolveOrientation(primary, visited);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
AwesomeFragment presenting = modalContainer.getPresentingFragment();
|
|
144
|
+
if (presenting != null && presenting != modalFragment && presenting != modalContainer) {
|
|
145
|
+
HybridFragment primary = reactManager.primaryFragment(presenting);
|
|
146
|
+
if (primary != null && primary != modalFragment && !visited.contains(primary)) {
|
|
147
|
+
return resolveOrientation(primary, visited);
|
|
148
|
+
}
|
|
149
|
+
if (presenting instanceof HybridFragment hybridPresenting && !visited.contains(hybridPresenting)) {
|
|
150
|
+
return resolveOrientation(hybridPresenting, visited);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private boolean isRequestedOrientationLandscape(int requestedOrientation) {
|
|
157
|
+
return requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
|
|
158
|
+
|| requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
|
|
159
|
+
|| requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
|
|
160
|
+
|| requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE;
|
|
161
|
+
}
|
|
162
|
+
}
|