Forráskód Böngészése

[feat]键盘插件,对接Flutter的开场白列表API

hezihao 8 hónapja
szülő
commit
dc3fc7c835

+ 71 - 36
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/child/impl/AiKeyboardComponent.kt

@@ -6,18 +6,15 @@ import android.util.AttributeSet
 import android.view.View
 import androidx.recyclerview.widget.RecyclerView
 import com.atmob.keyboard_android.R
-import com.atmob.keyboard_android.component.ComponentMediator
 import com.atmob.keyboard_android.component.base.BaseUIComponent
 import com.atmob.keyboard_android.component.base.FakeComponent
 import com.atmob.keyboard_android.component.base.RouteComponent
 import com.atmob.keyboard_android.component.child.IAiChatComponent
 import com.atmob.keyboard_android.component.child.IAiKeyboardComponent
 import com.atmob.keyboard_android.component.item.AiKeyboardKeyViewBinder
-import com.atmob.keyboard_android.component.root.IKeyboardRootComponent
 import com.atmob.keyboard_android.constant.Constants
 import com.atmob.keyboard_android.enums.HelpMode
 import com.atmob.keyboard_android.enums.KeyboardGlobalType
-import com.atmob.keyboard_android.enums.Tab
 import com.atmob.keyboard_android.ext.click
 import com.atmob.keyboard_android.ext.setGone
 import com.atmob.keyboard_android.ext.setVisible
