Sfoglia il codice sorgente

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>
Jason Chia-Hsien Ho 4 anni fa
parent
commit
b97d16c23d
No account linked to committer's email address

+ 37
- 7
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java Vedi File

@@ -109,7 +109,6 @@ import javax.annotation.Nullable;
109 109
 @ReactModule(name = RNCWebViewManager.REACT_CLASS)
110 110
 public class RNCWebViewManager extends SimpleViewManager<WebView> {
111 111
 
112
-  public static String activeUrl = null;
113 112
   public static final int COMMAND_GO_BACK = 1;
114 113
   public static final int COMMAND_GO_FORWARD = 2;
115 114
   public static final int COMMAND_RELOAD = 3;
@@ -623,6 +622,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
623 622
         if (args == null) {
624 623
           throw new RuntimeException("Arguments for loading an url are null!");
625 624
         }
625
+        ((RNCWebView) root).progressChangedFilter.setWaitingForCommandLoadUrl(false);
626 626
         root.loadUrl(args.getString(0));
627 627
         break;
628 628
       case COMMAND_FOCUS:
@@ -728,6 +728,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
728 728
     protected boolean mLastLoadFailed = false;
729 729
     protected @Nullable
730 730
     ReadableArray mUrlPrefixesForDefaultIntent;
731
+    protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
731 732
     protected @Nullable String ignoreErrFailedForThisURL = null;
732 733
 
733 734
     public void setIgnoreErrFailedForThisURL(@Nullable String url) {
@@ -761,7 +762,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
761 762
 
762 763
     @Override
763 764
     public boolean shouldOverrideUrlLoading(WebView view, String url) {
764
-      activeUrl = url;
765
+      progressChangedFilter.setWaitingForCommandLoadUrl(true);
765 766
       dispatchEvent(
766 767
         view,
767 768
         new TopShouldStartLoadWithRequestEvent(
@@ -858,6 +859,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
858 859
     public void setUrlPrefixesForDefaultIntent(ReadableArray specialUrls) {
859 860
       mUrlPrefixesForDefaultIntent = specialUrls;
860 861
     }
862
+
863
+    public void setProgressChangedFilter(RNCWebView.ProgressChangedFilter filter) {
864
+      progressChangedFilter = filter;
865
+    }
861 866
   }
862 867
 
863 868
   protected static class RNCWebChromeClient extends WebChromeClient implements LifecycleEventListener {
@@ -879,6 +884,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
879 884
     protected View mVideoView;
880 885
     protected WebChromeClient.CustomViewCallback mCustomViewCallback;
881 886
 
887
+    protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
888
+
882 889
     public RNCWebChromeClient(ReactContext reactContext, WebView webView) {
883 890
       this.mReactContext = reactContext;
884 891
       this.mWebView = webView;
@@ -933,11 +940,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
933 940
     public void onProgressChanged(WebView webView, int newProgress) {
934 941
       super.onProgressChanged(webView, newProgress);
935 942
       final String url = webView.getUrl();
936
-      if (
937
-        url != null
938
-        && activeUrl != null
939
-        && !url.equals(activeUrl)
940
-      ) {
943
+      if (progressChangedFilter.isWaitingForCommandLoadUrl()) {
941 944
         return;
942 945
       }
943 946
       WritableMap event = Arguments.createMap();
@@ -995,6 +998,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
995 998
     protected ViewGroup getRootView() {
996 999
       return (ViewGroup) mReactContext.getCurrentActivity().findViewById(android.R.id.content);
997 1000
     }
1001
+
1002
+    public void setProgressChangedFilter(RNCWebView.ProgressChangedFilter filter) {
1003
+      progressChangedFilter = filter;
1004
+    }
998 1005
   }
999 1006
 
1000 1007
   /**
@@ -1014,6 +1021,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1014 1021
     protected boolean sendContentSizeChangeEvents = false;
1015 1022
     private OnScrollDispatchHelper mOnScrollDispatchHelper;
1016 1023
     protected boolean hasScrollEvent = false;
1024
+    protected ProgressChangedFilter progressChangedFilter;
1017 1025
 
1018 1026
     /**
1019 1027
      * WebView must be created with an context of the current activity
@@ -1023,6 +1031,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1023 1031
      */
1024 1032
     public RNCWebView(ThemedReactContext reactContext) {
1025 1033
       super(reactContext);
1034
+      progressChangedFilter = new ProgressChangedFilter();
1026 1035
     }
1027 1036
 
1028 1037
     public void setIgnoreErrFailedForThisURL(String url) {
@@ -1073,6 +1082,15 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1073 1082
       super.setWebViewClient(client);
1074 1083
       if (client instanceof RNCWebViewClient) {
1075 1084
         mRNCWebViewClient = (RNCWebViewClient) client;
1085
+        mRNCWebViewClient.setProgressChangedFilter(progressChangedFilter);
1086
+      }
1087
+    }
1088
+
1089
+    @Override
1090
+    public void setWebChromeClient(WebChromeClient client) {
1091
+      super.setWebChromeClient(client);
1092
+      if (client instanceof RNCWebChromeClient) {
1093
+        ((RNCWebChromeClient) client).setProgressChangedFilter(progressChangedFilter);
1076 1094
       }
1077 1095
     }
1078 1096
 
@@ -1232,5 +1250,17 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1232 1250
         mContext.onMessage(message);
1233 1251
       }
1234 1252
     }
1253
+
1254
+    protected static class ProgressChangedFilter {
1255
+      private boolean waitingForCommandLoadUrl = false;
1256
+
1257
+      public void setWaitingForCommandLoadUrl(boolean isWaiting) {
1258
+        waitingForCommandLoadUrl = isWaiting;
1259
+      }
1260
+
1261
+      public boolean isWaitingForCommandLoadUrl() {
1262
+        return waitingForCommandLoadUrl;
1263
+      }
1264
+    }
1235 1265
   }
1236 1266
 }