hybrid-navigation 3.1.2 → 3.1.3

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.
@@ -0,0 +1,190 @@
1
+ package com.reactnative.hybridnavigation;
2
+
3
+ import android.annotation.SuppressLint;
4
+ import android.content.Context;
5
+ import android.os.Bundle;
6
+ import android.util.AttributeSet;
7
+ import android.view.MotionEvent;
8
+ import android.view.View;
9
+ import android.view.ViewGroup;
10
+ import android.widget.FrameLayout;
11
+
12
+ import androidx.annotation.NonNull;
13
+ import androidx.annotation.Nullable;
14
+
15
+ import com.facebook.react.bridge.UIManager;
16
+ import com.facebook.react.interfaces.fabric.ReactSurface;
17
+ import com.facebook.react.runtime.ReactSurfaceView;
18
+ import com.facebook.react.uimanager.TouchTargetHelper;
19
+ import com.facebook.react.uimanager.UIManagerHelper;
20
+ import com.facebook.react.uimanager.common.UIManagerType;
21
+
22
+ import java.lang.reflect.InvocationTargetException;
23
+ import java.lang.reflect.Method;
24
+
25
+ public class HBDRootView extends FrameLayout {
26
+
27
+ @Nullable
28
+ private ReactSurface surface;
29
+
30
+ @Nullable
31
+ private ReactSurfaceView reactSurfaceView;
32
+
33
+ private boolean passThroughTouches;
34
+
35
+ @Nullable
36
+ private Bundle appProperties;
37
+
38
+ public HBDRootView(@NonNull Context context) {
39
+ super(context);
40
+ }
41
+
42
+ public HBDRootView(@NonNull Context context, @Nullable AttributeSet attrs) {
43
+ super(context, attrs);
44
+ }
45
+
46
+ public HBDRootView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
47
+ super(context, attrs, defStyleAttr);
48
+ }
49
+
50
+ public void setPassThroughTouches(boolean passThroughTouches) {
51
+ this.passThroughTouches = passThroughTouches;
52
+ }
53
+
54
+ public void setSurface(@NonNull ReactSurface reactSurface) {
55
+ if (surface == reactSurface) {
56
+ return;
57
+ }
58
+
59
+ clearSurface();
60
+ surface = reactSurface;
61
+
62
+ View view = reactSurface.getView();
63
+ if (view == null) {
64
+ throw new IllegalStateException("[Navigation] ReactSurface.getView() 不能为空。");
65
+ }
66
+
67
+ addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
68
+ reactSurfaceView = findReactSurfaceView(this);
69
+
70
+ if (appProperties != null) {
71
+ applyAppProperties(appProperties);
72
+ }
73
+
74
+ reactSurface.start();
75
+ }
76
+
77
+ public void clearSurface() {
78
+ clearSurface(true);
79
+ }
80
+
81
+ public void clearSurface(boolean stopSurface) {
82
+ if (surface != null && stopSurface) {
83
+ surface.stop();
84
+ }
85
+ surface = null;
86
+ reactSurfaceView = null;
87
+ removeAllViews();
88
+ }
89
+
90
+ public void setAppProperties(@Nullable Bundle appProperties) {
91
+ this.appProperties = appProperties == null ? null : new Bundle(appProperties);
92
+ applyAppProperties(this.appProperties);
93
+ }
94
+
95
+ @Override
96
+ public boolean dispatchTouchEvent(MotionEvent ev) {
97
+ if (shouldPassTouches(ev)) {
98
+ return false;
99
+ }
100
+ return super.dispatchTouchEvent(ev);
101
+ }
102
+
103
+ @SuppressLint("ClickableViewAccessibility")
104
+ @Override
105
+ public boolean onTouchEvent(MotionEvent event) {
106
+ ReactSurfaceView surfaceView = getReactSurfaceView();
107
+ if (surfaceView != null) {
108
+ surfaceView.onChildStartedNativeGesture(surfaceView, event);
109
+ }
110
+ return true;
111
+ }
112
+
113
+ private boolean shouldPassTouches(@NonNull MotionEvent ev) {
114
+ if (!passThroughTouches) {
115
+ return false;
116
+ }
117
+
118
+ int action = ev.getAction() & MotionEvent.ACTION_MASK;
119
+ if (action != MotionEvent.ACTION_DOWN) {
120
+ return false;
121
+ }
122
+
123
+ ReactSurfaceView surfaceView = getReactSurfaceView();
124
+ if (surfaceView == null) {
125
+ return false;
126
+ }
127
+
128
+ ViewGroup rootViewGroup = surfaceView.getRootViewGroup();
129
+
130
+ int tag = TouchTargetHelper.findTargetTagForTouch(ev.getX(), ev.getY(), rootViewGroup);
131
+ UIManager uiManager = UIManagerHelper.getUIManager(surfaceView.getCurrentReactContext(), UIManagerType.FABRIC);
132
+ if (uiManager == null) {
133
+ return false;
134
+ }
135
+
136
+ View view = uiManager.resolveView(tag);
137
+ if (view == null) {
138
+ return false;
139
+ }
140
+
141
+ if (view == surfaceView || view == surfaceView.getChildAt(0)) {
142
+ if (view.getWidth() == surfaceView.getWidth() && view.getHeight() == surfaceView.getHeight()) {
143
+ surfaceView.onChildStartedNativeGesture(surfaceView, ev);
144
+ return true;
145
+ }
146
+ }
147
+
148
+ return false;
149
+ }
150
+
151
+ @Nullable
152
+ private ReactSurfaceView getReactSurfaceView() {
153
+ if (reactSurfaceView == null) {
154
+ reactSurfaceView = findReactSurfaceView(this);
155
+ }
156
+ return reactSurfaceView;
157
+ }
158
+
159
+ @Nullable
160
+ private ReactSurfaceView findReactSurfaceView(@NonNull ViewGroup parent) {
161
+ for (int i = 0; i < parent.getChildCount(); i++) {
162
+ View child = parent.getChildAt(i);
163
+ if (child instanceof ReactSurfaceView surfaceView) {
164
+ return surfaceView;
165
+ }
166
+
167
+ if (child instanceof ViewGroup childGroup) {
168
+ ReactSurfaceView result = findReactSurfaceView(childGroup);
169
+ if (result != null) {
170
+ return result;
171
+ }
172
+ }
173
+ }
174
+
175
+ return null;
176
+ }
177
+
178
+ private void applyAppProperties(@Nullable Bundle appProperties) {
179
+ if (appProperties == null || surface == null) {
180
+ return;
181
+ }
182
+
183
+ try {
184
+ Method updateInitProps = surface.getClass().getMethod("updateInitProps", Bundle.class);
185
+ updateInitProps.invoke(surface, new Bundle(appProperties));
186
+ } catch (NoSuchMethodException ignored) {
187
+ } catch (IllegalAccessException | InvocationTargetException ignored) {
188
+ }
189
+ }
190
+ }
@@ -28,9 +28,9 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
28
28
 
