hybrid-navigation 5.0.0 → 5.1.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.
Files changed (107) hide show
  1. package/android/src/main/java/com/navigation/androidx/AwesomeActivity.java +2 -4
  2. package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +48 -2
  3. package/android/src/main/java/com/navigation/androidx/DefaultTabBarProvider.java +2 -2
  4. package/android/src/main/java/com/navigation/androidx/FragmentHelper.java +7 -0
  5. package/android/src/main/java/com/navigation/androidx/PresentationDelegate.java +2 -4
  6. package/android/src/main/java/com/navigation/androidx/StackFragment.java +3 -0
  7. package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +50 -0
  8. package/android/src/main/java/com/navigation/androidx/TabBarItem.java +4 -0
  9. package/android/src/main/java/com/reactnative/hybridnavigation/DispatchResult.java +16 -4
  10. package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +19 -0
  11. package/android/src/main/java/com/reactnative/hybridnavigation/HBDRootView.java +17 -2
  12. package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +66 -7
  13. package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +116 -18
  14. package/android/src/main/java/com/reactnative/hybridnavigation/NavigationCommandQueue.java +8 -0
  15. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +376 -8
  16. package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +72 -2
  17. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarFragment.java +26 -0
  18. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +134 -12
  19. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +35 -3
  20. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +29 -4
  21. package/dist/NativeEvent.d.ts +15 -0
  22. package/dist/NativeNavigation.d.ts +8 -0
  23. package/dist/Navigation.d.ts +14 -1
  24. package/dist/Navigation.js +153 -12
  25. package/dist/NavigationComponentWrap.d.ts +3 -2
  26. package/dist/Navigator.d.ts +3 -2
  27. package/dist/Navigator.js +28 -9
  28. package/dist/Options.d.ts +12 -2
  29. package/dist/Route.d.ts +70 -5
  30. package/dist/defineRoutes.d.ts +19 -0
  31. package/dist/defineRoutes.js +6 -0
  32. package/dist/handler/DispatchCommandHandler.d.ts +16 -1
  33. package/dist/handler/DispatchCommandHandler.js +63 -5
  34. package/dist/handler/LayoutCommandHandler.d.ts +1 -0
  35. package/dist/handler/LayoutCommandHandler.js +3 -0
  36. package/dist/handler/LeaveGuardHandler.d.ts +17 -0
  37. package/dist/handler/LeaveGuardHandler.js +350 -0
  38. package/dist/handler/LifecycleEventHandler.d.ts +7 -3
  39. package/dist/handler/LifecycleEventHandler.js +36 -3
  40. package/dist/handler/PresentationCoordinator.d.ts +24 -0
  41. package/dist/handler/PresentationCoordinator.js +449 -0
  42. package/dist/handler/ResultEventHandler.d.ts +7 -0
  43. package/dist/handler/ResultEventHandler.js +13 -5
  44. package/dist/handler/TabEventHandler.d.ts +8 -0
  45. package/dist/handler/TabEventHandler.js +25 -0
  46. package/dist/hooks.d.ts +3 -1
  47. package/dist/hooks.js +20 -0
  48. package/dist/index.d.ts +11 -0
  49. package/dist/index.js +3 -0
  50. package/dist/reveal.d.ts +46 -0
  51. package/dist/reveal.js +431 -0
  52. package/dist/routeGraph.d.ts +12 -0
  53. package/dist/routeGraph.js +68 -0
  54. package/dist/router/index.js +5 -41
  55. package/dist/router/tabs.d.ts +1 -0
  56. package/dist/store.d.ts +2 -0
  57. package/dist/store.js +4 -0
  58. package/dist/testing.d.ts +79 -0
  59. package/dist/testing.js +251 -0
  60. package/ios/HBDGlobalStyle.h +3 -0
  61. package/ios/HBDGlobalStyle.mm +13 -0
  62. package/ios/HybridNavigation.h +1 -0
  63. package/ios/Navigator/HBDScreenNavigator.mm +177 -11
  64. package/ios/Navigator/HBDStackNavigator.mm +50 -7
  65. package/ios/Navigator/HBDTabNavigator.mm +33 -5
  66. package/ios/React/HBDNativeNavigation.mm +73 -12
  67. package/ios/React/HBDNavigator.h +8 -0
  68. package/ios/React/HBDOrientationCoordinator.h +2 -0
  69. package/ios/React/HBDOrientationCoordinator.mm +27 -0
  70. package/ios/React/HBDReactBridgeManager.h +10 -0
  71. package/ios/React/HBDReactBridgeManager.mm +48 -2
  72. package/ios/ViewController/HBDNavigationController.mm +14 -0
  73. package/ios/ViewController/HBDReactViewController.h +6 -0
  74. package/ios/ViewController/HBDReactViewController.mm +177 -1
  75. package/ios/ViewController/HBDTabBarController.h +11 -0
  76. package/ios/ViewController/HBDTabBarController.mm +170 -3
  77. package/ios/ViewController/HBDTabBarRenderer.h +28 -0
  78. package/ios/ViewController/HBDViewController.h +1 -0
  79. package/ios/ViewController/HBDViewController.mm +57 -9
  80. package/ios/ViewController/UIViewController+HBD.h +1 -0
  81. package/ios/ViewController/UIViewController+HBD.mm +13 -4
  82. package/package.json +3 -1
  83. package/src/NativeEvent.ts +17 -0
  84. package/src/NativeNavigation.ts +9 -0
  85. package/src/Navigation.ts +228 -12
  86. package/src/NavigationComponentWrap.tsx +11 -2
  87. package/src/Navigator.ts +38 -9
  88. package/src/Options.ts +9 -2
  89. package/src/Route.ts +102 -6
  90. package/src/defineRoutes.ts +46 -0
  91. package/src/handler/DispatchCommandHandler.ts +110 -11
  92. package/src/handler/LayoutCommandHandler.ts +4 -0
  93. package/src/handler/LeaveGuardHandler.ts +436 -0
  94. package/src/handler/LifecycleEventHandler.ts +85 -38
  95. package/src/handler/PresentationCoordinator.ts +560 -0
  96. package/src/handler/ResultEventHandler.ts +23 -9
  97. package/src/handler/TabEventHandler.ts +31 -0
  98. package/src/hooks.ts +37 -1
  99. package/src/index.ts +11 -0
  100. package/src/reveal.ts +565 -0
  101. package/src/routeGraph.ts +87 -0
  102. package/src/router/index.ts +5 -40
  103. package/src/router/tabs.ts +1 -0
  104. package/src/store.ts +5 -0
  105. package/src/testing.ts +395 -0
  106. package/testing.d.ts +1 -0
  107. package/testing.js +3 -0
