Bladeren bron

对接脉诊模块

cc12458 1 maand geleden
bovenliggende
commit
e7417e9030

+ 6 - 0
app/src/main/res/values/application.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+  <string name="app_id">Applet</string>
+  <string name="app_name">小程序容器</string>
+  <string name="app_screen" />
+</resources>

+ 0 - 4
app/src/main/res/values/route.xml

@@ -1,4 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<resources>
-  <string name="app_screen" />
-</resources>

+ 0 - 3
app/src/main/res/values/strings.xml

@@ -1,3 +0,0 @@
-<resources>
-  <string name="app_name">Six-applet.Container</string>
-</resources>

+ 0 - 17
core/src/main/java/com/hzliuzhi/applet/core/SharedFlowHub.kt

@@ -1,17 +0,0 @@
-package com.hzliuzhi.applet.core
-
-import kotlinx.coroutines.flow.MutableSharedFlow
-import kotlinx.coroutines.flow.asSharedFlow
-
-object SharedFlowHub {
-  data class Event<T>(
-    val type: String,
-    val payload: T?,
-    val callback: (() -> Unit)? = null,
-  )
-
-  private val _events = MutableSharedFlow<Event<Any>>(extraBufferCapacity = 16)
-  val events = _events.asSharedFlow()
-
-  fun emit(event: Event<Any>) = _events.tryEmit(event)
-}

+ 1 - 1
library/browser/src/main/java/com/hzliuzhi/applet/browser/request/Client.kt

@@ -22,7 +22,7 @@ object Client {
   suspend fun fetchWithView(request: WebResourceRequest, url: String): WebResourceResponse? {
     Log.i("log:webview", "[${request.method}] 被拦截的加载请求: ${request.url} -> $url")
     return fetchWithView {
-      Request.Builder().url(url).apply {
+      Request.Builder().url(url).method(request.method, null).apply {
         request.requestHeaders.onEach { (key, value) ->
           addHeader(key, value)
         }

+ 32 - 11
library/browser/src/main/java/com/hzliuzhi/applet/browser/webview/WebViewBridge.kt

@@ -6,11 +6,17 @@ import android.webkit.JavascriptInterface
 import android.webkit.WebView
 import com.google.gson.Gson
 import com.google.gson.JsonElement
-import com.hzliuzhi.applet.core.SharedFlowHub
+import com.hzliuzhi.applet.core.shared.Event
+import com.hzliuzhi.applet.core.shared.SharedFlowHub
+import com.hzliuzhi.applet.core.shared.SharedFlowHub.cast
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.flow.MutableSharedFlow
+import kotlinx.coroutines.flow.filter
+import kotlinx.coroutines.flow.launchIn
+import kotlinx.coroutines.flow.onEach
 import kotlinx.coroutines.withContext
+import org.json.JSONObject
 import java.io.InputStreamReader
 
 class WebViewBridge(private val coroutineScope: CoroutineScope) {
@@ -45,6 +51,14 @@ class WebViewBridge(private val coroutineScope: CoroutineScope) {
       }.also { lastScriptText = it }
     }.getOrNull()
     script?.also { webview.evaluateJavascript(it, null) }
+
+    SharedFlowHub.events.filter { it.type == "${SharedFlowHub.WEBVIEW_BRIDGE_EVENT}:js" }.onEach { event ->
+      Log.d("log:bridge", "发送消息事件: $event")
+      event.cast<Message, String>()?.also {
+        val payload = Gson().toJson(it.payload)
+        webview.evaluateJavascript("Bridge.getInstance().dispatch(${JSONObject.quote(payload)})", it.callback)
+      }
+    }.launchIn(coroutineScope)
   }
 
 
@@ -53,22 +67,29 @@ class WebViewBridge(private val coroutineScope: CoroutineScope) {
   @SuppressLint("JavascriptInterface")
   internal suspend fun WebView.handleBridge(): Nothing = withContext(Dispatchers.Main) {
     addJavascriptInterface(this@WebViewBridge, "AndroidBridge")
-    messages.collect {
-      Log.d("log:bridge", "接收到的事件: ${it.type}")
-      SharedFlowHub.emit(
-        SharedFlowHub.Event(
-        type = it.type,
-        payload = it.payload,
-        callback = {}
-      ))
-    }
+    messages.collect { SharedFlowHub.webViewEmit(it) }
   }
 
+  private fun SharedFlowHub.webViewEmit(message: Message) {
+    Log.d("log:bridge", "接收消息: $message")
+    Event<JsonElement, JsonElement>(
+      type = "$WEBVIEW_BRIDGE_EVENT:${message.type}",
+      payload = message.payload,
+      callback = { payload ->
+        Event<Message, String>(
+          type = "$WEBVIEW_BRIDGE_EVENT:js",
+          payload = message.copy(payload = payload)
+        ).also { emit(it) }
+      }
+    ).also { emit(it) }
+  }
 
+  /* 注入到 JavaScript 中的方法 */
   @JavascriptInterface
   fun postMessage(string: String) {
     Message.fromJson(string)?.also {
       messages.tryEmit(it)
     }
   }
-}
+}
+