196 lines
5.5 KiB
Dart
196 lines
5.5 KiB
Dart
// Author: fengshengxiong
|
|
// Date: 2024/5/8
|
|
// Description: 音乐非结构化数据盒子
|
|
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:hive/hive.dart';
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
import 'package:tone_snap/data/enum/play_mode.dart';
|
|
import 'package:tone_snap/data/models/ad_config_model.dart';
|
|
import 'package:tone_snap/data/storage/hive_storage.dart';
|
|
import 'package:tone_snap/global/app_config.dart';
|
|
import 'package:tone_snap/utils/log_util.dart';
|
|
import 'package:tone_snap/utils/obj_util.dart';
|
|
|
|
class MusicBox {
|
|
MusicBox._();
|
|
|
|
static final MusicBox _instance = MusicBox._();
|
|
|
|
factory MusicBox() {
|
|
return _instance;
|
|
}
|
|
|
|
/// 是否进入过B面
|
|
final _isOpenedSideBKey = 'isOpenedSideBKey';
|
|
|
|
/// openStatus
|
|
final _openStatusKey = 'openStatusKey';
|
|
|
|
/// dataVersion
|
|
final _dataVersionKey = 'dataVersionKey';
|
|
|
|
/// 开屏启动插页事件间隔时长
|
|
final _openAppEventDurationKey = 'openAppEventDurationKey';
|
|
|
|
/// 插页广告事件间隔时长
|
|
final _interstitialEventDurationKey = 'interstitialEventDurationKey';
|
|
|
|
/// 广告配置
|
|
final _adMobLevelIDsKey = 'adMobLevelIDsKey';
|
|
|
|
/// 播放模式
|
|
final _playModeKey = 'playModeKey';
|
|
|
|
/// 搜索历史
|
|
final _searchHistoryKey = 'searchHistoryKey';
|
|
|
|
/// 声明盒子
|
|
/// 注意, main函数中这个盒子已经打开, 可以进行存储操作
|
|
final _box = Hive.box(musicBox);
|
|
|
|
/// 设置是否打开过B面
|
|
Future<void> putIsOpenedSideB(bool isOpen) async {
|
|
return await _box.put(_isOpenedSideBKey, isOpen);
|
|
}
|
|
|
|
/// 获取是否打开过B面
|
|
bool getIsOpenedSideB() {
|
|
return _box.get(_isOpenedSideBKey, defaultValue: false);
|
|
}
|
|
|
|
/// 设置 openStatus
|
|
Future<void> putOpenStatus(String openStatus) async {
|
|
return await _box.put(_openStatusKey, openStatus);
|
|
}
|
|
|
|
/// 获取远程版本
|
|
Future<String> getVersionCode() async {
|
|
String? openStatus = _box.get(_openStatusKey);
|
|
String? versionCode;
|
|
if (ObjUtil.isNotEmpty(openStatus)) {
|
|
try {
|
|
versionCode = jsonDecode(openStatus!)['versionCode'];
|
|
} catch (e) {
|
|
LogUtil.e(e.toString());
|
|
}
|
|
}
|
|
final packageInfo = await PackageInfo.fromPlatform();
|
|
return ObjUtil.isEmpty(versionCode) ? packageInfo.version : versionCode!;
|
|
}
|
|
|
|
/// 获取开关
|
|
bool getEnter() {
|
|
String? openStatus = _box.get(_openStatusKey);
|
|
if (ObjUtil.isNotEmpty(openStatus)) {
|
|
try {
|
|
return jsonDecode(openStatus!)['enter'] ?? false;
|
|
} catch (e) {
|
|
LogUtil.e(e.toString());
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// 设置 dataVersion
|
|
Future<void> putDataVersion(String dataVersion) async {
|
|
return await _box.put(_dataVersionKey, dataVersion);
|
|
}
|
|
|
|
/// 获取 ClientVersion
|
|
String getClientVersion() {
|
|
String? dataVersion = _box.get(_dataVersionKey);
|
|
if (ObjUtil.isNotEmpty(dataVersion)) {
|
|
try {
|
|
if (ObjUtil.isNotEmpty(jsonDecode(dataVersion!)['ClientVersion'])) {
|
|
return jsonDecode(dataVersion)['ClientVersion'];
|
|
}
|
|
} catch (e) {
|
|
LogUtil.e(e.toString());
|
|
}
|
|
}
|
|
return AppConfig.clientVersion;
|
|
}
|
|
|
|
/// 获取 PlayerVersion
|
|
String getPlayerVersion() {
|
|
String? dataVersion = _box.get(_dataVersionKey);
|
|
if (ObjUtil.isNotEmpty(dataVersion)) {
|
|
try {
|
|
if (ObjUtil.isNotEmpty(jsonDecode(dataVersion!)['PlayerVersion'])) {
|
|
return jsonDecode(dataVersion)['PlayerVersion'];
|
|
}
|
|
} catch (e) {
|
|
LogUtil.e(e.toString());
|
|
}
|
|
}
|
|
return AppConfig.playerVersion;
|
|
}
|
|
|
|
/// 设置开屏启动插页事件间隔时长
|
|
Future<void> putOpenAppEventDuration(int time) async {
|
|
return await _box.put(_openAppEventDurationKey, time);
|
|
}
|
|
|
|
/// 获取开屏启动插页事件间隔时长
|
|
int getOpenAppEventDuration() {
|
|
return _box.get(_openAppEventDurationKey, defaultValue: AppConfig.openAppEventDurationTime);
|
|
}
|
|
|
|
/// 设置插页广告事件间隔时长
|
|
Future<void> putInterstitialEventDuration(int time) async {
|
|
return await _box.put(_interstitialEventDurationKey, time);
|
|
}
|
|
|
|
/// 获取插页广告事件间隔时长
|
|
int getInterstitialEventDuration() {
|
|
return _box.get(_interstitialEventDurationKey, defaultValue: AppConfig.interstitialEventDuration);
|
|
}
|
|
|
|
/// 设置广告配置
|
|
Future<void> putAdConfig(String adConfigStr) async {
|
|
return await _box.put(_adMobLevelIDsKey, adConfigStr);
|
|
}
|
|
|
|
/// 获取广告配置
|
|
AdConfigModel getAdConfig() {
|
|
var configStr = _box.get(_adMobLevelIDsKey, defaultValue: Platform.isIOS ? AppConfig.adIOSDefaultConfig : AppConfig.adAndroidDefaultConfig);
|
|
return AdConfigModel.fromJson(configStr);
|
|
}
|
|
|
|
/// 设置播放模式
|
|
Future<void> putPlayMode(PlayMode playMode) async {
|
|
return await _box.put(_playModeKey, playMode);
|
|
}
|
|
|
|
/// 获取播放模式
|
|
PlayMode getPlayMode() {
|
|
return _box.get(_playModeKey) ?? PlayMode.listLoop;
|
|
}
|
|
|
|
/// 添加搜索历史
|
|
Future<void> putSearchHistory(String history) async {
|
|
var historyList = getAllSearchHistory();
|
|
if (!historyList.contains(history)) {
|
|
if (historyList.length >= 10) {
|
|
historyList.removeLast();
|
|
}
|
|
historyList.insert(0, history);
|
|
await _box.put(_searchHistoryKey, historyList);
|
|
}
|
|
}
|
|
|
|
/// 获取所有搜索历史
|
|
List<String> getAllSearchHistory() {
|
|
return _box.get(_searchHistoryKey, defaultValue: <String>[]);
|
|
}
|
|
|
|
/// 删除所有搜索历史
|
|
Future<void> deleteAllSearchHistory() async {
|
|
await _box.delete(_searchHistoryKey);
|
|
await _box.flush();
|
|
}
|
|
}
|