43 lines
1.1 KiB
Dart
43 lines
1.1 KiB
Dart
// Author: fengshengxiong
|
|
// Date: 2024/5/8
|
|
// Description: 持久化储存
|
|
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
|
import 'package:tone_snap/data/enum/play_mode.dart';
|
|
import 'package:tone_snap/data/models/music_model.dart';
|
|
import 'package:tone_snap/data/models/voice_model.dart';
|
|
|
|
const myVoiceBox = 'myVoiceBox';
|
|
const favoriteBox = 'favoriteBox';
|
|
const musicBox = 'musicBox';
|
|
const loveSongsBox = 'loveSongsBox';
|
|
|
|
Future initHive() async {
|
|
// 初始化
|
|
await Hive.initFlutter();
|
|
// 注册类型适配器
|
|
Hive.registerAdapter(VoiceModelAdapter());
|
|
Hive.registerAdapter(PlayModeAdapter());
|
|
Hive.registerAdapter(MusicModelAdapter());
|
|
// 打开盒子
|
|
await Hive.openBox<VoiceModel>(myVoiceBox);
|
|
await Hive.openBox<VoiceModel>(favoriteBox);
|
|
await Hive.openBox(musicBox);
|
|
await Hive.openBox<MusicModel>(loveSongsBox);
|
|
}
|
|
|
|
Box<VoiceModel> getMyVoiceBox() {
|
|
return Hive.box<VoiceModel>(myVoiceBox);
|
|
}
|
|
|
|
Box<VoiceModel> getFavoriteBox() {
|
|
return Hive.box<VoiceModel>(favoriteBox);
|
|
}
|
|
|
|
Box getMusicBox() {
|
|
return Hive.box(musicBox);
|
|
}
|
|
|
|
Box<MusicModel> getLoveSongsBox() {
|
|
return Hive.box<MusicModel>(loveSongsBox);
|
|
} |