Просмотр исходного кода

[feat]更新人设时,通知键盘刷新人设列表

hezihao 7 месяцев назад
Родитель
Сommit
0155653ab2

+ 5 - 0
lib/module/keyboard_manage/keyboard_manage_controller.dart

@@ -16,6 +16,7 @@ import 'package:keyboard/utils/toast_util.dart';
 
 import '../../data/bean/keyboard_info.dart';
 import '../../data/consts/error_code.dart';
+import '../../plugins/keyboard_android_platform.dart';
 import '../../utils/error_handler.dart';
 import '../../utils/http_handler.dart';
 
@@ -392,6 +393,8 @@ class KeyboardManageController extends BaseController
           _currentCustomKeyboardCharacterList,
         );
         Get.find<CharacterGroupContentController>().refreshData();
+        // 通知键盘,刷新人设列表
+        KeyboardAndroidPlatform.refreshCharacterList();
         saveSuccessGetBack();
       },
     );
@@ -408,6 +411,8 @@ class KeyboardManageController extends BaseController
         _oldGeneralCharacterList = List<CharacterInfo>.from(
           _currentGeneralKeyboardCharacterList,
         );
+        // 通知键盘,刷新人设列表
+        KeyboardAndroidPlatform.refreshCharacterList();
       },
     );
   }

+ 5 - 0
lib/plugins/keyboard_android_platform.dart

@@ -101,4 +101,9 @@ class KeyboardAndroidPlatform {
   static void updateKeyboardInfo(KeyboardInfo keyboardInfo) {
     _keyboardAndroidService.updateKeyboardInfo(keyboardInfo);
   }
+
+  /// 刷新人设列表
+  static void refreshCharacterList() {
+    _keyboardAndroidService.refreshCharacterList();
+  }
 }

+ 4 - 0
lib/plugins/keyboard_android_service.dart

@@ -37,4 +37,8 @@ class KeyboardAndroidService {
   updateKeyboardInfo(KeyboardInfo keyboardInfo) {
     _plugin.updateKeyboardInfo(jsonEncode(keyboardInfo));
   }
+
+  void refreshCharacterList() {
+    _plugin.refreshCharacterList();
+  }
 }

+ 8 - 0
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/KeyboardAndroidPlugin.kt

@@ -136,6 +136,14 @@ class KeyboardAndroidPlugin : FlutterPlugin, MethodCallHandler {
                 result.success(null)
             }
 
+            // 刷新人设列表
+            "refreshCharacterList" -> {
+                LogUtil.d("刷新人设列表")
+                KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
+                    ?.getCharacterList()
+                result.success(null)
+            }
+
             else -> {
                 result.notImplemented()
             }

+ 40 - 26
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/child/impl/AiKeyboardCommonPanelComponent.kt

@@ -33,6 +33,7 @@ import com.atmob.keyboard_android.util.KeyboardHolder
 import com.atmob.keyboard_android.util.LogUtil
 import com.atmob.keyboard_android.util.UserInfoHelper
 import com.atmob.keyboard_android.util.bridge.FlutterBridgeManager
+import com.atmob.keyboard_android.util.bridge.model.resp.CharacterListResp.CharacterInfo
 import com.atmob.keyboard_android.util.recyclerview.GridDivider
 import com.atmob.keyboard_android.widget.LongTouchContainer
 import com.blankj.utilcode.util.ConvertUtils
