Commit a5fee3d8 by huangzhicong

webview界面调整

1、增加js响应函数link
2、调整storage接口的参数接收
parent d8200ebf
......@@ -11,12 +11,15 @@ import android.webkit.*
import androidx.fragment.app.Fragment
import androidx.fragment.app.clearFragmentResultListener
import androidx.fragment.app.setFragmentResultListener
import androidx.lifecycle.lifecycleScope
import com.google.gson.Gson
import com.maxrocky.nativeview.R
import com.maxrocky.nativeview.WebViewProxy
import com.maxrocky.nativeview.contract.*
import com.maxrocky.nativeview.model.PairOptions
import com.maxrocky.nativeview.model.StorageOptions
import com.maxrocky.nativeview.model.SingleOptions
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class MainFragment : Fragment() {
companion object {
......@@ -68,12 +71,6 @@ class MainFragment : Fragment() {
result: JsResult?
): Boolean = false
}
wv.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
Log.d(TAG, "onPageFinished: $url")
view?.evaluateJavascript("javascript:onMaxrockyReady()", null)
}
}
wv.settings.apply {
javaScriptEnabled = true
}
......@@ -104,6 +101,13 @@ class MainFragment : Fragment() {
private val gson = Gson()
@JavascriptInterface
fun link() {
lifecycleScope.launch(Dispatchers.Main) {
wv.evaluateJavascript("javascript:onMaxrockyReady()", null)
}
}
@JavascriptInterface
fun execute(sessionId: Int, method: String, args: String) {
when (method) {
"selectFile" -> {
......@@ -113,8 +117,12 @@ class MainFragment : Fragment() {
"selectPhoto" -> selectPhotoContract.selectPhoto(sessionId)
"openCamera" -> cameraContract.openCamera(sessionId)
"recordAudio" -> {
val options = gson.fromJson<SingleOptions<Int>>(args, SingleOptions::class.java)
recordAudioContract.recordAudio(sessionId, options.data1)
if (args.isNotEmpty()) {
val options = gson.fromJson<SingleOptions<Int>>(args, SingleOptions::class.java)
recordAudioContract.recordAudio(sessionId, options.data1)
} else {
recordAudioContract.recordAudio(sessionId)
}
}
"cropPhoto" -> {
val options = gson.fromJson<SingleOptions<String>>(args, SingleOptions::class.java)
......@@ -123,17 +131,11 @@ class MainFragment : Fragment() {
"captureVideo" -> videoContract.captureVideo(sessionId)
"qrcode" -> qrCodeContract.qrcode(sessionId)
"storageValue" -> {
val options = gson.fromJson<PairOptions<String, String>>(args, PairOptions::class.java)
storageContract.storageValue(sessionId, options.data1, options.data2)
}
"acquireValue" -> {
val options = gson.fromJson<SingleOptions<String>>(args, SingleOptions::class.java)
storageContract.acquireValue(sessionId, options.data1)
}
"clearValue" -> {
val options = gson.fromJson<SingleOptions<String>>(args, SingleOptions::class.java)
storageContract.clearValue(sessionId, options.data1)
val options = gson.fromJson<StorageOptions<String, String>>(args, StorageOptions::class.java)
storageContract.storageValue(sessionId, options.key, options.value)
}
"acquireValue" -> storageContract.acquireValue(sessionId, args)
"clearValue" -> storageContract.clearValue(sessionId, args)
}
}
}
......
......@@ -2,4 +2,4 @@ package com.maxrocky.nativeview.model
data class SingleOptions<T>(val data1: T)
data class PairOptions<T, U>(val data1: T, val data2: U)
data class StorageOptions<T, U>(val key: T, val value: U)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment