Update playlist class.

This commit is contained in:
stonega 2020-11-04 01:54:03 +08:00
parent 085eb1d78e
commit 1e42cde733
3 changed files with 73 additions and 16 deletions

View File

@ -0,0 +1,18 @@
import 'package:state_notifier/state_notifier.dart';
import '../local_storage/key_value_storage.dart';
import '../type/playlist.dart';
class PlaylistProvider extends StateNotifier<List<Playlist>> {
PlaylistProvider() : super([]);
Future<void> loadPlaylist() async {
var storage = KeyValueStorage(playlistsAllKey);
var playlistEntities = await storage.getPlaylists();
var initState = [...playlistEntities.map(Playlist.fromEntity).toList()];
for (var playlist in initState) {
await playlist.getPlaylist();
}
state = initState;
}
}

View File

@ -66,28 +66,22 @@ class PodcastGroup extends Equatable {
final String color; final String color;
/// Id lists of podcasts in group. /// Id lists of podcasts in group.
List<String> _podcastList; List<String> podcastList;
List<String> get podcastList => _podcastList;
set podcastList(list) {
_podcastList = list;
}
PodcastGroup(this.name, PodcastGroup(this.name,
{this.color = '#000000', String id, List<String> podcastList}) {this.color = '#000000', String id, List<String> podcastList})
: id = id ?? Uuid().v4(), : id = id ?? Uuid().v4(),
_podcastList = podcastList ?? []; podcastList = podcastList ?? [];
Future<void> getPodcasts() async { Future<void> getPodcasts() async {
var dbHelper = DBHelper(); var dbHelper = DBHelper();
if (_podcastList != []) { if (podcastList != []) {
try { try {
_podcasts = await dbHelper.getPodcastLocal(_podcastList); _podcasts = await dbHelper.getPodcastLocal(podcastList);
} catch (e) { } catch (e) {
await Future.delayed(Duration(milliseconds: 200)); await Future.delayed(Duration(milliseconds: 200));
try { try {
_podcasts = await dbHelper.getPodcastLocal(_podcastList); _podcasts = await dbHelper.getPodcastLocal(podcastList);
} catch (e) { } catch (e) {
developer.log(e.toString()); developer.log(e.toString());
} }
@ -526,13 +520,14 @@ class GroupList extends ChangeNotifier {
_isLoading = false; _isLoading = false;
notifyListeners(); notifyListeners();
} }
/// Delete podcsat from device.
Future<void> removePodcast( Future<void> removePodcast(
PodcastLocal podcast, PodcastLocal podcast,
) async { ) async {
_syncRemove(podcast.rssUrl); _syncRemove(podcast.rssUrl);
final id = podcast.id; await _unsubscribe(podcast.id);
await _unsubscribe(id); await File(podcast.imagePath)?.delete();
} }
Future<void> saveOrder(PodcastGroup group) async { Future<void> saveOrder(PodcastGroup group) async {

View File

@ -1,16 +1,60 @@
import 'package:uuid/uuid.dart';
import '../local_storage/key_value_storage.dart'; import '../local_storage/key_value_storage.dart';
import '../local_storage/sqflite_localpodcast.dart'; import '../local_storage/sqflite_localpodcast.dart';
import 'episodebrief.dart'; import 'episodebrief.dart';
class PlaylistEntity {
final String name;
final String id;
final List<String> episodeList;
PlaylistEntity(this.name, this.id, this.episodeList);
Map<String, Object> toJson() {
return {'name': name, 'id': id, 'episodeList': episodeList};
}
static PlaylistEntity fromJson(Map<String, Object> json) {
var list = List<String>.from(json['episodeList']);
return PlaylistEntity(json['name'] as String, json['id'] as String, list);
}
}
class Playlist { class Playlist {
String name; /// Playlist name. the default playlist is named "Playlist".
final String name;
/// Unique id for playlist.
final String id;
/// Episode url list for playlist.
final List<String> episodeList;
Playlist(this.name, {String id, List<String> episodeList})
: id = id ?? Uuid().v4(),
episodeList = episodeList ?? [];
PlaylistEntity toEntity() {
return PlaylistEntity(name, id, episodeList);
}
static Playlist fromEntity(PlaylistEntity entity) {
return Playlist(
entity.name,
id: entity.id,
episodeList: entity.episodeList,
);
}
final DBHelper _dbHelper = DBHelper(); final DBHelper _dbHelper = DBHelper();
List<EpisodeBrief> _playlist; List<EpisodeBrief> _playlist = [];
List<EpisodeBrief> get playlist => _playlist; List<EpisodeBrief> get playlist => _playlist;
final KeyValueStorage _playlistStorage = KeyValueStorage(playlistKey); final KeyValueStorage _playlistStorage = KeyValueStorage(playlistKey);
Future<void> getPlaylist() async { Future<void> getPlaylist() async {
var urls = await _playlistStorage.getStringList(); var urls = await _playlistStorage.getStringList();
episodeList.addAll(urls);
if (urls.length == 0) { if (urls.length == 0) {
_playlist = []; _playlist = [];
} else { } else {