|
|
@ -2,8 +2,11 @@ import 'dart:async'; |
|
|
import 'package:flutter/services.dart'; |
|
|
import 'package:flutter/services.dart'; |
|
|
import 'package:floating_ui_plugin/native_plugin.dart'; |
|
|
import 'package:floating_ui_plugin/native_plugin.dart'; |
|
|
import 'package:get/get.dart'; |
|
|
import 'package:get/get.dart'; |
|
|
|
|
|
import 'package:get_storage/get_storage.dart'; |
|
|
import '../utils/logger.dart'; |
|
|
import '../utils/logger.dart'; |
|
|
import '../utils/overlay_permission_util.dart'; |
|
|
import '../utils/overlay_permission_util.dart'; |
|
|
|
|
|
import '../../routes/app_routes.dart'; |
|
|
|
|
|
import '../../modules/translation/controllers/translation_controller.dart'; |
|
|
|
|
|
|
|
|
/// 悬浮窗状态 |
|
|
/// 悬浮窗状态 |
|
|
enum FloatingWindowState { |
|
|
enum FloatingWindowState { |
|
|
@ -48,6 +51,9 @@ class FloatingWindowManager extends GetxController { |
|
|
static MethodChannel get _channel => TranslationFloatingWindowApi.channel; |
|
|
static MethodChannel get _channel => TranslationFloatingWindowApi.channel; |
|
|
|
|
|
|
|
|
final TranslationFloatingWindowApi _api = TranslationFloatingWindowApi(); |
|
|
final TranslationFloatingWindowApi _api = TranslationFloatingWindowApi(); |
|
|
|
|
|
static const String _lastTranslationModeKey = |
|
|
|
|
|
'floating_window_last_translation_mode'; |
|
|
|
|
|
final GetStorage _storage = GetStorage(); |
|
|
|
|
|
|
|
|
final _isEnabled = false.obs; |
|
|
final _isEnabled = false.obs; |
|
|
final _currentState = FloatingWindowState.hidden.obs; |
|
|
final _currentState = FloatingWindowState.hidden.obs; |
|
|
@ -79,7 +85,7 @@ class FloatingWindowManager extends GetxController { |
|
|
break; |
|
|
break; |
|
|
case 'onFloatingWindowClicked': |
|
|
case 'onFloatingWindowClicked': |
|
|
// 悬浮窗被点击,可以执行相应操作(如返回主应用) |
|
|
// 悬浮窗被点击,可以执行相应操作(如返回主应用) |
|
|
_handleFloatingWindowClick(); |
|
|
await _handleFloatingWindowClick(); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -194,9 +200,33 @@ class FloatingWindowManager extends GetxController { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// 处理悬浮窗点击事件 |
|
|
/// 处理悬浮窗点击事件 |
|
|
void _handleFloatingWindowClick() { |
|
|
Future<void> _handleFloatingWindowClick() async { |
|
|
// 这里可以实现点击悬浮窗后的操作,比如返回主应用 |
|
|
final String mode = getLastTranslationMode(); |
|
|
Logger.info('悬浮窗被点击'); |
|
|
Logger.info('悬浮窗语言区域被点击,返回翻译页面: $mode'); |
|
|
// 可以发送事件给其他组件处理 |
|
|
|
|
|
|
|
|
if (Get.isRegistered<TranslationController>()) { |
|
|
|
|
|
final controller = Get.find<TranslationController>(); |
|
|
|
|
|
if (controller.currentMode.value != mode) { |
|
|
|
|
|
await controller.changeTranslationMode(mode); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (Get.currentRoute != Routes.translation) { |
|
|
|
|
|
Get.toNamed(Routes.translation, arguments: {'translationId': mode}); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void setLastTranslationMode(String mode) { |
|
|
|
|
|
_storage.write(_lastTranslationModeKey, _normalizeTranslationMode(mode)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String getLastTranslationMode() { |
|
|
|
|
|
final String? mode = _storage.read<String>(_lastTranslationModeKey); |
|
|
|
|
|
return _normalizeTranslationMode(mode); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String _normalizeTranslationMode(String? mode) { |
|
|
|
|
|
if (mode == 'audioVideo' || mode == 'call') return mode!; |
|
|
|
|
|
return 'call'; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|