Browse Source

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 years ago
parent
commit
b97d16c23d
No account linked to committer's email address

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

109
 @ReactModule(name = RNCWebViewManager.REACT_CLASS)
109
 @ReactModule(name = RNCWebViewManager.REACT_CLASS)
110
 public class RNCWebViewManager extends SimpleViewManager<WebView> {
110
 public class RNCWebViewManager extends SimpleViewManager<WebView> {
111
 
111
 
112
-  public static String activeUrl = null;
113
   public static final int COMMAND_GO_BACK = 1;
112
   public static final int COMMAND_GO_BACK = 1;
114
   public static final int COMMAND_GO_FORWARD = 2;
113
   public static final int COMMAND_GO_FORWARD = 2;
115
   public static final int COMMAND_RELOAD = 3;
114
   public static final int COMMAND_RELOAD = 3;
623
         if (args == null) {
622
         if (args == null) {
624
           throw new RuntimeException("Arguments for loading an url are null!");
623
           throw new RuntimeException("Arguments for loading an url are null!");
625
         }
624
         }
625
+        ((RNCWebView) root).progressChangedFilter.setWaitingForCommandLoadUrl(false);
626
         root.loadUrl(args.getString(0));
626
         root.loadUrl(args.getString(0));
627
         break;
627
         break;
628
       case COMMAND_FOCUS:
628
       case COMMAND_FOCUS:
728
     protected boolean mLastLoadFailed = false;
728
     protected boolean mLastLoadFailed = false;
729
     protected @Nullable
729
     protected @Nullable
730
     ReadableArray mUrlPrefixesForDefaultIntent;
730
     ReadableArray mUrlPrefixesForDefaultIntent;
731
+    protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
731
     protected @Nullable String ignoreErrFailedForThisURL = null;
732
     protected @Nullable String ignoreErrFailedForThisURL = null;
732
 
733
 
733
     public void setIgnoreErrFailedForThisURL(@Nullable String url) {
734
     public void setIgnoreErrFailedForThisURL(@Nullable String url) {
761
 
762
 
762
     @Override
763
     @Override
763
     public boolean shouldOverrideUrlLoading(WebView view, String url) {
764
     public boolean shouldOverrideUrlLoading(WebView view, String url) {
764
-      activeUrl = url;
765
+      progressChangedFilter.setWaitingForCommandLoadUrl(true);
765
       dispatchEvent(
766
       dispatchEvent(
766
         view,
767
         view,
767
         new TopShouldStartLoadWithRequestEvent(
768
         new TopShouldStartLoadWithRequestEvent(
858
     public void setUrlPrefixesForDefaultIntent(ReadableArray specialUrls) {
859
     public void setUrlPrefixesForDefaultIntent(ReadableArray specialUrls) {
859
       mUrlPrefixesForDefaultIntent = specialUrls;
860
       mUrlPrefixesForDefaultIntent = specialUrls;
860
     }
861
     }
862
+
863
+    public void setProgressChangedFilter(RNCWebView.ProgressChangedFilter filter) {
864
+      progressChangedFilter = filter;
865
+    }
861
   }
866
   }
862
 
867
 
863
   protected static class RNCWebChromeClient extends WebChromeClient implements LifecycleEventListener {
868
   protected static class RNCWebChromeClient extends WebChromeClient implements LifecycleEventListener {
879
     protected View mVideoView;
884
     protected View mVideoView;
880
     protected WebChromeClient.CustomViewCallback mCustomViewCallback;
885
     protected WebChromeClient.CustomViewCallback mCustomViewCallback;
881
 
886
 
887
+    protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
888
+
882
     public RNCWebChromeClient(ReactContext reactContext, WebView webView) {
889
     public RNCWebChromeClient(ReactContext reactContext, WebView webView) {
883
       this.mReactContext = reactContext;
890
       this.mReactContext = reactContext;
884
       this.mWebView = webView;
891
       this.mWebView = webView;
933
     public void onProgressChanged(WebView webView, int newProgress) {
940
     public void onProgressChanged(WebView webView, int newProgress) {
934
       super.onProgressChanged(webView, newProgress);
941
       super.onProgressChanged(webView, newProgress);
935
       final String url = webView.getUrl();
942
       final String url = webView.getUrl();
936
-      if (
937
-        url != null
938
-        && activeUrl != null
939
-        && !url.equals(activeUrl)
940
-      ) {
943
+      if (progressChangedFilter.isWaitingForCommandLoadUrl()) {
941
         return;
944
         return;
942
       }
945
       }
943
       WritableMap event = Arguments.createMap();
946
       WritableMap event = Arguments.createMap();
995
     protected ViewGroup getRootView() {
998
     protected ViewGroup getRootView() {
996
       return (ViewGroup) mReactContext.getCurrentActivity().findViewById(android.R.id.content);
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
     protected boolean sendContentSizeChangeEvents = false;
1021
     protected boolean sendContentSizeChangeEvents = false;
1015
     private OnScrollDispatchHelper mOnScrollDispatchHelper;
1022
     private OnScrollDispatchHelper mOnScrollDispatchHelper;
1016
     protected boolean hasScrollEvent = false;
1023
     protected boolean hasScrollEvent = false;
1024
+    protected ProgressChangedFilter progressChangedFilter;
1017
 
1025
 
1018
     /**
1026
     /**
1019
      * WebView must be created with an context of the current activity
1027
      * WebView must be created with an context of the current activity
1023
      */
1031
      */
1024
     public RNCWebView(ThemedReactContext reactContext) {
1032
     public RNCWebView(ThemedReactContext reactContext) {
1025
       super(reactContext);
1033
       super(reactContext);
1034
+      progressChangedFilter = new ProgressChangedFilter();
1026
     }
1035
     }
1027
 
1036
 
1028
     public void setIgnoreErrFailedForThisURL(String url) {
1037
     public void setIgnoreErrFailedForThisURL(String url) {
1073
       super.setWebViewClient(client);
1082
       super.setWebViewClient(client);
1074
       if (client instanceof RNCWebViewClient) {
1083
       if (client instanceof RNCWebViewClient) {
1075
         mRNCWebViewClient = (RNCWebViewClient) client;
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
         mContext.onMessage(message);
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
 }