Browse Source

[fix]键盘插件,如果用户未在键盘中设置过键盘,但在新人设置中新建了键盘,则使用新人设置中新建的键盘,而不是通用键盘

hezihao 11 months ago
parent
commit
c2b92acabc

+ 6 - 6
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/child/impl/KeyboardSelectComponent.kt

@@ -16,8 +16,8 @@ import com.atmob.keyboard_android.component.item.KeyboardSelectViewBinder
 import com.atmob.keyboard_android.constant.Constants
 import com.atmob.keyboard_android.ext.click
 import com.atmob.keyboard_android.model.EmptyPlaceholderModel
-import com.atmob.keyboard_android.model.KeyboardSelectModel
 import com.atmob.keyboard_android.util.KeyboardHolder
+import com.atmob.keyboard_android.util.bridge.model.resp.KeyboardInfo
 import com.atmob.keyboard_android.util.recyclerview.GridDivider
 import com.blankj.utilcode.util.ConvertUtils
 import me.drakeet.multitype.Items
@@ -55,7 +55,7 @@ class KeyboardSelectComponent @JvmOverloads constructor(
         }
         vSaveBtn.click {
             val selectKeyboard =
-                mListItems.filterIsInstance<KeyboardSelectModel>().find {
+                mListItems.filterIsInstance<KeyboardInfo>().find {
                     it.isSelect == true
                 }
             if (selectKeyboard == null) {
@@ -75,15 +75,15 @@ class KeyboardSelectComponent @JvmOverloads constructor(
                 // 空占位条目
                 register(EmptyPlaceholderModel::class.java, EmptyPlaceholderViewBinder())
                 // 键盘条目
-                register(KeyboardSelectModel::class.java, KeyboardSelectViewBinder { item ->
+                register(KeyboardInfo::class.java, KeyboardSelectViewBinder { item ->
                     // 先全部取消选中,再选中当前设置的键盘
                     mListItems.map { element ->
-                        if (element is KeyboardSelectModel) {
+                        if (element is KeyboardInfo) {
                             element.isSelect = false
                         }
                     }
                     val targetPosition = mListItems.indexOf(item)
-                    val targetItem = mListItems[targetPosition] as KeyboardSelectModel
+                    val targetItem = mListItems[targetPosition] as KeyboardInfo
                     targetItem.isSelect = true
                     notifyDataSetChanged()
                 })
@@ -182,7 +182,7 @@ class KeyboardSelectComponent @JvmOverloads constructor(
      * 选择键盘
      */
     private fun selectedKeyboard(
-        selectModel: KeyboardSelectModel,
+        selectModel: KeyboardInfo,
         onSuccess: () -> Unit
     ) {
         KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()?.selectedKeyboard(

+ 4 - 4
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/item/KeyboardSelectViewBinder.kt

@@ -11,16 +11,16 @@ import com.atmob.keyboard_android.R
 import com.atmob.keyboard_android.enums.KeyboardType
 import com.atmob.keyboard_android.ext.click
 import com.atmob.keyboard_android.ext.loadUrlImageToCorner
-import com.atmob.keyboard_android.model.KeyboardSelectModel
+import com.atmob.keyboard_android.util.bridge.model.resp.KeyboardInfo
 import me.drakeet.multitype.ItemViewBinder
 
 /**
  * 键盘选择条目
  */
 class KeyboardSelectViewBinder(
-    private val onItemClick: (item: KeyboardSelectModel) -> Unit
+    private val onItemClick: (item: KeyboardInfo) -> Unit
 ) :
-    ItemViewBinder<KeyboardSelectModel, KeyboardSelectViewBinder.InnerViewHolder>() {
+    ItemViewBinder<KeyboardInfo, KeyboardSelectViewBinder.InnerViewHolder>() {
     override fun onCreateViewHolder(
         inflater: LayoutInflater,
         parent: ViewGroup
@@ -30,7 +30,7 @@ class KeyboardSelectViewBinder(
 
     override fun onBindViewHolder(
         holder: InnerViewHolder,
-        item: KeyboardSelectModel
+        item: KeyboardInfo
     ) {
         val context = holder.itemView.context
 

+ 2 - 2
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/mvvm/repository/KeyboardRepository.kt

@@ -5,7 +5,7 @@ import com.atmob.keyboard_android.util.bridge.model.base.EmptyResp
 import com.atmob.keyboard_android.util.bridge.model.req.SuperReplyReq
 import com.atmob.keyboard_android.util.bridge.model.req.SuperSpeakReq
 import com.atmob.keyboard_android.util.bridge.model.resp.CharacterListResp
-import com.atmob.keyboard_android.util.bridge.model.resp.CurrentKeyboardInfoResp
+import com.atmob.keyboard_android.util.bridge.model.resp.KeyboardInfo
 import com.atmob.keyboard_android.util.bridge.model.resp.KeyboardListResp
 import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp
 import com.atmob.keyboard_android.util.bridge.model.resp.PrologueResp
@@ -65,7 +65,7 @@ class KeyboardRepository {
      * 获取当前键盘信息
      */
     fun getCurrentKeyboardInfo(
-        onSuccess: (resultObj: CurrentKeyboardInfoResp) -> Unit,
+        onSuccess: (resultObj: KeyboardInfo) -> Unit,
         onFail: (errorCode: Int, errorMsg: String) -> Unit
     ) {
         FlutterBridgeManager.getCurrentKeyboardInfo(onSuccess, onFail)

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

@@ -6,13 +6,13 @@ import androidx.lifecycle.ViewModel
 import com.atmob.keyboard_android.enums.HelpMode
 import com.atmob.keyboard_android.enums.KeyboardGlobalType
 import com.atmob.keyboard_android.enums.KeyboardType
-import com.atmob.keyboard_android.model.KeyboardSelectModel
 import com.atmob.keyboard_android.mvvm.repository.KeyboardRepository
 import com.atmob.keyboard_android.util.LogUtil
 import com.atmob.keyboard_android.util.bridge.model.base.EmptyResp
 import com.atmob.keyboard_android.util.bridge.model.req.SuperReplyReq
 import com.atmob.keyboard_android.util.bridge.model.req.SuperSpeakReq
 import com.atmob.keyboard_android.util.bridge.model.resp.CharacterListResp.CharacterInfo
+import com.atmob.keyboard_android.util.bridge.model.resp.KeyboardInfo
 import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp.PrologueListItem
 import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp.Topic
 import com.atmob.keyboard_android.util.error.ErrorHandler
@@ -79,8 +79,8 @@ class KeyboardViewModel : ViewModel() {
     /**
      * 当前键盘信息
      */
-    private val _currentKeyboardInfo = MutableLiveData<KeyboardSelectModel>()
-    val currentKeyboardInfo: LiveData<KeyboardSelectModel> = _currentKeyboardInfo
+    private val _currentKeyboardInfo = MutableLiveData<KeyboardInfo>()
+    val currentKeyboardInfo: LiveData<KeyboardInfo> = _currentKeyboardInfo
 
     /**
      * 当前选中的开场白Tab
@@ -128,8 +128,8 @@ class KeyboardViewModel : ViewModel() {
     /**
      * 键盘选择列表数据
      */
-    private val _keyboardList = MutableLiveData<List<KeyboardSelectModel>>()
-    val keyboardList: LiveData<List<KeyboardSelectModel>> = _keyboardList
+    private val _keyboardList = MutableLiveData<List<KeyboardInfo>>()
+    val keyboardList: LiveData<List<KeyboardInfo>> = _keyboardList
 
     // ----------------------------------- 更新状态方法 -----------------------------------
 
@@ -235,8 +235,11 @@ class KeyboardViewModel : ViewModel() {
     fun getKeyboardList() {
         mKeyboardRepository.getKeyboardList(onSuccess = {
             val infos = it.keyboardInfos
+            val keyboardList = infos.map {
+                fixKeyboardSelectField(it)
+            }.toList()
             // 如果所有都没选中,则默认选中通用键盘
-            val hasSelectKeyboard = infos.filter {
+            val hasSelectKeyboard = keyboardList.filter {
                 it.isSelect == true
             }.toList().isNotEmpty()
             if (!hasSelectKeyboard) {
@@ -258,7 +261,7 @@ class KeyboardViewModel : ViewModel() {
     /**
      * 更新当前键盘信息
      */
-    fun updateCurrentKeyboardInfo(keyboardInfo: KeyboardSelectModel) {
+    fun updateCurrentKeyboardInfo(keyboardInfo: KeyboardInfo) {
         _currentKeyboardInfo.value = keyboardInfo
     }
 
@@ -268,10 +271,10 @@ class KeyboardViewModel : ViewModel() {
      * @param selectModel 键盘选择信息
      */
     fun selectedKeyboard(
-        selectModel: KeyboardSelectModel,
+        selectModel: KeyboardInfo,
         onSuccess: (resultObj: EmptyResp) -> Unit
     ) {
-        mKeyboardRepository.selectedKeyboard(selectModel.id, onSuccess = {
+        mKeyboardRepository.selectedKeyboard(selectModel.id!!, onSuccess = {
             // 更新键盘信息
             _currentKeyboardInfo.value = selectModel
             onSuccess.invoke(it)
@@ -285,16 +288,7 @@ class KeyboardViewModel : ViewModel() {
      */
     fun getCurrentKeyboardInfo() {
         getCurrentKeyboardInfoWithDefault(onSuccess = {
-            _currentKeyboardInfo.value = KeyboardSelectModel(
-                id = it.id,
-                type = it.type,
-                name = it.name,
-                gender = it.gender,
-                birthday = it.birthday,
-                intimacy = it.intimacy,
-                imageUrl = it.imageUrl,
-                isSelect = it.isSelect
-            )
+            _currentKeyboardInfo.value = fixKeyboardSelectField(it)
         }, onFail = { errorCode, errorMsg ->
             ErrorHandler.handleError(errorCode, errorMsg)
         })
@@ -309,7 +303,7 @@ class KeyboardViewModel : ViewModel() {
         }
         // 先获取当前键盘信息,得到键盘Id
         getCurrentKeyboardInfoWithDefault(onSuccess = { keyboardInfo ->
-            val keyboardId = keyboardInfo.id
+            val keyboardId = keyboardInfo.id!!
             val keyboardName = keyboardInfo.name
             LogUtil.d("加载人设列表, id: ${keyboardId}, name: $keyboardName")
             // 查询该键盘下的人设列表
@@ -429,43 +423,56 @@ class KeyboardViewModel : ViewModel() {
      * 获取键盘信息,如果没有,则返回第一个键盘信息
      */
     fun getCurrentKeyboardInfoWithDefault(
-        onSuccess: (model: KeyboardSelectModel) -> Unit,
+        onSuccess: (model: KeyboardInfo) -> Unit,
         onFail: (errorCode: Int, errorMsg: String) -> Unit
     ) {
         mKeyboardRepository.getCurrentKeyboardInfo(onSuccess = {
+            // 如果当前未在键盘中,手动选择过,就会为null
             if (it.id.isNullOrBlank()) {
                 mKeyboardRepository.getKeyboardList(onSuccess = {
-                    val infos = it.keyboardInfos
+                    val infos = it.keyboardInfos.map {
+                        fixKeyboardSelectField(it)
+                    }
                     // 数据为空
                     if (infos.isEmpty()) {
                         onFail.invoke(-1, "")
                     } else {
-                        // 默认使用系统键盘
-                        val systemKeyboard = infos.filter {
-                            KeyboardType.isSystem(it.type ?: "")
-                        }.toList()
-                        if (systemKeyboard.isNotEmpty()) {
-                            onSuccess.invoke(systemKeyboard.first())
+                        // 先找已选中的键盘,否则找系统键盘
+                        val selectedKeyboard = infos.filter {
+                            it.isSelect == true
+                        }.toList().firstOrNull()
+                        if (selectedKeyboard != null) {
+                            onSuccess.invoke(selectedKeyboard)
                         } else {
-                            // 连系统键盘都没有,则取第一个
-                            onSuccess.invoke(infos[0])
+                            // 没有找到选中的键盘,则使用系统键盘
+                            val systemKeyboard = infos.filter {
+                                KeyboardType.isSystem(it.type ?: "")
+                            }.toList()
+                            if (systemKeyboard.isNotEmpty()) {
+                                onSuccess.invoke(systemKeyboard.first())
+                            } else {
+                                // 连系统键盘都没有,则取第一个
+                                onSuccess.invoke(infos[0])
+                            }
                         }
                     }
                 }, onFail)
             } else {
-                // 更新键盘
-                val item = KeyboardSelectModel(
-                    id = it.id,
-                    type = it.type,
-                    name = it.name,
-                    gender = it.gender,
-                    birthday = it.birthday,
-                    intimacy = it.intimacy,
-                    imageUrl = it.imageUrl,
-                    isSelect = it.isSelect
-                )
-                onSuccess.invoke(item)
+                onSuccess.invoke(fixKeyboardSelectField(it))
             }
         }, onFail)
     }
+
+    /**
+     * 处理isSelect字段
+     */
+    private fun fixKeyboardSelectField(info: KeyboardInfo): KeyboardInfo {
+        // 统一使用isSelect字段,优先使用isSelect字段,代表是用户手动在键盘中操作过的,否则就用isChoose,是其他页面中选择过的
+        info.isSelect = if (info.isSelect != null) {
+            info.isSelect == true
+        } else {
+            info.isChoose == true
+        }
+        return info
+    }
 }

+ 4 - 4
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/util/bridge/FlutterBridgeManager.kt

@@ -11,7 +11,7 @@ import com.atmob.keyboard_android.util.bridge.model.base.EmptyResp
 import com.atmob.keyboard_android.util.bridge.model.req.SuperReplyReq
 import com.atmob.keyboard_android.util.bridge.model.req.SuperSpeakReq
 import com.atmob.keyboard_android.util.bridge.model.resp.CharacterListResp
-import com.atmob.keyboard_android.util.bridge.model.resp.CurrentKeyboardInfoResp
+import com.atmob.keyboard_android.util.bridge.model.resp.KeyboardInfo
 import com.atmob.keyboard_android.util.bridge.model.resp.KeyboardListResp
 import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp
 import com.atmob.keyboard_android.util.bridge.model.resp.PrologueResp
@@ -144,12 +144,12 @@ object FlutterBridgeManager : IBridgeApi {
     }
 
     override fun getCurrentKeyboardInfo(
-        onSuccess: (CurrentKeyboardInfoResp) -> Unit,
+        onSuccess: (KeyboardInfo) -> Unit,
         onFail: (errorCode: Int, errorMsg: String) -> Unit
     ) {
-        mFlutterMethodCaller.callMethod<CurrentKeyboardInfoResp>(
+        mFlutterMethodCaller.callMethod<KeyboardInfo>(
             FlutterMethod.GET_CURRENT_KEYBOARD_INFO.methodName,
-            modelClazz = CurrentKeyboardInfoResp::class.java,
+            modelClazz = KeyboardInfo::class.java,
             onSuccess = onSuccess,
             onFail = onFail
         )

+ 2 - 2
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/util/bridge/api/KeyboardApi.kt

@@ -2,7 +2,7 @@ package com.atmob.keyboard_android.util.bridge.api
 
 import com.atmob.keyboard_android.util.bridge.model.base.EmptyResp
 import com.atmob.keyboard_android.util.bridge.model.resp.CharacterListResp
-import com.atmob.keyboard_android.util.bridge.model.resp.CurrentKeyboardInfoResp
+import com.atmob.keyboard_android.util.bridge.model.resp.KeyboardInfo
 import com.atmob.keyboard_android.util.bridge.model.resp.KeyboardListResp
 import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp
 
@@ -31,7 +31,7 @@ interface KeyboardApi {
      * 获取当前键盘信息
      */
     fun getCurrentKeyboardInfo(
-        onSuccess: (CurrentKeyboardInfoResp) -> Unit,
+        onSuccess: (KeyboardInfo) -> Unit,
         onFail: (errorCode: Int, errorMsg: String) -> Unit
     )
 

+ 2 - 2
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/util/bridge/method/KeyboardExposeNativeMethodHandler.kt

@@ -3,7 +3,6 @@ package com.atmob.keyboard_android.util.bridge.method
 import android.content.Context
 import android.os.Build
 import com.atmob.keyboard_android.floating.FloatingButtonService
-import com.atmob.keyboard_android.model.KeyboardSelectModel
 import com.atmob.keyboard_android.util.ContextUtil
 import com.atmob.keyboard_android.util.InputMethodUtil
 import com.atmob.keyboard_android.util.JsonUtil
@@ -13,6 +12,7 @@ import com.atmob.keyboard_android.util.bridge.FlutterBridgeManager
 import com.atmob.keyboard_android.util.bridge.callback.NativeMethodHandler
 import com.atmob.keyboard_android.util.bridge.callback.ResultCallback
 import com.atmob.keyboard_android.util.bridge.enums.ExposeNativeMethod
+import com.atmob.keyboard_android.util.bridge.model.resp.KeyboardInfo
 import com.atmob.keyboard_android.util.flow.FloatingWindowUtil
 
 /**
@@ -102,7 +102,7 @@ class KeyboardExposeNativeMethodHandler : NativeMethodHandler {
                     return
                 }
                 val currentKeyboardInfo =
-                    JsonUtil.parseJsonByClass(keyboardInfoJson, KeyboardSelectModel::class.java)
+                    JsonUtil.parseJsonByClass(keyboardInfoJson, KeyboardInfo::class.java)
                 KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
                     ?.updateCurrentKeyboardInfo(currentKeyboardInfo)
                 resultCallback.onSuccess(null)

+ 0 - 43
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/util/bridge/model/resp/CurrentKeyboardInfoResp.kt

@@ -1,43 +0,0 @@
-package com.atmob.keyboard_android.util.bridge.model.resp
-
-import androidx.annotation.Keep
-import java.io.Serializable
-
-/**
- * 获取当前键盘Id的响应体
- */
-@Keep
-data class CurrentKeyboardInfoResp(
-    /**
-     * 键盘Id
-     */
-    val id: String? = null,
-    /**
-     * 键盘类型,system:系统键盘、custom:定制键盘
-     */
-    val type: String,
-    /**
-     * 键盘名称
-     */
-    val name: String,
-    /**
-     * 性别
-     */
-    val gender: Int,
-    /**
-     * 生日
-     */
-    val birthday: String,
-    /**
-     * 亲密度
-     */
-    val intimacy: Int,
-    /**
-     * 键盘图标Url
-     */
-    val imageUrl: String,
-    /**
-     * 是否选中(Flutter端本地添加的字段,不是服务端返回的)
-     */
-    var isSelect: Boolean = false,
-) : Serializable

+ 12 - 6
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/model/KeyboardSelectModel.kt

@@ -1,15 +1,17 @@
-package com.atmob.keyboard_android.model
+package com.atmob.keyboard_android.util.bridge.model.resp
 
+import androidx.annotation.Keep
 import java.io.Serializable
 
 /**
- * 键盘选择模型
+ * 键盘信息
  */
-data class KeyboardSelectModel(
+@Keep
+data class KeyboardInfo(
     /**
      * 键盘Id
      */
-    val id: String,
+    val id: String? = null,
     /**
      * 键盘类型,system:系统键盘、custom:定制键盘
      */
@@ -33,9 +35,13 @@ data class KeyboardSelectModel(
     /**
      * 键盘图标Url
      */
-    val imageUrl: String? = "",
+    val imageUrl: String?,
     /**
-     * 是否选中(Flutter端本地添加的字段,不是服务端返回的)
+     * 服务端的是否选中,仅限于非系统键盘
+     */
+    var isChoose: Boolean? = false,
+    /**
+     * 是否选中(Flutter端本地添加的字段,不是服务端返回的,支持系统键盘)
      */
     var isSelect: Boolean? = false,
 ) : Serializable

+ 1 - 2
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/util/bridge/model/resp/KeyboardListResp.kt

@@ -1,7 +1,6 @@
 package com.atmob.keyboard_android.util.bridge.model.resp
 
 import androidx.annotation.Keep
-import com.atmob.keyboard_android.model.KeyboardSelectModel
 import java.io.Serializable
 
 /**
@@ -12,5 +11,5 @@ data class KeyboardListResp(
     /**
      * 键盘列表
      */
-    val keyboardInfos: List<KeyboardSelectModel>
+    val keyboardInfos: List<KeyboardInfo>
 ) : Serializable