// 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(favoriteBox); /// 获取数据 List getList() { return _box.values.toList(); } /// 添加数据 Future addData(VoiceModel voiceModel) async { return await _box.add(voiceModel); } /// 删除 Future delete(int index) async { await _box.deleteAt(index); await _box.flush(); } /// 清空所有数据 Future clear() async { await _box.clear(); await _box.flush(); } }