diff --git a/lib/modules/translation/controllers/translation_controller.dart b/lib/modules/translation/controllers/translation_controller.dart index 30aef106f..79cac6ab1 100644 --- a/lib/modules/translation/controllers/translation_controller.dart +++ b/lib/modules/translation/controllers/translation_controller.dart @@ -234,6 +234,8 @@ class TranslationController extends GetxController with WidgetsBindingObserver { Future changeTranslationMode(String mode) async { currentMode.value = mode; currentModeTitle.value = _getModeTitle(mode); + _historyManager.setMode(mode); + loadTranslationHistory(); //stopAll(); // 检查悬浮窗支持情况 @@ -397,7 +399,11 @@ class TranslationController extends GetxController with WidgetsBindingObserver { targetLanguageCode.value = targetAsrCode; } - // 加载历史记录 + // 初始化翻译模式(用于历史隔离的初始加载) + final initMode = Get.arguments?['translationId'] ?? 'simultaneous'; + _historyManager.setMode(initMode); + + // 按初始模式加载历史 loadTranslationHistory(); // 初始化服务 @@ -406,9 +412,8 @@ class TranslationController extends GetxController with WidgetsBindingObserver { // 初始化历史管理器 _historyManager.initialize(); - // 初始化翻译模式,根据跳转参数 - final mode = Get.arguments?['translationId'] ?? 'simultaneous'; - changeTranslationMode(mode); + // 应用模式逻辑(含ASR/TTS配置等) + changeTranslationMode(initMode); // 监听应用生命周期:当应用切到后台/失焦时兜底重置 activeSpeaker WidgetsBinding.instance.addObserver(this); diff --git a/lib/modules/translation/controllers/translation_history_controller.dart b/lib/modules/translation/controllers/translation_history_controller.dart index b7e7d15b3..f94f1f7a6 100644 --- a/lib/modules/translation/controllers/translation_history_controller.dart +++ b/lib/modules/translation/controllers/translation_history_controller.dart @@ -15,6 +15,12 @@ class TranslationHistoryManager extends GetxController { static const int _maxStorageItems = 1000; // 存储限制 static const int _maxDisplayItems = 20; // 显示限制 + String _currentMode = 'simultaneous'; // 当前模式 + String _getHistoryKey() => '${_historyKey}_$_currentMode'; // 模式化存储键 + void setMode(String mode) { + _currentMode = mode; + } + final GetStorage _storage = GetStorage(); // 翻译历史数据 @@ -40,7 +46,7 @@ class TranslationHistoryManager extends GetxController { /// 加载翻译历史记录 /// 初始化时只加载最近的显示条数,提高性能 void loadTranslationHistory() { - final historyJson = _storage.read(_historyKey); + final historyJson = _storage.read(_getHistoryKey()); if (historyJson != null) { try { final List historyList = jsonDecode(historyJson); @@ -64,15 +70,15 @@ class TranslationHistoryManager extends GetxController { Logger.info('翻译历史加载成功: ${translationHistory.length}条记录'); } catch (e) { - _storage.remove(_historyKey); + _storage.remove(_getHistoryKey()); Logger.error('加载翻译历史失败: ${e.toString()}'); } } else { - // 没有历史数据时初始化分页状态 hasMoreHistory.value = false; currentPage.value = 1; isLoadingMore.value = false; isLoadingMoreError.value = false; + translationHistory.clear(); } } @@ -94,7 +100,7 @@ class TranslationHistoryManager extends GetxController { // 保存到本地存储 final List> historyJson = historyToSave.map((item) => item.toJson()).toList(); - _storage.write(_historyKey, jsonEncode(historyJson)); + _storage.write(_getHistoryKey(), jsonEncode(historyJson)); Logger.info('翻译历史保存成功: 存储${historyToSave.length}条记录'); } catch (e) { @@ -141,7 +147,7 @@ class TranslationHistoryManager extends GetxController { Logger.i(TAG, '正在加载第 $nextPage 页翻译历史...'); // 从存储中获取所有历史记录 - final historyJson = _storage.read(_historyKey); + final historyJson = _storage.read(_getHistoryKey()); if (historyJson != null) { final List historyList = jsonDecode(historyJson); final List allHistory = @@ -194,8 +200,8 @@ class TranslationHistoryManager extends GetxController { final List result = []; final Set existingIds = {}; - // 添加存储的历史记录 - final historyJson = _storage.read(_historyKey); + // 添加存储的历史记录(按当前模式) + final historyJson = _storage.read(_getHistoryKey()); if (historyJson != null) { final List historyList = jsonDecode(historyJson); final List storedHistory = @@ -235,7 +241,7 @@ class TranslationHistoryManager extends GetxController { /// 清除所有翻译历史 void clearTranslationHistory() { translationHistory.clear(); - _storage.remove(_historyKey); + _storage.remove(_getHistoryKey()); Logger.info('翻译历史已清除'); } @@ -247,8 +253,8 @@ class TranslationHistoryManager extends GetxController { translationHistory.removeWhere((item) => item.sessionId == sessionId); final removedFromMemory = originalCount - translationHistory.length; - // 从存储中删除 - final historyJson = _storage.read(_historyKey); + // 从存储中删除(按当前模式) + final historyJson = _storage.read(_getHistoryKey()); int removedFromStorage = 0; if (historyJson != null) { @@ -263,7 +269,7 @@ class TranslationHistoryManager extends GetxController { // 重新保存 final List> updatedHistoryJson = storedHistory.map((item) => item.toJson()).toList(); - _storage.write(_historyKey, jsonEncode(updatedHistoryJson)); + _storage.write(_getHistoryKey(), jsonEncode(updatedHistoryJson)); } translationHistory.refresh(); @@ -287,8 +293,8 @@ class TranslationHistoryManager extends GetxController { timestamp.millisecondsSinceEpoch); final removedFromMemory = originalCount - translationHistory.length; - // 从存储中删除 - final historyJson = _storage.read(_historyKey); + // 从存储中删除(按当前模式) + final historyJson = _storage.read(_getHistoryKey()); int removedFromStorage = 0; if (historyJson != null) { @@ -306,7 +312,7 @@ class TranslationHistoryManager extends GetxController { // 重新保存 final List> updatedHistoryJson = storedHistory.map((item) => item.toJson()).toList(); - _storage.write(_historyKey, jsonEncode(updatedHistoryJson)); + _storage.write(_getHistoryKey(), jsonEncode(updatedHistoryJson)); } translationHistory.refresh();