150 lines
3.6 KiB
Dart
150 lines
3.6 KiB
Dart
import 'dart:convert';
|
|
|
|
class SCMusicMode {
|
|
SCMusicMode({
|
|
String? id,
|
|
String? title,
|
|
String? artist,
|
|
int? duration,
|
|
int? durationMs,
|
|
String? localPath,
|
|
String? contentUri,
|
|
String? folderPath,
|
|
String? fileName,
|
|
int? size,
|
|
int? sortIndex,
|
|
int? addedAt,
|
|
}) {
|
|
_id = id;
|
|
_title = title;
|
|
_artist = artist;
|
|
_durationMs = durationMs ?? duration;
|
|
_localPath = localPath;
|
|
_contentUri = contentUri;
|
|
_folderPath = folderPath;
|
|
_fileName = fileName;
|
|
_size = size;
|
|
_sortIndex = sortIndex;
|
|
_addedAt = addedAt;
|
|
}
|
|
|
|
String? _id;
|
|
String? _title;
|
|
String? _artist;
|
|
int? _durationMs;
|
|
String? _localPath;
|
|
String? _contentUri;
|
|
String? _folderPath;
|
|
String? _fileName;
|
|
int? _size;
|
|
int? _sortIndex;
|
|
int? _addedAt;
|
|
|
|
String get id {
|
|
final rawId = (_id ?? "").trim();
|
|
if (rawId.isNotEmpty) {
|
|
return rawId;
|
|
}
|
|
final source = playPath.isNotEmpty ? playPath : title;
|
|
return base64Url.encode(utf8.encode(source)).replaceAll('=', '');
|
|
}
|
|
|
|
String get title => (_title ?? _fileName ?? "").trim();
|
|
|
|
String get artist => (_artist ?? "").trim();
|
|
|
|
int get durationMs => _durationMs ?? 0;
|
|
|
|
int get duration => durationMs;
|
|
|
|
String get localPath => (_localPath ?? "").trim();
|
|
|
|
String get contentUri => (_contentUri ?? "").trim();
|
|
|
|
String get folderPath => (_folderPath ?? "").trim();
|
|
|
|
String get fileName => (_fileName ?? title).trim();
|
|
|
|
int get size => _size ?? 0;
|
|
|
|
int get sortIndex => _sortIndex ?? 0;
|
|
|
|
int get addedAt => _addedAt ?? 0;
|
|
|
|
String get playPath => contentUri.isNotEmpty ? contentUri : localPath;
|
|
|
|
bool get hasPlayablePath => playPath.isNotEmpty;
|
|
|
|
SCMusicMode copyWith({
|
|
String? id,
|
|
String? title,
|
|
String? artist,
|
|
int? durationMs,
|
|
String? localPath,
|
|
String? contentUri,
|
|
String? folderPath,
|
|
String? fileName,
|
|
int? size,
|
|
int? sortIndex,
|
|
int? addedAt,
|
|
}) {
|
|
return SCMusicMode(
|
|
id: id ?? _id,
|
|
title: title ?? _title,
|
|
artist: artist ?? _artist,
|
|
durationMs: durationMs ?? _durationMs,
|
|
localPath: localPath ?? _localPath,
|
|
contentUri: contentUri ?? _contentUri,
|
|
folderPath: folderPath ?? _folderPath,
|
|
fileName: fileName ?? _fileName,
|
|
size: size ?? _size,
|
|
sortIndex: sortIndex ?? _sortIndex,
|
|
addedAt: addedAt ?? _addedAt,
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toJson() {
|
|
final map = <String, dynamic>{};
|
|
map['id'] = _id;
|
|
map['title'] = _title;
|
|
map['artist'] = _artist;
|
|
map['duration'] = _durationMs;
|
|
map['durationMs'] = _durationMs;
|
|
map['localPath'] = _localPath;
|
|
map['contentUri'] = _contentUri;
|
|
map['folderPath'] = _folderPath;
|
|
map['fileName'] = _fileName;
|
|
map['size'] = _size;
|
|
map['sortIndex'] = _sortIndex;
|
|
map['addedAt'] = _addedAt;
|
|
return map;
|
|
}
|
|
|
|
SCMusicMode.fromJson(dynamic json) {
|
|
if (json is! Map) {
|
|
return;
|
|
}
|
|
_id = json['id']?.toString();
|
|
_title = json['title']?.toString();
|
|
_artist = json['artist']?.toString();
|
|
_durationMs = _asInt(json['durationMs'] ?? json['duration']);
|
|
_localPath = json['localPath']?.toString();
|
|
_contentUri = json['contentUri']?.toString();
|
|
_folderPath = json['folderPath']?.toString();
|
|
_fileName = json['fileName']?.toString();
|
|
_size = _asInt(json['size']);
|
|
_sortIndex = _asInt(json['sortIndex']);
|
|
_addedAt = _asInt(json['addedAt']);
|
|
}
|
|
|
|
static int? _asInt(dynamic value) {
|
|
if (value is int) {
|
|
return value;
|
|
}
|
|
if (value is num) {
|
|
return value.toInt();
|
|
}
|
|
return int.tryParse(value?.toString() ?? "");
|
|
}
|
|
}
|