Skip to content
This repository has been archived by the owner on Oct 18, 2022. It is now read-only.

Commit

Permalink
#34: basic android implementation
Browse files Browse the repository at this point in the history
- fix detail view
- remove onControllerLoad() js function as controller is most likely loaded earlier then document
- set controller/model private -> only in conjunction with html load
  • Loading branch information
bvolkmer committed Sep 29, 2018
1 parent 4fd93d2 commit 9337140
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ 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
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) {
Expand All @@ -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)
Expand All @@ -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) {}
}
}
}

Expand All @@ -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")
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,9 @@
document.getElementById("loadModal").hide()
}


function onControllerLoad(ev) {
document.onload = function (ev) {
refreshModel();
}

};
</script>
</head>
<body>
Expand Down Expand Up @@ -158,7 +156,7 @@
</ons-toolbar>
<div class="content">
<ons-row>
<img id="mainImage" style="max-width: 100%"/>
<img id="mainImage" style="max-width: 100%; width: auto; height: auto;"/>
</ons-row>
<ons-row>
<p id="tags">
Expand Down

0 comments on commit 9337140

Please sign in to comment.