fix(android): Improve onLoadProgress consistency (#1373 by @hojason117)
[skip ci] Co-authored-by: Tyler Coffman <tyler.coffman@appfolio.com> Co-authored-by: Jamon Holmgren <jamonholmgren@gmail.com>
This commit is contained in:
parent
79eb905a97
commit
b97d16c23d
|
@ -109,7 +109,6 @@ import javax.annotation.Nullable;
|
|||
@ReactModule(name = RNCWebViewManager.REACT_CLASS)
|
||||
public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
||||
|
||||
public static String activeUrl = null;
|
||||
public static final int COMMAND_GO_BACK = 1;
|
||||
public static final int COMMAND_GO_FORWARD = 2;
|
||||
public static final int COMMAND_RELOAD = 3;
|
||||
|
@ -623,6 +622,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
if (args == null) {
|
||||
throw new RuntimeException("Arguments for loading an url are null!");
|
||||
}
|
||||
((RNCWebView) root).progressChangedFilter.setWaitingForCommandLoadUrl(false);
|
||||
root.loadUrl(args.getString(0));
|
||||
break;
|
||||
case COMMAND_FOCUS:
|
||||
|
@ -728,6 +728,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
protected boolean mLastLoadFailed = false;
|
||||
protected @Nullable
|
||||
ReadableArray mUrlPrefixesForDefaultIntent;
|
||||
protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
|
||||
protected @Nullable String ignoreErrFailedForThisURL = null;
|
||||
|
||||
public void setIgnoreErrFailedForThisURL(@Nullable String url) {
|
||||
|
@ -761,7 +762,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
||||
activeUrl = url;
|
||||
progressChangedFilter.setWaitingForCommandLoadUrl(true);
|
||||
dispatchEvent(
|
||||
view,
|
||||
new TopShouldStartLoadWithRequestEvent(
|
||||
|
@ -858,6 +859,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
public void setUrlPrefixesForDefaultIntent(ReadableArray specialUrls) {
|
||||
mUrlPrefixesForDefaultIntent = specialUrls;
|
||||
}
|
||||
|
||||
public void setProgressChangedFilter(RNCWebView.ProgressChangedFilter filter) {
|
||||
progressChangedFilter = filter;
|
||||
}
|
||||
}
|
||||
|
||||
protected static class RNCWebChromeClient extends WebChromeClient implements LifecycleEventListener {
|
||||
|
@ -879,6 +884,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
protected View mVideoView;
|
||||
protected WebChromeClient.CustomViewCallback mCustomViewCallback;
|
||||
|
||||
protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
|
||||
|
||||
public RNCWebChromeClient(ReactContext reactContext, WebView webView) {
|
||||
this.mReactContext = reactContext;
|
||||
this.mWebView = webView;
|
||||
|
@ -933,11 +940,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
public void onProgressChanged(WebView webView, int newProgress) {
|
||||
super.onProgressChanged(webView, newProgress);
|
||||
final String url = webView.getUrl();
|
||||
if (
|
||||
url != null
|
||||
&& activeUrl != null
|
||||
&& !url.equals(activeUrl)
|
||||
) {
|
||||
if (progressChangedFilter.isWaitingForCommandLoadUrl()) {
|
||||
return;
|
||||
}
|
||||
WritableMap event = Arguments.createMap();
|
||||
|
@ -995,6 +998,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
protected ViewGroup getRootView() {
|
||||
return (ViewGroup) mReactContext.getCurrentActivity().findViewById(android.R.id.content);
|
||||
}
|
||||
|
||||
public void setProgressChangedFilter(RNCWebView.ProgressChangedFilter filter) {
|
||||
progressChangedFilter = filter;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1014,6 +1021,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
protected boolean sendContentSizeChangeEvents = false;
|
||||
private OnScrollDispatchHelper mOnScrollDispatchHelper;
|
||||
protected boolean hasScrollEvent = false;
|
||||
protected ProgressChangedFilter progressChangedFilter;
|
||||
|
||||
/**
|
||||
* WebView must be created with an context of the current activity
|
||||
|
@ -1023,6 +1031,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
*/
|
||||
public RNCWebView(ThemedReactContext reactContext) {
|
||||
super(reactContext);
|
||||
progressChangedFilter = new ProgressChangedFilter();
|
||||
}
|
||||
|
||||
public void setIgnoreErrFailedForThisURL(String url) {
|
||||
|
@ -1073,6 +1082,15 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
super.setWebViewClient(client);
|
||||
if (client instanceof RNCWebViewClient) {
|
||||
mRNCWebViewClient = (RNCWebViewClient) client;
|
||||
mRNCWebViewClient.setProgressChangedFilter(progressChangedFilter);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWebChromeClient(WebChromeClient client) {
|
||||
super.setWebChromeClient(client);
|
||||
if (client instanceof RNCWebChromeClient) {
|
||||
((RNCWebChromeClient) client).setProgressChangedFilter(progressChangedFilter);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1232,5 +1250,17 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
mContext.onMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
protected static class ProgressChangedFilter {
|
||||
private boolean waitingForCommandLoadUrl = false;
|
||||
|
||||
public void setWaitingForCommandLoadUrl(boolean isWaiting) {
|
||||
waitingForCommandLoadUrl = isWaiting;
|
||||
}
|
||||
|
||||
public boolean isWaitingForCommandLoadUrl() {
|
||||
return waitingForCommandLoadUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue