keyboard_method_handler.dart 7.2 KB

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