Преглед изворни кода

[feat]键盘插件,打开键盘时,获取当前键盘信息,渲染键盘名称

hezihao пре 9 месеци
родитељ
комит
a24fd737ca

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

@@ -55,7 +55,7 @@ class KeyboardSelectComponent @JvmOverloads constructor(
         }
         }
         vSaveBtn.click {
         vSaveBtn.click {
             val selectKeyboard =
             val selectKeyboard =
-                mListItems.filterIsInstance<KeyboardSelectModel>().find { it.isSelect }
+                mListItems.filterIsInstance<KeyboardSelectModel>().find { it.isSelect ?: false }
             if (selectKeyboard == null) {
             if (selectKeyboard == null) {
                 return@click
                 return@click
             }
             }
@@ -77,8 +77,8 @@ class KeyboardSelectComponent @JvmOverloads constructor(
                 // 键盘条目
                 // 键盘条目
                 register(KeyboardSelectModel::class.java, KeyboardSelectViewBinder { item ->
                 register(KeyboardSelectModel::class.java, KeyboardSelectViewBinder { item ->
                     // 先全部取消选中,再选中当前设置的键盘
                     // 先全部取消选中,再选中当前设置的键盘
-                    mListItems.forEachIndexed { index, item ->
-                        if (item is KeyboardSelectModel) {
+                    mListItems.forEach {
+                        if (it is KeyboardSelectModel) {
                             item.isSelect = false
                             item.isSelect = false
                         }
                         }
                     }
                     }

+ 14 - 2
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/child/impl/QuickSwitchComponent.kt

@@ -13,6 +13,7 @@ import com.atmob.keyboard_android.enums.HelpMode
 import com.atmob.keyboard_android.ext.click
 import com.atmob.keyboard_android.ext.click
 import com.atmob.keyboard_android.ui.popup.ModeSwitchPopupWindow
 import com.atmob.keyboard_android.ui.popup.ModeSwitchPopupWindow
 import com.atmob.keyboard_android.util.KeyboardHolder
 import com.atmob.keyboard_android.util.KeyboardHolder
+import com.atmob.keyboard_android.util.LogUtil
 
 
 /**
 /**
  * 快速切换组件
  * 快速切换组件
@@ -72,6 +73,7 @@ class QuickSwitchComponent @JvmOverloads constructor(
         }
         }
 
 
         setupViewModel()
         setupViewModel()
+        setData()
     }
     }
 
 
     /**
     /**
@@ -84,12 +86,22 @@ class QuickSwitchComponent @JvmOverloads constructor(
                 vModeText.text = newMode.modeName
                 vModeText.text = newMode.modeName
             }
             }
             // 监听键盘名称切换
             // 监听键盘名称切换
-            getKeyboardViewModel().keyboardName.observe(getLifecycleOwner()) { newKeyboardName ->
-                vKeyboardName.text = newKeyboardName
+            getKeyboardViewModel().currentKeyboardInfo.observe(getLifecycleOwner()) { newKeyboard ->
+                LogUtil.d("更新键盘名称:${newKeyboard.name}")
+                var name = newKeyboard.name
+                if (name.isBlank()) {
+                    name = context.resources.getString(R.string.common_keyboard)
+                }
+                vKeyboardName.text = name
             }
             }
         }
         }
     }
     }
 
 
+    private fun setData() {
+        // 获取当前键盘信息
+        KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()?.getCurrentKeyboardInfo()
+    }
+
     override fun getComponentInterfaceClazz(): Class<IQuickSwitchComponent> {
     override fun getComponentInterfaceClazz(): Class<IQuickSwitchComponent> {
         return IQuickSwitchComponent::class.java
         return IQuickSwitchComponent::class.java
     }
     }

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

@@ -45,7 +45,7 @@ class KeyboardSelectViewBinder(
         holder.vName.text = item.name
         holder.vName.text = item.name
 
 
         // 选中
         // 选中
-        if (item.isSelect) {
+        if (item.isSelect ?: false) {
             holder.itemContainer.setBackgroundResource(R.drawable.bg_keyboard_selected)
             holder.itemContainer.setBackgroundResource(R.drawable.bg_keyboard_selected)
             holder.vName.apply {
             holder.vName.apply {
                 setTextColor(context.resources.getColor(R.color.text_color_white))
                 setTextColor(context.resources.getColor(R.color.text_color_white))

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

@@ -33,9 +33,9 @@ data class KeyboardSelectModel(
     /**
     /**
      * 键盘图标Url
      * 键盘图标Url
      */
      */
-    val imageUrl: String,
+    val imageUrl: String? = "",
     /**
     /**
      * 是否选中(Flutter端本地添加的字段,不是服务端返回的)
      * 是否选中(Flutter端本地添加的字段,不是服务端返回的)
      */
      */
-    var isSelect: Boolean = false,
+    var isSelect: Boolean? = false,
 ) : Serializable
 ) : Serializable

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

@@ -57,9 +57,9 @@ class KeyboardRepository {
     }
     }
 
 
     /**
     /**
-     * 获取当前键盘Id
+     * 获取当前键盘信息
      */
      */
-    fun getCurrentKeyboardId(
+    fun getCurrentKeyboardInfo(
         onSuccess: (resultObj: CurrentKeyboardIdResp) -> Unit,
         onSuccess: (resultObj: CurrentKeyboardIdResp) -> Unit,
         onFail: (msg: String) -> Unit
         onFail: (msg: String) -> Unit
     ) {
     ) {

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

@@ -3,12 +3,10 @@ package com.atmob.keyboard_android.mvvm.viewmodel
 import androidx.lifecycle.LiveData
 import androidx.lifecycle.LiveData
 import androidx.lifecycle.MutableLiveData
 import androidx.lifecycle.MutableLiveData
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.ViewModel
-import com.atmob.keyboard_android.R
 import com.atmob.keyboard_android.enums.HelpMode
 import com.atmob.keyboard_android.enums.HelpMode
 import com.atmob.keyboard_android.enums.KeyboardGlobalType
 import com.atmob.keyboard_android.enums.KeyboardGlobalType
 import com.atmob.keyboard_android.model.KeyboardSelectModel
 import com.atmob.keyboard_android.model.KeyboardSelectModel
 import com.atmob.keyboard_android.mvvm.repository.KeyboardRepository
 import com.atmob.keyboard_android.mvvm.repository.KeyboardRepository
-import com.atmob.keyboard_android.util.ContextUtil
 import com.atmob.keyboard_android.util.bridge.model.base.EmptyResp
 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.CharacterListResp
 import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp.PrologueListItem
 import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp.PrologueListItem
@@ -68,11 +66,10 @@ class KeyboardViewModel : ViewModel() {
     val keyboardGlobalType: LiveData<KeyboardGlobalType> = _keyboardGlobalType
     val keyboardGlobalType: LiveData<KeyboardGlobalType> = _keyboardGlobalType
 
 
     /**
     /**
-     * 键盘名称
+     * 当前键盘信息
      */
      */
-    private val _keyboardName =
-        MutableLiveData(ContextUtil.getContext().resources.getString(R.string.common_keyboard))
-    val keyboardName: LiveData<String> = _keyboardName
+    private val _currentKeyboardInfo = MutableLiveData<KeyboardSelectModel>()
+    val currentKeyboardInfo: LiveData<KeyboardSelectModel> = _currentKeyboardInfo
 
 
     /**
     /**
      * 当前选中的开场白Tab
      * 当前选中的开场白Tab
@@ -206,21 +203,42 @@ class KeyboardViewModel : ViewModel() {
         onFail: (msg: String) -> Unit
         onFail: (msg: String) -> Unit
     ) {
     ) {
         mKeyboardRepository.selectedKeyboard(selectModel.id, onSuccess = {
         mKeyboardRepository.selectedKeyboard(selectModel.id, onSuccess = {
-            // 更新键盘名称
-            _keyboardName.value = selectModel.name
+            // 更新键盘信息
+            _currentKeyboardInfo.value = selectModel
             onSuccess.invoke(it)
             onSuccess.invoke(it)
         }, onFail)
         }, onFail)
     }
     }
 
 
     /**
     /**
+     * 获取当前键盘信息
+     */
+    fun getCurrentKeyboardInfo() {
+        mKeyboardRepository.getCurrentKeyboardInfo(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
+            )
+        }, onFail = {})
+    }
+
+    /**
      * 获取人设列表
      * 获取人设列表
      */
      */
     fun getCharacterList(
     fun getCharacterList(
         onSuccess: (CharacterListResp) -> Unit,
         onSuccess: (CharacterListResp) -> Unit,
         onFail: (String) -> Unit
         onFail: (String) -> Unit
     ) {
     ) {
-        mKeyboardRepository.getCurrentKeyboardId(onSuccess = {
-            val keyboardId = it.id ?: ""
+        // 先获取当前键盘信息,得到键盘Id
+        mKeyboardRepository.getCurrentKeyboardInfo(onSuccess = {
+            val keyboardId = it.id
+            // 查询该键盘下的人设列表
             mKeyboardRepository.getCharacterList(keyboardId, onSuccess, onFail)
             mKeyboardRepository.getCharacterList(keyboardId, onSuccess, onFail)
         }, onFail)
         }, onFail)
     }
     }

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

@@ -9,7 +9,7 @@ data class CurrentKeyboardIdResp(
     /**
     /**
      * 键盘Id
      * 键盘Id
      */
      */
-    val id: String? = null,
+    val id: String,
     /**
     /**
      * 键盘类型,system:系统键盘、custom:定制键盘
      * 键盘类型,system:系统键盘、custom:定制键盘
      */
      */
@@ -34,4 +34,8 @@ data class CurrentKeyboardIdResp(
      * 键盘图标Url
      * 键盘图标Url
      */
      */
     val imageUrl: String,
     val imageUrl: String,
+    /**
+     * 是否选中(Flutter端本地添加的字段,不是服务端返回的)
+     */
+    var isSelect: Boolean = false,
 ) : Serializable
 ) : Serializable