From c0e49f0355be4d971e6c8dddcac79c9ef150fba6 Mon Sep 17 00:00:00 2001 From: lxm <1274844347@qq.com> Date: Sun, 24 Aug 2025 18:44:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F?= =?UTF-8?q?=E7=AE=A1=E7=90=86):=20=E6=B7=BB=E5=8A=A0=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F=E7=AE=A1=E7=90=86=E5=99=A8?= =?UTF-8?q?=E5=B9=B6=E5=A4=84=E7=90=86=E7=B3=BB=E7=BB=9F=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 AppLifecycleManager 用于监控应用前后台状态变化 实现系统语言变化监听,根据用户设置决定是否跟随系统语言 --- lib/core/bindings/initial_binding.dart | 3 + lib/core/translations/app_translations.dart | 9 +++ lib/core/utils/app_lifecycle_manager.dart | 84 +++++++++++++++++---- 3 files changed, 82 insertions(+), 14 deletions(-) diff --git a/lib/core/bindings/initial_binding.dart b/lib/core/bindings/initial_binding.dart index 7354a5dc7..00c74738a 100644 --- a/lib/core/bindings/initial_binding.dart +++ b/lib/core/bindings/initial_binding.dart @@ -21,12 +21,15 @@ import '../../data/services/ble_manager.dart'; import '../../data/services/account_service.dart'; import '../../data/services/version_update_service.dart'; import '../../data/services/ota_check_service.dart'; +import '../../core/utils/app_lifecycle_manager.dart'; /// 初始绑定,用于管理全局依赖 class InitialBinding extends Bindings { @override void dependencies() { Logger.warning('InitialBinding dependencies'); + // 应用生命周期管理器 + Get.put(AppLifecycleManager(), permanent: true); // 语言管理器(需要最先初始化) Get.lazyPut(() => LanguageManager(), fenix: true); diff --git a/lib/core/translations/app_translations.dart b/lib/core/translations/app_translations.dart index feee5eb94..cf89b513a 100644 --- a/lib/core/translations/app_translations.dart +++ b/lib/core/translations/app_translations.dart @@ -88,6 +88,7 @@ class AppTranslations extends Translations { // 默认使用简体中文 return fallbackLocale; } + static final supportedLocales = [ const Locale('zh', 'CN'), // 简体中文 const Locale('zh', 'HK'), // 香港繁体中文 @@ -165,6 +166,14 @@ class AppTranslations extends Translations { final GetStorage storage = GetStorage(); String? language = storage.read('language'); + // print('lxm---系统语言发生变化: --------------------------'); + // print('lxm---Get.deviceLocale: ${Get.deviceLocale}'); + // print('lxm---Get.locale: ${Get.locale}'); + + // print('lxm---语言代码: ${Get.deviceLocale?.languageCode}'); + // print('lxm---国家代码: ${Get.deviceLocale?.countryCode}'); + // print('lxm---脚本代码: ${Get.deviceLocale?.scriptCode}'); + // 如果用户手动设置了语言且不是跟随系统 if (language != null && language != 'system') { List languageList = language.split('_'); diff --git a/lib/core/utils/app_lifecycle_manager.dart b/lib/core/utils/app_lifecycle_manager.dart index f1ea2b0b8..6c4e178c5 100644 --- a/lib/core/utils/app_lifecycle_manager.dart +++ b/lib/core/utils/app_lifecycle_manager.dart @@ -1,23 +1,28 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart'; +import 'package:get_storage/get_storage.dart'; import 'logger.dart'; +import '../translations/app_translations.dart'; +//import '../../modules/settings/controllers/settings_controller.dart'; /// 应用生命周期管理器 /// /// 监控应用的前台/后台状态变化,并在状态变化时执行相应操作 class AppLifecycleManager extends GetxService with WidgetsBindingObserver { // 当前应用生命周期状态 - final Rx _appLifecycleState = AppLifecycleState.resumed.obs; + final Rx _appLifecycleState = + AppLifecycleState.resumed.obs; AppLifecycleState get appLifecycleState => _appLifecycleState.value; - + // 状态变化流 final _lifecycleStateStream = AppLifecycleState.resumed.obs; - Stream get lifecycleStateStream => _lifecycleStateStream.stream; - + Stream get lifecycleStateStream => + _lifecycleStateStream.stream; + // 是否在前台 final RxBool _isInForeground = true.obs; bool get isInForeground => _isInForeground.value; - + @override void onInit() { super.onInit(); @@ -25,15 +30,15 @@ class AppLifecycleManager extends GetxService with WidgetsBindingObserver { WidgetsBinding.instance.addObserver(this); Logger.info('AppLifecycleManager 已初始化'); } - + @override void didChangeAppLifecycleState(AppLifecycleState state) { final previousState = _appLifecycleState.value; _appLifecycleState.value = state; _lifecycleStateStream.value = state; - + // Logger.info('应用生命周期状态变化: $previousState -> $state'); - + // 根据状态变化更新前台/后台标志 switch (state) { case AppLifecycleState.resumed: @@ -50,12 +55,12 @@ class AppLifecycleManager extends GetxService with WidgetsBindingObserver { break; } } - + /// 处理应用进入前台 void _handleAppResumed() { // Logger.info('应用进入前台'); // 这里不需要主动恢复服务,因为我们希望用户手动控制录音开始 - // 如果正在录音,停止录音 + // 如果正在录音,停止录音 // if (asrService.isRecording) { // Logger.info('应用进入后台,停止语音识别'); @@ -64,11 +69,11 @@ class AppLifecycleManager extends GetxService with WidgetsBindingObserver { // ttsService.disconnect(); // } } - + /// 处理应用进入后台 void _handleAppPaused(AppLifecycleState state) { // Logger.info('应用进入后台: $state'); - + // 获取 VolcanoAsrApiService 实例 // 如果正在录音,停止录音 @@ -80,7 +85,7 @@ class AppLifecycleManager extends GetxService with WidgetsBindingObserver { // } } - + @override void onClose() { // 移除应用生命周期观察者 @@ -88,4 +93,55 @@ class AppLifecycleManager extends GetxService with WidgetsBindingObserver { // Logger.info('AppLifecycleManager 已关闭'); super.onClose(); } -} \ No newline at end of file + + /// 处理系统语言变化 + @override + void didChangeLocales(List? locales) { + super.didChangeLocales(locales); + // print('lxm---系统语言发生变化: ${locales?.first}'); + // print('lxm---Get.deviceLocale: ${Get.deviceLocale}'); + // print('lxm---Get.locale: ${Get.locale}'); + // print( + // 'lxm---AppTranslations.deviceLocale(): ${AppTranslations.deviceLocale()}'); + // print('lxm---语言代码: ${locales?.first.languageCode}'); + // print('lxm---国家代码: ${locales?.first.countryCode}'); + // print('lxm---脚本代码: ${locales?.first.scriptCode}'); + + Logger.info('系统语言发生变化: ${locales?.first}'); + + // 检查用户是否手动设置了语言 + final storage = GetStorage(); + String? userLanguage = storage.read('language'); + + if (userLanguage != null && userLanguage != 'system') { + // 用户手动设置了语言,保持用户选择,不跟随系统 + Logger.info('用户已手动设置语言为: $userLanguage,忽略系统语言变化'); + + // 强制恢复用户设置的语言 + List languageList = userLanguage.split('_'); + Locale userLocale = Locale(languageList[0], languageList[1]); + + // 延迟执行,确保系统语言变化处理完成 + Future.delayed(const Duration(milliseconds: 100), () { + Get.updateLocale(userLocale); + Logger.info('已恢复用户设置的语言: ${userLocale.toString()}'); + }); + } else { + // 用户选择跟随系统,更新应用语言 + Logger.info('用户选择跟随系统语言,更新应用语言'); + + Locale? newLocale = AppTranslations.deviceLocale(); + if (newLocale != null) { + Get.updateLocale(newLocale); + + // 同步更新设置控制器中的语言状态 + try { + //final settingsController = Get.find(); + // 这里可能需要根据新的locale更新settingsController的language状态 + } catch (e) { + Logger.warning('未找到SettingsController实例: $e'); + } + } + } + } +}