|
|
@@ -1,17 +1,26 @@
|
|
|
import 'dart:convert';
|
|
|
+
|
|
|
+import 'package:collection/collection.dart';
|
|
|
import 'package:flutter/services.dart';
|
|
|
-import 'package:keyboard/data/bean/member_info.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 '../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 '../data/bean/keyboard_info.dart';
|
|
|
+import '../utils/http_handler.dart';
|
|
|
import '../utils/mmkv_util.dart';
|
|
|
+import '../utils/toast_util.dart';
|
|
|
|
|
|
class KeyboardMethodHandler {
|
|
|
+ final tag = "KeyboardMethodHandler";
|
|
|
final KeyboardRepository keyboardRepository;
|
|
|
final CharactersRepository charactersRepository;
|
|
|
final AccountRepository accountRepository;
|
|
|
+ final ChatRepository chatRepository;
|
|
|
|
|
|
// 用处存储选中的键盘id
|
|
|
static const String keyboardSelect = 'keyboard_select';
|
|
|
@@ -25,6 +34,7 @@ class KeyboardMethodHandler {
|
|
|
this.keyboardRepository,
|
|
|
this.charactersRepository,
|
|
|
this.accountRepository,
|
|
|
+ this.chatRepository,
|
|
|
);
|
|
|
|
|
|
Future<dynamic> handleMethodCall(MethodCall call) async {
|
|
|
@@ -43,6 +53,12 @@ class KeyboardMethodHandler {
|
|
|
return isLogin;
|
|
|
case 'isMember':
|
|
|
return isMember;
|
|
|
+ case 'chatSuperReply':
|
|
|
+ return _handleChatSuperReply(call);
|
|
|
+ case 'chatSuperSpeak':
|
|
|
+ return _handleChatSuperSpeak(call);
|
|
|
+ case 'chatPrologue':
|
|
|
+ return _handleChatPrologue(call);
|
|
|
|
|
|
default:
|
|
|
throw MissingPluginException('Not implemented: ${call.method}');
|
|
|
@@ -51,15 +67,25 @@ class KeyboardMethodHandler {
|
|
|
|
|
|
Future<String> _handleGetKeyboardList(MethodCall call) async {
|
|
|
String? type = call.arguments?['type'] as String?;
|
|
|
- final keyboardList = await keyboardRepository.getKeyboardList(type: type);
|
|
|
-
|
|
|
- final selectKeyboardId = KVUtil.getString(keyboardSelect, null);
|
|
|
|
|
|
- if (selectKeyboardId != null) {
|
|
|
- for (var element in keyboardList.keyboardInfos) {
|
|
|
- if (element.id == selectKeyboardId) {
|
|
|
- element.isSelect = true;
|
|
|
+ final keyboardList = await keyboardRepository.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;
|
|
|
@@ -69,20 +95,28 @@ class KeyboardMethodHandler {
|
|
|
Future<String> _handleSelectedKeyboard(MethodCall call) async {
|
|
|
final String keyboardId = call.arguments['keyboardId'];
|
|
|
|
|
|
- if ( keyboardListResponse.keyboardInfos.isEmpty) {
|
|
|
- return jsonEncode({"error": "Keyboard list not initialized"});
|
|
|
+ if (keyboardListResponse.keyboardInfos.isEmpty) {
|
|
|
+ keyboardListResponse = await keyboardRepository.getKeyboardList();
|
|
|
+ }
|
|
|
+ final selectedKeyboard = keyboardListResponse.keyboardInfos
|
|
|
+ .firstWhereOrNull((element) => element.id == keyboardId);
|
|
|
+ if (selectedKeyboard != null) {
|
|
|
+ KVUtil.putString(keyboardSelect, jsonEncode(selectedKeyboard.toJson()));
|
|
|
}
|
|
|
- final selectedKeyboard = keyboardListResponse.keyboardInfos.firstWhere(
|
|
|
- (element) => element.id == keyboardId,
|
|
|
- orElse: () => KeyboardInfo(),
|
|
|
- );
|
|
|
- KVUtil.putString(keyboardSelect, jsonEncode(selectedKeyboard.toJson()));
|
|
|
return "{}";
|
|
|
}
|
|
|
|
|
|
Future<String> _handleGetCurrentKeyboardInfo(MethodCall call) async {
|
|
|
final String? keyboardJsonStr = KVUtil.getString(keyboardSelect, null);
|
|
|
- return keyboardJsonStr ?? "{}";
|
|
|
+ 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 {
|
|
|
@@ -98,4 +132,70 @@ class KeyboardMethodHandler {
|
|
|
final prologueList = await keyboardRepository.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.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.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.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 '{}';
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|