Преглед на файлове

feat(Android): Implement direct communication between Android code and JS (#1203)

ivari преди 4 години
родител
ревизия
c88e380762
No account linked to committer's email address
променени са 2 файла, в които са добавени 45 реда и са изтрити 2 реда
  1. 39
    2
      android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java
  2. 6
    0
      src/WebView.android.tsx

+ 39
- 2
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java Целия файл

41
 import com.facebook.react.views.scroll.ScrollEventType;
41
 import com.facebook.react.views.scroll.ScrollEventType;
42
 import com.facebook.react.views.scroll.OnScrollDispatchHelper;
42
 import com.facebook.react.views.scroll.OnScrollDispatchHelper;
43
 import com.facebook.react.bridge.Arguments;
43
 import com.facebook.react.bridge.Arguments;
44
+import com.facebook.react.bridge.CatalystInstance;
44
 import com.facebook.react.bridge.LifecycleEventListener;
45
 import com.facebook.react.bridge.LifecycleEventListener;
45
 import com.facebook.react.bridge.ReactContext;
46
 import com.facebook.react.bridge.ReactContext;
46
 import com.facebook.react.bridge.ReadableArray;
47
 import com.facebook.react.bridge.ReadableArray;
47
 import com.facebook.react.bridge.ReadableMap;
48
 import com.facebook.react.bridge.ReadableMap;
48
 import com.facebook.react.bridge.ReadableMapKeySetIterator;
49
 import com.facebook.react.bridge.ReadableMapKeySetIterator;
49
 import com.facebook.react.bridge.WritableMap;
50
 import com.facebook.react.bridge.WritableMap;
51
+import com.facebook.react.bridge.WritableNativeArray;
52
+import com.facebook.react.bridge.WritableNativeMap;
50
 import com.facebook.react.common.MapBuilder;
53
 import com.facebook.react.common.MapBuilder;
51
 import com.facebook.react.common.build.ReactBuildConfig;
54
 import com.facebook.react.common.build.ReactBuildConfig;
52
 import com.facebook.react.module.annotations.ReactModule;
55
 import com.facebook.react.module.annotations.ReactModule;
973
     protected boolean messagingEnabled = false;
976
     protected boolean messagingEnabled = false;
974
     protected @Nullable
977
     protected @Nullable
975
     RNCWebViewClient mRNCWebViewClient;
978
     RNCWebViewClient mRNCWebViewClient;
979
+    protected @Nullable
980
+    CatalystInstance mCatalystInstance;
976
     protected boolean sendContentSizeChangeEvents = false;
981
     protected boolean sendContentSizeChangeEvents = false;
977
     private OnScrollDispatchHelper mOnScrollDispatchHelper;
982
     private OnScrollDispatchHelper mOnScrollDispatchHelper;
978
     protected boolean hasScrollEvent = false;
983
     protected boolean hasScrollEvent = false;
1047
       return new RNCWebViewBridge(webView);
1052
       return new RNCWebViewBridge(webView);
1048
     }
1053
     }
1049
 
1054
 
1055
+    protected void createCatalystInstance() {
1056
+      ReactContext reactContext = (ReactContext) this.getContext();
1057
+
1058
+      if (reactContext != null) {
1059
+        mCatalystInstance = reactContext.getCatalystInstance();
1060
+      }
1061
+    }
1062
+
1050
     @SuppressLint("AddJavascriptInterface")
1063
     @SuppressLint("AddJavascriptInterface")
