From 99df9db3fbd25a4415020cb61be85870fa525497 Mon Sep 17 00:00:00 2001 From: Marko Savic Date: Thu, 15 Oct 2020 12:53:30 -0400 Subject: [PATCH 1/3] Make sure that page is loaded before we try to insert block content --- .../GutenbergWebViewActivity.java | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 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..5a90cd72bc6c0 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 @@ -48,6 +48,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)) { + // 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); + } + } + } + }; @SuppressLint("SetJavaScriptEnabled") protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -77,22 +94,11 @@ 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); mProgressBar.setProgress(progress); } } From eeff0fea78dcaf93192ad27e0cf2e2ee052f22cf Mon Sep 17 00:00:00 2001 From: Marko Savic Date: Thu, 15 Oct 2020 15:58:10 -0400 Subject: [PATCH 2/3] Hide progress when page is loaded --- .../GutenbergWebViewActivity.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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 5a90cd72bc6c0..58c572f999e27 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; @@ -52,10 +53,10 @@ public class GutenbergWebViewActivity extends AppCompatActivity { 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) { - mProgressBar.setVisibility(View.GONE); final Handler handler = new Handler(); handler.postDelayed(() -> { // Insert block content @@ -357,6 +358,12 @@ public void finish() { super.finish(); } + @Override + protected void onDestroy() { + mWebPageLoadedHandler.removeCallbacks(mWebPageLoadedRunnable); + super.onDestroy(); + } + public class WPWebKit { @JavascriptInterface public void postMessage(String content) { From f1e71097e1db9c63c06a2dad70447c3908c8e8bc Mon Sep 17 00:00:00 2001 From: Marko Savic Date: Thu, 15 Oct 2020 16:17:35 -0400 Subject: [PATCH 3/3] If progress isn't visible and progress is less then 100%, make is visible --- .../ReactNativeGutenbergBridge/GutenbergWebViewActivity.java | 3 +++ 1 file changed, 3 insertions(+) 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 58c572f999e27..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 @@ -100,6 +100,9 @@ public void onProgressChanged(WebView view, int progress) { mWebPageLoadedHandler.postDelayed(mWebPageLoadedRunnable, 1500); } else { mIsWebPageLoaded.compareAndSet(true, false); + if (mProgressBar.getVisibility() == View.GONE) { + mProgressBar.setVisibility(View.VISIBLE); + } mProgressBar.setProgress(progress); } }