为 audioplayers_android 模块单独设置 Java 1.8,其他模块保持 Java 17

This commit is contained in:
Developer
2026-03-31 03:13:47 +08:00
parent 7c09ade2bb
commit d66dc73349
24 changed files with 367 additions and 527 deletions

View File

@@ -32,7 +32,6 @@ class HistoryController {
.map((item) => Map<String, dynamic>.from(item))
.toList();
} catch (e) {
print('获取历史记录失败: $e');
return [];
}
}
@@ -42,57 +41,42 @@ class HistoryController {
/// 如果诗词已存在,则不会重复添加
/// 返回是否添加成功
static Future<bool> addToHistory(Map<String, dynamic> poetryData) async {
// 防止并发调用
if (_isAdding) {
print('正在添加历史记录,跳过重复调用: ${poetryData['name']}');
return false;
}
_isAdding = true;
try {
print('开始添加历史记录: ${poetryData['name']} (ID: ${poetryData['id']})');
final historyJson = await SQLiteStorageController.getString(
_historyKey,
defaultValue: '[]',
);
final List<dynamic> historyList = json.decode(historyJson);
print('当前历史记录数量: ${historyList.length}');
// 检查是否已存在相同的诗词
final existingIndex = historyList.indexWhere(
(item) => item['id'] == poetryData['id'],
);
if (existingIndex >= 0) {
print('诗词已存在于历史记录中: ${poetryData['name']} (索引: $existingIndex)');
return false;
}
// 添加时间戳和日期
final enrichedPoetryData = Map<String, dynamic>.from(poetryData);
enrichedPoetryData['timestamp'] = DateTime.now().millisecondsSinceEpoch;
enrichedPoetryData['date'] = DateTime.now().toString().split(' ')[0];
// 插入到列表开头
historyList.insert(0, enrichedPoetryData);
// 保持最多指定数量的记录
if (historyList.length > _maxHistoryCount) {
historyList.removeRange(_maxHistoryCount, historyList.length);
}
// 保存到本地存储
final updatedHistoryJson = json.encode(historyList);
await SQLiteStorageController.setString(_historyKey, updatedHistoryJson);
print('已添加到历史记录: ${poetryData['name']} (新数量: ${historyList.length})');
return true;
} catch (e) {
print('添加历史记录失败: $e');
return false;
} finally {
_isAdding = false;
@@ -104,8 +88,6 @@ class HistoryController {
/// 返回是否移除成功
static Future<bool> removeFromHistory(int poetryId) async {
try {
print('开始删除历史记录: 诗词ID $poetryId');
final historyJson = await SQLiteStorageController.getString(
_historyKey,
defaultValue: '[]',
@@ -113,40 +95,21 @@ class HistoryController {
final List<dynamic> historyList = json.decode(historyJson);
final originalLength = historyList.length;
print('删除前历史记录数量: $originalLength');
// 查找匹配的记录
final matchingItems = <int>[];
for (int i = 0; i < historyList.length; i++) {
if (historyList[i]['id'] == poetryId) {
matchingItems.add(i);
}
}
print('找到匹配的记录索引: $matchingItems');
// 移除指定ID的诗词
historyList.removeWhere((item) => item['id'] == poetryId);
if (historyList.length < originalLength) {
// 保存更新后的列表
final updatedHistoryJson = json.encode(historyList);
await SQLiteStorageController.setString(
_historyKey,
updatedHistoryJson,
);
print(
'已从历史记录中移除诗词ID: $poetryId (删除了 ${originalLength - historyList.length} 条记录,剩余 ${historyList.length} 条)',
);
return true;
}
print('未找到要删除的诗词ID: $poetryId');
return false;
} catch (e) {
print('移除历史记录失败: $e');
return false;
}
}
@@ -156,44 +119,30 @@ class HistoryController {
static Future<bool> clearHistory() async {
try {
await SQLiteStorageController.remove(_historyKey);
print('已清空所有历史记录');
return true;
} catch (e) {
print('清空历史记录失败: $e');
return false;
}
}
/// 获取历史记录数量
/// 返回当前历史记录的总数
static Future<int> getHistoryCount() async {
try {
final history = await getHistory();
return history.length;
} catch (e) {
print('获取历史记录数量失败: $e');
return 0;
}
}
/// 检查诗词是否在历史记录中
/// [poetryId] 要检查的诗词ID
/// 返回是否存在
static Future<bool> isInHistory(int poetryId) async {
try {
final history = await getHistory();
return history.any((item) => item['id'] == poetryId);
} catch (e) {
print('检查历史记录失败: $e');
return false;
}
}
/// 搜索历史记录
/// [keyword] 搜索关键词
/// 返回匹配的历史记录列表
static Future<List<Map<String, dynamic>>> searchHistory(
String keyword,
) async {
@@ -215,7 +164,6 @@ class HistoryController {
introduce.contains(lowerKeyword);
}).toList();
} catch (e) {
print('搜索历史记录失败: $e');
return [];
}
}
@@ -285,20 +233,15 @@ class HistoryController {
'topDynasties': Map.fromEntries(sortedDynasties),
};
} catch (e) {
print('获取历史记录统计失败: $e');
return {};
}
}
/// 导出历史记录
/// [format] 导出格式 ('json' | 'csv')
/// 返回导出的字符串
static Future<String> exportHistory({String format = 'json'}) async {
try {
final history = await getHistory();
if (format.toLowerCase() == 'csv') {
// CSV格式导出
final buffer = StringBuffer();
buffer.writeln('ID,诗词名称,朝代,译文,原文,日期,时间戳');
@@ -310,11 +253,9 @@ class HistoryController {
return buffer.toString();
} else {
// JSON格式导出
return json.encode(history);
}
} catch (e) {
print('导出历史记录失败: $e');
return '';
}
}
@@ -336,38 +277,26 @@ class HistoryController {
final List<dynamic> likedList = json.decode(likedJson);
return likedList.map((item) => Map<String, dynamic>.from(item)).toList();
} catch (e) {
print('获取点赞列表失败: $e');
return [];
}
}
/// 添加诗词到点赞列表
/// [poetryData] 要保存的诗词数据
/// 如果诗词已存在,则不会重复添加
/// 返回是否添加成功
static Future<bool> addToLiked(Map<String, dynamic> poetryData) async {
try {
print('开始添加点赞记录: ${poetryData['name']} (ID: ${poetryData['id']})');
final likedJson = await SQLiteStorageController.getString(
_likedKey,
defaultValue: '[]',
);
final List<dynamic> likedList = json.decode(likedJson);
print('当前点赞记录数量: ${likedList.length}');
// 检查是否已存在相同的诗词
final existingIndex = likedList.indexWhere(
(item) => item['id'] == poetryData['id'],
);
if (existingIndex >= 0) {
print('诗词已存在于点赞记录中: ${poetryData['name']} (索引: $existingIndex)');
return false;
}
// 添加时间戳和日期
final enrichedPoetryData = Map<String, dynamic>.from(poetryData);
final now = DateTime.now();
enrichedPoetryData['liked_timestamp'] = now.millisecondsSinceEpoch;
@@ -375,29 +304,19 @@ class HistoryController {
enrichedPoetryData['liked_time'] =
'${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}';
// 插入到列表开头
likedList.insert(0, enrichedPoetryData);
// 保存到本地存储
final updatedLikedJson = json.encode(likedList);
await SQLiteStorageController.setString(_likedKey, updatedLikedJson);
print('已添加到点赞记录: ${poetryData['name']} (新数量: ${likedList.length})');
return true;
} catch (e) {
print('添加点赞记录失败: $e');
return false;
}
}
/// 从点赞列表中移除指定诗词
/// [poetryId] 要移除的诗词ID
/// 返回是否移除成功
static Future<bool> removeLikedPoetry(String poetryId) async {
try {
print('开始删除点赞记录: 诗词ID $poetryId');
final likedJson = await SQLiteStorageController.getString(
_likedKey,
defaultValue: '[]',
@@ -405,67 +324,45 @@ class HistoryController {
final List<dynamic> likedList = json.decode(likedJson);
final originalLength = likedList.length;
print('删除前点赞记录数量: $originalLength');
// 移除指定ID的诗词
likedList.removeWhere((item) => item['id'].toString() == poetryId);
if (likedList.length < originalLength) {
// 保存更新后的列表
final updatedLikedJson = json.encode(likedList);
await SQLiteStorageController.setString(_likedKey, updatedLikedJson);
print(
'已从点赞记录中移除诗词ID: $poetryId (删除了 ${originalLength - likedList.length} 条记录,剩余 ${likedList.length} 条)',
);
return true;
}
print('未找到要删除的诗词ID: $poetryId');
return false;
} catch (e) {
print('移除点赞记录失败: $e');
return false;
}
}
/// 检查诗词是否在点赞列表中
/// [poetryId] 要检查的诗词ID
/// 返回是否存在
static Future<bool> isInLiked(String poetryId) async {
try {
final likedList = await getLikedHistory();
return likedList.any((item) => item['id'].toString() == poetryId);
} catch (e) {
print('检查点赞记录失败: $e');
return false;
}
}
/// 清空所有点赞记录
/// 返回是否清空成功
static Future<bool> clearLikedHistory() async {
try {
await SQLiteStorageController.remove(_likedKey);
print('已清空所有点赞记录');
return true;
} catch (e) {
print('清空点赞记录失败: $e');
return false;
}
}
/// 获取点赞记录数量
/// 返回当前点赞记录的总数
static Future<int> getLikedCount() async {
try {
final likedList = await getLikedHistory();
return likedList.length;
} catch (e) {
print('获取点赞记录数量失败: $e');
return 0;
}
}
@@ -510,7 +407,6 @@ class HistoryController {
return notes;
} catch (e) {
print('获取笔记列表失败: $e');
return _getDefaultNotes();
}
}
@@ -635,16 +531,12 @@ class HistoryController {
final notesJson = json.encode(notes);
await SQLiteStorageController.setString(_notesKey, notesJson);
print('保存笔记成功: $id, 置顶: $pinned, 锁定: $locked, 分类: $cat');
return id;
} catch (e) {
print('保存笔记失败: $e');
return null;
}
}
/// 获取单个笔记
/// [noteId] 笔记ID
static Future<Map<String, dynamic>?> getNote(String noteId) async {
try {
final notes = await getNotes();
@@ -653,7 +545,6 @@ class HistoryController {
orElse: () => <String, dynamic>{},
);
} catch (e) {
print('获取笔记失败: $e');
return null;
}
}
@@ -668,24 +559,18 @@ class HistoryController {
final notesJson = json.encode(notes);
await SQLiteStorageController.setString(_notesKey, notesJson);
print('删除笔记成功: $noteId');
return true;
} catch (e) {
print('删除笔记失败: $e');
return false;
}
}
/// 切换笔记置顶状态
/// [noteId] 笔记ID
/// 返回新的置顶状态
static Future<bool> togglePinNote(String noteId) async {
try {
final notes = await getNotes();
final index = notes.indexWhere((n) => n['id'] == noteId);
if (index == -1) {
print('未找到笔记: $noteId');
return false;
}
@@ -695,34 +580,25 @@ class HistoryController {
final notesJson = json.encode(notes);
await SQLiteStorageController.setString(_notesKey, notesJson);
print('切换置顶状态: $noteId, 新状态: ${!currentPinned}');
return !currentPinned;
} catch (e) {
print('切换置顶状态失败: $e');
return false;
}
}
/// 设置笔记密码
/// [noteId] 笔记ID
/// [password] 密码(为空则取消锁定)
/// 返回是否成功
static Future<bool> setNotePassword(String noteId, String? password) async {
try {
final notes = await getNotes();
final index = notes.indexWhere((n) => n['id'] == noteId);
if (index == -1) {
print('未找到笔记: $noteId');
return false;
}
if (password == null || password.isEmpty) {
// 取消锁定
notes[index]['isLocked'] = false;
notes[index]['password'] = null;
} else {
// 设置密码并锁定
notes[index]['isLocked'] = true;
notes[index]['password'] = password;
}
@@ -730,20 +606,12 @@ class HistoryController {
final notesJson = json.encode(notes);
await SQLiteStorageController.setString(_notesKey, notesJson);
print(
'设置笔记密码成功: $noteId, 锁定: ${password != null && password.isNotEmpty}',
);
return true;
} catch (e) {
print('设置笔记密码失败: $e');
return false;
}
}
/// 验证笔记密码
/// [noteId] 笔记ID
/// [password] 输入的密码
/// 返回是否验证成功
static Future<bool> verifyNotePassword(String noteId, String password) async {
try {
final notes = await getNotes();
@@ -753,30 +621,25 @@ class HistoryController {
);
if (note.isEmpty) {
print('未找到笔记: $noteId');
return false;
}
final storedPassword = note['password'] as String?;
if (storedPassword == null || storedPassword.isEmpty) {
// 没有密码,直接通过
return true;
}
return storedPassword == password;
} catch (e) {
print('验证笔记密码失败: $e');
return false;
}
}
/// 获取笔记数量
static Future<int> getNotesCount() async {
try {
final notes = await getNotes();
return notes.length;
} catch (e) {
print('获取笔记数量失败: $e');
return 0;
}
}

View File

@@ -56,7 +56,7 @@ class LocalCacheManager {
return decoded.cast<Map<String, dynamic>>();
}
} catch (e) {
print('解析缓存数据失败: $e');
// 解析失败
}
return null;

View File

@@ -13,13 +13,8 @@ class SQLiteStorageController {
/// 初始化SharedPreferences
static Future<void> init() async {
try {
print('开始初始化SharedPreferences...');
_prefs = await SharedPreferences.getInstance();
print('SharedPreferences初始化成功');
} catch (e) {
print('SharedPreferences初始化失败: $e');
throw e;
}
}
@@ -37,9 +32,7 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
await instance.setString(key, value);
print('设置字符串值: $key');
} catch (e) {
print('设置字符串值失败: $e');
throw e;
}
}
@@ -52,10 +45,8 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
final value = instance.getString(key) ?? defaultValue;
print('获取字符串值: $key = $value');
return value;
} catch (e) {
print('获取字符串值失败: $e');
return defaultValue;
}
}
@@ -65,9 +56,7 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
await instance.setInt(key, value);
print('设置整数值: $key = $value');
} catch (e) {
print('设置整数值失败: $e');
throw e;
}
}
@@ -77,10 +66,8 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
final value = instance.getInt(key) ?? defaultValue;
print('获取整数值: $key = $value');
return value;
} catch (e) {
print('获取整数值失败: $e');
return defaultValue;
}
}
@@ -90,9 +77,7 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
await instance.setBool(key, value);
print('设置布尔值: $key = $value');
} catch (e) {
print('设置布尔值失败: $e');
throw e;
}
}
@@ -102,10 +87,8 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
final value = instance.getBool(key) ?? defaultValue;
print('获取布尔值: $key = $value');
return value;
} catch (e) {
print('获取布尔值失败: $e');
return defaultValue;
}
}
@@ -115,9 +98,7 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
await instance.setDouble(key, value);
print('设置双精度值: $key = $value');
} catch (e) {
print('设置双精度值失败: $e');
throw e;
}
}
@@ -130,10 +111,8 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
final value = instance.getDouble(key) ?? defaultValue;
print('获取双精度值: $key = $value');
return value;
} catch (e) {
print('获取双精度值失败: $e');
return defaultValue;
}
}
@@ -143,9 +122,7 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
await instance.setStringList(key, value);
print('设置字符串列表: $key (数量: ${value.length})');
} catch (e) {
print('设置字符串列表失败: $e');
throw e;
}
}
@@ -158,10 +135,8 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
final value = instance.getStringList(key) ?? defaultValue ?? [];
print('获取字符串列表: $key (数量: ${value.length})');
return value;
} catch (e) {
print('获取字符串列表失败: $e');
return defaultValue ?? [];
}
}
@@ -171,9 +146,7 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
await instance.remove(key);
print('移除存储值: $key');
} catch (e) {
print('移除存储值失败: $e');
throw e;
}
}
@@ -183,9 +156,7 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
await instance.clear();
print('清空所有存储');
} catch (e) {
print('清空存储失败: $e');
throw e;
}
}
@@ -195,10 +166,8 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
final keys = instance.getKeys();
print('获取存储键: ${keys.length}');
return keys;
} catch (e) {
print('获取存储键失败: $e');
return {};
}
}
@@ -208,10 +177,8 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
final exists = instance.containsKey(key);
print('检查键存在: $key = $exists');
return exists;
} catch (e) {
print('检查键存在失败: $e');
return false;
}
}
@@ -221,9 +188,8 @@ class SQLiteStorageController {
try {
final instance = await _getInstance();
await instance.reload();
print('重新加载存储数据');
} catch (e) {
print('重新加载数据失败: $e');
// 忽略错误
}
}
}