Browse Source

feat(生命周期管理): 添加应用生命周期管理器并处理系统语言变化

添加 AppLifecycleManager 用于监控应用前后台状态变化
实现系统语言变化监听,根据用户设置决定是否跟随系统语言
newdev_shunjiawei
lxm 1 year ago
parent
commit
c0e49f0355
  1. 3
      lib/core/bindings/initial_binding.dart
  2. 9
      lib/core/translations/app_translations.dart
  3. 62
      lib/core/utils/app_lifecycle_manager.dart

3
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/account_service.dart';
import '../../data/services/version_update_service.dart'; import '../../data/services/version_update_service.dart';
import '../../data/services/ota_check_service.dart'; import '../../data/services/ota_check_service.dart';
import '../../core/utils/app_lifecycle_manager.dart';
/// 初始绑定,用于管理全局依赖 /// 初始绑定,用于管理全局依赖
class InitialBinding extends Bindings { class InitialBinding extends Bindings {
@override @override
void dependencies() { void dependencies() {
Logger.warning('InitialBinding dependencies'); Logger.warning('InitialBinding dependencies');
// 应用生命周期管理器
Get.put<AppLifecycleManager>(AppLifecycleManager(), permanent: true);
// 语言管理器(需要最先初始化) // 语言管理器(需要最先初始化)
Get.lazyPut<LanguageManager>(() => LanguageManager(), fenix: true); Get.lazyPut<LanguageManager>(() => LanguageManager(), fenix: true);

9
lib/core/translations/app_translations.dart

@ -88,6 +88,7 @@ class AppTranslations extends Translations {
// 默认使用简体中文 // 默认使用简体中文
return fallbackLocale; return fallbackLocale;
} }
static final supportedLocales = [ static final supportedLocales = [
const Locale('zh', 'CN'), // 简体中文 const Locale('zh', 'CN'), // 简体中文
const Locale('zh', 'HK'), // 香港繁体中文 const Locale('zh', 'HK'), // 香港繁体中文
@ -165,6 +166,14 @@ class AppTranslations extends Translations {
final GetStorage storage = GetStorage(); final GetStorage storage = GetStorage();
String? language = storage.read('language'); 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') { if (language != null && language != 'system') {
List languageList = language.split('_'); List languageList = language.split('_');

62
lib/core/utils/app_lifecycle_manager.dart

@ -1,18 +1,23 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'logger.dart'; import 'logger.dart';
import '../translations/app_translations.dart';
//import '../../modules/settings/controllers/settings_controller.dart';
/// 应用生命周期管理器 /// 应用生命周期管理器
/// ///
/// 监控应用的前台/后台状态变化,并在状态变化时执行相应操作 /// 监控应用的前台/后台状态变化,并在状态变化时执行相应操作
class AppLifecycleManager extends GetxService with WidgetsBindingObserver { class AppLifecycleManager extends GetxService with WidgetsBindingObserver {
// 当前应用生命周期状态 // 当前应用生命周期状态
final Rx<AppLifecycleState> _appLifecycleState = AppLifecycleState.resumed.obs; final Rx<AppLifecycleState> _appLifecycleState =
AppLifecycleState.resumed.obs;
AppLifecycleState get appLifecycleState => _appLifecycleState.value; AppLifecycleState get appLifecycleState => _appLifecycleState.value;
// 状态变化流 // 状态变化流
final _lifecycleStateStream = AppLifecycleState.resumed.obs; final _lifecycleStateStream = AppLifecycleState.resumed.obs;
Stream<AppLifecycleState> get lifecycleStateStream => _lifecycleStateStream.stream; Stream<AppLifecycleState> get lifecycleStateStream =>
_lifecycleStateStream.stream;
// 是否在前台 // 是否在前台
final RxBool _isInForeground = true.obs; final RxBool _isInForeground = true.obs;
@ -55,7 +60,7 @@ class AppLifecycleManager extends GetxService with WidgetsBindingObserver {
void _handleAppResumed() { void _handleAppResumed() {
// Logger.info('应用进入前台'); // Logger.info('应用进入前台');
// 这里不需要主动恢复服务,因为我们希望用户手动控制录音开始 // 这里不需要主动恢复服务,因为我们希望用户手动控制录音开始
// 如果正在录音,停止录音 // 如果正在录音,停止录音
// if (asrService.isRecording) { // if (asrService.isRecording) {
// Logger.info('应用进入后台,停止语音识别'); // Logger.info('应用进入后台,停止语音识别');
@ -88,4 +93,55 @@ class AppLifecycleManager extends GetxService with WidgetsBindingObserver {
// Logger.info('AppLifecycleManager 已关闭'); // Logger.info('AppLifecycleManager 已关闭');
super.onClose(); super.onClose();
} }
/// 处理系统语言变化
@override
void didChangeLocales(List<Locale>? 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<SettingsController>();
// 这里可能需要根据新的locale更新settingsController的language状态
} catch (e) {
Logger.warning('未找到SettingsController实例: $e');
}
}
}
}
} }
Loading…
Cancel
Save