keyboard_method_handler.dart 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import 'dart:convert';
  2. import 'package:flutter/services.dart';
  3. import 'package:get/get.dart';
  4. import 'package:injectable/injectable.dart';
  5. import 'package:keyboard/data/repository/chat_repository.dart';
  6. import 'package:keyboard/data/repository/keyboard_repository.dart';
  7. import 'package:keyboard/module/keyboard_manage/keyboard_manage_controller.dart';
  8. import 'package:keyboard/utils/atmob_log.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 '../data/consts/constants.dart';
  13. import '../utils/http_handler.dart';
  14. import '../utils/mmkv_util.dart';
  15. import '../utils/toast_util.dart';
  16. @injectable
  17. class KeyboardMethodHandler {
  18. final tag = "KeyboardMethodHandler";
  19. bool get isLogin => KVUtil.getBool(Constants.keyIsLogin, false);
  20. bool get isMember => KVUtil.getBool(Constants.keyIsMember, false);
  21. late KeyboardListResponse keyboardListResponse;
  22. KeyboardMethodHandler();
  23. Future<dynamic> handleMethodCall(MethodCall call) async {
  24. switch (call.method) {
  25. case 'getKeyboardList':
  26. return _handleGetKeyboardList(call);
  27. case 'selectedKeyboard':
  28. return _handleSelectedKeyboard(call);
  29. case 'getCharacterList':
  30. return _handleGetCharacterList(call);
  31. case 'getCurrentKeyboardInfo':
  32. return _handleGetCurrentKeyboardInfo(call);
  33. case 'getPrologueList':
  34. return _handleGetPrologueList(call);
  35. case 'isLogin':
  36. return isLogin;
  37. case 'isMember':
  38. return isMember;
  39. case 'chatSuperReply':
  40. return _handleChatSuperReply(call);
  41. case 'chatSuperSpeak':
  42. return _handleChatSuperSpeak(call);
  43. case 'chatPrologue':
  44. return _handleChatPrologue(call);
  45. case 'jumpAppPage':
  46. return _handleJumpAppPage(call);
  47. default:
  48. throw MissingPluginException('Not implemented: ${call.method}');
  49. }
  50. }
  51. Future<String> _handleGetKeyboardList(MethodCall call) async {
  52. String? type = call.arguments?['type'] as String?;
  53. final keyboardList = await KeyboardRepository.getInstance().getKeyboardList(
  54. type: type,
  55. );
  56. final selectedKeyboardJson = KVUtil.getString(Constants.keyboardSelect, null);
  57. if (selectedKeyboardJson != null) {
  58. try {
  59. final Map<String, dynamic> keyboardMap = jsonDecode(
  60. selectedKeyboardJson,
  61. );
  62. final String? keyboardId = keyboardMap['id'] as String?;
  63. if (keyboardId != null) {
  64. for (var element in keyboardList.keyboardInfos) {
  65. if (element.id == keyboardId) {
  66. element.isSelect = true;
  67. break;
  68. }
  69. }
  70. }
  71. } catch (e) {
  72. AtmobLog.e(tag, "解析本地选中键盘失败: $e");
  73. }
  74. }
  75. keyboardListResponse = keyboardList;
  76. return jsonEncode(keyboardList.toJson());
  77. }
  78. Future<String> _handleSelectedKeyboard(MethodCall call) async {
  79. final String keyboardId = call.arguments['keyboardId'];
  80. if (keyboardListResponse.keyboardInfos.isEmpty) {
  81. keyboardListResponse =
  82. await KeyboardRepository.getInstance().getKeyboardList();
  83. }
  84. final selectedKeyboard = keyboardListResponse.keyboardInfos
  85. .firstWhereOrNull((element) => element.id == keyboardId);
  86. if (selectedKeyboard != null) {
  87. if(selectedKeyboard.type == KeyboardType.custom.name) {
  88. try {
  89. await KeyboardRepository.getInstance().keyboardChoose(keyboardId:keyboardId);
  90. KeyboardRepository.getInstance().refreshData();
  91. } catch (error) {
  92. return handleError(error);
  93. }
  94. }
  95. KVUtil.putString(Constants.keyboardSelect, jsonEncode(selectedKeyboard.toJson()));
  96. }
  97. return "{}";
  98. }
  99. Future<String> _handleGetCurrentKeyboardInfo(MethodCall call) async {
  100. AtmobLog.d(tag, "_handleGetCurrentKeyboardInfo");
  101. final String? keyboardJsonStr = KVUtil.getString(Constants.keyboardSelect, null);
  102. if (keyboardJsonStr != null) {
  103. try {
  104. final jsonMap = jsonDecode(keyboardJsonStr);
  105. return jsonEncode(jsonMap);
  106. } catch (e) {
  107. AtmobLog.e(tag, "Failed to decode keyboard JSON: $e");
  108. }
  109. }
  110. return "{}";
  111. }
  112. Future<String> _handleGetCharacterList(MethodCall call) async {
  113. final String keyboardId = call.arguments['keyboardId'];
  114. final characterList = await KeyboardRepository.getInstance()
  115. .getKeyboardCharacterList(keyboardId: keyboardId);
  116. return jsonEncode(characterList.toJson());
  117. }
  118. // 获取开场白列表
  119. Future<String> _handleGetPrologueList(MethodCall call) async {
  120. final prologueList =
  121. await KeyboardRepository.getInstance().getPrologueList();
  122. return jsonEncode(prologueList.toJson());
  123. }
  124. // 超会回
  125. Future<String> _handleChatSuperReply(MethodCall call) async {
  126. final String content = call.arguments['content'];
  127. final String keyboardId = call.arguments['keyboardId'];
  128. final String characterId = call.arguments['characterId'];
  129. try {
  130. final chatSuperReplyResponse = await ChatRepository.getInstance()
  131. .chatSuperReply(
  132. content: content,
  133. keyboardId: keyboardId,
  134. characterId: characterId,
  135. );
  136. return jsonEncode(chatSuperReplyResponse.toJson());
  137. } catch (error) {
  138. return handleError(error);
  139. }
  140. }
  141. // 超会说
  142. Future<String> _handleChatSuperSpeak(MethodCall call) async {
  143. final String content = call.arguments['content'];
  144. final String keyboardId = call.arguments['keyboardId'];
  145. final String characterId = call.arguments['characterId'];
  146. final ChatSuperSpeakResponse chatSuperSpeakResponse;
  147. try {
  148. chatSuperSpeakResponse = await ChatRepository.getInstance()
  149. .chatSuperSpeak(
  150. content: content,
  151. keyboardId: keyboardId,
  152. characterId: characterId,
  153. );
  154. return jsonEncode(chatSuperSpeakResponse.toJson());
  155. } catch (error) {
  156. return handleError(error);
  157. }
  158. }
  159. // 开场白
  160. Future<String> _handleChatPrologue(MethodCall call) async {
  161. final String name = call.arguments['name'];
  162. final ChatPrologueResponse chatPrologueResponse;
  163. try {
  164. chatPrologueResponse = await ChatRepository.getInstance().chatPrologue(
  165. name: name,
  166. );
  167. return jsonEncode(chatPrologueResponse.toJson());
  168. }catch (error) {
  169. return handleError(error);
  170. }
  171. }
  172. Future<String> _handleJumpAppPage(MethodCall call) async {
  173. try {
  174. final String path = call.arguments['path'];
  175. final dynamic args = call.arguments['args'];
  176. final dynamic offAll = call.arguments['offAll'] ?? false;
  177. Map<String, dynamic> parsedArgs = {};
  178. if (args is String && args.isNotEmpty) {
  179. try {
  180. parsedArgs = Map<String, dynamic>.from(json.decode(args));
  181. } catch (e) {
  182. return '{}';
  183. }
  184. } else if (args is Map) {
  185. parsedArgs = Map<String, dynamic>.from(args);
  186. }
  187. // 是否跳转到新页面后,并清除上层的所有页面
  188. if (offAll) {
  189. Get.offAllNamed(
  190. path,
  191. parameters: parsedArgs.map((k, v) => MapEntry(k, v.toString())),
  192. );
  193. } else {
  194. // 普通路由跳转
  195. Get.toNamed(
  196. path,
  197. parameters: parsedArgs.map((k, v) => MapEntry(k, v.toString())),
  198. );
  199. }
  200. return '{}';
  201. } catch (e) {
  202. print("跳转失败: $e");
  203. return '{}';
  204. }
  205. }
  206. String handleError(dynamic error) {
  207. if (error is ServerErrorException) {
  208. AtmobLog.e(tag, "接口请求失败: ${error.message}");
  209. // 抛异常给原生,同时也返回json
  210. throw PlatformException(
  211. code: error.code.toString(),
  212. message: error.message,
  213. details: {'code': error.code, 'message': error.message},
  214. );
  215. } else {
  216. AtmobLog.e(tag, "未知异常: $error");
  217. throw PlatformException(
  218. code: "-1",
  219. message: error.toString(),
  220. details: {'code': -1, 'message': error.toString()},
  221. );
  222. }
  223. }
  224. }