Browse Source

feat(translation): 实现按翻译模式隔离历史记录功能

添加模式隔离机制,使不同翻译模式的历史记录独立存储和加载
初始化时直接设置模式并加载对应历史,优化切换模式时的历史管理
weicu
lxm 11 months ago
parent
commit
941c1d7c43
  1. 13
      lib/modules/translation/controllers/translation_controller.dart
  2. 34
      lib/modules/translation/controllers/translation_history_controller.dart

13
lib/modules/translation/controllers/translation_controller.dart

@ -234,6 +234,8 @@ class TranslationController extends GetxController with WidgetsBindingObserver {
Future<void> 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);

34
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<dynamic> 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<Map<String, dynamic>> 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<dynamic> historyList = jsonDecode(historyJson);
final List<TranslationItem> allHistory =
@ -194,8 +200,8 @@ class TranslationHistoryManager extends GetxController {
final List<TranslationItem> result = <TranslationItem>[];
final Set<String> existingIds = <String>{};
// 添加存储的历史记录
final historyJson = _storage.read(_historyKey);
// 添加存储的历史记录(按当前模式)
final historyJson = _storage.read(_getHistoryKey());
if (historyJson != null) {
final List<dynamic> historyList = jsonDecode(historyJson);
final List<TranslationItem> 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<Map<String, dynamic>> 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<Map<String, dynamic>> updatedHistoryJson =
storedHistory.map((item) => item.toJson()).toList();
_storage.write(_historyKey, jsonEncode(updatedHistoryJson));
_storage.write(_getHistoryKey(), jsonEncode(updatedHistoryJson));
}
translationHistory.refresh();

Loading…
Cancel
Save