tsacdop-podcast-app-android/lib/type/playlist.dart

159 lines
4.2 KiB
Dart
Raw Normal View History

2020-12-20 10:35:39 +01:00
import 'package:equatable/equatable.dart';
2020-11-03 18:54:03 +01:00
import 'package:uuid/uuid.dart';
2020-10-28 13:10:43 +01:00
import '../local_storage/sqflite_localpodcast.dart';
import 'episodebrief.dart';
2020-11-03 18:54:03 +01:00
class PlaylistEntity {
final String name;
final String id;
2021-02-17 16:25:05 +01:00
final bool isLocal;
2020-11-03 18:54:03 +01:00
final List<String> episodeList;
2021-02-17 16:25:05 +01:00
PlaylistEntity(this.name, this.id, this.isLocal, this.episodeList);
2020-11-03 18:54:03 +01:00
Map<String, Object> toJson() {
2021-02-17 16:25:05 +01:00
return {
'name': name,
'id': id,
'isLocal': isLocal,
'episodeList': episodeList
};
2020-11-03 18:54:03 +01:00
}
static PlaylistEntity fromJson(Map<String, Object> json) {
var list = List<String>.from(json['episodeList']);
2021-02-17 16:25:05 +01:00
return PlaylistEntity(json['name'] as String, json['id'] as String,
json['isLocal'] == null ? false : json['isLocal'] as bool, list);
2020-11-03 18:54:03 +01:00
}
}
2020-12-20 10:35:39 +01:00
class Playlist extends Equatable {
2020-11-03 18:54:03 +01:00
/// Playlist name. the default playlist is named "Playlist".
final String name;
/// Unique id for playlist.
final String id;
2021-02-17 16:25:05 +01:00
final bool isLocal;
2020-11-03 18:54:03 +01:00
/// Episode url list for playlist.
final List<String> episodeList;
2020-12-20 10:35:39 +01:00
/// Eposides in playlist.
final List<EpisodeBrief> episodes;
2021-01-02 13:38:30 +01:00
bool get isEmpty => episodeList.isEmpty;
2021-01-02 09:21:05 +01:00
2021-01-02 13:38:30 +01:00
bool get isNotEmpty => episodeList.isNotEmpty;
2020-12-20 10:35:39 +01:00
2021-01-02 13:38:30 +01:00
int get length => episodeList.length;
2021-01-01 16:43:10 +01:00
2021-01-02 09:21:05 +01:00
bool get isQueue => name == 'Queue';
bool contains(EpisodeBrief episode) => episodes.contains(episode);
2020-12-20 10:35:39 +01:00
Playlist(this.name,
2021-02-17 16:25:05 +01:00
{String id,
this.isLocal = false,
List<String> episodeList,
List<EpisodeBrief> episodes})
2020-11-03 18:54:03 +01:00
: id = id ?? Uuid().v4(),
2021-01-01 16:43:10 +01:00
assert(name != ''),
2020-12-20 10:35:39 +01:00
episodeList = episodeList ?? [],
episodes = episodes ?? [];
2020-11-03 18:54:03 +01:00
PlaylistEntity toEntity() {
2021-02-17 16:25:05 +01:00
return PlaylistEntity(name, id, isLocal, episodeList.toSet().toList());
2020-11-03 18:54:03 +01:00
}
static Playlist fromEntity(PlaylistEntity entity) {
return Playlist(
entity.name,
id: entity.id,
2021-02-17 16:25:05 +01:00
isLocal: entity.isLocal,
2020-11-03 18:54:03 +01:00
episodeList: entity.episodeList,
);
}
2020-10-28 13:10:43 +01:00
final DBHelper _dbHelper = DBHelper();
2020-12-20 10:35:39 +01:00
// final KeyValueStorage _playlistStorage = KeyValueStorage(playlistKey);
2020-10-28 13:10:43 +01:00
Future<void> getPlaylist() async {
2020-12-20 10:35:39 +01:00
episodes.clear();
2021-01-02 15:50:19 +01:00
var error = [];
2020-12-20 10:35:39 +01:00
if (episodeList.isNotEmpty) {
for (var url in episodeList) {
2020-10-28 13:10:43 +01:00
var episode = await _dbHelper.getRssItemWithUrl(url);
2021-01-02 13:38:30 +01:00
if (episode != null) {
episodes.add(episode);
} else {
2021-01-02 15:50:19 +01:00
error.add(url);
2021-01-02 13:38:30 +01:00
}
2020-10-28 13:10:43 +01:00
}
}
2021-01-02 15:50:19 +01:00
if (error.isNotEmpty) {
for (var u in error) {
episodeList.remove(u);
}
}
2020-10-28 13:10:43 +01:00
}
2020-12-20 10:35:39 +01:00
// Future<void> savePlaylist() async {
// var urls = <String>[];
// urls.addAll(_playlist.map((e) => e.enclosureUrl));
// await _playlistStorage.saveStringList(urls.toSet().toList());
// }
2020-10-28 13:10:43 +01:00
2020-12-20 10:35:39 +01:00
void addToPlayList(EpisodeBrief episodeBrief) {
if (!episodes.contains(episodeBrief)) {
episodes.add(episodeBrief);
episodeList.add(episodeBrief.enclosureUrl);
2020-10-28 13:10:43 +01:00
}
}
2020-12-20 10:35:39 +01:00
void addToPlayListAt(EpisodeBrief episodeBrief, int index,
{bool existed = true}) {
2020-10-28 13:10:43 +01:00
if (existed) {
2020-12-20 10:35:39 +01:00
episodes.removeWhere((episode) => episode == episodeBrief);
episodeList.removeWhere((url) => url == episodeBrief.enclosureUrl);
2020-10-28 13:10:43 +01:00
}
2020-12-20 10:35:39 +01:00
episodes.insert(index, episodeBrief);
episodeList.insert(index, episodeBrief.enclosureUrl);
}
void updateEpisode(EpisodeBrief episode) {
var index = episodes.indexOf(episode);
if (index != -1) episodes[index] = episode;
2020-10-28 13:10:43 +01:00
}
2020-12-20 10:35:39 +01:00
int delFromPlaylist(EpisodeBrief episodeBrief) {
var index = episodes.indexOf(episodeBrief);
episodes.removeWhere(
2020-10-28 13:10:43 +01:00
(episode) => episode.enclosureUrl == episodeBrief.enclosureUrl);
2020-12-20 10:35:39 +01:00
episodeList.removeWhere((url) => url == episodeBrief.enclosureUrl);
2021-02-17 16:25:05 +01:00
if (isLocal) {
_dbHelper.deleteLocalEpisodes([episodeBrief.enclosureUrl]);
}
2020-10-28 13:10:43 +01:00
return index;
}
2020-12-20 10:35:39 +01:00
void reorderPlaylist(int oldIndex, int newIndex) {
if (newIndex > oldIndex) {
newIndex -= 1;
}
final episode = episodes.removeAt(oldIndex);
episodes.insert(newIndex, episode);
episodeList.removeAt(oldIndex);
episodeList.insert(newIndex, episode.enclosureUrl);
}
void clear() {
episodeList.clear();
episodes.clear();
}
@override
List<Object> get props => [id, name];
2020-10-28 13:10:43 +01:00
}