cc12458 3 týždňov pred
rodič
commit
c2b62e05ae

+ 2 - 2
app/src/main/java/com/hzliuzhi/applet/container/MainActivity.kt

@@ -13,8 +13,8 @@ import androidx.compose.ui.platform.LocalContext
 import androidx.lifecycle.compose.LocalLifecycleOwner
 import androidx.navigation.NavHostController
 import androidx.navigation.compose.rememberNavController
-import com.hzliuzhi.applet.browser.webview.WebViewBridge
 import com.hzliuzhi.applet.container.navigation.Host
+import com.hzliuzhi.applet.core.shared.Message
 import com.hzliuzhi.applet.core.shared.Payload
 import com.hzliuzhi.applet.core.shared.SharedFlowHub
 import com.hzliuzhi.applet.core.theme.SixTheme
@@ -56,7 +56,7 @@ class MainActivity : AndroidActivity() {
             else -> Payload.data(data = result).toJson()
           }
         }?.also {
-          val event = WebViewBridge.Message(type = "scan", payload = it).toEvent()
+          val event = Message(type = "scan", payload = it).toWebEvent()
           SharedFlowHub.emit(event)
         }
       }

+ 31 - 0
core/src/main/java/com/hzliuzhi/applet/core/shared/Message.kt

@@ -0,0 +1,31 @@
+package com.hzliuzhi.applet.core.shared
+
+import com.google.gson.Gson
+import com.google.gson.JsonElement
+
+data class Message(
+  val type: String,
+  val payload: JsonElement? = Gson().toJsonTree(""),
+  val callbackId: String? = null,
+) {
+  companion object {
+    fun fromJson(json: String) = try {
+      Gson().fromJson(json, Message::class.java)
+    } catch (_: Throwable) {
+      null
+    }
+
+    fun toJson(message: Message) = try {
+      Gson().toJson(message)
+    } catch (_: Throwable) {
+      null
+    }
+  }
+
+  fun toWebEvent() = Event<Message, String>(
+    type = "${SharedFlowHub.WEBVIEW_BRIDGE_EVENT}:js",
+    payload = this
+  )
+
+  val isPayloadNull: Boolean get() = payload?.isJsonNull ?: true
+}

+ 2 - 0
library/browser/src/main/assets/browser/bridge.js

@@ -60,6 +60,8 @@ class Bridge extends EventTarget {
     return () => super.removeEventListener(type, callback);
   }
 
+  postMessage(...args) { return this.#postMessage(...args); }
+
   #postMessage(type, payload, resolvers) {
     const callbackId = `${type}:${Bridge.UUID}`;
     this.#pool.set(callbackId, resolvers);

+ 3 - 28
library/browser/src/main/java/com/hzliuzhi/applet/browser/webview/WebViewBridge.kt

@@ -8,6 +8,7 @@ import android.webkit.WebView
 import com.google.gson.Gson
 import com.google.gson.JsonElement
 import com.hzliuzhi.applet.core.shared.Event
+import com.hzliuzhi.applet.core.shared.Message
 import com.hzliuzhi.applet.core.shared.SharedFlowHub
 import com.hzliuzhi.applet.core.shared.SharedFlowHub.cast
 import kotlinx.coroutines.CoroutineScope
@@ -21,32 +22,6 @@ import org.json.JSONObject
 import java.io.InputStreamReader
 
 class WebViewBridge(private val coroutineScope: CoroutineScope) {
-  data class Message(
-    val type: String,
-    val payload: JsonElement? = Gson().toJsonTree(""),
-    val callbackId: String? = null,
-  ) {
-    companion object {
-      fun fromJson(json: String) = try {
-        Gson().fromJson(json, Message::class.java)
-      } catch (_: Throwable) {
-        null
-      }
-
-      fun toJson(message: Message) = try {
-        Gson().toJson(message)
-      } catch (_: Throwable) {
-        null
-      }
-    }
-
-    fun toEvent() = Event<Message, String>(
-      type = "${SharedFlowHub.WEBVIEW_BRIDGE_EVENT}:js",
-      payload = this
-    )
-
-    val isPayloadNull: Boolean get() = payload?.isJsonNull ?: true
-  }
 
 
   private var lastScriptText: String? = null
@@ -85,7 +60,7 @@ class WebViewBridge(private val coroutineScope: CoroutineScope) {
         type = message.type,
         payload = webview.createPrintDocumentAdapter("WebViewDocument"),
         callback = { payload ->
-          val event = message.copy(payload = payload).toEvent()
+          val event = message.copy(payload = payload).toWebEvent()
           emit(event)
         }
       ).also { emit(it) }
@@ -94,7 +69,7 @@ class WebViewBridge(private val coroutineScope: CoroutineScope) {
         type = "$WEBVIEW_BRIDGE_EVENT:${message.type}",
         payload = message.payload,
         callback = { payload ->
-          val event = message.copy(payload = payload).toEvent()
+          val event = message.copy(payload = payload).toWebEvent()
           emit(event)
         }
       ).also { emit(it) }