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 rebind method #22

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.reactnativecommunity.webview;

import android.view.View;
import android.webkit.WebView;
import android.widget.FrameLayout;

Expand All @@ -8,20 +9,77 @@
import com.facebook.common.logging.FLog;
import com.facebook.react.uimanager.ThemedReactContext;

import java.util.Map;

public class RNCWebView extends FrameLayout {
private static final String TAG = "RNCWebView";
public static final int INVALID_VIEW_ID = -1;

private String webViewKey;
private RNCWebViewManager.InternalWebView internalWebView;

public RNCWebView(ThemedReactContext reactContext) {
super(reactContext);

// There is an issue with react-native where if a view is moved
// the parent and children views are not resized
// Since we are moving views outside of React, the layout request might be dropped
// on a normal add/remove view
// By calling .layout directly on the parent, it'll force the layout change
// See this issue for more context: https://github.com/facebook/react-native/issues/17968

Choose a reason for hiding this comment

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

facebook/react-native#17968 (comment)

Unbelievable that this bug is still open.

Wow, same. Anyway, nice work on identifying this issue, and I'm glad calling layout directly fixes this!

setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
@Override
public void onChildViewAdded(View parent, View child) {
if (parent != null) {
parent.measure(
MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY)
);
parent.layout(0, 0, parent.getMeasuredWidth(), parent.getMeasuredHeight());

Choose a reason for hiding this comment

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

nit: It may be slightly more readable to name the l / t / r / b parameters inline with code comments sort of like this.

parent.layout(
  0 /* left */ ,
  0 /* top */,
  parent.getMeasuredWidth() /* right */,
  parent.getMeasuredHeight() /* bottom */
);

}
}

@Override
public void onChildViewRemoved(View parent, View child) {}
});
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();

if (webViewKey == null) return;

Map<String, WebView> internalWebViewMap = RNCWebViewMapManager.INSTANCE.getInternalWebViewMap();
Map<String, RNCWebView> rncWebViewMap = RNCWebViewMapManager.INSTANCE.getRncWebViewMap();

// If there is an existing RNCWebView that has an internal webview, re-attach it to this view
if (rncWebViewMap.containsKey(webViewKey)) {
RNCWebView existingView = rncWebViewMap.get(webViewKey);
RNCWebViewManager.InternalWebView existingInternalWebView = existingView.detachWebView();
attachWebView(existingInternalWebView);

// If there is a detached internal webview attach it to this RNCWebView
} else if (internalWebViewMap.containsKey(webViewKey)) {
RNCWebViewManager.InternalWebView webView = (RNCWebViewManager.InternalWebView) internalWebViewMap.get(webViewKey);
attachWebView(webView);
}

if (internalWebView != null) {
internalWebView.setWebViewKey(webViewKey);
RNCWebViewMapManager.INSTANCE.getViewIdMap().put(internalWebView.getId(), this.getId());
internalWebViewMap.put(webViewKey, internalWebView);
rncWebViewMap.put(webViewKey, this);
}
}

public interface Action {
void apply(RNCWebViewManager.InternalWebView webView);
}

public void setWebViewKey(String webViewKey) {
this.webViewKey = webViewKey;
}

/**
* Attaches a {@link RNCWebViewManager.InternalWebView} to the RNCWebView parent
* Throws an exception if the provided internal webView is already attached to a parent
Expand Down
Loading