From e1cbcf7ec6a8ef508dd37c4e4960b7bd619d55b3 Mon Sep 17 00:00:00 2001 From: Marko Savic Date: Thu, 15 Oct 2020 17:21:46 -0400 Subject: [PATCH] [RN Mobile] Ube cannot view or interact with the classic block on jetpack sites (#26168) --- .../GutenbergWebViewActivity.java | 46 +++++++++++++------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/packages/react-native-bridge/android/src/main/java/org/wordpress/mobile/ReactNativeGutenbergBridge/GutenbergWebViewActivity.java b/packages/react-native-bridge/android/src/main/java/org/wordpress/mobile/ReactNativeGutenbergBridge/GutenbergWebViewActivity.java index 78b4882666fc6..f89df4562b42d 100644 --- a/packages/react-native-bridge/android/src/main/java/org/wordpress/mobile/ReactNativeGutenbergBridge/GutenbergWebViewActivity.java +++ b/packages/react-native-bridge/android/src/main/java/org/wordpress/mobile/ReactNativeGutenbergBridge/GutenbergWebViewActivity.java @@ -4,6 +4,7 @@ import android.graphics.Bitmap; import android.os.Bundle; import android.os.Handler; +import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -48,6 +49,23 @@ public class GutenbergWebViewActivity extends AppCompatActivity { private boolean mIsGutenbergReady; private AtomicBoolean mIsWebPageLoaded = new AtomicBoolean(false); private AtomicBoolean mIsBlockContentInserted = new AtomicBoolean(false); + private final Handler mWebPageLoadedHandler = new Handler(); + private final Runnable mWebPageLoadedRunnable = new Runnable() { + @Override public void run() { + if (!mIsWebPageLoaded.getAndSet(true)) { + mProgressBar.setVisibility(View.GONE); + // We want to insert block content + // only if gutenberg is ready + if (mIsGutenbergReady) { + final Handler handler = new Handler(); + handler.postDelayed(() -> { + // Insert block content + insertBlockScript(); + }, 200); + } + } + } + }; @SuppressLint("SetJavaScriptEnabled") protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -77,21 +95,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { mWebView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int progress) { - if (progress == 100 && !mIsWebPageLoaded.getAndSet(true)) { - // We want to insert block content - // only if gutenberg is ready - if (mIsGutenbergReady) { - mProgressBar.setVisibility(View.GONE); - final Handler handler = new Handler(); - handler.postDelayed(() -> { - // Insert block content - insertBlockScript(); - }, 200); - } - } - else { - if (progress < 100) { - mIsWebPageLoaded.compareAndSet(true, false); + if (progress == 100) { + mWebPageLoadedHandler.removeCallbacks(mWebPageLoadedRunnable); + mWebPageLoadedHandler.postDelayed(mWebPageLoadedRunnable, 1500); + } else { + mIsWebPageLoaded.compareAndSet(true, false); + if (mProgressBar.getVisibility() == View.GONE) { + mProgressBar.setVisibility(View.VISIBLE); } mProgressBar.setProgress(progress); } @@ -351,6 +361,12 @@ public void finish() { super.finish(); } + @Override + protected void onDestroy() { + mWebPageLoadedHandler.removeCallbacks(mWebPageLoadedRunnable); + super.onDestroy(); + } + public class WPWebKit { @JavascriptInterface public void postMessage(String content) {