| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- import 'dart:convert';
- import 'package:flutter/services.dart';
- import 'package:keyboard/data/repository/account_repository.dart';
- import 'package:keyboard/data/repository/characters_repository.dart';
- import 'package:keyboard/data/repository/chat_repository.dart';
- import 'package:keyboard/data/repository/keyboard_repository.dart';
- import 'package:keyboard/utils/atmob_log.dart';
- import 'package:get/get.dart';
- import '../data/api/response/chat_prologue_response.dart';
- import '../data/api/response/chat_super_speak_response.dart';
- import '../data/api/response/keyboard_list_response.dart';
- import '../di/get_it.dart';
- import '../utils/http_handler.dart';
- import '../utils/mmkv_util.dart';
- import '../utils/toast_util.dart';
- class KeyboardMethodHandler {
- final tag = "KeyboardMethodHandler";
- // 用处存储选中的键盘id
- static const String keyboardSelect = 'keyboard_select';
- bool get isLogin => AccountRepository.getInstance().isLoginTest();
- bool get isMember => AccountRepository.getInstance().isMember.value;
- late KeyboardListResponse keyboardListResponse;
- KeyboardMethodHandler();
- Future<dynamic> handleMethodCall(MethodCall call) async {
- switch (call.method) {
- case 'getKeyboardList':
- return _handleGetKeyboardList(call);
- case 'selectedKeyboard':
- return _handleSelectedKeyboard(call);
- case 'getCharacterList':
- return _handleGetCharacterList(call);
- case 'getCurrentKeyboardInfo':
- return _handleGetCurrentKeyboardInfo(call);
- case 'getPrologueList':
- return _handleGetPrologueList(call);
- case 'isLogin':
- return isLogin;
- case 'isMember':
- return isMember;
- case 'chatSuperReply':
- return _handleChatSuperReply(call);
- case 'chatSuperSpeak':
- return _handleChatSuperSpeak(call);
- case 'chatPrologue':
- return _handleChatPrologue(call);
- case 'jumpAppPage':
- return _handleJumpAppPage(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.getInstance().getKeyboardList(type: type);
- final selectedKeyboardJson = KVUtil.getString(keyboardSelect, null);
- if (selectedKeyboardJson != null) {
- try {
- final Map<String, dynamic> keyboardMap = jsonDecode(
- selectedKeyboardJson,
- );
- final String? keyboardId = keyboardMap['id'] as String?;
- if (keyboardId != null) {
- for (var element in keyboardList.keyboardInfos) {
- if (element.id == keyboardId) {
- element.isSelect = true;
- break;
- }
- }
- }
- } catch (e) {
- AtmobLog.e(tag, "解析本地选中键盘失败: $e");
- }
- }
- keyboardListResponse = keyboardList;
- return jsonEncode(keyboardList.toJson());
- }
- Future<String> _handleSelectedKeyboard(MethodCall call) async {
- final String keyboardId = call.arguments['keyboardId'];
- if (keyboardListResponse.keyboardInfos.isEmpty) {
- keyboardListResponse = await KeyboardRepository.getInstance().getKeyboardList();
- }
- final selectedKeyboard = keyboardListResponse.keyboardInfos
- .firstWhereOrNull((element) => element.id == keyboardId);
- if (selectedKeyboard != null) {
- KVUtil.putString(keyboardSelect, jsonEncode(selectedKeyboard.toJson()));
- }
- return "{}";
- }
- Future<String> _handleGetCurrentKeyboardInfo(MethodCall call) async {
- final String? keyboardJsonStr = KVUtil.getString(keyboardSelect, null);
- if (keyboardJsonStr != null) {
- try {
- final jsonMap = jsonDecode(keyboardJsonStr);
- return jsonEncode(jsonMap);
- } catch (e) {
- AtmobLog.e(tag, "Failed to decode keyboard JSON: $e");
- }
- }
- return "{}";
- }
- Future<String> _handleGetCharacterList(MethodCall call) async {
- final String keyboardId = call.arguments['keyboardId'];
- final characterList = await KeyboardRepository.getInstance().getKeyboardCharacterList(
- keyboardId: keyboardId,
- );
- return jsonEncode(characterList.toJson());
- }
- // 获取开场白列表
- Future<String> _handleGetPrologueList(MethodCall call) async {
- final prologueList = await KeyboardRepository.getInstance().getPrologueList();
- return jsonEncode(prologueList.toJson());
- }
- // 超会回
- Future<String> _handleChatSuperReply(MethodCall call) async {
- final String content = call.arguments['content'];
- final String keyboardId = call.arguments['keyboardId'];
- final String characterId = call.arguments['characterId'];
- try {
- final chatSuperReplyResponse = await ChatRepository.getInstance().chatSuperReply(
- content: content,
- keyboardId: keyboardId,
- characterId: characterId,
- );
- return jsonEncode(chatSuperReplyResponse.toJson());
- } catch (error) {
- if (error is ServerErrorException) {
- AtmobLog.d(tag, "超会回失败: ${error.message}");
- ToastUtil.show(error.message);
- } else {
- AtmobLog.d(tag, "超会回失败: $error");
- }
- return '{}';
- }
- }
- // 超会说
- Future<String> _handleChatSuperSpeak(MethodCall call) async {
- final String content = call.arguments['content'];
- final String keyboardId = call.arguments['keyboardId'];
- final String characterId = call.arguments['characterId'];
- final ChatSuperSpeakResponse chatSuperSpeakResponse;
- try {
- chatSuperSpeakResponse = await ChatRepository.getInstance().chatSuperSpeak(
- content: content,
- keyboardId: keyboardId,
- characterId: characterId,
- );
- return jsonEncode(chatSuperSpeakResponse.toJson());
- } catch (error) {
- if (error is ServerErrorException) {
- AtmobLog.d(tag, "超会说失败: ${error.message}");
- ToastUtil.show(error.message);
- } else {
- AtmobLog.d(tag, "超会说失败: $error");
- }
- return '{}';
- }
- }
- // 开场白
- Future<String> _handleChatPrologue(MethodCall call) async {
- final String name = call.arguments['name'];
- final ChatPrologueResponse chatPrologueResponse;
- try {
- chatPrologueResponse = await ChatRepository.getInstance().chatPrologue(name: name);
- return jsonEncode(chatPrologueResponse.toJson());
- } catch (error) {
- if (error is ServerErrorException) {
- ToastUtil.show(error.message);
- AtmobLog.i(tag, "开场白失败: ${error.message}");
- } else {
- AtmobLog.i(tag, "开场白失败: $error");
- }
- return '{}';
- }
- }
- Future<String> _handleJumpAppPage(MethodCall call) async {
- try {
- final String path = call.arguments['path'];
- final dynamic args = call.arguments['args'];
- Map<String, dynamic> parsedArgs = {};
- if (args is String && args.isNotEmpty) {
- try {
- parsedArgs = Map<String, dynamic>.from(json.decode(args));
- } catch (e) {
- return '{}';
- }
- } else if (args is Map) {
- parsedArgs = Map<String, dynamic>.from(args);
- }
- Get.toNamed(
- path,
- parameters: parsedArgs.map((k, v) => MapEntry(k, v.toString())),
- );
- return '{}';
- } catch (e) {
- print("跳转失败: $e");
- return '{}';
- }
- }
- }
|