Files
wushu/lib/controllers/sqlite_storage_controller.dart
2026-03-30 02:35:31 +08:00

230 lines
6.0 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// 时间: 2025-03-25
/// 功能: SharedPreferences存储控制器
/// 介绍: 使用SharedPreferences实现本地存储功能替代SQLite
/// 最新变化: 将SQLite替换为SharedPreferences实现
import 'package:shared_preferences/shared_preferences.dart';
/// SharedPreferences存储控制器类
/// 负责管理本地键值对存储基于SharedPreferences实现
class SQLiteStorageController {
static SharedPreferences? _prefs;
/// 初始化SharedPreferences
static Future<void> init() async {
try {
print('开始初始化SharedPreferences...');
_prefs = await SharedPreferences.getInstance();
print('SharedPreferences初始化成功');
} catch (e) {
print('SharedPreferences初始化失败: $e');
throw e;
}
}
/// 获取SharedPreferences实例
static Future<SharedPreferences> _getInstance() async {
if (_prefs == null) {
await init();
}
return _prefs!;
}
/// 存储字符串值
static Future<void> setString(String key, String value) async {
try {
final instance = await _getInstance();
await instance.setString(key, value);
print('设置字符串值: $key');
} catch (e) {
print('设置字符串值失败: $e');
throw e;
}
}
/// 获取字符串值
static Future<String> getString(
String key, {
String defaultValue = '',
}) async {
try {
final instance = await _getInstance();
final value = instance.getString(key) ?? defaultValue;
print('获取字符串值: $key = $value');
return value;
} catch (e) {
print('获取字符串值失败: $e');
return defaultValue;
}
}
/// 存储整数值
static Future<void> setInt(String key, int value) async {
try {
final instance = await _getInstance();
await instance.setInt(key, value);
print('设置整数值: $key = $value');
} catch (e) {
print('设置整数值失败: $e');
throw e;
}
}
/// 获取整数值
static Future<int> getInt(String key, {int defaultValue = 0}) async {
try {
final instance = await _getInstance();
final value = instance.getInt(key) ?? defaultValue;
print('获取整数值: $key = $value');
return value;
} catch (e) {
print('获取整数值失败: $e');
return defaultValue;
}
}
/// 存储布尔值
static Future<void> setBool(String key, bool value) async {
try {
final instance = await _getInstance();
await instance.setBool(key, value);
print('设置布尔值: $key = $value');
} catch (e) {
print('设置布尔值失败: $e');
throw e;
}
}
/// 获取布尔值
static Future<bool> getBool(String key, {bool defaultValue = false}) async {
try {
final instance = await _getInstance();
final value = instance.getBool(key) ?? defaultValue;
print('获取布尔值: $key = $value');
return value;
} catch (e) {
print('获取布尔值失败: $e');
return defaultValue;
}
}
/// 存储双精度值
static Future<void> setDouble(String key, double value) async {
try {
final instance = await _getInstance();
await instance.setDouble(key, value);
print('设置双精度值: $key = $value');
} catch (e) {
print('设置双精度值失败: $e');
throw e;
}
}
/// 获取双精度值
static Future<double> getDouble(
String key, {
double defaultValue = 0.0,
}) async {
try {
final instance = await _getInstance();
final value = instance.getDouble(key) ?? defaultValue;
print('获取双精度值: $key = $value');
return value;
} catch (e) {
print('获取双精度值失败: $e');
return defaultValue;
}
}
/// 存储字符串列表
static Future<void> setStringList(String key, List<String> value) async {
try {
final instance = await _getInstance();
await instance.setStringList(key, value);
print('设置字符串列表: $key (数量: ${value.length})');
} catch (e) {
print('设置字符串列表失败: $e');
throw e;
}
}
/// 获取字符串列表
static Future<List<String>> getStringList(
String key, {
List<String>? defaultValue,
}) async {
try {
final instance = await _getInstance();
final value = instance.getStringList(key) ?? defaultValue ?? [];
print('获取字符串列表: $key (数量: ${value.length})');
return value;
} catch (e) {
print('获取字符串列表失败: $e');
return defaultValue ?? [];
}
}
/// 移除指定键的值
static Future<void> remove(String key) async {
try {
final instance = await _getInstance();
await instance.remove(key);
print('移除存储值: $key');
} catch (e) {
print('移除存储值失败: $e');
throw e;
}
}
/// 清空所有存储
static Future<void> clear() async {
try {
final instance = await _getInstance();
await instance.clear();
print('清空所有存储');
} catch (e) {
print('清空存储失败: $e');
throw e;
}
}
/// 获取所有存储的键
static Future<Set<String>> getKeys() async {
try {
final instance = await _getInstance();
final keys = instance.getKeys();
print('获取存储键: ${keys.length}');
return keys;
} catch (e) {
print('获取存储键失败: $e');
return {};
}
}
/// 检查键是否存在
static Future<bool> containsKey(String key) async {
try {
final instance = await _getInstance();
final exists = instance.containsKey(key);
print('检查键存在: $key = $exists');
return exists;
} catch (e) {
print('检查键存在失败: $e');
return false;
}
}
/// 重新加载数据
static Future<void> reload() async {
try {
final instance = await _getInstance();
await instance.reload();
print('重新加载存储数据');
} catch (e) {
print('重新加载数据失败: $e');
}
}
}