feat(android): improve initialization scripts (#5028)

This commit is contained in:
Lucas Fernandes Nogueira 2022-08-24 13:06:45 -03:00 committed by GitHub
parent 0500d3b4b1
commit 641d56dcb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 29 deletions

View File

@ -4223,8 +4223,7 @@ dependencies = [
[[package]]
name = "wry"
version = "0.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea48fb3b68ab76f62837bfcea63baed9a185dbec9c14d4e5d70033e22fefffd2"
source = "git+https://github.com/tauri-apps/wry?branch=dev#1b26d605d6e33f5417eb6566a7381d8feb239c8b"
dependencies = [
"block",
"cocoa",

View File

@ -6,6 +6,9 @@ edition = "2021"
rust-version = "1.57"
license = "Apache-2.0 OR MIT"
[patch.crates-io]
wry = { git = "https://github.com/tauri-apps/wry", branch = "dev" }
[lib]
crate-type = ["staticlib", "cdylib", "rlib"]

View File

@ -1,26 +0,0 @@
package {{reverse-domain app.domain}}.{{snake-case app.name}}
import android.webkit.*
class RustWebChromeClient: WebChromeClient() {
private var loadedUrl: String? = null
override fun onProgressChanged(view: WebView, progress: Int) {
var url = view.url ?: ""
if (url.endsWith("##")) {
url = url.dropLast(2)
}
if (loadedUrl != url && progress >= 20) {
loadedUrl = url
runInitializationScripts()
}
}
companion object {
init {
System.loadLibrary("{{snake-case app.name}}")
}
}
private external fun runInitializationScripts()
}

View File

@ -1,8 +1,22 @@
package {{reverse-domain app.domain}}.{{snake-case app.name}}
import android.graphics.Bitmap
import android.webkit.*
class RustWebViewClient: WebViewClient() {
class RustWebViewClient(initScripts: Array<String>): WebViewClient() {
private val initializationScripts: Array<String>
init {
initializationScripts = initScripts
}
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
for (script in initializationScripts) {
view?.evaluateJavascript(script, null)
}
super.onPageStarted(view, url, favicon)
}
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
return false
}