Wallpaper-Genie/lib/common/storage/custom_data.dart
xuhang-x 121e423c8b 1
2024-07-24 20:13:27 +08:00

42 lines
968 B
Dart

import 'package:wallpaperx/common/storage/hive_storage.dart';
import 'package:wallpaperx/entity/image_model.dart';
class CustomData {
/// 私有构造函数
CustomData._();
/// 静态常量用于保存类的唯一实例
static final CustomData _instance = CustomData._();
/// 工厂构造函数返回类的唯一实例
factory CustomData() {
return _instance;
}
/// 声明盒子
/// 注意, main函数中这个盒子已经打开, 可以进行存储操作
final _box = getCustomBox();
/// 获取壁纸
List<ImageModel> getWallpaperData() {
return _box.values.toList();
}
/// 存储壁纸
Future<int> setWallpaperData(ImageModel wallpaperData) async {
return await _box.add(wallpaperData);
}
/// 删除壁纸
Future<void> delete(index) async {
await _box.deleteAt(index);
await _box.flush();
}
/// 删除所有壁纸
Future<void> clear() async {
await _box.clear();
await _box.flush();
}
}