keyboard_android_platform.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import 'dart:convert';
  2. import 'package:flutter/services.dart';
  3. import 'package:flutter/widgets.dart';
  4. import 'package:injectable/injectable.dart';
  5. import 'package:keyboard/data/repository/chat_repository.dart';
  6. import 'package:keyboard/utils/atmob_log.dart';
  7. import 'package:keyboard_android/keyboard_android.dart';
  8. import '../data/bean/stream_deepseek_data.dart' as deepseek_data;
  9. import '../di/get_it.dart';
  10. @lazySingleton
  11. class KeyboardAndroidPlatform {
  12. static const MethodChannel _channel = MethodChannel('keyboard_android');
  13. var tag = "KeyboardAndroidPlatform";
  14. final ChatRepository chatRepository;
  15. KeyboardAndroidPlatform(this.chatRepository) {
  16. AtmobLog.d(tag, '$tag....init');
  17. init();
  18. }
  19. void init() {
  20. _channel.setMethodCallHandler((MethodCall call) async {
  21. switch (call.method) {
  22. case 'getKeyMappings':
  23. // 返回键映射
  24. return [
  25. {'label': 'A', 'method': 'MethodA'},
  26. {'label': 'B', 'method': 'MethodB'},
  27. {'label': 'C', 'method': 'MethodC'},
  28. {'label': 'D', 'method': 'MethodD'},
  29. {'label': 'E', 'method': 'MethodE'},
  30. ];
  31. case 'sendDynamicTextRequest':
  32. String method = call.arguments['method'];
  33. String currentContent = call.arguments['currentContent'];
  34. AtmobLog.d(tag, 'method: $method, currentContent: $currentContent');
  35. sendDynamicTextRequest(method, currentContent);
  36. break;
  37. default:
  38. throw MissingPluginException('Not implemented');
  39. }
  40. });
  41. }
  42. Future<void> sendDynamicTextRequest(
  43. String method,
  44. String currentContent,
  45. ) async {
  46. switch (method) {
  47. case 'MethodA':
  48. AtmobLog.d(tag, 'MethodA');
  49. deepSeek(message: currentContent);
  50. break;
  51. default:
  52. AtmobLog.d(tag, 'default');
  53. }
  54. }
  55. void enableFloatingWindow(bool enable) {
  56. debugPrint('enableFloatingWindow $enable');
  57. KeyboardAndroid.enableFloatingWindow(enable);
  58. }
  59. void openInputMethodSettings() {
  60. debugPrint('openInputMethodSettings');
  61. KeyboardAndroid.openInputMethodSettings();
  62. }
  63. Future<bool> isTargetKeyboardEnabled() {
  64. var enable = KeyboardAndroid.isTargetKeyboardEnabled();
  65. debugPrint('isTargetKeyboardEnabled value : $enable');
  66. return enable;
  67. }
  68. void inputText(String text) {
  69. debugPrint('inputText $text');
  70. _channel.invokeMethod('inputText', {'text': text});
  71. }
  72. Future<void> deepSeek({required String message}) async {
  73. try {
  74. // 获取数据流
  75. var stream = await chatRepository.streamDeepSeek(message);
  76. // 创建一个 StringBuffer 来拼接流中的字符串数据
  77. StringBuffer responseBuffer = StringBuffer();
  78. // 监听流
  79. await for (var event in stream) {
  80. try {
  81. // 解析JSON数据
  82. Map<String, dynamic> json = jsonDecode(event.data);
  83. // 如果数据不为空,则添加到 responseBuffer
  84. if (json.isNotEmpty) {
  85. deepseek_data.StreamDeepseekData data = deepseek_data
  86. .StreamDeepseekData.fromJson(json);
  87. if (data.choices == null || data.choices!.isEmpty) {
  88. AtmobLog.d(tag, "data.choices == null || data.choices!.isEmpty");
  89. }
  90. deepseek_data.Delta? delta = data.choices![0].delta;
  91. if (delta == null) {
  92. AtmobLog.d(tag, "delta == null");
  93. return;
  94. }
  95. // 有内容时才输入
  96. if (delta.content != null) {
  97. inputText(delta.content!);
  98. }
  99. responseBuffer.write(delta.content ?? '');
  100. }
  101. } catch (e) {
  102. // 捕获 JSON 解析错误
  103. print('Error parsing JSON: $e');
  104. }
  105. }
  106. AtmobLog.d(tag, "responseBuffer.toString() ${responseBuffer.toString()}");
  107. } catch (error) {
  108. // 捕获请求错误
  109. AtmobLog.d(tag, "Error in deepSeek: $error");
  110. }
  111. }
  112. static KeyboardAndroidPlatform getInstance() {
  113. return getIt.get<KeyboardAndroidPlatform>();
  114. }
  115. }