hybrid-navigation 4.1.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 (177) hide show
  1. package/HybridNavigation.podspec +9 -1
  2. package/README.md +12 -4
  3. package/android/build.gradle +4 -4
  4. package/android/proguard-rules.pro +0 -14
  5. package/android/src/main/java/com/navigation/androidx/AwesomeActivity.java +116 -27
  6. package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +162 -13
  7. package/android/src/main/java/com/navigation/androidx/DefaultTabBarProvider.java +2 -2
  8. package/android/src/main/java/com/navigation/androidx/DrawableUtils.java +83 -18
  9. package/android/src/main/java/com/navigation/androidx/DrawerFragment.java +185 -25
  10. package/android/src/main/java/com/navigation/androidx/FragmentHelper.java +18 -0
  11. package/android/src/main/java/com/navigation/androidx/HybridDrawerLayout.java +563 -143
  12. package/android/src/main/java/com/navigation/androidx/LifecycleDelegate.java +39 -12
  13. package/android/src/main/java/com/navigation/androidx/PresentableActivity.java +3 -0
  14. package/android/src/main/java/com/navigation/androidx/PresentationDelegate.java +10 -7
  15. package/android/src/main/java/com/navigation/androidx/StackDelegate.java +29 -9
  16. package/android/src/main/java/com/navigation/androidx/StackFragment.java +31 -3
  17. package/android/src/main/java/com/navigation/androidx/SystemUI.java +59 -90
  18. package/android/src/main/java/com/navigation/androidx/TabBar.java +21 -2
  19. package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +74 -7
  20. package/android/src/main/java/com/navigation/androidx/TabBarItem.java +4 -0
  21. package/android/src/main/java/com/navigation/androidx/TabView.java +11 -2
  22. package/android/src/main/java/com/reactnative/hybridnavigation/DispatchResult.java +71 -0
  23. package/android/src/main/java/com/reactnative/hybridnavigation/Garden.java +18 -10
  24. package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +34 -2
  25. package/android/src/main/java/com/reactnative/hybridnavigation/HBDRootView.java +20 -10
  26. package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +179 -13
  27. package/android/src/main/java/com/reactnative/hybridnavigation/HybridNavigationPackage.java +13 -24
  28. package/android/src/main/java/com/reactnative/hybridnavigation/NativeEvent.java +5 -1
  29. package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +283 -183
  30. package/android/src/main/java/com/reactnative/hybridnavigation/NavigationCommandQueue.java +507 -0
  31. package/android/src/main/java/com/reactnative/hybridnavigation/Navigator.java +4 -1
  32. package/android/src/main/java/com/reactnative/hybridnavigation/OrientationCoordinator.java +162 -0
  33. package/android/src/main/java/com/reactnative/hybridnavigation/ReactAppCompatActivity.java +103 -66
  34. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +421 -92
  35. package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +227 -78
  36. package/android/src/main/java/com/reactnative/hybridnavigation/ReactStackFragment.java +6 -6
  37. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarFragment.java +26 -0
  38. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarProvider.java +8 -1
  39. package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleState.java +21 -0
  40. package/android/src/main/java/com/reactnative/hybridnavigation/SceneLifecycleStateMachine.java +116 -0
  41. package/android/src/main/java/com/reactnative/hybridnavigation/ScreenOrientation.java +42 -0
  42. package/android/src/main/java/com/reactnative/hybridnavigation/Utils.java +17 -0
  43. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/DrawerNavigator.java +14 -6
  44. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +188 -26
  45. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +67 -16
  46. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +42 -14
  47. package/dist/DeepLink.d.ts +3 -2
  48. package/dist/DeepLink.js +37 -14
  49. package/dist/NativeEvent.d.ts +17 -10
  50. package/dist/NativeNavigation.d.ts +24 -18
  51. package/dist/Navigation.d.ts +34 -19
  52. package/dist/Navigation.js +225 -90
  53. package/dist/NavigationComponentWrap.d.ts +5 -3
  54. package/dist/NavigationComponentWrap.js +3 -6
  55. package/dist/Navigator.d.ts +32 -42
  56. package/dist/Navigator.js +117 -91
  57. package/dist/Options.d.ts +15 -3
  58. package/dist/Route.d.ts +149 -17
  59. package/dist/defineRoutes.d.ts +19 -0
  60. package/dist/defineRoutes.js +6 -0
  61. package/dist/handler/DispatchCommandHandler.d.ts +25 -6
  62. package/dist/handler/DispatchCommandHandler.js +99 -17
  63. package/dist/handler/LayoutCommandHandler.d.ts +8 -7
  64. package/dist/handler/LayoutCommandHandler.js +24 -15
  65. package/dist/handler/LeaveGuardHandler.d.ts +17 -0
  66. package/dist/handler/LeaveGuardHandler.js +350 -0
  67. package/dist/handler/LifecycleEventHandler.d.ts +30 -0
  68. package/dist/handler/LifecycleEventHandler.js +169 -0
  69. package/dist/handler/PresentationCoordinator.d.ts +24 -0
  70. package/dist/handler/PresentationCoordinator.js +449 -0
  71. package/dist/handler/ResultEventHandler.d.ts +21 -9
  72. package/dist/handler/ResultEventHandler.js +96 -13
  73. package/dist/handler/TabEventHandler.d.ts +8 -0
  74. package/dist/handler/TabEventHandler.js +25 -0
  75. package/dist/hooks.d.ts +7 -2
  76. package/dist/hooks.js +46 -4
  77. package/dist/index.d.ts +24 -0
  78. package/dist/index.js +3 -2
  79. package/dist/reveal.d.ts +46 -0
  80. package/dist/reveal.js +431 -0
  81. package/dist/routeConfigRegistry.d.ts +6 -0
  82. package/dist/routeConfigRegistry.js +19 -0
  83. package/dist/routeGraph.d.ts +12 -0
  84. package/dist/routeGraph.js +68 -0
  85. package/dist/router/drawer.d.ts +2 -2
  86. package/dist/router/drawer.js +6 -3
  87. package/dist/router/index.d.ts +8 -5
  88. package/dist/router/index.js +106 -58
  89. package/dist/router/screen.d.ts +2 -2
  90. package/dist/router/screen.js +1 -1
  91. package/dist/router/stack.d.ts +2 -2
  92. package/dist/router/stack.js +59 -30
  93. package/dist/router/tabs.d.ts +3 -2
  94. package/dist/router/tabs.js +4 -1
  95. package/dist/store.d.ts +4 -2
  96. package/dist/store.js +4 -0
  97. package/dist/testing.d.ts +79 -0
  98. package/dist/testing.js +251 -0
  99. package/ios/HBDGlobalStyle.h +29 -0
  100. package/ios/{GlobalStyle.mm → HBDGlobalStyle.mm} +43 -29
  101. package/ios/HybridNavigation.h +5 -1
  102. package/ios/Navigator/HBDDrawerNavigator.mm +24 -8
  103. package/ios/Navigator/HBDNavigatorRegistry.mm +1 -0
  104. package/ios/Navigator/HBDScreenNavigator.mm +278 -58
  105. package/ios/Navigator/HBDStackNavigator.mm +152 -61
  106. package/ios/Navigator/HBDTabNavigator.mm +60 -14
  107. package/ios/React/HBDNativeNavigation.mm +162 -123
  108. package/ios/React/HBDNavigator.h +37 -5
  109. package/ios/React/HBDOrientationCoordinator.h +29 -0
  110. package/ios/React/HBDOrientationCoordinator.mm +309 -0
  111. package/ios/React/HBDReactBridgeManager.h +46 -20
  112. package/ios/React/HBDReactBridgeManager.mm +496 -108
  113. package/ios/React/HBDSceneLifecycleStateMachine.h +38 -0
  114. package/ios/React/HBDSceneLifecycleStateMachine.mm +180 -0
  115. package/ios/View/HBDRootView.h +2 -0
  116. package/ios/View/HBDRootView.mm +148 -0
  117. package/ios/View/UIImage+WithBadge.h +3 -3
  118. package/ios/View/UIImage+WithBadge.mm +10 -15
  119. package/ios/ViewController/HBDDrawerController.mm +126 -5
  120. package/ios/ViewController/HBDNavigationController.mm +31 -4
  121. package/ios/ViewController/HBDReactViewController.h +6 -0
  122. package/ios/ViewController/HBDReactViewController.mm +220 -106
  123. package/ios/ViewController/HBDTabBarController.h +15 -0
  124. package/ios/ViewController/HBDTabBarController.mm +227 -45
  125. package/ios/ViewController/HBDTabBarRenderer.h +28 -0
  126. package/ios/ViewController/HBDViewController.h +11 -3
  127. package/ios/ViewController/HBDViewController.mm +209 -159
  128. package/ios/ViewController/UINavigationController+HBD.h +2 -2
  129. package/ios/ViewController/UINavigationController+HBD.mm +3 -3
  130. package/ios/ViewController/UIViewController+HBD.h +18 -10
  131. package/ios/ViewController/UIViewController+HBD.mm +90 -65
  132. package/package.json +3 -1
  133. package/src/DeepLink.ts +41 -14
  134. package/src/NativeEvent.ts +18 -11
  135. package/src/NativeNavigation.ts +29 -31
  136. package/src/Navigation.ts +377 -120
  137. package/src/NavigationComponentWrap.tsx +18 -9
  138. package/src/Navigator.ts +219 -152
  139. package/src/Options.ts +12 -3
  140. package/src/Route.ts +228 -17
  141. package/src/defineRoutes.ts +46 -0
  142. package/src/handler/DispatchCommandHandler.ts +154 -21
  143. package/src/handler/LayoutCommandHandler.ts +40 -17
  144. package/src/handler/LeaveGuardHandler.ts +436 -0
  145. package/src/handler/LifecycleEventHandler.ts +219 -0
  146. package/src/handler/PresentationCoordinator.ts +560 -0
  147. package/src/handler/ResultEventHandler.ts +126 -19
  148. package/src/handler/TabEventHandler.ts +31 -0
  149. package/src/hooks.ts +75 -7
  150. package/src/index.ts +37 -2
  151. package/src/reveal.ts +565 -0
  152. package/src/routeConfigRegistry.ts +26 -0
  153. package/src/routeGraph.ts +87 -0
  154. package/src/router/drawer.ts +8 -5
  155. package/src/router/index.ts +139 -63
  156. package/src/router/screen.ts +3 -3
  157. package/src/router/stack.ts +80 -29
  158. package/src/router/tabs.ts +16 -7
  159. package/src/store.ts +6 -1
  160. package/src/testing.ts +395 -0
  161. package/testing.d.ts +1 -0
  162. package/testing.js +3 -0
  163. package/android/src/main/java/androidx/fragment/app/InternalFragment.java +0 -22
  164. package/android/src/main/java/com/navigation/androidx/SystemUI30.java +0 -87
  165. package/android/src/main/java/com/reactnative/hybridnavigation/NativeGarden.java +0 -176
  166. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBar.java +0 -130
  167. package/android/src/main/res/layout/nav_tab_bar_react.xml +0 -32
  168. package/android/src/main/res/layout/nav_tab_bar_react_indeterminate.xml +0 -32
  169. package/dist/NativeGarden.d.ts +0 -13
  170. package/dist/NativeGarden.js +0 -2
  171. package/dist/handler/VisibilityEventHandler.d.ts +0 -13
  172. package/dist/handler/VisibilityEventHandler.js +0 -39
  173. package/ios/GlobalStyle.h +0 -15
  174. package/ios/React/HBDNativeGarden.h +0 -10
  175. package/ios/React/HBDNativeGarden.mm +0 -111
  176. package/src/NativeGarden.ts +0 -16
  177. package/src/handler/VisibilityEventHandler.ts +0 -52
