keyboard_android_platform.dart 4.1 KB

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