Skip to content
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

Merged
merged 12 commits into from
Sep 6, 2022

Conversation

nealmanaktola
Copy link

@nealmanaktola nealmanaktola commented Sep 1, 2022

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.

Here's a demo:

message-passing.mov

@nealmanaktola nealmanaktola changed the title [android] add messaging passing [android] add messaging passing (not-ready-for-review) Sep 1, 2022
const scriptMessageEmitter = new NativeEventEmitter(
Platform.select({
ios: NativeModules.ScriptMessageEventEmitter,
android: null
Copy link
Author

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

@@ -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
Copy link
Author

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

Copy link

@donaldchen donaldchen Sep 2, 2022

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?

Copy link
Author

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.");
Copy link
Author

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;
Copy link
Author

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);
Copy link
Author

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nealmanaktola nealmanaktola changed the title [android] add messaging passing (not-ready-for-review) [android] add messaging passing without a react tag Sep 2, 2022
Copy link

@donaldchen donaldchen left a 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

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

@@ -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
Copy link

@donaldchen donaldchen Sep 2, 2022

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?

@@ -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);
Copy link

@donaldchen donaldchen Sep 2, 2022

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

Copy link
Author

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.

@nealmanaktola nealmanaktola merged commit cc8a18a into 11.18.1-discord-1 Sep 6, 2022
nealmanaktola added a commit that referenced this pull request Sep 13, 2022
…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
donaldchen pushed a commit that referenced this pull request Mar 28, 2023
…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
donaldchen pushed a commit that referenced this pull request Mar 28, 2023
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants