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. 84
      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/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>(AppLifecycleManager(), permanent: true);
// 语言管理器(需要最先初始化)
Get.lazyPut<LanguageManager>(() => LanguageManager(), fenix: true);

9
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('_');

84
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 = AppLifecycleState.resumed.obs;
final Rx<AppLifecycleState> _appLifecycleState =
AppLifecycleState.resumed.obs;
AppLifecycleState get appLifecycleState => _appLifecycleState.value;
// 状态变化流
final _lifecycleStateStream = AppLifecycleState.resumed.obs;
Stream<AppLifecycleState> get lifecycleStateStream => _lifecycleStateStream.stream;
Stream<AppLifecycleState> 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();
}
}
/// 处理系统语言变化
@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