@@ -234,6 +235,10 @@ class AiKeyboardCommonPanelComponent @JvmOverloads constructor(
      */
     private fun setupViewModel() {
         KeyboardHolder.getKeyboardService()?.run {
+            // 监听人设列表更新
+            getKeyboardViewModel().characterList.observe(getLifecycleOwner()) {
+                renderList(it)
+            }
             // 监听帮助模式更新,切换键盘
             getKeyboardViewModel().helpMode.observe(getLifecycleOwner()) { newMode ->
                 // 隐藏AI生成内容面板
@@ -294,27 +299,33 @@ class AiKeyboardCommonPanelComponent @JvmOverloads constructor(
     @SuppressLint("NotifyDataSetChanged")
     private fun loadKeyList() {
         KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
-            ?.getCharacterList(onSuccess = {
-                val newList = (it.characterInfos ?: listOf()).map {
-                    AiKeyboardKeyModel(
-                        // 显示的文本 = 表情 + 名字
-                        text = "${it.emoji} ${it.name}",
-                        // 拓展字段,存储人设Id
-                        payload = it.id,
-                        // 是否需要VIP
-                        isVip = it.isVip
-                    )
-                }
-                mKeyListItems.clear()
-                // 添加按键列表
-                mKeyListItems.addAll(newList)
-                // 添加人设按钮
-                mKeyListItems.add(AddCharacterModel())
-                mKeyListAdapter.notifyDataSetChanged()
+            ?.getCharacterList()
+    }
 
-                // 显示键盘引导页
-                tryShowKeyboardGuide()
-            })
+    /**
+     * 根据数据,渲染列表
+     */
+    @SuppressLint("NotifyDataSetChanged")
+    private fun renderList(characterList: List<CharacterInfo>) {
+        val newList = characterList.map {
+            AiKeyboardKeyModel(
+                // 显示的文本 = 表情 + 名字
+                text = "${it.emoji} ${it.name}",
+                // 拓展字段,存储人设Id
+                payload = it.id,
+                // 是否需要VIP
+                isVip = it.isVip
+            )
+        }
+        mKeyListItems.clear()
+        // 添加按键列表
+        mKeyListItems.addAll(newList)
+        // 添加人设按钮
+        mKeyListItems.add(AddCharacterModel())
+        mKeyListAdapter.notifyDataSetChanged()
+
+        // 显示键盘引导页
+        tryShowKeyboardGuide()
     }
 
     /**
@@ -353,12 +364,15 @@ class AiKeyboardCommonPanelComponent @JvmOverloads constructor(
             // 找到目标条目,则显示引导
             if (viewHolder != null) {
                 val itemView = viewHolder.itemView
-                KeyboardGuideDialogUtil.showGuideDialog(itemView, itemData, object : IKeyboardGuideComponent.OnActionCallback {
-                    override fun onFinishGuide() {
-                        mKeyboardGuideShowing = false
-                        KeyboardGuideRecordUtil.setFirstShowKeyboardGuide(false)
-                    }
-                })
+                KeyboardGuideDialogUtil.showGuideDialog(
+                    itemView,
+                    itemData,
+                    object : IKeyboardGuideComponent.OnActionCallback {
+                        override fun onFinishGuide() {
+                            mKeyboardGuideShowing = false
+                            KeyboardGuideRecordUtil.setFirstShowKeyboardGuide(false)
+                        }
+                    })
                 mKeyboardGuideShowing = true
             }
         }

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

@@ -11,7 +11,7 @@ import com.atmob.keyboard_android.mvvm.repository.KeyboardRepository
 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.CharacterListResp.CharacterInfo
 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
@@ -88,6 +88,12 @@ class KeyboardViewModel : ViewModel() {
     val currentPrologueTab: LiveData<String> = _currentPrologueTab
 
     /**
+     * 人设列表
+     */
+    private val _characterList = MutableLiveData<List<CharacterInfo>>()
+    val characterList: LiveData<List<CharacterInfo>> = _characterList
+
+    /**
      * 开场白列表
      */
     private val _prologueList =
@@ -297,9 +303,7 @@ class KeyboardViewModel : ViewModel() {
     /**
      * 获取人设列表
      */
-    fun getCharacterList(
-        onSuccess: (CharacterListResp) -> Unit
-    ) {
+    fun getCharacterList() {
         val errorCallback = { errorCode: Int, errorMsg: String ->
             ErrorHandler.handleError(errorCode, errorMsg)
         }
@@ -307,7 +311,9 @@ class KeyboardViewModel : ViewModel() {
         getCurrentKeyboardInfoWithDefault(onSuccess = {
             val keyboardId = it.id
             // 查询该键盘下的人设列表
-            mKeyboardRepository.getCharacterList(keyboardId, onSuccess, errorCallback)
+            mKeyboardRepository.getCharacterList(keyboardId, onSuccess = {
+                _characterList.value = it.characterInfos ?: listOf()
+            }, errorCallback)
         }, onFail = errorCallback)
     }
 

+ 5 - 0
plugins/keyboard_android/lib/keyboard_android.dart

@@ -45,6 +45,11 @@ class KeyboardAndroid {
     KeyboardAndroidPlatform.instance.updateKeyboardInfo(keyboardInfoJson);
   }
 
+  /// 刷新人设列表
+  void refreshCharacterList() {
+    KeyboardAndroidPlatform.instance.refreshCharacterList();
+  }
+
   // /// 获取键映射
   // static Future<List<Map<String, String>>> getKeyMappings() {
   //   return KeyboardAndroidPlatform.instance.getKeyMappings();

+ 5 - 0
plugins/keyboard_android/lib/keyboard_android_method_channel.dart

@@ -61,6 +61,11 @@ class MethodChannelKeyboardAndroid extends KeyboardAndroidPlatform {
     methodChannel.invokeMethod('updateKeyboardInfo', keyboardInfoJson);
   }
 
+  @override
+  void refreshCharacterList() {
+    methodChannel.invokeMethod('refreshCharacterList');
+  }
+
   // /// 获取键映射
   // @override
   // Future<List<Map<String, String>>> getKeyMappings() async {

+ 6 - 0
plugins/keyboard_android/lib/keyboard_android_platform_interface.dart

@@ -75,6 +75,12 @@ abstract class KeyboardAndroidPlatform extends PlatformInterface {
     );
   }
 
+  void refreshCharacterList() {
+    throw UnimplementedError(
+      'refreshCharacterList() has not been implemented.',
+    );
+  }
+
   // /// 获取键映射(需要在 `MethodChannelKeyboardAndroid` 实现)
   // Future<List<Map<String, String>>> getKeyMappings() {
   //   throw UnimplementedError('getKeyMappings() has not been implemented.');

+ 5 - 0
plugins/keyboard_android/test/keyboard_android_test.dart

@@ -49,6 +49,11 @@ class MockKeyboardAndroidPlatform
   void updateKeyboardInfo(String keyboardInfoJson) {
     throw UnimplementedError();
   }
+
+  @override
+  void refreshCharacterList() {
+    throw UnimplementedError();
+  }
 }
 
 void main() {