@@ -1,31 +1,188 @@
1
1
  package com.reactnative.hybridnavigation;
2
2
 
3
- import android.annotation.SuppressLint;
4
- import android.content.pm.ActivityInfo;
3
+ import android.content.Context;
4
+ import android.graphics.Insets;
5
+ import android.graphics.Rect;
6
+ import android.os.Build;
5
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;
6
12
  import android.view.LayoutInflater;
7
13
  import android.view.View;
8
14
  import android.view.ViewGroup;
15
+ import android.view.Window;
16
+ import android.view.WindowInsets;
9
17
 
10
18
  import androidx.annotation.NonNull;
11
19
  import androidx.annotation.Nullable;
20
+ import androidx.fragment.app.FragmentActivity;
12
21
 
13
22
  import com.facebook.common.logging.FLog;
14
23
  import com.facebook.react.ReactHost;
15
24
  import com.facebook.react.bridge.Arguments;
16
- import com.facebook.react.bridge.ReactContext;
17
- import com.facebook.react.common.LifecycleState;
25
+ import com.facebook.react.interfaces.TaskInterface;
18
26
  import com.facebook.react.interfaces.fabric.ReactSurface;
19
- import com.navigation.androidx.FragmentHelper;
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;
20
32
 
21
33
  public class ReactFragment extends HybridFragment implements ReactManager.ReactBridgeReloadListener {
22
34
 
23
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());
24
39
 
