ToneSnap_FSX_Flutter/lib/data/storage/favorite_box.dart
2024-08-01 13:38:25 +08:00

47 lines
1.1 KiB
Dart

// Author: fengshengxiong
// Date: 2024/5/8
// Description: 收藏音频盒子
import 'package:hive/hive.dart';
import 'package:tone_snap/data/models/voice_model.dart';
import 'package:tone_snap/data/storage/hive_storage.dart';
class FavoriteBox {
/// 私有构造函数
FavoriteBox._();
/// 静态常量用于保存类的唯一实例
static final FavoriteBox _instance = FavoriteBox._();
/// 工厂构造函数返回类的唯一实例
factory FavoriteBox() {
return _instance;
}
/// 声明盒子
/// 注意, main函数中这个盒子已经打开, 可以进行存储操作
final _box = Hive.box<VoiceModel>(favoriteBox);
/// 获取数据
List<VoiceModel> getList() {
return _box.values.toList();
}
/// 添加数据
Future<int> addData(VoiceModel voiceModel) async {
return await _box.add(voiceModel);
}
/// 删除
Future<void> delete(int index) async {
await _box.deleteAt(index);
await _box.flush();
}
/// 清空所有数据
Future<void> clear() async {
await _box.clear();
await _box.flush();
}
}