重构
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
/// 最新变化: 添加笔记管理功能
|
||||
|
||||
import 'dart:convert';
|
||||
import 'sqlite_storage_controller.dart';
|
||||
import 'shared_preferences_storage_controller.dart';
|
||||
|
||||
/// 历史记录控制器类
|
||||
/// 负责管理诗词浏览历史记录和点赞记录的本地存储和读取
|
||||
@@ -19,7 +19,7 @@ class HistoryController {
|
||||
/// 返回按时间倒序排列的诗词历史记录
|
||||
static Future<List<Map<String, dynamic>>> getHistory() async {
|
||||
try {
|
||||
final historyJson = await SQLiteStorageController.getString(
|
||||
final historyJson = await SharedPreferencesStorageController.getString(
|
||||
_historyKey,
|
||||
defaultValue: '[]',
|
||||
);
|
||||
@@ -48,7 +48,7 @@ class HistoryController {
|
||||
_isAdding = true;
|
||||
|
||||
try {
|
||||
final historyJson = await SQLiteStorageController.getString(
|
||||
final historyJson = await SharedPreferencesStorageController.getString(
|
||||
_historyKey,
|
||||
defaultValue: '[]',
|
||||
);
|
||||
@@ -73,7 +73,10 @@ class HistoryController {
|
||||
}
|
||||
|
||||
final updatedHistoryJson = json.encode(historyList);
|
||||
await SQLiteStorageController.setString(_historyKey, updatedHistoryJson);
|
||||
await SharedPreferencesStorageController.setString(
|
||||
_historyKey,
|
||||
updatedHistoryJson,
|
||||
);
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
@@ -88,7 +91,7 @@ class HistoryController {
|
||||
/// 返回是否移除成功
|
||||
static Future<bool> removeFromHistory(int poetryId) async {
|
||||
try {
|
||||
final historyJson = await SQLiteStorageController.getString(
|
||||
final historyJson = await SharedPreferencesStorageController.getString(
|
||||
_historyKey,
|
||||
defaultValue: '[]',
|
||||
);
|
||||
@@ -100,7 +103,7 @@ class HistoryController {
|
||||
|
||||
if (historyList.length < originalLength) {
|
||||
final updatedHistoryJson = json.encode(historyList);
|
||||
await SQLiteStorageController.setString(
|
||||
await SharedPreferencesStorageController.setString(
|
||||
_historyKey,
|
||||
updatedHistoryJson,
|
||||
);
|
||||
@@ -118,7 +121,7 @@ class HistoryController {
|
||||
/// 返回是否清空成功
|
||||
static Future<bool> clearHistory() async {
|
||||
try {
|
||||
await SQLiteStorageController.remove(_historyKey);
|
||||
await SharedPreferencesStorageController.remove(_historyKey);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
@@ -266,7 +269,7 @@ class HistoryController {
|
||||
/// 返回点赞的诗词列表
|
||||
static Future<List<Map<String, dynamic>>> getLikedHistory() async {
|
||||
try {
|
||||
final likedJson = await SQLiteStorageController.getString(
|
||||
final likedJson = await SharedPreferencesStorageController.getString(
|
||||
_likedKey,
|
||||
defaultValue: '[]',
|
||||
);
|
||||
@@ -283,7 +286,7 @@ class HistoryController {
|
||||
|
||||
static Future<bool> addToLiked(Map<String, dynamic> poetryData) async {
|
||||
try {
|
||||
final likedJson = await SQLiteStorageController.getString(
|
||||
final likedJson = await SharedPreferencesStorageController.getString(
|
||||
_likedKey,
|
||||
defaultValue: '[]',
|
||||
);
|
||||
@@ -307,7 +310,10 @@ class HistoryController {
|
||||
likedList.insert(0, enrichedPoetryData);
|
||||
|
||||
final updatedLikedJson = json.encode(likedList);
|
||||
await SQLiteStorageController.setString(_likedKey, updatedLikedJson);
|
||||
await SharedPreferencesStorageController.setString(
|
||||
_likedKey,
|
||||
updatedLikedJson,
|
||||
);
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
@@ -317,7 +323,7 @@ class HistoryController {
|
||||
|
||||
static Future<bool> removeLikedPoetry(String poetryId) async {
|
||||
try {
|
||||
final likedJson = await SQLiteStorageController.getString(
|
||||
final likedJson = await SharedPreferencesStorageController.getString(
|
||||
_likedKey,
|
||||
defaultValue: '[]',
|
||||
);
|
||||
@@ -329,7 +335,10 @@ class HistoryController {
|
||||
|
||||
if (likedList.length < originalLength) {
|
||||
final updatedLikedJson = json.encode(likedList);
|
||||
await SQLiteStorageController.setString(_likedKey, updatedLikedJson);
|
||||
await SharedPreferencesStorageController.setString(
|
||||
_likedKey,
|
||||
updatedLikedJson,
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -351,7 +360,7 @@ class HistoryController {
|
||||
|
||||
static Future<bool> clearLikedHistory() async {
|
||||
try {
|
||||
await SQLiteStorageController.remove(_likedKey);
|
||||
await SharedPreferencesStorageController.remove(_likedKey);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
@@ -385,7 +394,9 @@ class HistoryController {
|
||||
/// 返回笔记列表(置顶的排在前面)
|
||||
static Future<List<Map<String, dynamic>>> getNotes() async {
|
||||
try {
|
||||
final notesJson = await SQLiteStorageController.getString(_notesKey);
|
||||
final notesJson = await SharedPreferencesStorageController.getString(
|
||||
_notesKey,
|
||||
);
|
||||
if (notesJson.isEmpty) {
|
||||
// 返回默认笔记示例
|
||||
return _getDefaultNotes();
|
||||
@@ -529,7 +540,7 @@ class HistoryController {
|
||||
}
|
||||
|
||||
final notesJson = json.encode(notes);
|
||||
await SQLiteStorageController.setString(_notesKey, notesJson);
|
||||
await SharedPreferencesStorageController.setString(_notesKey, notesJson);
|
||||
|
||||
return id;
|
||||
} catch (e) {
|
||||
@@ -557,7 +568,7 @@ class HistoryController {
|
||||
notes.removeWhere((n) => n['id'] == noteId);
|
||||
|
||||
final notesJson = json.encode(notes);
|
||||
await SQLiteStorageController.setString(_notesKey, notesJson);
|
||||
await SharedPreferencesStorageController.setString(_notesKey, notesJson);
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
@@ -578,7 +589,7 @@ class HistoryController {
|
||||
notes[index]['isPinned'] = !currentPinned;
|
||||
|
||||
final notesJson = json.encode(notes);
|
||||
await SQLiteStorageController.setString(_notesKey, notesJson);
|
||||
await SharedPreferencesStorageController.setString(_notesKey, notesJson);
|
||||
|
||||
return !currentPinned;
|
||||
} catch (e) {
|
||||
@@ -604,7 +615,7 @@ class HistoryController {
|
||||
}
|
||||
|
||||
final notesJson = json.encode(notes);
|
||||
await SQLiteStorageController.setString(_notesKey, notesJson);
|
||||
await SharedPreferencesStorageController.setString(_notesKey, notesJson);
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/// 时间: 2025-03-25
|
||||
/// 功能: SharedPreferences存储控制器
|
||||
/// 介绍: 使用SharedPreferences实现本地存储功能,替代SQLite
|
||||
/// 最新变化: 将SQLite替换为SharedPreferences实现
|
||||
/// 介绍: 使用SharedPreferences实现本地存储功能
|
||||
/// 最新变化: 重命名为SharedPreferencesStorageController
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// SharedPreferences存储控制器类
|
||||
/// 负责管理本地键值对存储,基于SharedPreferences实现
|
||||
class SQLiteStorageController {
|
||||
class SharedPreferencesStorageController {
|
||||
static SharedPreferences? _prefs;
|
||||
|
||||
/// 初始化SharedPreferences
|
||||
Reference in New Issue
Block a user