29
29
  protected static final String TAG = "Navigation";
30
30
 
31
- private ViewGroup reactViewHolder;
31
+ private HBDRootView reactRootView;
32
32
 
33
- private ReactSurface reactRootView;
33
+ private ReactSurface reactSurface;
34
34
  private ReactSurface reactTitleView;
35
35
  private boolean firstRenderCompleted;
36
36
 
@@ -38,7 +38,7 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
38
38
  @Override
39
39
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
40
40
  View view = inflater.inflate(R.layout.nav_fragment_react, container, false);
41
- reactViewHolder = view.findViewById(R.id.react_content);
41
+ reactRootView = view.findViewById(R.id.react_content);
42
42
  if (isReactModuleRegisterCompleted()) {
43
43
  mountReactView();
44
44
  }
@@ -56,6 +56,7 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
56
56
  public void onDestroyView() {
57
57
  super.onDestroyView();
58
58
  unmountReactView();
59
+ reactRootView = null;
59
60
  }
60
61
 
61
62
  @Override
@@ -111,7 +112,7 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
111
112
  }
112
113
 
113
114
  private boolean isViewReady() {
114
- if (reactRootView == null) {
115
+ if (reactSurface == null) {
115
116
  return false;
116
117
  }
117
118
  return firstRenderCompleted;
@@ -159,19 +160,25 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
159
160
  private void unmountReactView() {
160
161
  getReactManager().removeReactBridgeReloadListener(this);
161
162
 
163
+ boolean canStopSurface = false;
162
164
  ReactContext reactContext = getCurrentReactContext();
163
- if (reactContext == null || !reactContext.hasActiveReactInstance()) {
164
- return;
165
+ if (reactContext != null && reactContext.hasActiveReactInstance()) {
166
+ canStopSurface = true;
165
167
  }
166
168
 
167
- if (reactRootView != null) {
169
+ if (reactSurface != null) {
168
170
  FLog.w(TAG, "销毁页面-:" + getModuleName());
169
- reactRootView.stop();
170
- reactRootView = null;
171
+ reactSurface = null;
172
+ }
173
+
174
+ if (reactRootView != null) {
175
+ reactRootView.clearSurface(canStopSurface);
171
176
  }
172
177
 
173
178
  if (reactTitleView != null) {
174
- reactTitleView.stop();
179
+ if (canStopSurface) {
180
+ reactTitleView.stop();
181
+ }
175
182
  reactTitleView = null;
176
183
  }
177
184
  }
@@ -215,10 +222,15 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
215
222
  private void initReactRootView() {
216
223
  ReactHost reactHost = getReactManager().getReactHost();
217
224
  ReactSurface reactSurface = reactHost.createSurface(requireContext(), getModuleName(), getProps());
218
- reactRootView = reactSurface;
219
- ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT);
220
- reactViewHolder.addView(reactSurface.getView(), layoutParams);
221
- reactSurface.start();
225
+ this.reactSurface = reactSurface;
226
+
227
+ HBDRootView rootView = reactRootView;
228
+ if (rootView == null) {
229
+ throw new IllegalStateException("[Navigation] HBDRootView 还没有创建。");
230
+ }
231
+ rootView.setPassThroughTouches(shouldPassThroughTouches());
232
+ rootView.setAppProperties(getProps());
233
+ rootView.setSurface(reactSurface);
222
234
  }
223
235
 
224
236
  private void initReactTitleView() {
@@ -244,12 +256,21 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
244
256
  boolean expanded = "expanded".equals(fitting);
245
257
  ReactHost reactHost = getReactManager().getReactHost();
246
258
  ReactSurface reactSurface = reactHost.createSurface(requireContext(), moduleName, getProps());
259
+ reactTitleView = reactSurface;
247
260
  Toolbar.LayoutParams layoutParams = createTitleLayoutParams(expanded);
248
261
  Toolbar toolbar = getToolbar();
249
262
  toolbar.addView(reactSurface.getView(), layoutParams);
250
263
  reactSurface.start();
251
264
  }
252
265
 
266
+ @Override
267
+ public void setAppProperties(@NonNull Bundle props) {
268
+ super.setAppProperties(props);
269
+ if (isReactModuleRegisterCompleted() && reactRootView != null) {
270
+ reactRootView.setAppProperties(getProps());
271
+ }
272
+ }
273
+
253
274
  @NonNull
254
275
  private Toolbar.LayoutParams createTitleLayoutParams(boolean expanded) {
255
276
  if (expanded) {
@@ -1,7 +1,5 @@
1
1
  <?xml version="1.0" encoding="utf-8"?>
2
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2
+ <com.reactnative.hybridnavigation.HBDRootView xmlns:android="http://schemas.android.com/apk/res/android"
3
3
  android:id="@+id/react_content"
4
4
  android:layout_width="match_parent"
5
- android:layout_height="match_parent">
6
-
7
- </FrameLayout>
5
+ android:layout_height="match_parent" />
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hybrid-navigation",
3
3
  "description": "React Native Navigation that supports seamless navigation between Native and React.",
4
- "version": "3.1.2",
4
+ "version": "3.1.3",
5
5
  "main": "./dist/index",
6
6
  "module": "./dist/index",
7
7
  "types": "./dist/index.d.ts",