|
@@ -3,6 +3,7 @@ package com.hzliuzhi.applet.container
|
|
|
import android.annotation.SuppressLint
|
|
|
import android.os.Bundle
|
|
|
import android.view.KeyEvent
|
|
|
+import android.widget.Toast
|
|
|
import androidx.activity.ComponentActivity
|
|
|
import androidx.activity.compose.LocalActivity
|
|
|
import androidx.activity.compose.setContent
|
|
@@ -11,21 +12,19 @@ import androidx.compose.foundation.layout.padding
|
|
|
import androidx.compose.material3.Scaffold
|
|
|
import androidx.compose.runtime.rememberCoroutineScope
|
|
|
import androidx.compose.ui.Modifier
|
|
|
+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.container.scanner.ScannerListener
|
|
|
-import com.hzliuzhi.applet.container.scanner.rememberScannerListener
|
|
|
-import com.hzliuzhi.applet.core.shared.Event
|
|
|
import com.hzliuzhi.applet.core.shared.Payload
|
|
|
import com.hzliuzhi.applet.core.shared.SharedFlowHub
|
|
|
import com.hzliuzhi.applet.core.theme.SixTheme
|
|
|
-import com.hzliuzhi.applet.device.pulse.PulseEventHandle
|
|
|
+import com.hzliuzhi.applet.scanner.Scanner
|
|
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
|
private var navController: NavHostController? = null
|
|
|
- private var scanner: ScannerListener? = null
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
super.onCreate(savedInstanceState)
|
|
@@ -42,29 +41,38 @@ class MainActivity : ComponentActivity() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- LocalActivity.current?.also {
|
|
|
- scanner = rememberScannerListener { result ->
|
|
|
- val code = result.code
|
|
|
- code.takeIf { code.isNotEmpty() }?.also {
|
|
|
- val message = Payload.data(data = result).toEvent()?.let { payload ->
|
|
|
- WebViewBridge.Message(type = "scan", payload = payload)
|
|
|
- }?.let { message ->
|
|
|
- Event<WebViewBridge.Message, String>(
|
|
|
- type = "${SharedFlowHub.WEBVIEW_BRIDGE_EVENT}:js",
|
|
|
- payload = message
|
|
|
- )
|
|
|
- }?.let { event ->
|
|
|
- SharedFlowHub.emit(event)
|
|
|
- }
|
|
|
+ val context = LocalContext.current
|
|
|
+ val owner = LocalLifecycleOwner.current
|
|
|
+ Scanner.getInstance(context).observe(owner) { result ->
|
|
|
+ if (result == null) {
|
|
|
+ Payload.error<Unit>(message = "扫码失败: 结果为空").toJson()
|
|
|
+ } else {
|
|
|
+ Toast.makeText(context, result.message, Toast.LENGTH_SHORT).show()
|
|
|
+ when {
|
|
|
+ result.code.startsWith("six:") -> null
|
|
|
+ else -> Payload.data(data = result).toJson()
|
|
|
+ }
|
|
|
+ }?.also {
|
|
|
+ val event = WebViewBridge.Message(type = "scan", payload = it).toEvent()
|
|
|
+ SharedFlowHub.emit(event)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Scanner.getInstance(context).eventHandle(rememberCoroutineScope())
|
|
|
+
|
|
|
+ val tag = context.resources.getString(R.string.app_id)
|
|
|
+ when {
|
|
|
+ tag.startsWith("AIO") -> run {
|
|
|
+ LocalActivity.current?.also {
|
|
|
+ com.hzliuzhi.applet.device.pulse.PulseEventHandle(it, rememberCoroutineScope())
|
|
|
}
|
|
|
}
|
|
|
- PulseEventHandle(it, rememberCoroutineScope())
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@SuppressLint("RestrictedApi")
|
|
|
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
|
|
|
- return scanner?.dispatchKeyEvent(event)?.takeIf { it } ?: super.dispatchKeyEvent(event)
|
|
|
+ val context = applicationContext
|
|
|
+ return Scanner.getInstance(context).dispatchKeyEvent(event).takeIf { it } ?: super.dispatchKeyEvent(event)
|
|
|
}
|
|
|
}
|