|
|
@ -15,6 +15,12 @@ class TranslationHistoryManager extends GetxController { |
|
|
static const int _maxStorageItems = 1000; // 存储限制 |
|
|
static const int _maxStorageItems = 1000; // 存储限制 |
|
|
static const int _maxDisplayItems = 20; // 显示限制 |
|
|
static const int _maxDisplayItems = 20; // 显示限制 |
|
|
|
|
|
|
|
|
|
|
|
String _currentMode = 'simultaneous'; // 当前模式 |
|
|
|
|
|
String _getHistoryKey() => '${_historyKey}_$_currentMode'; // 模式化存储键 |
|
|
|
|
|
void setMode(String mode) { |
|
|
|
|
|
_currentMode = mode; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
final GetStorage _storage = GetStorage(); |
|
|
final GetStorage _storage = GetStorage(); |
|
|
|
|
|
|
|
|
// 翻译历史数据 |
|
|
// 翻译历史数据 |
|
|
@ -40,7 +46,7 @@ class TranslationHistoryManager extends GetxController { |
|
|
/// 加载翻译历史记录 |
|
|
/// 加载翻译历史记录 |
|
|
/// 初始化时只加载最近的显示条数,提高性能 |
|
|
/// 初始化时只加载最近的显示条数,提高性能 |
|
|
void loadTranslationHistory() { |
|
|
void loadTranslationHistory() { |
|
|
final historyJson = _storage.read(_historyKey); |
|
|
final historyJson = _storage.read(_getHistoryKey()); |
|
|
if (historyJson != null) { |
|
|
if (historyJson != null) { |
|
|
try { |
|
|
try { |
|
|
final List<dynamic> historyList = jsonDecode(historyJson); |
|
|
final List<dynamic> historyList = jsonDecode(historyJson); |
|
|
@ -64,15 +70,15 @@ class TranslationHistoryManager extends GetxController { |
|
|
|
|
|
|
|
|
Logger.info('翻译历史加载成功: ${translationHistory.length}条记录'); |
|
|
Logger.info('翻译历史加载成功: ${translationHistory.length}条记录'); |
|
|
} catch (e) { |
|
|
} catch (e) { |
|
|
_storage.remove(_historyKey); |
|
|
_storage.remove(_getHistoryKey()); |
|
|
Logger.error('加载翻译历史失败: ${e.toString()}'); |
|
|
Logger.error('加载翻译历史失败: ${e.toString()}'); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
// 没有历史数据时初始化分页状态 |
|
|
|
|
|
hasMoreHistory.value = false; |
|
|
hasMoreHistory.value = false; |
|
|
currentPage.value = 1; |
|
|
currentPage.value = 1; |
|
|
isLoadingMore.value = false; |
|
|
isLoadingMore.value = false; |
|
|
isLoadingMoreError.value = false; |
|
|
isLoadingMoreError.value = false; |
|
|
|
|
|
translationHistory.clear(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -94,7 +100,7 @@ class TranslationHistoryManager extends GetxController { |
|
|
// 保存到本地存储 |
|
|
// 保存到本地存储 |
|
|
final List<Map<String, dynamic>> historyJson = |
|
|
final List<Map<String, dynamic>> historyJson = |
|
|
historyToSave.map((item) => item.toJson()).toList(); |
|
|
historyToSave.map((item) => item.toJson()).toList(); |
|
|
_storage.write(_historyKey, jsonEncode(historyJson)); |
|
|
_storage.write(_getHistoryKey(), jsonEncode(historyJson)); |
|
|
|
|
|
|
|
|
Logger.info('翻译历史保存成功: 存储${historyToSave.length}条记录'); |
|
|
Logger.info('翻译历史保存成功: 存储${historyToSave.length}条记录'); |
|
|
} catch (e) { |
|
|
} catch (e) { |
|
|
@ -141,7 +147,7 @@ class TranslationHistoryManager extends GetxController { |
|
|
Logger.i(TAG, '正在加载第 $nextPage 页翻译历史...'); |
|
|
Logger.i(TAG, '正在加载第 $nextPage 页翻译历史...'); |
|
|
|
|
|
|
|
|
// 从存储中获取所有历史记录 |
|
|
// 从存储中获取所有历史记录 |
|
|
final historyJson = _storage.read(_historyKey); |
|
|
final historyJson = _storage.read(_getHistoryKey()); |
|
|
if (historyJson != null) { |
|
|
if (historyJson != null) { |
|
|
final List<dynamic> historyList = jsonDecode(historyJson); |
|
|
final List<dynamic> historyList = jsonDecode(historyJson); |
|
|
final List<TranslationItem> allHistory = |
|
|
final List<TranslationItem> allHistory = |
|
|
@ -194,8 +200,8 @@ class TranslationHistoryManager extends GetxController { |
|
|
final List<TranslationItem> result = <TranslationItem>[]; |
|
|
final List<TranslationItem> result = <TranslationItem>[]; |
|
|
final Set<String> existingIds = <String>{}; |
|
|
final Set<String> existingIds = <String>{}; |
|
|
|
|
|
|
|
|
// 添加存储的历史记录 |
|
|
// 添加存储的历史记录(按当前模式) |
|
|
final historyJson = _storage.read(_historyKey); |
|
|
final historyJson = _storage.read(_getHistoryKey()); |
|
|
if (historyJson != null) { |
|
|
if (historyJson != null) { |
|
|
final List<dynamic> historyList = jsonDecode(historyJson); |
|
|
final List<dynamic> historyList = jsonDecode(historyJson); |
|
|
final List<TranslationItem> storedHistory = |
|
|
final List<TranslationItem> storedHistory = |
|
|
@ -235,7 +241,7 @@ class TranslationHistoryManager extends GetxController { |
|
|
/// 清除所有翻译历史 |
|
|
/// 清除所有翻译历史 |
|
|
void clearTranslationHistory() { |
|
|
void clearTranslationHistory() { |
|
|
translationHistory.clear(); |
|
|
translationHistory.clear(); |
|
|
_storage.remove(_historyKey); |
|
|
_storage.remove(_getHistoryKey()); |
|
|
Logger.info('翻译历史已清除'); |
|
|
Logger.info('翻译历史已清除'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -247,8 +253,8 @@ class TranslationHistoryManager extends GetxController { |
|
|
translationHistory.removeWhere((item) => item.sessionId == sessionId); |
|
|
translationHistory.removeWhere((item) => item.sessionId == sessionId); |
|
|
final removedFromMemory = originalCount - translationHistory.length; |
|
|
final removedFromMemory = originalCount - translationHistory.length; |
|
|
|
|
|
|
|
|
// 从存储中删除 |
|
|
// 从存储中删除(按当前模式) |
|
|
final historyJson = _storage.read(_historyKey); |
|
|
final historyJson = _storage.read(_getHistoryKey()); |
|
|
int removedFromStorage = 0; |
|
|
int removedFromStorage = 0; |
|
|
|
|
|
|
|
|
if (historyJson != null) { |
|
|
if (historyJson != null) { |
|
|
@ -263,7 +269,7 @@ class TranslationHistoryManager extends GetxController { |
|
|
// 重新保存 |
|
|
// 重新保存 |
|
|
final List<Map<String, dynamic>> updatedHistoryJson = |
|
|
final List<Map<String, dynamic>> updatedHistoryJson = |
|
|
storedHistory.map((item) => item.toJson()).toList(); |
|
|
storedHistory.map((item) => item.toJson()).toList(); |
|
|
_storage.write(_historyKey, jsonEncode(updatedHistoryJson)); |
|
|
_storage.write(_getHistoryKey(), jsonEncode(updatedHistoryJson)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
translationHistory.refresh(); |
|
|
translationHistory.refresh(); |
|
|
@ -287,8 +293,8 @@ class TranslationHistoryManager extends GetxController { |
|
|
timestamp.millisecondsSinceEpoch); |
|
|
timestamp.millisecondsSinceEpoch); |
|
|
final removedFromMemory = originalCount - translationHistory.length; |
|
|
final removedFromMemory = originalCount - translationHistory.length; |
|
|
|
|
|
|
|
|
// 从存储中删除 |
|
|
// 从存储中删除(按当前模式) |
|
|
final historyJson = _storage.read(_historyKey); |
|
|
final historyJson = _storage.read(_getHistoryKey()); |
|
|
int removedFromStorage = 0; |
|
|
int removedFromStorage = 0; |
|
|
|
|
|
|
|
|
if (historyJson != null) { |
|
|
if (historyJson != null) { |
|
|
@ -306,7 +312,7 @@ class TranslationHistoryManager extends GetxController { |
|
|
// 重新保存 |
|
|
// 重新保存 |
|
|
final List<Map<String, dynamic>> updatedHistoryJson = |
|
|
final List<Map<String, dynamic>> updatedHistoryJson = |
|
|
storedHistory.map((item) => item.toJson()).toList(); |
|
|
storedHistory.map((item) => item.toJson()).toList(); |
|
|
_storage.write(_historyKey, jsonEncode(updatedHistoryJson)); |
|
|
_storage.write(_getHistoryKey(), jsonEncode(updatedHistoryJson)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
translationHistory.refresh(); |
|
|
translationHistory.refresh(); |
|
|
|