25
40
  private HBDRootView reactRootView;
26
41
 
27
42
  private ReactSurface reactSurface;
28
- private boolean firstRenderCompleted;
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
+
54
+ @Nullable
55
+ private ReactManager.ReactModuleRegisterListener pendingRegisterListener;
56
+
57
+ @Override
58
+ protected boolean isNativeManagedLifecycle() {
59
+ return false;
60
+ }
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
+ }
29
186
 
30
187
  @Nullable
31
188
  @Override
@@ -34,117 +191,91 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
34
191
  reactRootView = view.findViewById(R.id.react_content);
35
192
  if (isReactModuleRegisterCompleted()) {
36
193
  mountReactView();
194
+ } else {
195
+ scheduleMountWhenRegistered();
37
196
  }
38
197
  return view;
39
198
  }
40
199
 
200
+ private void scheduleMountWhenRegistered() {
201
+ clearPendingRegisterListener();
202
+ pendingRegisterListener = new ReactManager.ReactModuleRegisterListener() {
203
+ @Override
204
+ public void onReactModuleRegisterCompleted() {
205
+ clearPendingRegisterListener();
206
+ if (reactRootView == null || reactSurface != null || !isAdded()) {
207
+ return;
208
+ }
209
+ mountReactView();
210
+ if (isResumed() && !isResumingForRedirect()) {
211
+ getLifecycleStateMachine().onHostResume(canEmitReactVisibility());
212
+ }
213
+ }
214
+ };
215
+ getReactManager().addReactModuleRegisterListener(pendingRegisterListener);
216
+ }
217
+
218
+ private void clearPendingRegisterListener() {
219
+ if (pendingRegisterListener != null) {
220
+ getReactManager().removeReactModuleRegisterListener(pendingRegisterListener);
221
+ pendingRegisterListener = null;
222
+ }
223
+ }
224
+
41
225
  @Override
