keyboard_method_handler.dart 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/utils/atmob_log.dart';
  8. import '../data/api/response/chat_prologue_response.dart';
  9. import '../data/api/response/chat_super_speak_response.dart';
  10. import '../data/api/response/keyboard_list_response.dart';
  11. import '../data/consts/constants.dart';
  12. import '../utils/http_handler.dart';
  13. import '../utils/mmkv_util.dart';
  14. import '../utils/toast_util.dart';
  15. @injectable
  16. class KeyboardMethodHandler {
  17. final tag = "KeyboardMethodHandler";
  18. // 用处存储选中的键盘id
  19. static const String keyboardSelect = 'keyboard_select';
  20. bool get isLogin => KVUtil.getBool(Constants.keyIsLogin, false);
  21. bool get isMember => KVUtil.getBool(Constants.keyIsMember, false);
  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(
  55. type: type,
  56. );
  57. final selectedKeyboardJson = KVUtil.getString(keyboardSelect, null);
  58. if (selectedKeyboardJson != null) {
  59. try {
  60. final Map<String, dynamic> keyboardMap = jsonDecode(
  61. selectedKeyboardJson,
  62. );
  63. final String? keyboardId = keyboardMap['id'] as String?;
  64. if (keyboardId != null) {
  65. for (var element in keyboardList.keyboardInfos) {
  66. if (element.id == keyboardId) {
  67. element.isSelect = true;
  68. break;
  69. }
  70. }
  71. }
  72. } catch (e) {
  73. AtmobLog.e(tag, "解析本地选中键盘失败: $e");
  74. }
  75. }
  76. keyboardListResponse = keyboardList;
  77. return jsonEncode(keyboardList.toJson());
  78. }
  79. Future<String> _handleSelectedKeyboard(MethodCall call) async {
  80. final String keyboardId = call.arguments['keyboardId'];
  81. if (keyboardListResponse.keyboardInfos.isEmpty) {
  82. keyboardListResponse =
  83. await KeyboardRepository.getInstance().getKeyboardList();
  84. }
  85. final selectedKeyboard = keyboardListResponse.keyboardInfos
  86. .firstWhereOrNull((element) => element.id == keyboardId);
  87. if (selectedKeyboard != null) {
  88. KVUtil.putString(keyboardSelect, jsonEncode(selectedKeyboard.toJson()));
  89. }
  90. return "{}";
  91. }
  92. Future<String> _handleGetCurrentKeyboardInfo(MethodCall call) async {
  93. final String? keyboardJsonStr = KVUtil.getString(keyboardSelect, null);
  94. if (keyboardJsonStr != null) {
  95. try {
  96. final jsonMap = jsonDecode(keyboardJsonStr);
  97. return jsonEncode(jsonMap);
  98. } catch (e) {
  99. AtmobLog.e(tag, "Failed to decode keyboard JSON: $e");
  100. }
  101. }
  102. return "{}";
  103. }
  104. Future<String> _handleGetCharacterList(MethodCall call) async {
  105. final String keyboardId = call.arguments['keyboardId'];
  106. final characterList = await KeyboardRepository.getInstance()
  107. .getKeyboardCharacterList(keyboardId: keyboardId);
  108. return jsonEncode(characterList.toJson());
  109. }
  110. // 获取开场白列表
  111. Future<String> _handleGetPrologueList(MethodCall call) async {
  112. final prologueList =
  113. await KeyboardRepository.getInstance().getPrologueList();
  114. return jsonEncode(prologueList.toJson());
  115. }
  116. // 超会回
  117. Future<String> _handleChatSuperReply(MethodCall call) async {
  118. final String content = call.arguments['content'];
  119. final String keyboardId = call.arguments['keyboardId'];
  120. final String characterId = call.arguments['characterId'];
  121. try {
  122. final chatSuperReplyResponse = await ChatRepository.getInstance()
  123. .chatSuperReply(
  124. content: content,
  125. keyboardId: keyboardId,
  126. characterId: characterId,
  127. );
  128. return jsonEncode(chatSuperReplyResponse.toJson());
  129. } catch (error) {
  130. if (error is ServerErrorException) {
  131. AtmobLog.d(tag, "超会回失败: ${error.message}");
  132. ToastUtil.show(error.message);
  133. } else {
  134. AtmobLog.d(tag, "超会回失败: $error");
  135. }
  136. return '{}';
  137. }
  138. }
  139. // 超会说
  140. Future<String> _handleChatSuperSpeak(MethodCall call) async {
  141. final String content = call.arguments['content'];
  142. final String keyboardId = call.arguments['keyboardId'];
  143. final String characterId = call.arguments['characterId'];
  144. final ChatSuperSpeakResponse chatSuperSpeakResponse;
  145. try {
  146. chatSuperSpeakResponse = await ChatRepository.getInstance()
  147. .chatSuperSpeak(
  148. content: content,
  149. keyboardId: keyboardId,
  150. characterId: characterId,
  151. );
  152. return jsonEncode(chatSuperSpeakResponse.toJson());
  153. } catch (error) {
  154. if (error is ServerErrorException) {
  155. AtmobLog.d(tag, "超会说失败: ${error.message}");
  156. ToastUtil.show(error.message);
  157. } else {
  158. AtmobLog.d(tag, "超会说失败: $error");
  159. }
  160. return '{}';
  161. }
  162. }
  163. // 开场白
  164. Future<String> _handleChatPrologue(MethodCall call) async {
  165. final String name = call.arguments['name'];
  166. final ChatPrologueResponse chatPrologueResponse;
  167. try {
  168. chatPrologueResponse = await ChatRepository.getInstance().chatPrologue(
  169. name: name,
  170. );
  171. return jsonEncode(chatPrologueResponse.toJson());
  172. } catch (error) {
  173. if (error is ServerErrorException) {
  174. ToastUtil.show(error.message);
  175. AtmobLog.i(tag, "开场白失败: ${error.message}");
  176. } else {
  177. AtmobLog.i(tag, "开场白失败: $error");
  178. }
  179. return '{}';
  180. }
  181. }
  182. Future<String> _handleJumpAppPage(MethodCall call) async {
  183. try {
  184. final String path = call.arguments['path'];
  185. final dynamic args = call.arguments['args'];
  186. final dynamic offAll = call.arguments['offAll'] ?? false;
  187. Map<String, dynamic> parsedArgs = {};
  188. if (args is String && args.isNotEmpty) {
  189. try {
  190. parsedArgs = Map<String, dynamic>.from(json.decode(args));
  191. } catch (e) {
  192. return '{}';
  193. }
  194. } else if (args is Map) {
  195. parsedArgs = Map<String, dynamic>.from(args);
  196. }
  197. // 是否跳转到新页面后,并清除上层的所有页面
  198. if (offAll) {
  199. Get.offAllNamed(
  200. path,
  201. parameters: parsedArgs.map((k, v) => MapEntry(k, v.toString())),
  202. );
  203. } else {
  204. // 普通路由跳转
  205. Get.toNamed(
  206. path,
  207. parameters: parsedArgs.map((k, v) => MapEntry(k, v.toString())),
  208. );
  209. }
  210. return '{}';
  211. } catch (e) {
  212. print("跳转失败: $e");
  213. return '{}';
  214. }
  215. }
  216. }