-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[android] add messaging passing without a react tag #12
[android] add messaging passing without a react tag #12
Conversation
const scriptMessageEmitter = new NativeEventEmitter( | ||
Platform.select({ | ||
ios: NativeModules.ScriptMessageEventEmitter, | ||
android: null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
android doesn't really need an associated module to emit an event
src/WebViewTypes.ts
Outdated
@@ -1280,4 +1280,32 @@ export interface WebViewSharedProps extends ViewProps { | |||
* An object that specifies the credentials of a user to be used for basic authentication. | |||
*/ | |||
basicAuthCredential?: BasicAuthCredential; | |||
|
|||
/** | |||
* By default, if this is undefined or false, the native WebView will get released when |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've copied these props to Android
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you've added these to export interface WebViewSharedProps extends ViewProps
. They were previously defined on export interface IOSWebViewProps extends WebViewSharedProps
. Instead of copying this to WebViewSharedProps
, should we copy this to export interface AndroidWebViewProps extends WebViewSharedProps
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops - good catch 👍
@@ -1835,6 +1849,11 @@ protected void onScrollChanged(int x, int y, int oldX, int oldY) { | |||
} | |||
|
|||
protected void dispatchEvent(WebView webView, Event event) { | |||
if (event.getViewTag() == RNCWebView.INVALID_VIEW_ID) { | |||
FLog.w(TAG, "Unable to dispatch event: ", event.getEventName() + "due to InternalWebView not being attached."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a warning is enough. This will likely happen for any callbacks such as onLoadingStarted
etc when the webview is unmounted. I am assuming it's likely we'll have the same issue on iOS. It might be nicer in the future to dispatch events for all of these callbacks so they can be listened to without a react tag.
@@ -9,6 +10,7 @@ | |||
|
|||
public class RNCWebView extends FrameLayout { | |||
private static final String TAG = "RNCWebView"; | |||
public static final int INVALID_VIEW_ID = -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://developer.android.com/reference/android/view/View#findViewById(int)
if the ID is invalid (< 0) or there is no matching view in the hierarchy.
I used -1 as it's a recognized invalid view id
WritableMap data = mRNCWebViewClient.createWebViewEvent(webView, webView.getUrl()); | ||
data.putString("webViewKey", webViewKey); | ||
data.putString("data", message); | ||
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("onMessage", data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I learned that the event names need to be unique for your entire app, I think we should rename this to ReactNativeWebViewOnMessage
or something of that sort.
https://github.com/facebook/react-native/blob/main/Libraries/EventEmitter/NativeEventEmitter.js#L35
I'll rename both in a follow up PR for both iOS and Android
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Nice work 🙂 . My comments are all non-blocking
/** | ||
* Provides the associated parent RNCWebView viewId for the provided | ||
* webView view id. | ||
* @param webView |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: On all these javadoc comments, we probably don't need the @param parameter name
lines unless there's a comment that specifically describes what that parameter is. Otherwise, that line from the javadoc will be redundant with the method signature
src/WebViewTypes.ts
Outdated
@@ -1280,4 +1280,32 @@ export interface WebViewSharedProps extends ViewProps { | |||
* An object that specifies the credentials of a user to be used for basic authentication. | |||
*/ | |||
basicAuthCredential?: BasicAuthCredential; | |||
|
|||
/** | |||
* By default, if this is undefined or false, the native WebView will get released when |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you've added these to export interface WebViewSharedProps extends ViewProps
. They were previously defined on export interface IOSWebViewProps extends WebViewSharedProps
. Instead of copying this to WebViewSharedProps
, should we copy this to export interface AndroidWebViewProps extends WebViewSharedProps
?
android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java
Outdated
Show resolved
Hide resolved
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java
Show resolved
Hide resolved
@@ -1,4 +1,4 @@ | |||
import { NativeModules } from 'react-native'; | |||
export default function injectJavaScriptWithWebViewKey(webViewKey, script) { | |||
NativeModules.RNCWebView.injectJavaScript(webViewKey, script); | |||
NativeModules.RNCWebView.injectJavaScriptWithWebViewKey(webViewKey, script); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change generated from yarn build
? In the past, sometimes when I rebuilt the lib folder, it resulted in some filename.d.ts
changes. I'm wondering if we should expect to see similar changes given the WebViewTypes.ts
changes in this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was generated via yarn build
. I don't think we should see any d.ts
changes since no types were changed.
…bView in view hierarchy * add support to reuse instance with webViewKey * change up * address comments related to layout params * add nullability checks * fix * get full stack trace * add secondary map * rename to ifHasInternalWebView * additional fixes * fix * fix boolean [android] add messaging passing without a react tag (#12) This PR adds messaging passing. This was much simpler to do than the iOS counter part. This is the analogue to the following PR: #6. I've followed the same test plan. The sample app was really nice to use when testing. In the previous PR #11, I broke all native->react-native communication since events were being passed by the `InternalWebView` viewId not the `RNCWebView` viewId. This is now fixed. [android] check source prop if changed (#14) * [android] check source prop if changed * detect if new source is null too
…bView in view hierarchy * add support to reuse instance with webViewKey * change up * address comments related to layout params * add nullability checks * fix * get full stack trace * add secondary map * rename to ifHasInternalWebView * additional fixes * fix * fix boolean [android] add messaging passing without a react tag (#12) This PR adds messaging passing. This was much simpler to do than the iOS counter part. This is the analogue to the following PR: #6. I've followed the same test plan. The sample app was really nice to use when testing. In the previous PR #11, I broke all native->react-native communication since events were being passed by the `InternalWebView` viewId not the `RNCWebView` viewId. This is now fixed. [android] check source prop if changed (#14) * [android] check source prop if changed * detect if new source is null too
…bView in view hierarchy * add support to reuse instance with webViewKey * change up * address comments related to layout params * add nullability checks * fix * get full stack trace * add secondary map * rename to ifHasInternalWebView * additional fixes * fix * fix boolean [android] add messaging passing without a react tag (#12) This PR adds messaging passing. This was much simpler to do than the iOS counter part. This is the analogue to the following PR: #6. I've followed the same test plan. The sample app was really nice to use when testing. In the previous PR #11, I broke all native->react-native communication since events were being passed by the `InternalWebView` viewId not the `RNCWebView` viewId. This is now fixed. [android] check source prop if changed (#14) * [android] check source prop if changed * detect if new source is null too
This PR adds messaging passing. This was much simpler to do than the iOS counter part. This is the analogue to the following PR: #6. I've followed the same test plan. The sample app was really nice to use when testing.
In the previous PR #11, I broke all native->react-native communication since events were being passed by the
InternalWebView
viewId not theRNCWebView
viewId. This is now fixed.Here's a demo:
message-passing.mov