diff --git a/android/src/main/java/de/x4fyr/paiman/app/service/AndroidWebViewService.kt b/android/src/main/java/de/x4fyr/paiman/app/service/AndroidWebViewService.kt index 2a0b5c6..9c9c6b0 100644 --- a/android/src/main/java/de/x4fyr/paiman/app/service/AndroidWebViewService.kt +++ b/android/src/main/java/de/x4fyr/paiman/app/service/AndroidWebViewService.kt @@ -2,8 +2,10 @@ package de.x4fyr.paiman.app.service import android.annotation.SuppressLint import android.content.Context +import android.graphics.Bitmap import android.util.Log import android.webkit.WebView +import android.webkit.WebViewClient import de.x4fyr.paiman.app.services.WebViewService import de.x4fyr.paiman.app.ui.Controller import de.x4fyr.paiman.app.ui.Model @@ -11,12 +13,16 @@ import de.x4fyr.paiman.app.ui.produceString import kotlinx.coroutines.experimental.android.UI import kotlinx.coroutines.experimental.launch import org.w3c.dom.Element +import kotlin.properties.Delegates /** Android implementations of [WebViewService] */ class AndroidWebViewService(private val context: Context) : WebViewService { + private val webViewClient = LoadStatusWebViewClient() /** WebView instance */ - val webView: WebView = WebView(context) + val webView: WebView = WebView(context).also { + it.webViewClient = webViewClient + } init { launch(UI) { @@ -40,14 +46,18 @@ class AndroidWebViewService(private val context: Context) : WebViewService { override fun loadHtml(html: String, controller: Controller, model: Model?) { launch(UI) { webView.loadUrl(viewResourcePrefix + html) - setControllerAndModel(controller, model) + webViewClient.runOnFinishedPage { + setControllerAndModel(controller, model) + } } } /** Set Controller for callbacks in ui */ @SuppressLint("JavascriptInterface") - override fun setControllerAndModel(controller: Controller, model: Model?) { + private fun setControllerAndModel(controller: Controller, model: Model?) { + Log.d(this@AndroidWebViewService.javaClass.simpleName, "add controller 1") launch(UI) { + Log.d(this@AndroidWebViewService.javaClass.simpleName, "add controller 2") webView.removeJavascriptInterface(WebViewService.javascriptControllerModuleName) webView.removeJavascriptInterface(WebViewService.javascriptModelModuleName) webView.addJavascriptInterface(controller, WebViewService.javascriptControllerModuleName) @@ -57,8 +67,10 @@ class AndroidWebViewService(private val context: Context) : WebViewService { /** execute javascript in current document */ override fun executeJS(script: String) { - launch(UI) { - webView.evaluateJavascript(script) {} + webViewClient.runOnFinishedPage { + launch(UI) { + webView.evaluateJavascript(script) {} + } } } @@ -67,4 +79,33 @@ class AndroidWebViewService(private val context: Context) : WebViewService { */ private val viewResourcePrefix: String = "file:///android_asset" + WebViewService.viewResourcePrefix.removePrefix("/assets") + + private class LoadStatusWebViewClient : WebViewClient() { + + private val onPageFinishedTasks: MutableList<(() -> Unit)> = mutableListOf() + private var finishedPage: Boolean by Delegates.observable(true) { _, old, new -> + if (new && !old) onPageFinishedTasks.forEach { task -> + Log.d(this@LoadStatusWebViewClient.javaClass.simpleName, "run task") + task() + onPageFinishedTasks.remove(task) + } + } + + fun runOnFinishedPage(task: () -> Unit) { + if (finishedPage) task() + else onPageFinishedTasks += task + } + + override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { + super.onPageStarted(view, url, favicon) + finishedPage = false + } + + override fun onPageFinished(view: WebView?, url: String?) { + super.onPageFinished(view, url) + finishedPage = true + Log.d(this.javaClass.simpleName, "onPageFinished - ${onPageFinishedTasks.size} tasks to do") + } + } + } \ No newline at end of file diff --git a/desktop/src/main/java/de/x4fyr/paiman/app/services/JavaFxWebViewService.kt b/desktop/src/main/java/de/x4fyr/paiman/app/services/JavaFxWebViewService.kt index bcf3619..b210dc5 100644 --- a/desktop/src/main/java/de/x4fyr/paiman/app/services/JavaFxWebViewService.kt +++ b/desktop/src/main/java/de/x4fyr/paiman/app/services/JavaFxWebViewService.kt @@ -51,7 +51,7 @@ class JavaFxWebViewService : WebViewService { } } - override fun setControllerAndModel(controller: Controller, model: Model?) { + fun setControllerAndModel(controller: Controller, model: Model?) { launch(JavaFx) { webView.engine.loadWorker.stateProperty().addListener { _, _, newValue -> if (newValue == Worker.State.SUCCEEDED) { @@ -63,9 +63,6 @@ class JavaFxWebViewService : WebViewService { .removeMember(WebViewService.javascriptModelModuleName) if (model != null) (webView.engine.executeScript("window") as JSObject) .setMember(WebViewService.javascriptModelModuleName, model) - try { - webView.engine.executeScript("onControllerLoad()") - } catch (e: JSException) {} } } } diff --git a/ui/src/main/java/de/x4fyr/paiman/app/services/WebViewService.kt b/ui/src/main/java/de/x4fyr/paiman/app/services/WebViewService.kt index 57bc6da..174c53b 100644 --- a/ui/src/main/java/de/x4fyr/paiman/app/services/WebViewService.kt +++ b/ui/src/main/java/de/x4fyr/paiman/app/services/WebViewService.kt @@ -13,9 +13,6 @@ interface WebViewService { /** Load html file into WebView */ fun loadHtml(html: String, controller: Controller, model: Model? = null) - /** Set Controller for callbacks in ui */ - fun setControllerAndModel(controller: Controller, model: Model? = null) - /** execute javascript in current document */ fun executeJS(script: String) diff --git a/ui/src/main/resources/assets/de/x4fyr/paiman/ui/html/detail.html b/ui/src/main/resources/assets/de/x4fyr/paiman/ui/html/detail.html index 000ee0f..4035592 100644 --- a/ui/src/main/resources/assets/de/x4fyr/paiman/ui/html/detail.html +++ b/ui/src/main/resources/assets/de/x4fyr/paiman/ui/html/detail.html @@ -120,11 +120,9 @@ document.getElementById("loadModal").hide() } - - function onControllerLoad(ev) { + document.onload = function (ev) { refreshModel(); - } - + };
@@ -158,7 +156,7 @@