// 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 MyVoiceBox {
  /// 私有构造函数
  MyVoiceBox._();

  /// 静态常量用于保存类的唯一实例
  static final MyVoiceBox _instance = MyVoiceBox._();

  /// 工厂构造函数返回类的唯一实例
  factory MyVoiceBox() {
    return _instance;
  }

  /// 声明盒子
  /// 注意, main函数中这个盒子已经打开, 可以进行存储操作
  final _box = Hive.box<VoiceModel>(myVoiceBox);

  /// 获取数据
  List<VoiceModel> getList() {
    return _box.values.toList();
  }

  /// 添加数据
  Future<int> addData(VoiceModel voiceModel) async {
    return await _box.add(voiceModel);
  }

  /// 删除
  Future<void> delete(index) async {
    await _box.deleteAt(index);
    await _box.flush();
  }

  /// 清空所有数据
  Future<void> clear() async {
    await _box.clear();
    await _box.flush();
  }
}
