From f13558a9d08337d67cb7a40684a1212e42c76feb Mon Sep 17 00:00:00 2001 From: baka kaba Date: Sun, 20 Jan 2019 00:04:21 +0000 Subject: [PATCH] Fix blank initial pages when the WebView is (re)created This basically does a refresh call when the HTML is loaded and the container is first initialised. If the container wasn't ready when a "display content" call came in, then it ends up loading it here It's *possible* that some content might refresh the display twice, if the container initialised refresh coincides with the app setting the content, so there are two JS #showPageHtml calls after bodyHtml is set. I'm only mentioning it in case it comes up as a "why's that happening" issue - it should be rare if it ever happens, and it would only be a single glitch when the WebView is first initialised Fixes #653 --- .../src/main/assets/javascript/thread.js | 3 +++ .../awfulapp/webview/WebViewJsInterface.java | 20 ++++--------------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/Awful.apk/src/main/assets/javascript/thread.js b/Awful.apk/src/main/assets/javascript/thread.js index 54dc2af16..26ae03dcd 100644 --- a/Awful.apk/src/main/assets/javascript/thread.js +++ b/Awful.apk/src/main/assets/javascript/thread.js @@ -83,6 +83,9 @@ function containerInit() { window.addEventListener('awful-scroll-post', function scrollToPost() { window.topScrollID = window.requestAnimationFrame(scrollPost.bind(null, null)); }); + + // trigger a page content load, in case some was sent before the container was ready to handle it + loadPageHtml(); } /** diff --git a/Awful.apk/src/main/java/com/ferg/awfulapp/webview/WebViewJsInterface.java b/Awful.apk/src/main/java/com/ferg/awfulapp/webview/WebViewJsInterface.java index cd040e5d9..058dbd294 100644 --- a/Awful.apk/src/main/java/com/ferg/awfulapp/webview/WebViewJsInterface.java +++ b/Awful.apk/src/main/java/com/ferg/awfulapp/webview/WebViewJsInterface.java @@ -2,8 +2,6 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.annotation.WorkerThread; -import android.util.Log; import android.webkit.JavascriptInterface; import com.ferg.awfulapp.preferences.AwfulPreferences; @@ -12,6 +10,8 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import timber.log.Timber; + /** * Created by baka kaba on 23/01/2017. *

@@ -21,27 +21,17 @@ * be used in a {@link com.ferg.awfulapp.ThreadDisplayFragment} and needs to react to other UI clicks. */ // TODO: 12/02/2017 JS interface methods are called on a separate thread apparently - none of our implementations are thread-safe at all -@WorkerThread public class WebViewJsInterface { - private static final String TAG = "WebViewJsInterface"; - private final Map preferences = new ConcurrentHashMap<>(); @NonNull - private String bodyHtml = ""; - - @Nullable - private AwfulWebView webView = null; + private volatile String bodyHtml = ""; public WebViewJsInterface() { updatePreferences(); } - void setWebView(@NonNull AwfulWebView webView) { - this.webView = webView; - } - /** * Updates the JavaScript-accessible preference store from the current values in AwfulPreferences. */ @@ -77,8 +67,6 @@ public final void updatePreferences() { protected void setCustomPreferences(Map preferences) { } - // TODO: sync for threads? check html -> update html needs to be atomic? - @NonNull @JavascriptInterface public final String getBodyHtml() { @@ -97,7 +85,7 @@ public String getPreference(String preference) { @JavascriptInterface public void debugMessage(final String msg) { - Log.d(TAG, "Awful DEBUG: " + msg); + Timber.d("Awful DEBUG: %s", msg); } // TODO: 28/01/2017 work out if any other common interface methods can go in here