@@ -22,6 +22,7 @@ import com.facebook.react.bridge.ReactContext;
22
22
  import com.facebook.react.bridge.ReadableArray;
23
23
  import com.facebook.react.bridge.ReadableMap;
24
24
  import com.facebook.react.bridge.UiThreadUtil;
25
+ import com.facebook.react.bridge.WritableMap;
25
26
  import com.facebook.react.module.annotations.ReactModule;
26
27
  import com.navigation.androidx.AwesomeFragment;
27
28
  import com.navigation.androidx.FragmentHelper;
@@ -30,6 +31,7 @@ import com.navigation.androidx.TabBarFragment;
30
31
 
31
32
  import java.util.ArrayList;
32
33
  import java.util.HashMap;
34
+ import java.util.List;
33
35
  import java.util.Map;
34
36
  import java.util.concurrent.atomic.AtomicBoolean;
35
37
  import java.util.function.Function;
@@ -99,6 +101,9 @@ public class NativeNavigation extends NativeNavigationSpec {
99
101
 
100
102
  activity.scheduleTaskAtStarted(() -> {
101
103
  AwesomeFragment fragment = FragmentHelper.findAwesomeFragment(activity.getSupportFragmentManager(), sceneId);
104
+ if (fragment == null) {
105
+ fragment = reactManager.findPrewarmingFragmentBySceneId(sceneId);
106
+ }
102
107
  if (fragment instanceof ReactFragment reactFragment) {
103
108
  reactFragment.signalFirstRenderComplete();
104
109
  }
@@ -171,23 +176,29 @@ public class NativeNavigation extends NativeNavigationSpec {
171
176
  });
172
177
  }
173
178
 
179
+ @Nullable
180
+ private WritableMap buildCurrentRouteMap(@NonNull FragmentManager fragmentManager) {
181
+ HybridFragment current = reactManager.primaryFragment(fragmentManager);
182
+ if (current == null) {
183
+ return null;
184
+ }
185
+ Bundle bundle = new Bundle();
186
+ bundle.putString("moduleName", current.getModuleName());
187
+ bundle.putString("sceneId", current.getSceneId());
188
+ bundle.putString("mode", Navigator.Util.getMode(current));
189
+ bundle.putString("presentingId", resolvePresentingSceneId(current));
190
+ bundle.putInt("requestCode", resolveRequestCode(current));
191
+ return Arguments.fromBundle(bundle);
192
+ }
193
+
174
194
  @Override
175
195
  public void currentRoute(Promise promise) {
176
196
  resolveWhenReady(promise, () -> null, activity -> {
177
- FragmentManager fragmentManager = activity.getSupportFragmentManager();
178
- HybridFragment current = reactManager.primaryFragment(fragmentManager);
179
- if (current == null) {
197
+ WritableMap route = buildCurrentRouteMap(activity.getSupportFragmentManager());
198
+ if (route == null) {
180
199
  FLog.w(TAG, "Can't find primary fragment for currentRoute.");
181
- return null;
182
200
  }
183
-
184
- Bundle bundle = new Bundle();
185
- bundle.putString("moduleName", current.getModuleName());
186
- bundle.putString("sceneId", current.getSceneId());
187
- bundle.putString("mode", Navigator.Util.getMode(current));
188
- bundle.putString("presentingId", resolvePresentingSceneId(current));
189
- bundle.putInt("requestCode", resolveRequestCode(current));
190
- return Arguments.fromBundle(bundle);
201
+ return route;
191
202
  });
192
203
  }
193
204
 
@@ -199,6 +210,30 @@ public class NativeNavigation extends NativeNavigationSpec {
199
210
  });
200
211
  }