@@ -68,7 +65,7 @@ class AiKeyboardComponent @JvmOverloads constructor(
     /**
      * Tab列表
      */
-    private lateinit var mTabList: List<Tab>
+    private lateinit var mTabList: MutableList<String>
 
     override fun onInflateViewId(): Int {
         return R.layout.component_ai_keyboard
@@ -103,7 +100,8 @@ class AiKeyboardComponent @JvmOverloads constructor(
      * 配置Tab栏
      */
     private fun setupTabBar() {
-        mTabList = Tab.getAll()
+        // 初始化Tab列表
+        mTabList = mutableListOf()
 
         // 配置指示器
         vMagicIndicator.setNavigator(CommonNavigator(context).apply {
@@ -129,7 +127,7 @@ class AiKeyboardComponent @JvmOverloads constructor(
                         }
                     }
                     // Tab文字
-                    titleView.setText(mTabList[index].tabName)
+                    titleView.setText(mTabList[index])
                     // Tab未选中时的字体颜色
                     titleView.setTextColor(context.resources.getColor(R.color.text_color_secondary))
                     // Tab选中时的字体颜色
@@ -142,16 +140,10 @@ class AiKeyboardComponent @JvmOverloads constructor(
                     // 点击Tab,切换页面
                     titleView.setOnClickListener(object : OnClickListener {
                         override fun onClick(v: View?) {
-                            // Tab没有改变,不处理
-                            val currentTab = KeyboardHolder.getKeyboardService()
-                                ?.getKeyboardViewModel()?.tab?.value
-                            if (Tab.getTabByIndex(index) == currentTab) {
-                                return
-                            }
                             // 更新Tab
                             val newTab = mTabList[index]
                             KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
-                                ?.updateTab(newTab)
+                                ?.updateCurrentPrologueTab(newTab)
                         }
                     })
                     return titleView
@@ -275,6 +267,17 @@ class AiKeyboardComponent @JvmOverloads constructor(
      */
     private fun setupViewModel() {
         KeyboardHolder.getKeyboardService()?.run {
+            // 监听开场白列表更新
+            getKeyboardViewModel().prologueList.observe(getLifecycleOwner()) { list ->
+                // 更新Tab列表
+                mTabList.clear()
+                mTabList.addAll(list.map {
+                    it.title
+                }.toList())
+                // 刷新Tab栏
+                vMagicIndicator.navigator.notifyDataSetChanged()
+                LogUtil.d("开场白列表更新,刷新Tab栏...")
+            }
             // 监听键盘类型切换
             getKeyboardViewModel().keyboardGlobalType.observe(getLifecycleOwner()) { globalKeyboardType ->
                 if (KeyboardGlobalType.AI_KEYBOARD == globalKeyboardType) {
@@ -289,9 +292,9 @@ class AiKeyboardComponent @JvmOverloads constructor(
                 updateKeyboardByHelpMode(newMode)
             }
             // 监听Tab切换,更新键盘列表
-            getKeyboardViewModel().tab.observe(getLifecycleOwner()) { newTab ->
+            getKeyboardViewModel().currentPrologueTab.observe(getLifecycleOwner()) { newTab ->
                 // 切换Tab
-                vMagicIndicator.onPageSelected(newTab.getIndex())
+                vMagicIndicator.onPageSelected(mTabList.indexOf(newTab))
                 // 关闭Ai内容生成面板
                 controlAiChatPageShowing(false)
                 // 根据Tab,重新加载数据
@@ -315,28 +318,63 @@ class AiKeyboardComponent @JvmOverloads constructor(
      */
     @SuppressLint("NotifyDataSetChanged")
     private fun setData() {
+        KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()?.getPrologueList(onFail = {
+            ToastUtils.showShort(it)
+        })
     }
 
     /**
-     * 根据Tab类型,加载键盘列表
+     * 根据选择的帮助模式,加载按键列表
      */
     @SuppressLint("NotifyDataSetChanged")
-    private fun loadKeyListByTab(tab: Tab) {
-        KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()?.getCharacterList(onSuccess = {
-            val newList = (it.characterInfos ?: listOf()).map {
-                AiKeyboardKeyModel(
-                    // 显示的文本 = 表情 + 名字
-                    text = "${it.emoji} ${it.name}",
-                    // 是否需要VIP
-                    isVip = it.isVip
-                )
+    private fun loadKeyListByHelpMode(helpMode: HelpMode) {
+        // 开场白
+        if (HelpMode.OPEN_REMARKS == helpMode) {
+            val currentTab = KeyboardHolder.getKeyboardService()
+                ?.getKeyboardViewModel()?.currentPrologueTab?.value ?: ""
+            if (currentTab.isBlank()) {
+                return
             }
-            mKeyListItems.clear()
-            mKeyListItems.addAll(newList)
-            mKeyListAdapter.notifyDataSetChanged()
-        }, onFail = {
-            ToastUtils.showShort(it)
-        })
+            loadKeyListByTab(currentTab)
+        } else {
+            // 其他类型
+            KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
+                ?.getCharacterList(onSuccess = {
+                    val newList = (it.characterInfos ?: listOf()).map {
+                        AiKeyboardKeyModel(
+                            // 显示的文本 = 表情 + 名字
+                            text = "${it.emoji} ${it.name}",
+                            // 是否需要VIP
+                            isVip = it.isVip
+                        )
+                    }
+                    mKeyListItems.clear()
+                    mKeyListItems.addAll(newList)
+                    mKeyListAdapter.notifyDataSetChanged()
+                }, onFail = {
+                    ToastUtils.showShort(it)
+                })
+        }
+    }
+
+    /**
+     * 根据Tab,加载按键列表
+     */
+    @SuppressLint("NotifyDataSetChanged")
+    private fun loadKeyListByTab(tab: String) {
+        val list = KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
+            ?.filterPrologueListByTab(tab) ?: listOf()
+        val newList = list.map {
+            AiKeyboardKeyModel(
+                // 显示的文本 = 名字
+                text = it.name,
+                // 是否需要VIP
+                isVip = false
+            )
+        }.toList()
+        mKeyListItems.clear()
+        mKeyListItems.addAll(newList)
+        mKeyListAdapter.notifyDataSetChanged()
     }
 
     /**
@@ -355,11 +393,8 @@ class AiKeyboardComponent @JvmOverloads constructor(
         }
         // 隐藏AI生成内容面板
         controlAiChatPageShowing(false)
-
-        // 重新加载人设列表
-        KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()?.tab?.value?.let {
-            loadKeyListByTab(it)
-        }
+        // 重新加载按键列表
+        loadKeyListByHelpMode(helpMode)
     }
 
     /**

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

@@ -23,7 +23,7 @@ class QuickSwitchComponent @JvmOverloads constructor(
     private lateinit var vModeSwitchLayout: View
     private lateinit var vModeText: TextView
     private lateinit var vKeyboardSelectLayout: View
-    private lateinit var vRoleType: TextView
+    private lateinit var vKeyboardName: TextView
 
     /**
      * 模式切换的弹窗
@@ -38,7 +38,7 @@ class QuickSwitchComponent @JvmOverloads constructor(
         vModeSwitchLayout = view.findViewById(R.id.mode_switch_layout)
         vModeText = view.findViewById(R.id.mode_text)
         vKeyboardSelectLayout = view.findViewById(R.id.keyboard_select_layout)
-        vRoleType = view.findViewById(R.id.role_type)
+        vKeyboardName = view.findViewById(R.id.keyboard_name)
     }
 
     override fun bindView(view: View) {
@@ -54,9 +54,10 @@ class QuickSwitchComponent @JvmOverloads constructor(
                         KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
                             ?.updateHelpMode(newMode)
                         // 切换到键盘页
-                        val keyboardRootComponent = ComponentMediator.findComponent<IKeyboardRootComponent>(
-                            IKeyboardRootComponent::class.java
-                        )
+                        val keyboardRootComponent =
+                            ComponentMediator.findComponent<IKeyboardRootComponent>(
+                                IKeyboardRootComponent::class.java
+                            )
                         keyboardRootComponent?.switchKeyBoardPage()
                     }
                 })
@@ -82,9 +83,9 @@ class QuickSwitchComponent @JvmOverloads constructor(
             getKeyboardViewModel().helpMode.observe(getLifecycleOwner()) { newMode ->
                 vModeText.text = newMode.modeName
             }
-            // 监听键盘类型切换
-            getKeyboardViewModel().keyboardType.observe(getLifecycleOwner()) { newRoleType ->
-                vRoleType.text = newRoleType
+            // 监听键盘名称切换
+            getKeyboardViewModel().keyboardName.observe(getLifecycleOwner()) { newKeyboardName ->
+                vKeyboardName.text = newKeyboardName
             }
         }
     }

+ 0 - 52
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/enums/Tab.kt

@@ -1,52 +0,0 @@
-package com.atmob.keyboard_android.enums
-
-import com.atmob.keyboard_android.R
-import com.atmob.keyboard_android.ext.getResString
-
-/**
- * 开场白的Tab类型
- */
-enum class Tab(val type: Int, val tabName: String) {
-    /**
-     * 发起聊天
-     */
-    START_CHAT(1, getResString(R.string.start_chat)),
-
-    /**
-     * 邀约话术
-     */
-    INVITATION_SCRIPT(2, getResString(R.string.invitation_script)),
-
-    /**
-     * 如何告白
-     */
-    HOW_TO_CONFESS(3, getResString(R.string.how_to_confess)),
-
-    /**
-     * 关系升温
-     */
-    PROMOTION_RELATIONSHIP(4, getResString(R.string.promotion_relationship));
-
-    /**
-     * 获取Tab的索引
-     */
-    fun getIndex(): Int {
-        return Tab.getAll().indexOf(this)
-    }
-
-    companion object {
-        /**
-         * 获取全部
-         */
-        fun getAll(): List<Tab> {
-            return values().toList()
-        }
-
-        /**
-         * 根据索引,获取对应的Tab
-         */
-        fun getTabByIndex(index: Int): Tab {
-            return getAll()[index]
-        }
-    }
-}

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

@@ -5,6 +5,7 @@ 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.CurrentKeyboardIdResp
 import com.atmob.keyboard_android.util.bridge.model.resp.KeyboardListResp
+import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp
 
 /**
  * 键盘Repository
@@ -75,4 +76,14 @@ class KeyboardRepository {
     ) {
         FlutterBridgeManager.getCharacterList(keyboardId, onSuccess, onFail)
     }
+
+    /**
+     * 获取开场白列表
+     */
+    fun getPrologueList(
+        onSuccess: (PrologueListResp) -> Unit,
+        onFail: (String) -> Unit
+    ) {
+        FlutterBridgeManager.getPrologueList(onSuccess, onFail)
+    }
 }

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

@@ -6,12 +6,13 @@ import androidx.lifecycle.ViewModel
 import com.atmob.keyboard_android.R
 import com.atmob.keyboard_android.enums.HelpMode
 import com.atmob.keyboard_android.enums.KeyboardGlobalType
-import com.atmob.keyboard_android.enums.Tab
 import com.atmob.keyboard_android.model.KeyboardSelectModel
 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.resp.CharacterListResp
+import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp.PrologueListItem
+import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp.Topic
 
 /**
  * 键盘ViewModel
@@ -67,18 +68,24 @@ class KeyboardViewModel : ViewModel() {
     val keyboardGlobalType: LiveData<KeyboardGlobalType> = _keyboardGlobalType
 
     /**
-     * 键盘类型
+     * 键盘名称
      */
-    // TODO: hezihao,这里要改成具体的实体类,要到数据对接时,定义数据结构
-    private val _keyboardType =
+    private val _keyboardName =
         MutableLiveData(ContextUtil.getContext().resources.getString(R.string.common_keyboard))
-    val keyboardType: LiveData<String> = _keyboardType
+    val keyboardName: LiveData<String> = _keyboardName
 
     /**
-     * Tab类型
+     * 当前选中的开场白Tab
      */
-    private val _tab = MutableLiveData(Tab.START_CHAT)
-    val tab: LiveData<Tab> = _tab
+    private val _currentPrologueTab = MutableLiveData<String>()
+    val currentPrologueTab: LiveData<String> = _currentPrologueTab
+
+    /**
+     * 开场白列表
+     */
+    private val _prologueList =
+        MutableLiveData<List<PrologueListItem>>()
+    val prologueList: LiveData<List<PrologueListItem>> = _prologueList
 
     /**
      * 用户的剪切板数据
@@ -144,10 +151,10 @@ class KeyboardViewModel : ViewModel() {
     }
 
     /**
-     * 更新键盘的Tab
+     * 更新当前选中的开场白的Tab
      */
-    fun updateTab(tab: Tab) {
-        _tab.value = tab
+    fun updateCurrentPrologueTab(tab: String) {
+        _currentPrologueTab.value = tab
     }
 
     /**
@@ -199,8 +206,8 @@ class KeyboardViewModel : ViewModel() {
         onFail: (msg: String) -> Unit
     ) {
         mKeyboardRepository.selectedKeyboard(selectModel.id, onSuccess = {
-            // 更新键盘类型
-            _keyboardType.value = selectModel.name
+            // 更新键盘名称
+            _keyboardName.value = selectModel.name
             onSuccess.invoke(it)
         }, onFail)
     }
@@ -217,4 +224,27 @@ class KeyboardViewModel : ViewModel() {
             mKeyboardRepository.getCharacterList(keyboardId, onSuccess, onFail)
         }, onFail)
     }
+
+    /**
+     * 获取开场白列表
+     */
+    fun getPrologueList(onFail: (String) -> Unit) {
+        mKeyboardRepository.getPrologueList(onSuccess = {
+            _prologueList.value = it.prologues ?: listOf()
+        }, onFail = onFail)
+    }
+
+    /**
+     * 过滤出对应Tab的开场白列表
+     *
+     * @param tab 选择的Tab类型
+     */
+    fun filterPrologueListByTab(tab: String): List<Topic> {
+        // 按照选择Tab,过滤出对应Tab的开场白数据
+        val tabPrologues = _prologueList.value?.firstOrNull {
+            it.title == tab
+        }
+        // 该Tab下的开场白列表
+        return tabPrologues?.topics ?: listOf()
+    }
 }

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

@@ -9,7 +9,7 @@ data class PrologueListResp(
     /**
      * 开场白列表
      */
-    val prologues: List<PrologueListItem>
+    val prologues: List<PrologueListItem>? = null
 ) : Serializable {
     data class PrologueListItem(
         /**

+ 1 - 1
plugins/keyboard_android/android/src/main/res/layout/quick_switch_component.xml

@@ -51,7 +51,7 @@
             android:orientation="horizontal">
 
             <TextView
-                android:id="@+id/role_type"
+                android:id="@+id/keyboard_name"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_gravity="center_vertical"