Browse Source

[feat]键盘插件,keyboardId可能拿不到时的处理

hezihao 6 months ago
parent
commit
5b7f9a1681

+ 67 - 35
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/mvvm/viewmodel/KeyboardViewModel.kt

@@ -348,23 +348,39 @@ class KeyboardViewModel : BaseViewModel() {
         onSuccess: () -> Unit,
         onFail: (String) -> Unit
     ) {
-        val req = SuperReplyReq(
-            // 键盘Id
-            keyboardId = _currentKeyboardInfo.value?.id ?: "",
-            // 人设Id
-            characterId = characterId,
-            // 用户粘贴板中的内容
-            content = _userClipboardData.value ?: ""
-        )
-        _chatSuperReplyResult.value = ""
-        mKeyboardRepository.chatSuperReply(req, onSuccess = {
-            // 只有一条数据,直接添加到输入框
-            _chatSuperReplyResult.value = it.content
-            onSuccess.invoke()
-        }, onFail = { errorCode, errorMsg ->
-            ErrorHandler.handleError(errorCode, errorMsg)
-            onFail.invoke(errorMsg)
-        })
+        fun doAction(keyboardId: String) {
+            val req = SuperReplyReq(
+                // 键盘Id
+                keyboardId = keyboardId,
+                // 人设Id
+                characterId = characterId,
+                // 用户粘贴板中的内容
+                content = _userClipboardData.value ?: ""
+            )
+            _chatSuperReplyResult.value = ""
+            mKeyboardRepository.chatSuperReply(req, onSuccess = {
+                // 只有一条数据,直接添加到输入框
+                _chatSuperReplyResult.value = it.content
+                onSuccess.invoke()
+            }, onFail = { errorCode, errorMsg ->
+                ErrorHandler.handleError(errorCode, errorMsg)
+                onFail.invoke(errorMsg)
+            })
+        }
+
+        var keyboardId = _currentKeyboardInfo.value?.id ?: ""
+        // 如果没有,则重新获取
+        if (keyboardId.isBlank()) {
+            getCurrentKeyboardInfoWithDefault(onSuccess = {
+                keyboardId = it.id ?: ""
+                doAction(keyboardId)
+            }, onFail = { errorCode, errorMsg ->
+                ErrorHandler.handleError(errorCode, errorMsg)
+                onFail.invoke(errorMsg)
+            })
+        } else {
+            doAction(keyboardId)
+        }
     }
 
     /**
@@ -378,24 +394,40 @@ class KeyboardViewModel : BaseViewModel() {
         onSuccess: () -> Unit,
         onFail: (String) -> Unit
     ) {
-        onStart.invoke()
-        _superSpeakChatListResult.value = listOf<String>()
-        val req = SuperSpeakReq(
-            // 键盘Id
-            keyboardId = _currentKeyboardInfo.value?.id ?: "",
-            // 人设Id
-            characterId = characterId,
-            // 用户粘贴板中的内容
-            content = _userClipboardData.value ?: ""
-        )
-        mKeyboardRepository.chatSuperSpeak(req, onSuccess = {
-            // 有多条数据,需要跳转列表页面中显示
-            _superSpeakChatListResult.value = it.list
-            onSuccess.invoke()
-        }, onFail = { errorCode, errorMsg ->
-            ErrorHandler.handleError(errorCode, errorMsg)
-            onFail.invoke(errorMsg)
-        })
+        fun doAction(keyboardId: String) {
+            onStart.invoke()
+            _superSpeakChatListResult.value = listOf<String>()
+            val req = SuperSpeakReq(
+                // 键盘Id
+                keyboardId = keyboardId,
+                // 人设Id
+                characterId = characterId,
+                // 用户粘贴板中的内容
+                content = _userClipboardData.value ?: ""
+            )
+            mKeyboardRepository.chatSuperSpeak(req, onSuccess = {
+                // 有多条数据,需要跳转列表页面中显示
+                _superSpeakChatListResult.value = it.list
+                onSuccess.invoke()
+            }, onFail = { errorCode, errorMsg ->
+                ErrorHandler.handleError(errorCode, errorMsg)
+                onFail.invoke(errorMsg)
+            })
+        }
+
+        var keyboardId = _currentKeyboardInfo.value?.id ?: ""
+        // 如果没有,则重新获取
+        if (keyboardId.isBlank()) {
+            getCurrentKeyboardInfoWithDefault(onSuccess = {
+                keyboardId = it.id ?: ""
+                doAction(keyboardId)
+            }, onFail = { errorCode, errorMsg ->
+                ErrorHandler.handleError(errorCode, errorMsg)
+                onFail.invoke(errorMsg)
+            })
+        } else {
+            doAction(keyboardId)
+        }
     }
 
     /**

+ 4 - 2
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/util/ToastUtil.kt

@@ -2,7 +2,7 @@ package com.atmob.keyboard_android.util
 
 import android.os.Handler
 import android.os.Looper
-import android.widget.Toast
+import com.blankj.utilcode.util.ToastUtils
 
 /**
  * Toast工具类
@@ -19,7 +19,9 @@ class ToastUtil private constructor() {
                 return
             }
             val action = {
-                Toast.makeText(ContextUtil.getContext(), msg, Toast.LENGTH_SHORT).show()
+                ToastUtils.showShort(msg)
+                // 三星手机,如果不自定义Toast,后台显示Toast会显示不出来
+                // Toast.makeText(ContextUtil.getContext(), msg, Toast.LENGTH_SHORT).show()
             }
             if (Thread.currentThread() == Looper.getMainLooper().thread) {
                 action.invoke()