201
212
 
213
+ @Override
214
+ public void navigationSnapshot(Promise promise) {
215
+ resolveWhenReady(promise, () -> {
216
+ WritableMap empty = Arguments.createMap();
217
+ empty.putDouble("rootGeneration", reactManager.getRootGeneration());
218
+ empty.putNull("currentRoute");
219
+ empty.putArray("layers", Arguments.createArray());
220
+ return empty;
221
+ }, activity -> {
222
+ FragmentManager fragmentManager = activity.getSupportFragmentManager();
223
+ WritableMap snapshot = Arguments.createMap();
224
+ snapshot.putDouble("rootGeneration", reactManager.getRootGeneration());
225
+ WritableMap route = buildCurrentRouteMap(fragmentManager);
226
+ if (route != null) {
227
+ snapshot.putMap("currentRoute", route);
228
+ } else {
229
+ snapshot.putNull("currentRoute");
230
+ }
231
+ ArrayList<Bundle> graph = reactManager.buildRouteGraph(fragmentManager);
232
+ snapshot.putArray("layers", Arguments.fromList(graph));
233
+ return snapshot;
234
+ });
235
+ }
236
+
202
237
  @Override
203
238
  public void setDefaultOptions(ReadableMap options) {
204
239
  UiThreadUtil.runOnUiThread(() -> {
@@ -213,7 +248,17 @@ public class NativeNavigation extends NativeNavigationSpec {
213
248
  UiThreadUtil.runOnUiThread(() -> {
214
249
  HybridFragment fragment = findHybridFragmentBySceneId(sceneId);
215
250
  if (fragment != null) {
216
- fragment.getGarden().updateOptions(options);
251
+ fragment.updateOptions(options);
252
+ }
253
+ });
254
+ }
255
+
256
+ @Override
257
+ public void setBeforeRemoveEnabled(String sceneId, boolean enabled) {
258
+ UiThreadUtil.runOnUiThread(() -> {
259
+ HybridFragment fragment = findHybridFragmentBySceneId(sceneId);
260
+ if (fragment != null) {
261
+ fragment.setBeforeRemoveEnabled(enabled);
217
262
  }
218
263
  });
219
264
  }
@@ -243,9 +288,29 @@ public class NativeNavigation extends NativeNavigationSpec {
243
288
  return;
244
289
  }
245
290
 
291
+ ArrayList<Bundle> rawList = toList(items);
292
+ ArrayList<Bundle> resolvedList = new ArrayList<>(rawList.size());
293
+ for (Bundle item : rawList) {
294
+ if (!item.containsKey("index")) {
295
+ String key = item.getString("key");
296
+ if (key == null) {
297
+ continue;
298
+ }
299
+ int resolvedIndex = tabBarFragment.findIndexByTabKey(key);
300
+ if (resolvedIndex < 0) {
301
+ continue;
302
+ }
303
+ item.putDouble("index", resolvedIndex);
304
+ }
305
+ resolvedList.add(item);
306
+ }
307
+ if (resolvedList.isEmpty()) {
308
+ return;
309
+ }
310
+
246
311
  Bundle bundle = new Bundle();
247
312
  bundle.putString(ARG_ACTION, ACTION_SET_TAB_ITEM);
248
- bundle.putParcelableArrayList(ARG_OPTIONS, toList(items));
313
+ bundle.putParcelableArrayList(ARG_OPTIONS, resolvedList);
249
314
  tabBarFragment.updateTabBar(bundle);
250
315
  });
251
316
  }
@@ -362,10 +427,43 @@ public class NativeNavigation extends NativeNavigationSpec {
362
427
  }
363
428
 
364
429
  @Nullable
