70 lines
1.9 KiB
Dart
70 lines
1.9 KiB
Dart
// lib/models/playlist.dart
|
|
import 'package:qingting_player/services/audio_service.dart';
|
|
|
|
class Playlist {
|
|
final String id;
|
|
final String name;
|
|
final PlayMode playMode;
|
|
final DateTime createdAt;
|
|
final DateTime updatedAt;
|
|
|
|
Playlist({
|
|
required this.id,
|
|
required this.name,
|
|
this.playMode = PlayMode.sequential,
|
|
DateTime? createdAt,
|
|
DateTime? updatedAt,
|
|
}) : createdAt = createdAt ?? DateTime.now(),
|
|
updatedAt = updatedAt ?? DateTime.now();
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
'id': id,
|
|
'name': name,
|
|
'play_mode': Playlist.playModeToString(playMode), // ⭐ 改用公开静态方法
|
|
'created_at': createdAt.millisecondsSinceEpoch,
|
|
'updated_at': updatedAt.millisecondsSinceEpoch,
|
|
};
|
|
|
|
factory Playlist.fromJson(Map<String, dynamic> json) => Playlist(
|
|
id: json['id'] as String,
|
|
name: json['name'] as String,
|
|
playMode: Playlist.stringToPlayMode(
|
|
json['play_mode'] as String? ?? 'sequential'),
|
|
createdAt:
|
|
DateTime.fromMillisecondsSinceEpoch(json['created_at'] as int),
|
|
updatedAt:
|
|
DateTime.fromMillisecondsSinceEpoch(json['updated_at'] as int),
|
|
);
|
|
|
|
Playlist copyWith({String? name, PlayMode? playMode}) => Playlist(
|
|
id: id,
|
|
name: name ?? this.name,
|
|
playMode: playMode ?? this.playMode,
|
|
createdAt: createdAt,
|
|
updatedAt: DateTime.now(),
|
|
);
|
|
|
|
// ⭐ 改为公开静态方法
|
|
static String playModeToString(PlayMode mode) {
|
|
switch (mode) {
|
|
case PlayMode.sequential:
|
|
return 'sequential';
|
|
case PlayMode.repeatOne:
|
|
return 'repeat_one';
|
|
case PlayMode.shuffle:
|
|
return 'shuffle';
|
|
}
|
|
}
|
|
|
|
static PlayMode stringToPlayMode(String value) {
|
|
switch (value) {
|
|
case 'repeat_one':
|
|
return PlayMode.repeatOne;
|
|
case 'shuffle':
|
|
return PlayMode.shuffle;
|
|
default:
|
|
return PlayMode.sequential;
|
|
}
|
|
}
|
|
}
|