From 5c01a9c83c03cd4f74fc5b681690239292f76c8b Mon Sep 17 00:00:00 2001 From: stonegate Date: Fri, 14 Aug 2020 20:13:10 +0800 Subject: [PATCH] Minor change. --- lib/local_storage/key_value_storage.dart | 2 + lib/podcasts/podcast_group.dart | 20 ++++----- lib/state/audio_state.dart | 4 ++ lib/state/podcast_group.dart | 54 ++++++++++++++---------- lib/type/episodebrief.dart | 12 ++---- lib/type/fireside_data.dart | 13 ++++-- lib/type/play_histroy.dart | 4 +- lib/type/playlist.dart | 18 ++++---- lib/type/podcastlocal.dart | 28 ++++++------ pubspec.yaml | 3 +- 10 files changed, 89 insertions(+), 69 deletions(-) diff --git a/lib/local_storage/key_value_storage.dart b/lib/local_storage/key_value_storage.dart index c294015..a95c12e 100644 --- a/lib/local_storage/key_value_storage.dart +++ b/lib/local_storage/key_value_storage.dart @@ -6,6 +6,8 @@ import 'package:shared_preferences/shared_preferences.dart'; import '../state/podcast_group.dart'; +const String groupsKey = 'groups'; +const String playlistKey = 'playlist'; const String autoPlayKey = 'autoPlay'; const String audioPositionKey = 'audioposition'; const String lastWorkKey = 'lastWork'; diff --git a/lib/podcasts/podcast_group.dart b/lib/podcasts/podcast_group.dart index cae1c1e..9110ede 100644 --- a/lib/podcasts/podcast_group.dart +++ b/lib/podcasts/podcast_group.dart @@ -41,7 +41,7 @@ class _PodcastGroupListState extends State { final podcast = widget.group.podcasts.removeAt(oldIndex); widget.group.podcasts.insert(newIndex, podcast); }); - widget.group.setOrderedPodcasts = widget.group.podcasts; + widget.group.orderedPodcasts = widget.group.podcasts; groupList.addToOrderChanged(widget.group); }, children: widget.group.podcasts.map((podcastLocal) { @@ -49,7 +49,7 @@ class _PodcastGroupListState extends State { decoration: BoxDecoration(color: Theme.of(context).primaryColor), key: ObjectKey(podcastLocal.title), - child: PodcastCard( + child: _PodcastCard( podcastLocal: podcastLocal, group: widget.group, ), @@ -60,15 +60,15 @@ class _PodcastGroupListState extends State { } } -class PodcastCard extends StatefulWidget { +class _PodcastCard extends StatefulWidget { final PodcastLocal podcastLocal; final PodcastGroup group; - PodcastCard({this.podcastLocal, this.group, Key key}) : super(key: key); + _PodcastCard({this.podcastLocal, this.group, Key key}) : super(key: key); @override - _PodcastCardState createState() => _PodcastCardState(); + __PodcastCardState createState() => __PodcastCardState(); } -class _PodcastCardState extends State +class __PodcastCardState extends State<_PodcastCard> with SingleTickerProviderStateMixin { bool _loadMenu; bool _addGroup; @@ -161,11 +161,11 @@ class _PodcastCardState extends State @override Widget build(BuildContext context) { - var _c = (Theme.of(context).brightness == Brightness.light) + final c = (Theme.of(context).brightness == Brightness.light) ? widget.podcastLocal.primaryColor.colorizedark() : widget.podcastLocal.primaryColor.colorizeLight(); final s = context.s; - var _width = MediaQuery.of(context).size.width; + var width = context.width; var groupList = context.watch(); _belongGroups = groupList.getPodcastGroup(widget.podcastLocal.id); @@ -199,7 +199,7 @@ class _PodcastCardState extends State Container( child: Icon( Icons.unfold_more, - color: _c, + color: c, ), ), Container( @@ -214,7 +214,7 @@ class _PodcastCardState extends State ), ), Container( - width: _width / 2, + width: width / 2, padding: EdgeInsets.symmetric(horizontal: 10), alignment: Alignment.centerLeft, child: Column( diff --git a/lib/state/audio_state.dart b/lib/state/audio_state.dart index b0d8d51..937b89b 100644 --- a/lib/state/audio_state.dart +++ b/lib/state/audio_state.dart @@ -551,6 +551,10 @@ class AudioPlayerNotifier extends ChangeNotifier { await AudioService.removeQueueItem(episodeNew.toMediaItem()); } var index = await _queue.delFromPlaylist(episodeNew); + if (index == 0) { + _lastPostion = 0; + await positionStorage.saveInt(0); + } _queueUpdate = !_queueUpdate; notifyListeners(); return index; diff --git a/lib/state/podcast_group.dart b/lib/state/podcast_group.dart index 8883024..ab462a4 100644 --- a/lib/state/podcast_group.dart +++ b/lib/state/podcast_group.dart @@ -13,6 +13,7 @@ import 'package:image/image.dart' as img; import 'package:path_provider/path_provider.dart'; import 'package:webfeed/webfeed.dart'; import 'package:uuid/uuid.dart'; +import 'package:equatable/equatable.dart'; import '../local_storage/key_value_storage.dart'; import '../local_storage/sqflite_localpodcast.dart'; @@ -38,7 +39,7 @@ class GroupEntity { } } -class PodcastGroup { +class PodcastGroup extends Equatable { /// Group name. final String name; @@ -48,22 +49,28 @@ class PodcastGroup { final String color; /// Id lists of podcasts in group. - List podcastList; + List _podcastList; + + List get podcastList => _podcastList; + + set podcastList(list) { + _podcastList = list; + } PodcastGroup(this.name, {this.color = '#000000', String id, List podcastList}) : id = id ?? Uuid().v4(), - podcastList = podcastList ?? []; + _podcastList = podcastList ?? []; - Future getPodcasts() async { + Future getPodcasts() async { var dbHelper = DBHelper(); - if (podcastList != []) { + if (_podcastList != []) { try { - _podcasts = await dbHelper.getPodcastLocal(podcastList); + _podcasts = await dbHelper.getPodcastLocal(_podcastList); } catch (e) { await Future.delayed(Duration(milliseconds: 200)); try { - _podcasts = await dbHelper.getPodcastLocal(podcastList); + _podcasts = await dbHelper.getPodcastLocal(_podcastList); } catch (e) { developer.log(e.toString()); } @@ -86,9 +93,9 @@ class PodcastGroup { ///Ordered podcast list. List _orderedPodcasts; - List get ordereddPodcasts => _orderedPodcasts; + List get orderedPodcasts => _orderedPodcasts; - set setOrderedPodcasts(List list) => _orderedPodcasts = list; + set orderedPodcasts(list) => _orderedPodcasts = list; GroupEntity toEntity() { return GroupEntity(name, id, color, podcastList); @@ -102,6 +109,9 @@ class PodcastGroup { podcastList: entity.podcastList, ); } + + @override + List get props => [id, name]; } enum SubscribeState { none, start, subscribe, fetch, stop, exist, error } @@ -136,10 +146,10 @@ class GroupList extends ChangeNotifier { final List _groups = []; List get groups => _groups; - DBHelper dbHelper = DBHelper(); + final DBHelper _dbHelper = DBHelper(); /// Groups save in shared_prefrences. - KeyValueStorage storage = KeyValueStorage('groups'); + final KeyValueStorage _groupStorage = KeyValueStorage(groupsKey); //GroupList({List groups}) : _groups = groups ?? []; @@ -238,7 +248,7 @@ class GroupList extends ChangeNotifier { notifyListeners(); } - clearOrderChanged() async { + Future clearOrderChanged() async { if (_orderChanged.length > 0) { for (var group in _orderChanged) { await group.getPodcasts(); @@ -264,7 +274,7 @@ class GroupList extends ChangeNotifier { Future loadGroups() async { _isLoading = true; notifyListeners(); - storage.getGroups().then((loadgroups) async { + _groupStorage.getGroups().then((loadgroups) async { _groups.addAll(loadgroups.map(PodcastGroup.fromEntity)); for (var group in _groups) { await group.getPodcasts(); @@ -306,7 +316,7 @@ class GroupList extends ChangeNotifier { notifyListeners(); } - updateGroup(PodcastGroup podcastGroup) async { + Future updateGroup(PodcastGroup podcastGroup) async { var oldGroup = _groups.firstWhere((it) => it.id == podcastGroup.id); var index = _groups.indexOf(oldGroup); _groups.replaceRange(index, index + 1, [podcastGroup]); @@ -315,21 +325,21 @@ class GroupList extends ChangeNotifier { _saveGroup(); } - _saveGroup() async { - await storage.saveGroup(_groups.map((it) => it.toEntity()).toList()); + Future _saveGroup() async { + await _groupStorage.saveGroup(_groups.map((it) => it.toEntity()).toList()); } /// Subscribe podcast from search result. Future subscribe(PodcastLocal podcastLocal) async { _groups[0].podcastList.insert(0, podcastLocal.id); await _saveGroup(); - await dbHelper.savePodcastLocal(podcastLocal); + await _dbHelper.savePodcastLocal(podcastLocal); await _groups[0].getPodcasts(); notifyListeners(); } Future updatePodcast(String id) async { - var counts = await dbHelper.getPodcastCounts(id); + var counts = await _dbHelper.getPodcastCounts(id); for (var group in _groups) { if (group.podcastList.contains(id)) { group.podcasts.firstWhere((podcast) => podcast.id == id) @@ -381,7 +391,7 @@ class GroupList extends ChangeNotifier { } //Change podcast groups - changeGroup(String id, List list) async { + Future changeGroup(String id, List list) async { _isLoading = true; notifyListeners(); @@ -404,14 +414,14 @@ class GroupList extends ChangeNotifier { } /// Unsubscribe podcast - removePodcast(String id) async { + Future removePodcast(String id) async { _isLoading = true; notifyListeners(); for (var group in _groups) { group.podcastList.remove(id); } await _saveGroup(); - await dbHelper.delPodcastLocal(id); + await _dbHelper.delPodcastLocal(id); for (var group in _groups) { await group.getPodcasts(); } @@ -420,7 +430,7 @@ class GroupList extends ChangeNotifier { } saveOrder(PodcastGroup group) async { - group.podcastList = group.ordereddPodcasts.map((e) => e.id).toList(); + group.podcastList = group.orderedPodcasts.map((e) => e.id).toList(); await _saveGroup(); await group.getPodcasts(); notifyListeners(); diff --git a/lib/type/episodebrief.dart b/lib/type/episodebrief.dart index 50ca89a..31a7a61 100644 --- a/lib/type/episodebrief.dart +++ b/lib/type/episodebrief.dart @@ -1,8 +1,8 @@ -import 'dart:ui'; +import 'package:equatable/equatable.dart'; import 'package:audio_service/audio_service.dart'; -class EpisodeBrief { +class EpisodeBrief extends Equatable { final String title; final String description; final int pubDate; @@ -50,11 +50,5 @@ class EpisodeBrief { } @override - bool operator ==(Object episode) => - episode is EpisodeBrief && - episode.title == title && - episode.enclosureUrl == enclosureUrl; - - @override - int get hashCode => hashValues(enclosureUrl, title); + List get props => [enclosureUrl, title]; } diff --git a/lib/type/fireside_data.dart b/lib/type/fireside_data.dart index da60196..b6f5415 100644 --- a/lib/type/fireside_data.dart +++ b/lib/type/fireside_data.dart @@ -15,7 +15,7 @@ class FiresideData { List get hosts => _hosts; FiresideData(this.id, this.link); - DBHelper dbHelper = DBHelper(); + final DBHelper _dbHelper = DBHelper(); String parseLink(String link) { if (link == "http://www.shengfm.cn/") { @@ -26,7 +26,12 @@ class FiresideData { } Future fatchData() async { - var response = await Dio().get(parseLink(link)); + var options = BaseOptions( + connectTimeout: 20000, + receiveTimeout: 20000, + ); + + var response = await Dio(options).get(parseLink(link)); if (response.statusCode == 200) { var doc = parse(response.data); var reg = RegExp(r'https(.+)jpg'); @@ -54,12 +59,12 @@ class FiresideData { backgroundImage, json.encode({'hosts': hosts.map((host) => host.toJson()).toList()}) ]; - await dbHelper.saveFiresideData(data); + await _dbHelper.saveFiresideData(data); } } Future getData() async { - var data = await dbHelper.getFiresideData(id); + var data = await _dbHelper.getFiresideData(id); _background = data[0]; if (data[1] != '') { _hosts = json diff --git a/lib/type/play_histroy.dart b/lib/type/play_histroy.dart index e8c6bb9..069e1d6 100644 --- a/lib/type/play_histroy.dart +++ b/lib/type/play_histroy.dart @@ -2,7 +2,7 @@ import '../local_storage/sqflite_localpodcast.dart'; import 'episodebrief.dart'; class PlayHistory { - DBHelper dbHelper = DBHelper(); + final DBHelper _dbHelper = DBHelper(); /// Episdoe title. String title; @@ -26,6 +26,6 @@ class PlayHistory { EpisodeBrief get episode => _episode; getEpisode() async { - _episode = await dbHelper.getRssItemWithUrl(url); + _episode = await _dbHelper.getRssItemWithUrl(url); } } diff --git a/lib/type/playlist.dart b/lib/type/playlist.dart index fda4f28..433bd5a 100644 --- a/lib/type/playlist.dart +++ b/lib/type/playlist.dart @@ -4,28 +4,28 @@ import 'episodebrief.dart'; class Playlist { String name; - DBHelper dbHelper = DBHelper(); + final DBHelper _dbHelper = DBHelper(); List _playlist; List get playlist => _playlist; - KeyValueStorage storage = KeyValueStorage('playlist'); + final KeyValueStorage _playlistStorage = KeyValueStorage(playlistKey); - getPlaylist() async { - var urls = await storage.getStringList(); + Future getPlaylist() async { + var urls = await _playlistStorage.getStringList(); if (urls.length == 0) { _playlist = []; } else { _playlist = []; for (var url in urls) { - var episode = await dbHelper.getRssItemWithUrl(url); + var episode = await _dbHelper.getRssItemWithUrl(url); if (episode != null) _playlist.add(episode); } } } - savePlaylist() async { + Future savePlaylist() async { var urls = []; urls.addAll(_playlist.map((e) => e.enclosureUrl)); - await storage.saveStringList(urls.toSet().toList()); + await _playlistStorage.saveStringList(urls.toSet().toList()); } Future addToPlayList(EpisodeBrief episodeBrief) async { @@ -33,7 +33,7 @@ class Playlist { _playlist.add(episodeBrief); await savePlaylist(); if (episodeBrief.isNew == 1) { - await dbHelper.removeEpisodeNewMark(episodeBrief.enclosureUrl); + await _dbHelper.removeEpisodeNewMark(episodeBrief.enclosureUrl); } } } @@ -44,7 +44,7 @@ class Playlist { _playlist.removeWhere( (episode) => episode.enclosureUrl == episodeBrief.enclosureUrl); if (episodeBrief.isNew == 1) { - await dbHelper.removeEpisodeNewMark(episodeBrief.enclosureUrl); + await _dbHelper.removeEpisodeNewMark(episodeBrief.enclosureUrl); } } _playlist.insert(index, episodeBrief); diff --git a/lib/type/podcastlocal.dart b/lib/type/podcastlocal.dart index 955a347..b2b56c4 100644 --- a/lib/type/podcastlocal.dart +++ b/lib/type/podcastlocal.dart @@ -1,6 +1,6 @@ -import 'dart:ui'; +import 'package:equatable/equatable.dart'; -class PodcastLocal { +class PodcastLocal extends Equatable { final String title; final String imageUrl; final String rssUrl; @@ -13,18 +13,22 @@ class PodcastLocal { final String link; final String description; - int upateCount; - int episodeCount; + + int _upateCount; + int get updateCount => _upateCount; + set updateCount(i) => _upateCount = i; + + int _episodeCount; + int get episodeCount => _episodeCount; + set episodeCount(i) => _episodeCount = i; + PodcastLocal(this.title, this.imageUrl, this.rssUrl, this.primaryColor, this.author, this.id, this.imagePath, this.provider, this.link, - {this.description = '', this.upateCount = 0, this.episodeCount = 0}) - : assert(rssUrl != null); - @override - bool operator ==(Object podcastLocal) => - podcastLocal is PodcastLocal && - podcastLocal.rssUrl == rssUrl && - podcastLocal.id == id; + {this.description = '', int upateCount, int episodeCount}) + : assert(rssUrl != null), + _episodeCount = episodeCount ?? 0, + _upateCount = upateCount ?? 0; @override - int get hashCode => hashValues(id, rssUrl); + List get props => [id, rssUrl]; } diff --git a/pubspec.yaml b/pubspec.yaml index 9d1c829..517f8fa 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: tsacdop -description: An easy-use podacasts player. +description: An open source podacasts player. version: 0.4.11+28 @@ -20,6 +20,7 @@ dependencies: dio: ^3.0.9 extended_nested_scroll_view: ^1.0.1 effective_dart: ^1.2.4 + equatable: ^1.2.3 feature_discovery: ^0.10.0 file_picker: ^1.12.0 flutter_html: ^0.11.1