42
226
  public void onDestroyView() {
227
+ cancelPushPrewarm(false);
43
228
  super.onDestroyView();
229
+ clearPendingRegisterListener();
44
230
  unmountReactView();
45
231
  reactRootView = null;
46
232
  }
47
233
 
48
- @SuppressLint("SourceLockedOrientationActivity")
49
234
  @Override
50
- public void onStop() {
51
- super.onStop();
52
- if (FragmentHelper.isRemoving(this) && forceScreenLandscape()) {
53
- unmountReactView();
54
- }
235
+ public void onDestroy() {
236
+ cancelPushPrewarm(true);
237
+ super.onDestroy();
55
238
  }
56
239
 
57
- @SuppressLint("SourceLockedOrientationActivity")
58
240
  @Override
59
241
  public void onResume() {
60
242
  super.onResume();
61
243
  if (isResumingForRedirect()) {
62
244
  return;
63
245
  }
64
- if (isViewReady()) {
65
- sendViewAppearEvent(true);
66
- }
67
-
68
- if (!forceScreenLandscape() &&
69
- !Navigator.MODE_MODAL.equals(Navigator.Util.getMode(this)) &&
70
- requireActivity().getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
71
- requireView().clearAnimation();
72
- requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
73
- }
74
-
75
- if (forceScreenLandscape()) {
76
- if (!isRequestedOrientationLandscape()) {
77
- requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
78
- }
79
- }
80
- }
81
-
82
- private boolean isRequestedOrientationLandscape() {
83
- int requestedOrientation = requireActivity().getRequestedOrientation();
84
- return requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
85
- requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE ||
86
- requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE ||
87
- requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE;
246
+ getLifecycleStateMachine().onHostResume(canEmitReactVisibility());
88
247
  }
89
248
 
90
249
  @Override
91
250
  public void onPause() {
92
251
  super.onPause();
93
- if (isViewReady()) {
94
- sendViewAppearEvent(false);
95
- }
252
+ getLifecycleStateMachine().onHostPause(canEmitReactVisibility());
96
253
  }
97
254
 
98
- private boolean isViewReady() {
99
- if (reactSurface == null) {
100
- return false;
101
- }
102
- return firstRenderCompleted;
103
- }
104
-
105
- private boolean reactViewAppeared = false;
106
-
107
- private void sendViewAppearEvent(boolean appear) {
108
- if (!isReactModuleRegisterCompleted()) {
109
- return;
110
- }
111
-
112
- // 当从前台进入后台时,不会触发 disappear, 这和 iOS 保持一致
113
- ReactContext reactContext = getCurrentReactContext();
114
- boolean isResumed = reactContext != null && reactContext.getLifecycleState() == LifecycleState.RESUMED;
115
- if (!isResumed) {
116
- return;
117
- }
118
-
119
- if (reactViewAppeared == appear) {
120
- return;
121
- }
122
- reactViewAppeared = appear;
123
-
124
- Bundle bundle = new Bundle();
125
- bundle.putString("sceneId", getSceneId());
126
- NativeEvent nativeEvent = NativeEvent.getInstance();
127
- if (appear) {
128
- nativeEvent.emitOnComponentAppear(Arguments.fromBundle(bundle));
129
- } else {
130
- nativeEvent.emitOnComponentDisappear(Arguments.fromBundle(bundle));
131
- }
255
+ private boolean canEmitReactVisibility() {
256
+ return reactSurface != null && isReactModuleRegisterCompleted() && isReactContextResumed();
132
257
  }
133
258
 
134
259
  @Override
135
260
  public void onReload() {
261
+ cancelPushPrewarm(true);
262
+ getLifecycleStateMachine().onReload();
136
263
  unmountReactView();
137
264
  }
138
265
 
139
266
  private void mountReactView() {
140
- initReactRootView();
267
+ boolean reusedPrewarmed = initReactRootView();
141
268
  getReactManager().addReactBridgeReloadListener(this);
269
+ if (!reusedPrewarmed || !getLifecycleStateMachine().isMounted()) {
270
+ getLifecycleStateMachine().onMount();
271
+ }
142
272
  }
143
273
 
144
274
  private void unmountReactView() {
145
275
  getReactManager().removeReactBridgeReloadListener(this);
146
- firstRenderCompleted = false;
147
- reactViewAppeared = false;
276
+ getLifecycleStateMachine().onUnmount();
277
+ surfacePrewarmed = false;
278
+ prewarmedStartTask = null;
148
279
 
149
280
  if (reactSurface != null) {
150
281
  FLog.w(TAG, "销毁页面-:" + getModuleName());
@@ -163,18 +294,17 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
163
294
  }
164
295
 
165
296
  public void signalFirstRenderComplete() {
166
- if (firstRenderCompleted) {
167
- return;
168
- }
169
- firstRenderCompleted = true;
170
-
171
- if (isViewReady() && isResumed() && !isResumingForRedirect()) {
172
- sendViewAppearEvent(true);
297
+ boolean canEmit = canEmitReactVisibility() && isResumed() && !isResumingForRedirect();
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"));
173
303
  }
174
304
  }
175
305
 
176
306
  public boolean isFirstRenderCompleted() {
177
- return firstRenderCompleted;
307
+ return getLifecycleStateMachine().isFirstRendered();
178
308
  }
179
309
 
180
310
  @Override
@@ -185,20 +315,219 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
185
315
  result.putInt("resultCode", resultCode);
186
316
  result.putBundle("resultData", data);
187
317
  result.putString("sceneId", getSceneId());
188
- NativeEvent.getInstance().emitOnResult(Arguments.fromBundle(result));
318
+ NativeEvent nativeEvent = NativeEvent.getInstance();
319
+ if (nativeEvent != null) {
320
+ nativeEvent.emitOnResult(Arguments.fromBundle(result));
321
+ }
189
322
  }
190
323
 
191
- private void initReactRootView() {
192
- ReactHost reactHost = getReactManager().getReactHost();
193
- ReactSurface reactSurface = reactHost.createSurface(requireContext(), getModuleName(), getProps());
194
- this.reactSurface = reactSurface;
195
-
324
+ private boolean initReactRootView() {
196
325
  HBDRootView rootView = reactRootView;
197
326
  if (rootView == null) {
198
327
  throw new IllegalStateException("[Navigation] HBDRootView 还没有创建。");
199
328
  }
200
- 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);
201
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;
202
531
  }
203
532
 
204
533
  @Override