keyboard_method_handler.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import 'dart:convert';
  2. import 'package:collection/collection.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:keyboard/data/repository/account_repository.dart';
  5. import 'package:keyboard/data/repository/characters_repository.dart';
  6. import 'package:keyboard/data/repository/chat_repository.dart';
  7. import 'package:keyboard/data/repository/keyboard_repository.dart';
  8. import 'package:keyboard/utils/atmob_log.dart';
  9. import 'package:get/get.dart';
  10. import '../data/api/response/chat_prologue_response.dart';
  11. import '../data/api/response/chat_super_speak_response.dart';
  12. import '../data/api/response/keyboard_list_response.dart';
  13. import '../utils/http_handler.dart';
  14. import '../utils/mmkv_util.dart';
  15. import '../utils/toast_util.dart';
  16. class KeyboardMethodHandler {
  17. final tag = "KeyboardMethodHandler";
  18. final KeyboardRepository keyboardRepository;
  19. final CharactersRepository charactersRepository;
  20. final AccountRepository accountRepository;
  21. final ChatRepository chatRepository;
  22. // 用处存储选中的键盘id
  23. static const String keyboardSelect = 'keyboard_select';
  24. bool get isLogin => accountRepository.isLogin.value;
  25. bool get isMember => accountRepository.isMember.value;
  26. late KeyboardListResponse keyboardListResponse;
  27. KeyboardMethodHandler(
  28. this.keyboardRepository,
  29. this.charactersRepository,
  30. this.accountRepository,
  31. this.chatRepository,
  32. );
  33. Future<dynamic> handleMethodCall(MethodCall call) async {
  34. switch (call.method) {
  35. case 'getKeyboardList':
  36. return _handleGetKeyboardList(call);
  37. case 'selectedKeyboard':
  38. return _handleSelectedKeyboard(call);
  39. case 'getCharacterList':
  40. return _handleGetCharacterList(call);
  41. case 'getCurrentKeyboardInfo':
  42. return _handleGetCurrentKeyboardInfo(call);
  43. case 'getPrologueList':
  44. return _handleGetPrologueList(call);
  45. case 'isLogin':
  46. return isLogin;
  47. case 'isMember':
  48. return isMember;
  49. case 'chatSuperReply':
  50. return _handleChatSuperReply(call);
  51. case 'chatSuperSpeak':
  52. return _handleChatSuperSpeak(call);
  53. case 'chatPrologue':
  54. return _handleChatPrologue(call);
  55. case 'jumpAppPage':
  56. return _handleJumpAppPage(call);
  57. default:
  58. throw MissingPluginException('Not implemented: ${call.method}');
  59. }
  60. }
  61. Future<String> _handleGetKeyboardList(MethodCall call) async {
  62. String? type = call.arguments?['type'] as String?;
  63. final keyboardList = await keyboardRepository.getKeyboardList(type: type);
  64. final selectedKeyboardJson = KVUtil.getString(keyboardSelect, null);
  65. if (selectedKeyboardJson != null) {
  66. try {
  67. final Map<String, dynamic> keyboardMap = jsonDecode(
  68. selectedKeyboardJson,
  69. );
  70. final String? keyboardId = keyboardMap['id'] as String?;
  71. if (keyboardId != null) {
  72. for (var element in keyboardList.keyboardInfos) {
  73. if (element.id == keyboardId) {
  74. element.isSelect = true;
  75. break;
  76. }
  77. }
  78. }
  79. } catch (e) {
  80. AtmobLog.e(tag, "解析本地选中键盘失败: $e");
  81. }
  82. }
  83. keyboardListResponse = keyboardList;
  84. return jsonEncode(keyboardList.toJson());
  85. }
  86. Future<String> _handleSelectedKeyboard(MethodCall call) async {
  87. final String keyboardId = call.arguments['keyboardId'];
  88. if (keyboardListResponse.keyboardInfos.isEmpty) {
  89. keyboardListResponse = await keyboardRepository.getKeyboardList();
  90. }
  91. final selectedKeyboard = keyboardListResponse.keyboardInfos
  92. .firstWhereOrNull((element) => element.id == keyboardId);
  93. if (selectedKeyboard != null) {
  94. KVUtil.putString(keyboardSelect, jsonEncode(selectedKeyboard.toJson()));
  95. }
  96. return "{}";
  97. }
  98. Future<String> _handleGetCurrentKeyboardInfo(MethodCall call) async {
  99. final String? keyboardJsonStr = KVUtil.getString(keyboardSelect, null);
  100. if (keyboardJsonStr != null) {
  101. try {
  102. final jsonMap = jsonDecode(keyboardJsonStr);
  103. return jsonEncode(jsonMap);
  104. } catch (e) {
  105. AtmobLog.e(tag, "Failed to decode keyboard JSON: $e");
  106. }
  107. }
  108. return "{}";
  109. }
  110. Future<String> _handleGetCharacterList(MethodCall call) async {
  111. final String keyboardId = call.arguments['keyboardId'];
  112. final characterList = await keyboardRepository.getKeyboardCharacterList(
  113. keyboardId: keyboardId,
  114. );
  115. return jsonEncode(characterList.toJson());
  116. }
  117. // 获取开场白列表
  118. Future<String> _handleGetPrologueList(MethodCall call) async {
  119. final prologueList = await keyboardRepository.getPrologueList();
  120. return jsonEncode(prologueList.toJson());
  121. }
  122. // 超会回
  123. Future<String> _handleChatSuperReply(MethodCall call) async {
  124. final String content = call.arguments['content'];
  125. final String keyboardId = call.arguments['keyboardId'];
  126. final String characterId = call.arguments['characterId'];
  127. try {
  128. final chatSuperReplyResponse = await chatRepository.chatSuperReply(
  129. content: content,
  130. keyboardId: keyboardId,
  131. characterId: characterId,
  132. );
  133. return jsonEncode(chatSuperReplyResponse.toJson());
  134. } catch (error) {
  135. if (error is ServerErrorException) {
  136. AtmobLog.d(tag, "超会回失败: ${error.message}");
  137. ToastUtil.show(error.message);
  138. } else {
  139. AtmobLog.d(tag, "超会回失败: $error");
  140. }
  141. return '{}';
  142. }
  143. }
  144. // 超会说
  145. Future<String> _handleChatSuperSpeak(MethodCall call) async {
  146. final String content = call.arguments['content'];
  147. final String keyboardId = call.arguments['keyboardId'];
  148. final String characterId = call.arguments['characterId'];
  149. final ChatSuperSpeakResponse chatSuperSpeakResponse;
  150. try {
  151. chatSuperSpeakResponse = await chatRepository.chatSuperSpeak(
  152. content: content,
  153. keyboardId: keyboardId,
  154. characterId: characterId,
  155. );
  156. return jsonEncode(chatSuperSpeakResponse.toJson());
  157. } catch (error) {
  158. if (error is ServerErrorException) {
  159. AtmobLog.d(tag, "超会说失败: ${error.message}");
  160. ToastUtil.show(error.message);
  161. } else {
  162. AtmobLog.d(tag, "超会说失败: $error");
  163. }
  164. return '{}';
  165. }
  166. }
  167. // 开场白
  168. Future<String> _handleChatPrologue(MethodCall call) async {
  169. final String name = call.arguments['name'];
  170. final ChatPrologueResponse chatPrologueResponse;
  171. try {
  172. chatPrologueResponse = await chatRepository.chatPrologue(name: name);
  173. return jsonEncode(chatPrologueResponse.toJson());
  174. } catch (error) {
  175. if (error is ServerErrorException) {
  176. ToastUtil.show(error.message);
  177. AtmobLog.i(tag, "开场白失败: ${error.message}");
  178. } else {
  179. AtmobLog.i(tag, "开场白失败: $error");
  180. }
  181. return '{}';
  182. }
  183. }
  184. Future<String> _handleJumpAppPage(MethodCall call) async {
  185. try {
  186. final String path = call.arguments['path'];
  187. final dynamic args = call.arguments['args'];
  188. Map<String, dynamic> parsedArgs = {};
  189. if (args is String && args.isNotEmpty) {
  190. try {
  191. parsedArgs = Map<String, dynamic>.from(json.decode(args));
  192. } catch (e) {
  193. return '{}';
  194. }
  195. } else if (args is Map) {
  196. parsedArgs = Map<String, dynamic>.from(args);
  197. }
  198. Get.toNamed(
  199. path,
  200. parameters: parsedArgs.map((k, v) => MapEntry(k, v.toString())),
  201. );
  202. return '{}';
  203. } catch (e) {
  204. print("跳转失败: $e");
  205. return '{}';
  206. }
  207. }
  208. }