1051
     public void setMessagingEnabled(boolean enabled) {
1064
     public void setMessagingEnabled(boolean enabled) {
1052
       if (messagingEnabled == enabled) {
1065
       if (messagingEnabled == enabled) {
1057
 
1070
 
1058
       if (enabled) {
1071
       if (enabled) {
1059
         addJavascriptInterface(createRNCWebViewBridge(this), JAVASCRIPT_INTERFACE);
1072
         addJavascriptInterface(createRNCWebViewBridge(this), JAVASCRIPT_INTERFACE);
1073
+        this.createCatalystInstance();
1060
       } else {
1074
       } else {
1061
         removeJavascriptInterface(JAVASCRIPT_INTERFACE);
1075
         removeJavascriptInterface(JAVASCRIPT_INTERFACE);
1062
       }
1076
       }
1085
     }
1099
     }
1086
 
1100
 
1087
     public void onMessage(String message) {
1101
     public void onMessage(String message) {
1102
+      ReactContext reactContext = (ReactContext) this.getContext();
1103
+      RNCWebView mContext = this;
1104
+
1088
       if (mRNCWebViewClient != null) {
1105
       if (mRNCWebViewClient != null) {
1089
         WebView webView = this;
1106
         WebView webView = this;
1090
         webView.post(new Runnable() {
1107
         webView.post(new Runnable() {
1095
             }
1112
             }
1096
             WritableMap data = mRNCWebViewClient.createWebViewEvent(webView, webView.getUrl());
1113
             WritableMap data = mRNCWebViewClient.createWebViewEvent(webView, webView.getUrl());
1097
             data.putString("data", message);
1114
             data.putString("data", message);
1098
-            dispatchEvent(webView, new TopMessageEvent(webView.getId(), data));
1115
+
1116
+            if (mCatalystInstance != null) {
1117
+              mContext.sendDirectMessage(data);
1118
+            } else {
1119
+              dispatchEvent(webView, new TopMessageEvent(webView.getId(), data));
1120
+            }
1099
           }
1121
           }
1100
         });
1122
         });
1101
       } else {
1123
       } else {
1102
         WritableMap eventData = Arguments.createMap();
1124
         WritableMap eventData = Arguments.createMap();
1103
         eventData.putString("data", message);
1125
         eventData.putString("data", message);
1104
-        dispatchEvent(this, new TopMessageEvent(this.getId(), eventData));
1126
+
1127
+        if (mCatalystInstance != null) {
1128
+          this.sendDirectMessage(eventData);
1129
+        } else {
1130
+          dispatchEvent(this, new TopMessageEvent(this.getId(), eventData));
1131
+        }
1105
       }
1132
       }
1106
     }
1133
     }
1107
 
1134
 
1135
+    protected void sendDirectMessage(WritableMap data) {
1136
+      WritableNativeMap event = new WritableNativeMap();
1137
+      event.putMap("nativeEvent", data);
1138
+
1139
+      WritableNativeArray params = new WritableNativeArray();
1140
+      params.pushMap(event);
1141
+
1142
+      mCatalystInstance.callFunction("WebViewMessageHandler", "onMessage", params);
1143
+    }
1144
+
1108
     protected void onScrollChanged(int x, int y, int oldX, int oldY) {
1145
     protected void onScrollChanged(int x, int y, int oldX, int oldY) {
1109
       super.onScrollChanged(x, y, oldX, oldY);
1146
       super.onScrollChanged(x, y, oldX, oldY);
1110
 
1147
 

+ 6
- 0
src/WebView.android.tsx Целия файл

10
   findNodeHandle,
10
   findNodeHandle,
11
 } from 'react-native';
11
 } from 'react-native';
12
 
12
 
13
+import BatchedBridge from 'react-native/Libraries/BatchedBridge/BatchedBridge';
14
+
13
 import invariant from 'invariant';
15
 import invariant from 'invariant';
14
 
16
 
15
 import {
17
 import {
70
 
72
 
71
   webViewRef = React.createRef<NativeWebViewAndroid>();
73
   webViewRef = React.createRef<NativeWebViewAndroid>();
72
 
74
 
75
+  componentDidMount = () => {
76
+    BatchedBridge.registerCallableModule('WebViewMessageHandler', this);
77
+  }
78
+
73
   getCommands = () => UIManager.getViewManagerConfig('RNCWebView').Commands;
79
   getCommands = () => UIManager.getViewManagerConfig('RNCWebView').Commands;
74
 
80
 
75
   goForward = () => {
81
   goForward = () => {