Parcourir la source

[feat]增加选择键盘的本地存储、获取键盘人设列表

云天逵 il y a 8 mois
Parent
commit
3abf08196b

+ 2 - 0
lib/data/api/response/keyboard_character_list_response.dart

@@ -13,4 +13,6 @@ class KeyboardCharacterListResponse {
 
   factory KeyboardCharacterListResponse.fromJson(Map<String, dynamic> json) =>
       _$KeyboardCharacterListResponseFromJson(json);
+
+  Map<String, dynamic> toJson() => _$KeyboardCharacterListResponseToJson(this);
 }

+ 5 - 0
lib/data/bean/keyboard_info.dart

@@ -39,6 +39,10 @@ class KeyboardInfo {
   @JsonKey(name: 'isChoose')
   bool? isChoose;
 
+  // android原生
+  @JsonKey(name: 'isSelect')
+  bool? isSelect;
+
   KeyboardInfo({
     this.id,
     this.type,
@@ -49,6 +53,7 @@ class KeyboardInfo {
     this.imageUrl,
     this.isChoose,
     this.avatar,
+    this.isSelect,
   });
 
   factory KeyboardInfo.fromJson(Map<String, dynamic> json) =>

+ 2 - 0
lib/data/bean/keyboard_info.g.dart

@@ -16,6 +16,7 @@ KeyboardInfo _$KeyboardInfoFromJson(Map<String, dynamic> json) => KeyboardInfo(
   imageUrl: json['imageUrl'] as String?,
   isChoose: json['isChoose'] as bool?,
   avatar: json['avatar'] as String?,
+  isSelect: json['isSelect'] as bool?,
 );
 
 Map<String, dynamic> _$KeyboardInfoToJson(KeyboardInfo instance) =>
@@ -29,4 +30,5 @@ Map<String, dynamic> _$KeyboardInfoToJson(KeyboardInfo instance) =>
       'imageUrl': instance.imageUrl,
       'avatar': instance.avatar,
       'isChoose': instance.isChoose,
+      'isSelect': instance.isSelect,
     };

+ 1 - 0
lib/data/repository/keyboard_repository.dart

@@ -29,6 +29,7 @@ class KeyboardRepository {
   Future refreshKeyboardList() async {
     return getKeyboardList().then((response) {
       _keyboardInfoList.value = response.keyboardInfos;
+      print('$tag refreshKeyboardList: ${response.keyboardInfos.first.id}');
     });
   }
 

+ 6 - 3
lib/di/get_it.config.dart

@@ -127,9 +127,6 @@ extension GetItInjectableX on _i174.GetIt {
     gh.factory<_i922.KeyboardManageController>(
       () => _i922.KeyboardManageController(gh<_i274.KeyboardRepository>()),
     );
-    gh.lazySingleton<_i79.KeyboardAndroidPlatform>(
-      () => _i79.KeyboardAndroidPlatform(gh<_i274.KeyboardRepository>()),
-    );
     gh.factory<_i876.FeedbackController>(
       () => _i876.FeedbackController(gh<_i83.AccountRepository>()),
     );
@@ -150,6 +147,12 @@ extension GetItInjectableX on _i174.GetIt {
     gh.factory<_i15.CharacterCustomController>(
       () => _i15.CharacterCustomController(gh<_i50.ConfigRepository>()),
     );
+    gh.lazySingleton<_i79.KeyboardAndroidPlatform>(
+      () => _i79.KeyboardAndroidPlatform(
+        gh<_i274.KeyboardRepository>(),
+        gh<_i421.CharactersRepository>(),
+      ),
+    );
     gh.factory<_i888.CharacterController>(
       () => _i888.CharacterController(
         gh<_i421.CharactersRepository>(),

+ 1 - 1
lib/dialog/content/character_add_tab_controller.dart

@@ -72,7 +72,7 @@ class CharacterAddTabController extends BaseController
     });
 
     ever(keyboardRepository.keyboardInfoList, (value) {
-      AtmobLog.d(tag, "keyboardInfoList1 changed");
+
       if (value.isNotEmpty) {
         currentKeyboardInfo = keyboardInfoList.first;
         print("currentKeyboardInfo.value: $currentKeyboardInfo");

+ 3 - 0
lib/module/character/character_controller.dart

@@ -46,6 +46,9 @@ class CharacterController extends BaseController
   @override
   void onInit() {
     super.onInit();
+
+    currentCharacterGroupInfo.value = characterGroupList.first;
+    currentKeyboardInfo.value = keyboardInfoList.first;
     _dataLoad();
   }
 

+ 4 - 4
lib/module/profile/profile_controller.dart

@@ -16,9 +16,6 @@ class ProfileController extends BaseController {
 
   final AccountRepository accountRepository;
 
-  RxList<KeyboardInfo> get keyboardInfoList =>
-      keyboardRepository.keyboardInfoList;
-
   UserInfoResponse? get userInfo => accountRepository.userInfo.value;
 
   int get userGender => userInfo?.gender ?? 0;
@@ -28,8 +25,11 @@ class ProfileController extends BaseController {
   KeyboardInfo get currentCustomKeyboardInfo =>
       _currentCustomKeyboardInfo.value;
 
-  final RxList<KeyboardInfo> _customKeyboardInfoList = RxList();
 
+
+
+  final RxList<KeyboardInfo> _customKeyboardInfoList = RxList();
+  RxList<KeyboardInfo> get keyboardInfoList =>  _customKeyboardInfoList;
   // 当前自定义键盘亲密度
   final RxInt _currentCustomIntimacy = 0.obs;
 

+ 26 - 41
lib/plugins/keyboard_android_platform.dart

@@ -1,81 +1,66 @@
-import 'dart:convert';
-
 import 'package:flutter/services.dart';
 import 'package:flutter/widgets.dart';
-import 'package:get/get_rx/src/rx_types/rx_types.dart';
 import 'package:injectable/injectable.dart';
-import 'package:keyboard/data/repository/chat_repository.dart';
+import 'package:keyboard/data/repository/characters_repository.dart';
 import 'package:keyboard/data/repository/keyboard_repository.dart';
 import 'package:keyboard/utils/atmob_log.dart';
-import 'package:keyboard_android/keyboard_android.dart';
 
-import '../../data/bean/stream_deepseek_data.dart' as deepseek_data;
 import '../../di/get_it.dart';
-import '../data/bean/keyboard_info.dart';
+import 'keyboard_android_service.dart';
+import 'keyboard_method_handler.dart';
 
 @lazySingleton
 class KeyboardAndroidPlatform {
-  static const MethodChannel _channel = MethodChannel('keyboard_android');
-  var tag = "KeyboardAndroidPlatform";
+  static const _channel = MethodChannel('keyboard_android');
+  final _tag = 'KeyboardAndroidPlatform';
 
   final KeyboardRepository keyboardRepository;
+  final CharactersRepository charactersRepository;
+  final KeyboardAndroidService _keyboardAndroidService;
+  final KeyboardMethodHandler _methodHandler;
 
-  /// 插件对象
-  final _keyboardAndroidPlugin = KeyboardAndroid();
-
-  RxList<KeyboardInfo> get keyboardInfoList =>
-      keyboardRepository.keyboardInfoList;
-
-  KeyboardAndroidPlatform(this.keyboardRepository) {
-    AtmobLog.d(tag, '$tag....init');
-    init();
+  KeyboardAndroidPlatform(this.keyboardRepository, this.charactersRepository)
+    : _keyboardAndroidService = KeyboardAndroidService(),
+      _methodHandler = KeyboardMethodHandler(
+        keyboardRepository,
+        charactersRepository,
+      ) {
+    AtmobLog.d(_tag, '初始化 KeyboardAndroidPlatform');
+    _init();
   }
 
-  void init() {
-    _channel.setMethodCallHandler((MethodCall call) async {
-      switch (call.method) {
-        case 'getKeyboardList':
-          String? type = call.arguments?['type'] as String?;
-          var keyboardList = await keyboardRepository.getKeyboardList(
-            type: type,
-          );
-          String json = jsonEncode(keyboardList.toJson());
-          return json;
-
-        default:
-          throw MissingPluginException('Not implemented');
-      }
-    });
+  void _init() {
+    _channel.setMethodCallHandler(_methodHandler.handleMethodCall);
   }
 
   Future<void> sendDynamicTextRequest(
     String method,
     String currentContent,
   ) async {
+    AtmobLog.d(_tag, 'sendDynamicTextRequest: $method');
     switch (method) {
       case 'MethodA':
-        AtmobLog.d(tag, 'MethodA');
-
+        AtmobLog.d(_tag, '处理 MethodA');
         break;
       default:
-        AtmobLog.d(tag, 'default');
+        AtmobLog.d(_tag, '默认处理');
     }
   }
 
   void enableFloatingWindow(bool enable) {
     debugPrint('enableFloatingWindow $enable');
-    _keyboardAndroidPlugin.enableFloatingWindow(enable);
+    _keyboardAndroidService.enableFloatingWindow(enable);
   }
 
   void openInputMethodSettings() {
     debugPrint('openInputMethodSettings');
-    _keyboardAndroidPlugin.openInputMethodSettings();
+    _keyboardAndroidService.openInputMethodSettings();
   }
 
   Future<bool> isTargetKeyboardEnabled() {
-    var enable = _keyboardAndroidPlugin.isTargetKeyboardEnabled();
-    debugPrint('isTargetKeyboardEnabled value : $enable');
-    return enable;
+    final result = _keyboardAndroidService.isTargetKeyboardEnabled();
+    debugPrint('isTargetKeyboardEnabled value: $result');
+    return result;
   }
 
   static KeyboardAndroidPlatform getInstance() {

+ 17 - 0
lib/plugins/keyboard_android_service.dart

@@ -0,0 +1,17 @@
+import 'package:keyboard_android/keyboard_android.dart';
+
+class KeyboardAndroidService {
+  final _plugin = KeyboardAndroid();
+
+  void enableFloatingWindow(bool enable) {
+    _plugin.enableFloatingWindow(enable);
+  }
+
+  void openInputMethodSettings() {
+    _plugin.openInputMethodSettings();
+  }
+
+  Future<bool> isTargetKeyboardEnabled() {
+    return _plugin.isTargetKeyboardEnabled();
+  }
+}

+ 62 - 0
lib/plugins/keyboard_method_handler.dart

@@ -0,0 +1,62 @@
+import 'dart:convert';
+import 'package:flutter/services.dart';
+import 'package:keyboard/data/repository/characters_repository.dart';
+import 'package:keyboard/data/repository/keyboard_repository.dart';
+import '../utils/mmkv_util.dart';
+
+class KeyboardMethodHandler {
+  final KeyboardRepository keyboardRepository;
+  final CharactersRepository charactersRepository;
+
+  static const String keyboardSelect = 'keyboard_select';
+
+  KeyboardMethodHandler(
+      this.keyboardRepository,
+      this.charactersRepository,
+      );
+
+  Future<dynamic> handleMethodCall(MethodCall call) async {
+    switch (call.method) {
+      case 'getKeyboardList':
+        return await _handleGetKeyboardList(call);
+      case 'selectedKeyboard':
+        return _handleSelectedKeyboard(call);
+      case 'getCharacterList':
+        return await _handleGetCharacterList(call);
+      default:
+        throw MissingPluginException('Not implemented: ${call.method}');
+    }
+  }
+
+  Future<String> _handleGetKeyboardList(MethodCall call) async {
+    String? type = call.arguments?['type'] as String?;
+    final keyboardList = await keyboardRepository.getKeyboardList(type: type);
+
+    final selectKeyboardId = KVUtil.getString(keyboardSelect, null);
+
+    if (selectKeyboardId != null) {
+      for (var element in keyboardList.keyboardInfos) {
+        if (element.id == selectKeyboardId) {
+          element.isSelect = true;
+        }
+      }
+    }
+    return jsonEncode(keyboardList.toJson());
+  }
+
+
+  void _handleSelectedKeyboard(MethodCall call) {
+    final String keyboardId = call.arguments['keyboardId'];
+    KVUtil.putString(keyboardSelect, keyboardId);
+  }
+
+  Future<String> _handleGetCharacterList(MethodCall call) async {
+    final String keyboardId = call.arguments['keyboardId'];
+    final characterList = await keyboardRepository.getKeyboardCharacterList(keyboardId: keyboardId);
+    return jsonEncode(characterList.toJson());
+  }
+
+
+
+
+}