365
- private TabBarFragment getTabBarFragment(String sceneId) {
366
- AwesomeFragment fragment = findFragmentBySceneId(sceneId);
367
- if (fragment != null) {
368
- return fragment.getTabBarFragment();
430
+ private TabBarFragment getTabBarFragment(@Nullable String sceneId) {
431
+ if (sceneId != null && !sceneId.isEmpty()) {
432
+ AwesomeFragment fragment = findFragmentBySceneId(sceneId);
433
+ if (fragment != null) {
434
+ return fragment.getTabBarFragment();
435
+ }
436
+ return null;
437
+ }
438
+ ReactAppCompatActivity activity = reactManager.getActiveActivity();
439
+ if (activity == null) {
440
+ return null;
441
+ }
442
+ return findTabBarFragment(activity.getSupportFragmentManager());
443
+ }
444
+
445
+ @Nullable
446
+ private TabBarFragment findTabBarFragment(@NonNull FragmentManager fragmentManager) {
447
+ List<AwesomeFragment> fragments = FragmentHelper.getFragments(fragmentManager);
448
+ for (AwesomeFragment fragment : fragments) {
449
+ TabBarFragment found = findTabBarFragmentInTree(fragment);
450
+ if (found != null) {
451
+ return found;
452
+ }
453
+ }
454
+ return null;
455
+ }
456
+
457
+ @Nullable
458
+ private TabBarFragment findTabBarFragmentInTree(@NonNull AwesomeFragment fragment) {
459
+ if (fragment instanceof TabBarFragment tabBarFragment) {
460
+ return tabBarFragment;
461
+ }
462
+ for (AwesomeFragment child : fragment.getChildAwesomeFragments()) {
463
+ TabBarFragment found = findTabBarFragmentInTree(child);
464
+ if (found != null) {
465
+ return found;
466
+ }
369
467
  }
370
468
  return null;
371
469
  }
@@ -146,6 +146,14 @@ final class NavigationCommandQueue {
146
146
  return viewHierarchyReady && getActiveActivity() != null;
147
147
  }
148
148
 
149
+ long getRootGeneration() {
150
+ return rootGeneration;
151
+ }
152
+
153
+ int getCommandGeneration() {
154
+ return commandGeneration;
155
+ }
156
+
149
157
  void setViewHierarchyReady(boolean ready) {
150
158
  viewHierarchyReady = ready;
151
159
  if (ready) {
@@ -1,26 +1,56 @@
1
1
  package com.reactnative.hybridnavigation;
2
2
 
3
+ import android.content.Context;
4
+ import android.graphics.Insets;
5
+ import android.graphics.Rect;
6
+ import android.os.Build;
3
7
  import android.os.Bundle;
8
+ import android.os.Handler;
9
+ import android.os.Looper;
10
+ import android.os.SystemClock;
11
+ import android.view.Choreographer;
4
12
  import android.view.LayoutInflater;
5
13
  import android.view.View;
6
14
  import android.view.ViewGroup;
15
+ import android.view.Window;
16
+ import android.view.WindowInsets;
7
17
 
8
18
  import androidx.annotation.NonNull;
9
19
  import androidx.annotation.Nullable;
20
+ import androidx.fragment.app.FragmentActivity;
10
21
 
11
22
  import com.facebook.common.logging.FLog;
12
23
  import com.facebook.react.ReactHost;
13
24
  import com.facebook.react.bridge.Arguments;
25
+ import com.facebook.react.interfaces.TaskInterface;
14
26
  import com.facebook.react.interfaces.fabric.ReactSurface;
27
+ import com.facebook.react.uimanager.PixelUtil;
28
+ import com.navigation.androidx.AwesomeFragment;
29
+ import com.navigation.androidx.DrawerFragment;
30
+ import com.navigation.androidx.StackFragment;
31
+ import com.navigation.androidx.SystemUI;
15
32
 
16
33
  public class ReactFragment extends HybridFragment implements ReactManager.ReactBridgeReloadListener {
17
34
 
18
35
  protected static final String TAG = "Navigation";
36
+ private static final long DEFAULT_PUSH_PREWARM_TIMEOUT_MS = 250L;
37
+
38
+ private final Handler mainHandler = new Handler(Looper.getMainLooper());
19
39
 
20
40
  private HBDRootView reactRootView;
21
41
 
22
42
  private ReactSurface reactSurface;
23
43
 
44
+ @Nullable
45
+ private TaskInterface<Void> prewarmedStartTask;
46
+ private boolean surfacePrewarmed = false;
47
+ @Nullable
48
+ private Runnable pushPrewarmOnReady;
49
+ @Nullable
50
+ private Runnable pushPrewarmTimeoutRunnable;
51
+ private int pushPrewarmGeneration = 0;
52
+ private long pushPrewarmStartUptimeMs = 0L;
53
+
24
54
  @Nullable
25
55
  private ReactManager.ReactModuleRegisterListener pendingRegisterListener;
26
56
 
@@ -29,6 +59,131 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
29
59
  return false;
30
60
  }
31
61
 
62
+ public boolean prepareForAnimatedPush(
63
+ @NonNull StackFragment stackFragment,
64
+ @NonNull View container,
65
+ long timeoutMs,
66
+ @NonNull Runnable onReady
67
+ ) {
68
+ if (!getReactManager().getGlobalStyle().isPrewarmPush()
69
+ || !isReactModuleRegisterCompleted()
70
+ || !shouldAnimatedTransition()
71
+ || reactSurface != null) {
72
+ return false;
73
+ }
74
+ FragmentActivity activity = stackFragment.getActivity();
75
+ if (activity == null || activity.isFinishing() || activity.isDestroyed()) {
76
+ return false;
77
+ }
78
+ int width = container.getWidth();
79
+ int height = container.getHeight();
80
+ if (width <= 0 || height <= 0) {
81
+ return false;
82
+ }
83
+ View contentView = activity.getWindow() != null
84
+ ? activity.getWindow().getDecorView().findViewById(android.R.id.content)
85
+ : null;
86
+ if (contentView == null || contentView.getWidth() != width || contentView.getHeight() != height) {
87
+ return false;
88
+ }
89
+ if (stackFragment.getTabBarFragment() != null || isFragmentInsideDrawerMenu(stackFragment)) {
90
+ return false;
91
+ }
92
+ if (stackFragment.getPresentingFragment() != null) {
93
+ return false;
94
+ }
95
+ if (!canUseCurrentWindowGeometry(activity, stackFragment)) {
96
+ return false;
97
+ }
98
+
99
+ Bundle initialProps = buildInitialProps(activity, stackFragment);
100
+
101
+ ReactHost reactHost = getReactManager().getReactHost();
102
+ ReactSurface surface = reactHost.createSurface(activity, getModuleName(), initialProps);
103
+ View surfaceView = surface.getView();
104
+ if (surfaceView == null) {
105
+ return false;
106
+ }
107
+
108
+ // 离屏先按真实容器尺寸测量,让 Fabric 立即使用精确约束执行首帧渲染与挂载,避免右滑前半段空白。
109
+ int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
110
+ int heightSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
111
+ surfaceView.measure(widthSpec, heightSpec);
112
+ surfaceView.layout(0, 0, width, height);
113
+
114
+ pushPrewarmGeneration += 1;
115
+ int generation = pushPrewarmGeneration;
116
+ pushPrewarmOnReady = onReady;
117
+ pushPrewarmStartUptimeMs = SystemClock.uptimeMillis();
118
+ reactSurface = surface;
119
+ surfacePrewarmed = true;
120
+
121
+ getReactManager().registerPrewarmingFragment(this);
122
+ getReactManager().addReactBridgeReloadListener(this);
123
+ getLifecycleStateMachine().onMount();
124
+ prewarmedStartTask = surface.start();
125
+
126
+ long safeTimeout = timeoutMs > 0 ? timeoutMs : DEFAULT_PUSH_PREWARM_TIMEOUT_MS;
127
+ Runnable timeoutTask = () -> finishPushPrewarmWait(generation, "timeout");
128
+ pushPrewarmTimeoutRunnable = timeoutTask;
129
+ mainHandler.postDelayed(timeoutTask, safeTimeout);
130
+ return true;
131
+ }
132
+
133
+ public void cancelPushPrewarm(boolean stopUnattachedSurface) {
134
+ pushPrewarmGeneration += 1;
135
+ pushPrewarmOnReady = null;
136
+ if (pushPrewarmTimeoutRunnable != null) {
137
+ mainHandler.removeCallbacks(pushPrewarmTimeoutRunnable);
138
+ pushPrewarmTimeoutRunnable = null;
139
+ }
140
+ getReactManager().unregisterPrewarmingFragment(this);
141
+ if (stopUnattachedSurface && surfacePrewarmed && reactRootView == null) {
142
+ surfacePrewarmed = false;
143
+ ReactSurface oldSurface = reactSurface;
144
+ TaskInterface<Void> oldStartTask = prewarmedStartTask;
145
+ reactSurface = null;
146
+ prewarmedStartTask = null;
147
+ getReactManager().removeReactBridgeReloadListener(this);
148
+ getLifecycleStateMachine().onUnmount();
149
+ if (!isAdded()) {
150
+ // 预热期间已提前 onMount;未 attach 即取消时 Fragment 不会走 onDestroy,补发 destroyed 闭合生命周期。
151
+ getLifecycleStateMachine().onDestroy();
152
+ }
153
+ if (oldSurface != null) {
154
+ ReactSurfaceCleanup.stopThenRun(oldSurface, oldStartTask, null);
155
+ }
156
+ }
157
+ }
158
+
159
+ private void finishPushPrewarmWait(int generation, @NonNull String reason) {
160
+ if (generation != pushPrewarmGeneration) {
161
+ return;
162
+ }
163
+ Runnable onReady = pushPrewarmOnReady;
164
+ if (onReady == null) {
165
+ return;
166
+ }
167
+ pushPrewarmGeneration += 1;
168
+ pushPrewarmOnReady = null;
169
+ if (pushPrewarmTimeoutRunnable != null) {
170
+ mainHandler.removeCallbacks(pushPrewarmTimeoutRunnable);
171
+ pushPrewarmTimeoutRunnable = null;
172
+ }
173
+ long elapsedMs = SystemClock.uptimeMillis() - pushPrewarmStartUptimeMs;
174
+ FLog.i(TAG, "ReactFragment#pushPrewarm ready module=" + getModuleName() + " reason=" + reason + " elapsedMs=" + elapsedMs);
175
+ onReady.run();
176
+ if (isAdded()) {
177
+ getReactManager().unregisterPrewarmingFragment(this);
178
+ }
179
+ }
180
+
181
+ @Override
182
+ public void onAttach(@NonNull Context context) {
183
+ super.onAttach(context);
184
+ getReactManager().unregisterPrewarmingFragment(this);
185
+ }
186
+
32
187
  @Nullable
33
188
  @Override
34
189
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@@ -69,12 +224,19 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
69
224
 
70
225
  @Override
71
226
  public void onDestroyView() {
227
+ cancelPushPrewarm(false);
72
228
  super.onDestroyView();
73
229
  clearPendingRegisterListener();
74
230
  unmountReactView();
75
231
  reactRootView = null;
76
232
  }
77
233
 
234
+ @Override
235
+ public void onDestroy() {
236
+ cancelPushPrewarm(true);
237
+ super.onDestroy();
238
+ }
239
+
78
240
  @Override
79
241
  public void onResume() {
80
242
  super.onResume();
@@ -96,19 +258,24 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
96
258
 
97
259
  @Override
98
260
  public void onReload() {
261
+ cancelPushPrewarm(true);
99
262
  getLifecycleStateMachine().onReload();
100
263
  unmountReactView();
101
264
  }
102
265
 
103
266
  private void mountReactView() {
104
- initReactRootView();
267
+ boolean reusedPrewarmed = initReactRootView();
105
268
  getReactManager().addReactBridgeReloadListener(this);
106
- getLifecycleStateMachine().onMount();
269
+ if (!reusedPrewarmed || !getLifecycleStateMachine().isMounted()) {
270
+ getLifecycleStateMachine().onMount();
271
+ }
107
272
  }
108
273
 
109
274
  private void unmountReactView() {
110
275
  getReactManager().removeReactBridgeReloadListener(this);
111
276
  getLifecycleStateMachine().onUnmount();
277
+ surfacePrewarmed = false;
278
+ prewarmedStartTask = null;
112
279
 
113
280
  if (reactSurface != null) {
114
281
  FLog.w(TAG, "销毁页面-:" + getModuleName());
@@ -129,6 +296,11 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
129
296
  public void signalFirstRenderComplete() {
130
297
  boolean canEmit = canEmitReactVisibility() && isResumed() && !isResumingForRedirect();
131
298
  getLifecycleStateMachine().onFirstRenderComplete(canEmit);
299
+ if (pushPrewarmOnReady != null) {
300
+ int generation = pushPrewarmGeneration;
301
+ // 等一帧 Choreographer,让 Fabric MountingManager 将当前提交的挂载项同步执行进 ReactSurfaceView。
302
+ Choreographer.getInstance().postFrameCallback(frameTimeNanos -> finishPushPrewarmWait(generation, "firstRender"));
303
+ }
132
304
  }
133
305
 
134
306
  public boolean isFirstRenderCompleted() {
@@ -149,17 +321,213 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
149
321
  }
150
322
  }
151
323
 
152
- private void initReactRootView() {
153
- ReactHost reactHost = getReactManager().getReactHost();
154
- ReactSurface reactSurface = reactHost.createSurface(requireContext(), getModuleName(), getProps());
155
- this.reactSurface = reactSurface;
156
-
324
+ private boolean initReactRootView() {
157
325
  HBDRootView rootView = reactRootView;
158
326
  if (rootView == null) {
159
327
  throw new IllegalStateException("[Navigation] HBDRootView 还没有创建。");
160
328
  }
161
- rootView.setAppProperties(getProps());
329
+ if (surfacePrewarmed && reactSurface != null) {
330
+ surfacePrewarmed = false;
331
+ TaskInterface<Void> startTask = prewarmedStartTask;
332
+ prewarmedStartTask = null;
333
+ rootView.attachPrewarmedSurface(reactSurface, startTask);
334
+ return true;
335
+ }
336
+ ReactHost reactHost = getReactManager().getReactHost();
337
+ Bundle initialProps = buildInitialProps(getActivity(), this);
338
+ ReactSurface reactSurface = reactHost.createSurface(requireContext(), getModuleName(), initialProps);
339
+ this.reactSurface = reactSurface;
340
+
341
+ rootView.setAppProperties(initialProps);
162
342
  rootView.setSurface(reactSurface);
343
+ return false;
344
+ }
345
+
346
+ @NonNull
347
+ private Bundle buildInitialProps(@Nullable FragmentActivity activity, @Nullable AwesomeFragment hostFragment) {
348
+ Bundle initialProps = new Bundle(getProps());
349
+ if (!initialProps.containsKey("initialMetrics") && getReactManager().getGlobalStyle().isInjectInitialMetrics()) {
350
+ Bundle metrics = buildInitialMetrics(activity, hostFragment);
351
+ if (metrics != null) {
352
+ initialProps.putBundle("initialMetrics", metrics);
353
+ }
354
+ }
355
+ return initialProps;
356
+ }
357
+
358
+ private boolean canUseCurrentWindowGeometry(@Nullable FragmentActivity activity, @Nullable AwesomeFragment hostFragment) {
359
+ if (activity == null || activity.isFinishing() || activity.isDestroyed()) {
360
+ return false;
361
+ }
362
+ Window window = activity.getWindow();
363
+ if (window == null) {
364
+ return false;
365
+ }
366
+ View decorView = window.getDecorView();
367
+ if (!(decorView instanceof ViewGroup) || !decorView.isAttachedToWindow()) {
368
+ return false;
369
+ }
370
+ View contentView = decorView.findViewById(android.R.id.content);
371
+ if (contentView == null || contentView.getWidth() <= 0 || contentView.getHeight() <= 0) {
372
+ return false;
373
+ }
374
+ // Tab 根页与 Drawer 侧栏容器不按全屏窗口几何预热或注入。
375
+ AwesomeFragment contextFragment = hostFragment != null ? hostFragment : this;
376
+ if (contextFragment.getTabBarFragment() != null || isFragmentInsideDrawerMenu(contextFragment)) {
377
+ return false;
378
+ }
379
+ OrientationCoordinator coordinator = getOrientationCoordinator();
380
+ if (coordinator != null && coordinator.requiresOrientationChange(this)) {
381
+ return false;
382
+ }
383
+ ScreenOrientation targetOrientation = coordinator != null
384
+ ? coordinator.resolveOrientation(this)
385
+ : getReactManager().getGlobalStyle().getDefaultOrientation();
386
+ boolean windowIsLandscape = contentView.getWidth() > contentView.getHeight();
387
+ if (targetOrientation == ScreenOrientation.LANDSCAPE && !windowIsLandscape) {
388
+ return false;
389
+ }
390
+ if (targetOrientation == ScreenOrientation.PORTRAIT && windowIsLandscape) {
391
+ return false;
392
+ }
393
+ if (SystemUI.isStatusBarHidden(window) != resolvePreferredStatusBarHidden()) {
394
+ return false;
395
+ }
396
+ if (SystemUI.isNavigationBarHidden(window) != resolvePreferredNavigationBarHidden()) {
397
+ return false;
398
+ }
399
+ return true;
400
+ }
401
+
402
+ @Nullable
403
+ private Bundle buildInitialMetrics(@Nullable FragmentActivity activity, @Nullable AwesomeFragment hostFragment) {
404
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
405
+ return null;
406
+ }
407
+ if (activity == null || !canUseCurrentWindowGeometry(activity, hostFragment)) {
408
+ return null;
409
+ }
410
+ Window window = activity.getWindow();
411
+ if (window == null) {
412
+ return null;
413
+ }
414
+ View decorView = window.getDecorView();
415
+ ViewGroup decorGroup = (ViewGroup) decorView;
416
+ View contentView = decorView.findViewById(android.R.id.content);
417
+ if (contentView == null) {
418
+ return null;
419
+ }
420
+ WindowInsets rootWindowInsets = decorView.getRootWindowInsets();
421
+ if (rootWindowInsets == null) {
422
+ return null;
423
+ }
424
+
425
+ float rootTop;
426
+ float rootRight;
427
+ float rootBottom;
428
+ float rootLeft;
429
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
430
+ Insets systemInsets = rootWindowInsets.getInsets(
431
+ WindowInsets.Type.statusBars()
432
+ | WindowInsets.Type.displayCutout()
433
+ | WindowInsets.Type.navigationBars()
434
+ | WindowInsets.Type.captionBar()
435
+ );
436
+ rootTop = systemInsets.top;
437
+ rootRight = systemInsets.right;
438
+ rootBottom = systemInsets.bottom;
439
+ rootLeft = systemInsets.left;
440
+ } else {
441
+ @SuppressWarnings("deprecation")
442
+ int insetTop = rootWindowInsets.getSystemWindowInsetTop();
443
+ @SuppressWarnings("deprecation")
444
+ int insetRight = rootWindowInsets.getSystemWindowInsetRight();
445
+ @SuppressWarnings("deprecation")
446
+ int insetBottom = Math.min(rootWindowInsets.getSystemWindowInsetBottom(), rootWindowInsets.getStableInsetBottom());
447
+ @SuppressWarnings("deprecation")
448
+ int insetLeft = rootWindowInsets.getSystemWindowInsetLeft();
449
+ rootTop = insetTop;
450
+ rootRight = insetRight;
451
+ rootBottom = insetBottom;
452
+ rootLeft = insetLeft;
453
+ }
454
+
455
+ float windowWidth = decorView.getWidth();
456
+ float windowHeight = decorView.getHeight();
457
+ Rect visibleRect = new Rect();
458
+ if (!contentView.getGlobalVisibleRect(visibleRect)) {
459
+ return null;
460
+ }
461
+ float top = Math.max(rootTop - visibleRect.top, 0f);
462
+ float right = Math.max(Math.min(visibleRect.left + contentView.getWidth() - windowWidth, 0f) + rootRight, 0f);
463
+ float bottom = Math.max(Math.min(visibleRect.top + contentView.getHeight() - windowHeight, 0f) + rootBottom, 0f);
464
+ float left = Math.max(rootLeft - visibleRect.left, 0f);
465
+
466
+ Rect frameRect = new Rect();
467
+ contentView.getDrawingRect(frameRect);
468
+ try {
469
+ decorGroup.offsetDescendantRectToMyCoords(contentView, frameRect);
470
+ } catch (IllegalArgumentException ignored) {
471
+ return null;
472
+ }
473
+
474
+ Bundle insetsBundle = new Bundle();
475
+ insetsBundle.putDouble("top", PixelUtil.toDIPFromPixel(top));
476
+ insetsBundle.putDouble("right", PixelUtil.toDIPFromPixel(right));
477
+ insetsBundle.putDouble("bottom", PixelUtil.toDIPFromPixel(bottom));
478
+ insetsBundle.putDouble("left", PixelUtil.toDIPFromPixel(left));
479
+
480
+ Bundle frameBundle = new Bundle();
481
+ frameBundle.putDouble("x", PixelUtil.toDIPFromPixel(frameRect.left));
482
+ frameBundle.putDouble("y", PixelUtil.toDIPFromPixel(frameRect.top));
483
+ frameBundle.putDouble("width", PixelUtil.toDIPFromPixel(contentView.getWidth()));
484
+ frameBundle.putDouble("height", PixelUtil.toDIPFromPixel(contentView.getHeight()));
485
+
486
+ Bundle metrics = new Bundle();
487
+ metrics.putBundle("insets", insetsBundle);
488
+ metrics.putBundle("frame", frameBundle);
489
+ return metrics;
490
+ }
491
+
492
+ private boolean resolvePreferredStatusBarHidden() {
493
+ if (isAdded()) {
494
+ return preferredStatusBarHidden();
495
+ }
496
+ Bundle options = getOptions();
497
+ if (options.get("statusBarHidden") != null) {
498
+ return options.getBoolean("statusBarHidden");
499
+ }
500
+ return false;
501
+ }
502
+
503
+ private boolean resolvePreferredNavigationBarHidden() {
504
+ if (isAdded()) {
505
+ return preferredNavigationBarHidden();
506
+ }
507
+ Bundle options = getOptions();
508
+ if (options.get("navigationBarHiddenAndroid") != null) {
509
+ return options.getBoolean("navigationBarHiddenAndroid");
510
+ }
511
+ return false;
512
+ }
513
+
514
+ private boolean isFragmentInsideDrawerMenu(@NonNull AwesomeFragment fragment) {
515
+ DrawerFragment drawer = fragment.getDrawerFragment();
516
+ if (drawer == null) {
517
+ return false;
518
+ }
519
+ AwesomeFragment menu = drawer.getMenuFragment();
520
+ if (menu == null) {
521
+ return false;
522
+ }
523
+ AwesomeFragment current = fragment;
524
+ while (current != null) {
525
+ if (current == menu) {
526
+ return true;
527
+ }
528
+ current = current.getParentAwesomeFragment();
529
+ }
530
+ return false;
163
531
  }
164
532
 
165
533
  @Override