2020-02-20 10:09:21 +01:00
|
|
|
import 'dart:core';
|
2020-07-14 17:45:45 +02:00
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:isolate';
|
|
|
|
import 'dart:math' as math;
|
2020-06-12 19:56:13 +02:00
|
|
|
|
2020-02-20 10:09:21 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2020-07-14 17:45:45 +02:00
|
|
|
import 'package:flutter_isolate/flutter_isolate.dart';
|
2020-02-20 10:09:21 +01:00
|
|
|
import 'package:tsacdop/local_storage/key_value_storage.dart';
|
|
|
|
import 'package:tsacdop/local_storage/sqflite_localpodcast.dart';
|
|
|
|
import 'package:uuid/uuid.dart';
|
2020-07-14 17:45:45 +02:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:image/image.dart' as img;
|
|
|
|
import 'package:color_thief_flutter/color_thief_flutter.dart';
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
|
|
|
import '../webfeed/webfeed.dart';
|
|
|
|
import '../local_storage/sqflite_localpodcast.dart';
|
|
|
|
import '../local_storage/key_value_storage.dart';
|
|
|
|
import '../type/fireside_data.dart';
|
2020-05-06 18:50:32 +02:00
|
|
|
import '../type/podcastlocal.dart';
|
2020-02-20 10:09:21 +01:00
|
|
|
|
|
|
|
class GroupEntity {
|
|
|
|
final String name;
|
|
|
|
final String id;
|
|
|
|
final String color;
|
|
|
|
final List<String> podcastList;
|
|
|
|
|
|
|
|
GroupEntity(this.name, this.id, this.color, this.podcastList);
|
|
|
|
|
|
|
|
Map<String, Object> toJson() {
|
|
|
|
return {'name': name, 'id': id, 'color': color, 'podcastList': podcastList};
|
|
|
|
}
|
|
|
|
|
|
|
|
static GroupEntity fromJson(Map<String, Object> json) {
|
|
|
|
List<String> list = List.from(json['podcastList']);
|
2020-02-21 16:04:02 +01:00
|
|
|
return GroupEntity(json['name'] as String, json['id'] as String,
|
|
|
|
json['color'] as String, list);
|
2020-02-20 10:09:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PodcastGroup {
|
2020-07-14 17:45:45 +02:00
|
|
|
/// Group name.
|
2020-02-20 10:09:21 +01:00
|
|
|
final String name;
|
2020-07-14 17:45:45 +02:00
|
|
|
|
2020-02-20 10:09:21 +01:00
|
|
|
final String id;
|
2020-07-14 17:45:45 +02:00
|
|
|
|
|
|
|
/// Group theme color, not used.
|
2020-02-20 10:09:21 +01:00
|
|
|
final String color;
|
2020-07-14 17:45:45 +02:00
|
|
|
|
|
|
|
/// Id lists of podcasts in group.
|
2020-02-20 16:44:42 +01:00
|
|
|
List<String> podcastList;
|
2020-02-20 10:09:21 +01:00
|
|
|
|
|
|
|
PodcastGroup(this.name,
|
|
|
|
{this.color = '#000000', String id, List<String> podcastList})
|
|
|
|
: id = id ?? Uuid().v4(),
|
|
|
|
podcastList = podcastList ?? [];
|
|
|
|
|
|
|
|
Future getPodcasts() async {
|
|
|
|
var dbHelper = DBHelper();
|
|
|
|
if (podcastList != []) {
|
2020-02-20 16:44:42 +01:00
|
|
|
_podcasts = await dbHelper.getPodcastLocal(podcastList);
|
2020-02-20 10:09:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 17:04:23 +01:00
|
|
|
Color getColor() {
|
|
|
|
if (color != '#000000') {
|
|
|
|
int colorInt = int.parse('FF' + color.toUpperCase(), radix: 16);
|
|
|
|
return Color(colorInt).withOpacity(1.0);
|
|
|
|
} else {
|
|
|
|
return Colors.blue[400];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 14:04:44 +02:00
|
|
|
///Podcast in group.
|
2020-02-20 10:09:21 +01:00
|
|
|
List<PodcastLocal> _podcasts;
|
2020-07-14 17:45:45 +02:00
|
|
|
List<PodcastLocal> get podcasts => _podcasts;
|
2020-07-13 14:04:44 +02:00
|
|
|
|
|
|
|
///Ordered podcast list.
|
2020-03-14 04:14:24 +01:00
|
|
|
List<PodcastLocal> _orderedPodcasts;
|
|
|
|
List<PodcastLocal> get ordereddPodcasts => _orderedPodcasts;
|
2020-03-31 18:36:20 +02:00
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
set setOrderedPodcasts(List<PodcastLocal> list) {
|
|
|
|
_orderedPodcasts = list;
|
|
|
|
}
|
2020-02-20 10:09:21 +01:00
|
|
|
|
|
|
|
GroupEntity toEntity() {
|
|
|
|
return GroupEntity(name, id, color, podcastList);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PodcastGroup fromEntity(GroupEntity entity) {
|
|
|
|
return PodcastGroup(
|
|
|
|
entity.name,
|
|
|
|
id: entity.id,
|
|
|
|
color: entity.color,
|
|
|
|
podcastList: entity.podcastList,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 17:45:45 +02:00
|
|
|
enum SubscribeState { none, start, subscribe, fetch, stop, exist, error }
|
|
|
|
|
|
|
|
class SubscribeItem {
|
|
|
|
///Rss url.
|
|
|
|
String url;
|
|
|
|
|
|
|
|
///Rss title.
|
|
|
|
String title;
|
|
|
|
|
|
|
|
/// Subscribe status.
|
|
|
|
SubscribeState subscribeState;
|
|
|
|
|
|
|
|
/// Podcast id.
|
|
|
|
String id;
|
|
|
|
|
|
|
|
///Avatar image link.
|
|
|
|
String imgUrl;
|
|
|
|
|
|
|
|
///Podcast group, default Home.
|
|
|
|
String group;
|
|
|
|
SubscribeItem(this.url, this.title,
|
|
|
|
{this.subscribeState = SubscribeState.none,
|
|
|
|
this.id = '',
|
|
|
|
this.imgUrl = '',
|
|
|
|
this.group = ''});
|
|
|
|
}
|
|
|
|
|
2020-02-20 10:09:21 +01:00
|
|
|
class GroupList extends ChangeNotifier {
|
2020-07-14 17:45:45 +02:00
|
|
|
/// List of all gourps.
|
|
|
|
List<PodcastGroup> _groups = [];
|
2020-06-11 17:13:10 +02:00
|
|
|
List<PodcastGroup> get groups => _groups;
|
2020-02-20 10:09:21 +01:00
|
|
|
|
2020-07-14 17:45:45 +02:00
|
|
|
DBHelper dbHelper = DBHelper();
|
|
|
|
|
|
|
|
/// Groups save in shared_prefrences.
|
2020-02-20 10:09:21 +01:00
|
|
|
KeyValueStorage storage = KeyValueStorage('groups');
|
|
|
|
|
2020-07-14 17:45:45 +02:00
|
|
|
//GroupList({List<PodcastGroup> groups}) : _groups = groups ?? [];
|
|
|
|
|
|
|
|
/// Default false, true during loading groups from storage.
|
2020-02-20 10:09:21 +01:00
|
|
|
bool _isLoading = false;
|
|
|
|
bool get isLoading => _isLoading;
|
|
|
|
|
2020-07-14 17:45:45 +02:00
|
|
|
/// Svae ordered gourps info before saved.
|
2020-03-31 18:36:20 +02:00
|
|
|
List<PodcastGroup> _orderChanged = [];
|
|
|
|
List<PodcastGroup> get orderChanged => _orderChanged;
|
|
|
|
|
2020-07-14 17:45:45 +02:00
|
|
|
/// Subscribe worker isolate
|
|
|
|
FlutterIsolate subIsolate;
|
|
|
|
ReceivePort receivePort;
|
|
|
|
SendPort subSendPort;
|
|
|
|
|
|
|
|
/// Current subsribe item from isolate.
|
|
|
|
SubscribeItem _currentSubscribeItem = SubscribeItem('', '');
|
|
|
|
SubscribeItem get currentSubscribeItem => _currentSubscribeItem;
|
|
|
|
|
|
|
|
/// Default false, true if subscribe isolate is created.
|
|
|
|
bool _created = false;
|
|
|
|
bool get created => _created;
|
|
|
|
|
|
|
|
/// Add subsribe item
|
|
|
|
SubscribeItem _subscribeItem;
|
|
|
|
setSubscribeItem(SubscribeItem item) async {
|
|
|
|
_subscribeItem = item;
|
|
|
|
await _start();
|
|
|
|
}
|
|
|
|
|
|
|
|
_setCurrentSubscribeItem(SubscribeItem item) {
|
|
|
|
_currentSubscribeItem = item;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future _start() async {
|
|
|
|
if (_created == false) {
|
|
|
|
await _createIsolate();
|
|
|
|
_created = true;
|
|
|
|
listen();
|
|
|
|
} else
|
|
|
|
subSendPort.send([
|
|
|
|
_subscribeItem.url,
|
|
|
|
_subscribeItem.title,
|
|
|
|
_subscribeItem.imgUrl,
|
|
|
|
_subscribeItem.group
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _createIsolate() async {
|
|
|
|
receivePort = ReceivePort();
|
|
|
|
subIsolate =
|
|
|
|
await FlutterIsolate.spawn(subIsolateEntryPoint, receivePort.sendPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Isolate listener to get subscrribe status.
|
|
|
|
void listen() {
|
|
|
|
receivePort.distinct().listen((message) {
|
|
|
|
if (message is SendPort) {
|
|
|
|
subSendPort = message;
|
|
|
|
subSendPort.send([
|
|
|
|
_subscribeItem.url,
|
|
|
|
_subscribeItem.title,
|
|
|
|
_subscribeItem.imgUrl,
|
|
|
|
_subscribeItem.group
|
|
|
|
]);
|
|
|
|
} else if (message is List) {
|
|
|
|
_setCurrentSubscribeItem(SubscribeItem(
|
|
|
|
message[1],
|
|
|
|
message[0],
|
|
|
|
subscribeState: SubscribeState.values[message[2]],
|
|
|
|
));
|
|
|
|
if (message.length == 5)
|
|
|
|
_subscribeNewPodcast(id: message[3], groupName: message[4]);
|
|
|
|
} else if (message is String && message == "done") {
|
|
|
|
subIsolate.kill();
|
|
|
|
subIsolate = null;
|
|
|
|
_currentSubscribeItem = SubscribeItem('', '');
|
|
|
|
_created = false;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 18:36:20 +02:00
|
|
|
void addToOrderChanged(PodcastGroup group) {
|
|
|
|
_orderChanged.add(group);
|
2020-03-14 04:14:24 +01:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void drlFromOrderChanged(String name) {
|
2020-03-31 18:36:20 +02:00
|
|
|
_orderChanged.removeWhere((group) => group.name == name);
|
2020-03-14 04:14:24 +01:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-03-31 18:36:20 +02:00
|
|
|
clearOrderChanged() async {
|
|
|
|
if (_orderChanged.length > 0) {
|
2020-07-13 09:41:59 +02:00
|
|
|
for (var group in _orderChanged) await group.getPodcasts();
|
2020-03-31 18:36:20 +02:00
|
|
|
_orderChanged.clear();
|
2020-04-18 06:48:02 +02:00
|
|
|
// notifyListeners();
|
2020-03-31 18:36:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 10:09:21 +01:00
|
|
|
@override
|
|
|
|
void addListener(VoidCallback listener) {
|
2020-06-11 17:13:10 +02:00
|
|
|
loadGroups().then((value) => super.addListener(listener));
|
2020-02-20 10:09:21 +01:00
|
|
|
}
|
|
|
|
|
2020-07-14 17:45:45 +02:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
subIsolate?.kill();
|
|
|
|
subIsolate = null;
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Load groups from storage at start.
|
2020-02-20 10:09:21 +01:00
|
|
|
Future loadGroups() async {
|
|
|
|
_isLoading = true;
|
|
|
|
notifyListeners();
|
|
|
|
storage.getGroups().then((loadgroups) async {
|
|
|
|
_groups.addAll(loadgroups.map((e) => PodcastGroup.fromEntity(e)));
|
2020-07-13 09:41:59 +02:00
|
|
|
for (var group in _groups) await group.getPodcasts();
|
2020-02-21 16:04:02 +01:00
|
|
|
_isLoading = false;
|
|
|
|
notifyListeners();
|
2020-02-20 10:09:21 +01:00
|
|
|
});
|
|
|
|
}
|
2020-04-18 06:48:02 +02:00
|
|
|
|
2020-07-14 17:45:45 +02:00
|
|
|
/// Update podcasts of each group
|
2020-04-18 06:48:02 +02:00
|
|
|
Future updateGroups() async {
|
2020-07-13 09:41:59 +02:00
|
|
|
for (var group in _groups) await group.getPodcasts();
|
2020-04-06 14:18:08 +02:00
|
|
|
notifyListeners();
|
|
|
|
}
|
2020-02-20 10:09:21 +01:00
|
|
|
|
2020-07-14 17:45:45 +02:00
|
|
|
/// Add new group.
|
2020-02-20 10:09:21 +01:00
|
|
|
Future addGroup(PodcastGroup podcastGroup) async {
|
2020-03-14 04:14:24 +01:00
|
|
|
_isLoading = true;
|
2020-02-20 10:09:21 +01:00
|
|
|
_groups.add(podcastGroup);
|
2020-06-11 17:13:10 +02:00
|
|
|
await _saveGroup();
|
2020-03-14 04:14:24 +01:00
|
|
|
_isLoading = false;
|
2020-02-20 10:09:21 +01:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-07-14 17:45:45 +02:00
|
|
|
/// Remove group.
|
2020-02-20 10:09:21 +01:00
|
|
|
Future delGroup(PodcastGroup podcastGroup) async {
|
2020-03-03 17:04:23 +01:00
|
|
|
_isLoading = true;
|
2020-07-13 09:41:59 +02:00
|
|
|
for (var podcast in podcastGroup.podcastList)
|
2020-03-03 17:04:23 +01:00
|
|
|
if (!_groups.first.podcastList.contains(podcast)) {
|
|
|
|
_groups[0].podcastList.insert(0, podcast);
|
|
|
|
}
|
2020-06-11 17:13:10 +02:00
|
|
|
await _saveGroup();
|
2020-03-03 17:04:23 +01:00
|
|
|
_groups.remove(podcastGroup);
|
|
|
|
await _groups[0].getPodcasts();
|
2020-03-14 04:14:24 +01:00
|
|
|
_isLoading = false;
|
2020-03-03 17:04:23 +01:00
|
|
|
notifyListeners();
|
2020-02-20 10:09:21 +01:00
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
updateGroup(PodcastGroup podcastGroup) async {
|
2020-02-20 10:09:21 +01:00
|
|
|
var oldGroup = _groups.firstWhere((it) => it.id == podcastGroup.id);
|
|
|
|
var index = _groups.indexOf(oldGroup);
|
|
|
|
_groups.replaceRange(index, index + 1, [podcastGroup]);
|
2020-03-03 17:04:23 +01:00
|
|
|
await podcastGroup.getPodcasts();
|
2020-02-20 10:09:21 +01:00
|
|
|
notifyListeners();
|
|
|
|
_saveGroup();
|
|
|
|
}
|
|
|
|
|
2020-06-11 17:13:10 +02:00
|
|
|
_saveGroup() async {
|
|
|
|
await storage.saveGroup(_groups.map((it) => it.toEntity()).toList());
|
2020-02-20 10:09:21 +01:00
|
|
|
}
|
|
|
|
|
2020-07-14 17:45:45 +02:00
|
|
|
/// Subscribe podcast from search result.
|
2020-02-20 10:09:21 +01:00
|
|
|
Future subscribe(PodcastLocal podcastLocal) async {
|
2020-02-21 16:04:02 +01:00
|
|
|
_groups[0].podcastList.insert(0, podcastLocal.id);
|
2020-06-11 17:13:10 +02:00
|
|
|
await _saveGroup();
|
2020-02-20 10:09:21 +01:00
|
|
|
await dbHelper.savePodcastLocal(podcastLocal);
|
|
|
|
await _groups[0].getPodcasts();
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-04-18 06:48:02 +02:00
|
|
|
Future updatePodcast(String id) async {
|
2020-04-23 19:46:36 +02:00
|
|
|
int counts = await dbHelper.getPodcastCounts(id);
|
2020-07-13 09:41:59 +02:00
|
|
|
for (var group in _groups)
|
2020-04-18 06:48:02 +02:00
|
|
|
if (group.podcastList.contains(id)) {
|
|
|
|
group.podcasts.firstWhere((podcast) => podcast.id == id)
|
2020-04-23 19:46:36 +02:00
|
|
|
..episodeCount = counts;
|
2020-03-31 18:36:20 +02:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 17:45:45 +02:00
|
|
|
/// Subscribe podcast from OMPL.
|
2020-07-16 15:55:41 +02:00
|
|
|
Future<bool> _subscribeNewPodcast(
|
|
|
|
{String id, String groupName = 'Home'}) async {
|
|
|
|
//List<String> groupNames = _groups.map((e) => e.name).toList();
|
2020-07-14 17:45:45 +02:00
|
|
|
for (PodcastGroup group in _groups) {
|
|
|
|
if (group.name == groupName) {
|
2020-07-16 15:55:41 +02:00
|
|
|
if (group.podcastList.contains(id))
|
|
|
|
return true;
|
|
|
|
else {
|
|
|
|
_isLoading = true;
|
|
|
|
notifyListeners();
|
2020-07-14 17:45:45 +02:00
|
|
|
group.podcastList.insert(0, id);
|
|
|
|
await _saveGroup();
|
|
|
|
await group.getPodcasts();
|
2020-07-16 15:55:41 +02:00
|
|
|
_isLoading = false;
|
2020-07-14 17:45:45 +02:00
|
|
|
notifyListeners();
|
2020-07-16 15:55:41 +02:00
|
|
|
return true;
|
2020-07-14 17:45:45 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-29 18:12:53 +02:00
|
|
|
}
|
2020-07-14 17:45:45 +02:00
|
|
|
_isLoading = true;
|
2020-07-16 15:55:41 +02:00
|
|
|
notifyListeners();
|
|
|
|
_groups.add(PodcastGroup(groupName, podcastList: [id]));
|
|
|
|
//_groups.last.podcastList.insert(0, id);
|
2020-07-14 17:45:45 +02:00
|
|
|
await _saveGroup();
|
|
|
|
await _groups.last.getPodcasts();
|
|
|
|
_isLoading = false;
|
|
|
|
notifyListeners();
|
2020-07-16 15:55:41 +02:00
|
|
|
return true;
|
2020-04-18 06:48:02 +02:00
|
|
|
}
|
|
|
|
|
2020-02-20 10:09:21 +01:00
|
|
|
List<PodcastGroup> getPodcastGroup(String id) {
|
2020-02-21 16:04:02 +01:00
|
|
|
List<PodcastGroup> result = [];
|
2020-07-13 09:41:59 +02:00
|
|
|
for (var group in _groups)
|
2020-02-20 10:09:21 +01:00
|
|
|
if (group.podcastList.contains(id)) {
|
|
|
|
result.add(group);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-03 17:04:23 +01:00
|
|
|
//Change podcast groups
|
2020-02-21 16:04:02 +01:00
|
|
|
changeGroup(String id, List<PodcastGroup> list) async {
|
|
|
|
_isLoading = true;
|
|
|
|
notifyListeners();
|
2020-07-13 09:41:59 +02:00
|
|
|
|
|
|
|
for (var group in getPodcastGroup(id)) {
|
2020-03-14 04:14:24 +01:00
|
|
|
if (list.contains(group)) {
|
|
|
|
list.remove(group);
|
|
|
|
} else {
|
|
|
|
group.podcastList.remove(id);
|
|
|
|
}
|
2020-07-13 09:41:59 +02:00
|
|
|
}
|
|
|
|
for (var s in list) s.podcastList.insert(0, id);
|
2020-06-11 17:13:10 +02:00
|
|
|
await _saveGroup();
|
2020-07-13 09:41:59 +02:00
|
|
|
for (var group in _groups) await group.getPodcasts();
|
2020-02-21 16:04:02 +01:00
|
|
|
_isLoading = false;
|
2020-02-20 10:09:21 +01:00
|
|
|
notifyListeners();
|
2020-02-20 16:44:42 +01:00
|
|
|
}
|
|
|
|
|
2020-07-14 17:45:45 +02:00
|
|
|
/// Unsubscribe podcast
|
2020-02-21 16:04:02 +01:00
|
|
|
removePodcast(String id) async {
|
|
|
|
_isLoading = true;
|
|
|
|
notifyListeners();
|
2020-07-13 09:41:59 +02:00
|
|
|
for (var group in _groups) group.podcastList.remove(id);
|
2020-06-11 17:13:10 +02:00
|
|
|
await _saveGroup();
|
2020-02-20 16:44:42 +01:00
|
|
|
await dbHelper.delPodcastLocal(id);
|
2020-07-13 09:41:59 +02:00
|
|
|
for (var group in _groups) await group.getPodcasts();
|
2020-02-21 16:04:02 +01:00
|
|
|
_isLoading = false;
|
|
|
|
notifyListeners();
|
2020-02-20 16:44:42 +01:00
|
|
|
}
|
|
|
|
|
2020-03-14 04:14:24 +01:00
|
|
|
saveOrder(PodcastGroup group) async {
|
|
|
|
group.podcastList = group.ordereddPodcasts.map((e) => e.id).toList();
|
2020-06-11 17:13:10 +02:00
|
|
|
await _saveGroup();
|
2020-02-21 16:04:02 +01:00
|
|
|
await group.getPodcasts();
|
|
|
|
notifyListeners();
|
2020-02-20 10:09:21 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-14 17:45:45 +02:00
|
|
|
|
|
|
|
Future<void> subIsolateEntryPoint(SendPort sendPort) async {
|
|
|
|
List<SubscribeItem> items = [];
|
|
|
|
bool _running = false;
|
|
|
|
final List<String> listColor = [
|
|
|
|
'388E3C',
|
|
|
|
'1976D2',
|
|
|
|
'D32F2F',
|
|
|
|
'00796B',
|
|
|
|
];
|
|
|
|
ReceivePort subReceivePort = ReceivePort();
|
|
|
|
sendPort.send(subReceivePort.sendPort);
|
|
|
|
|
|
|
|
Future<String> _getColor(File file) async {
|
|
|
|
final imageProvider = FileImage(file);
|
|
|
|
var colorImage = await getImageFromProvider(imageProvider);
|
|
|
|
var color = await getColorFromImage(colorImage);
|
|
|
|
String primaryColor = color.toString();
|
|
|
|
return primaryColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _subscribe(SubscribeItem item) async {
|
|
|
|
var dbHelper = DBHelper();
|
|
|
|
String rss = item.url;
|
|
|
|
sendPort.send([item.title, item.url, 1]);
|
2020-07-18 10:00:52 +02:00
|
|
|
BaseOptions options = BaseOptions(
|
2020-07-14 17:45:45 +02:00
|
|
|
connectTimeout: 20000,
|
|
|
|
receiveTimeout: 20000,
|
|
|
|
);
|
|
|
|
print(rss);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Response response = await Dio(options).get(rss);
|
|
|
|
RssFeed p;
|
|
|
|
try {
|
|
|
|
p = RssFeed.parse(response.data);
|
2020-07-18 10:00:52 +02:00
|
|
|
} catch (e) {
|
2020-07-14 17:45:45 +02:00
|
|
|
print(e);
|
|
|
|
sendPort.send([item.title, item.url, 6]);
|
|
|
|
await Future.delayed(Duration(seconds: 2));
|
|
|
|
sendPort.send([item.title, item.url, 4]);
|
|
|
|
items.removeWhere((element) => element.url == item.url);
|
|
|
|
if (items.isNotEmpty) {
|
|
|
|
await _subscribe(items.first);
|
|
|
|
} else
|
|
|
|
sendPort.send("done");
|
|
|
|
}
|
|
|
|
|
|
|
|
var dir = await getApplicationDocumentsDirectory();
|
|
|
|
|
|
|
|
String realUrl =
|
|
|
|
response.redirects.isEmpty ? rss : response.realUri.toString();
|
|
|
|
|
2020-07-16 12:21:19 +02:00
|
|
|
String checkUrl = await dbHelper.checkPodcast(realUrl);
|
2020-07-14 17:45:45 +02:00
|
|
|
|
2020-07-16 12:21:19 +02:00
|
|
|
/// If url not existe in database.
|
|
|
|
if (checkUrl == '') {
|
2020-07-14 17:45:45 +02:00
|
|
|
img.Image thumbnail;
|
|
|
|
String imageUrl;
|
|
|
|
try {
|
2020-07-18 10:00:52 +02:00
|
|
|
Response<List<int>> imageResponse =
|
|
|
|
await Dio().get<List<int>>(p.itunes.image.href,
|
|
|
|
options: Options(
|
|
|
|
responseType: ResponseType.bytes,
|
|
|
|
receiveTimeout: 60000,
|
|
|
|
));
|
2020-07-14 17:45:45 +02:00
|
|
|
imageUrl = p.itunes.image.href;
|
|
|
|
img.Image image = img.decodeImage(imageResponse.data);
|
|
|
|
thumbnail = img.copyResize(image, width: 300);
|
|
|
|
} catch (e) {
|
|
|
|
try {
|
2020-07-18 10:00:52 +02:00
|
|
|
Response<List<int>> imageResponse = await Dio(BaseOptions(
|
|
|
|
connectTimeout: 20000,
|
|
|
|
receiveTimeout: 60000,
|
|
|
|
)).get<List<int>>(item.imgUrl,
|
2020-07-14 17:45:45 +02:00
|
|
|
options: Options(responseType: ResponseType.bytes));
|
|
|
|
imageUrl = item.imgUrl;
|
|
|
|
img.Image image = img.decodeImage(imageResponse.data);
|
|
|
|
thumbnail = img.copyResize(image, width: 300);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
try {
|
|
|
|
int index = math.Random().nextInt(3);
|
|
|
|
Response<List<int>> imageResponse = await Dio().get<List<int>>(
|
|
|
|
"https://ui-avatars.com/api/?size=300&background="
|
|
|
|
"${listColor[index]}&color=fff&name=${item.title}&length=2&bold=true",
|
|
|
|
options: Options(responseType: ResponseType.bytes));
|
|
|
|
imageUrl = "https://ui-avatars.com/api/?size=300&background="
|
|
|
|
"${listColor[index]}&color=fff&name=${item.title}&length=2&bold=true";
|
|
|
|
thumbnail = img.decodeImage(imageResponse.data);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
sendPort.send([item.title, item.url, 6]);
|
|
|
|
await Future.delayed(Duration(seconds: 2));
|
|
|
|
sendPort.send([item.title, item.url, 4]);
|
|
|
|
items.removeWhere((element) => element.url == item.url);
|
|
|
|
if (items.length > 0) {
|
|
|
|
await _subscribe(items.first);
|
|
|
|
} else
|
|
|
|
sendPort.send("done");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String uuid = Uuid().v4();
|
|
|
|
File("${dir.path}/$uuid.png")
|
|
|
|
..writeAsBytesSync(img.encodePng(thumbnail));
|
|
|
|
|
|
|
|
String imagePath = "${dir.path}/$uuid.png";
|
|
|
|
String primaryColor = await _getColor(File("${dir.path}/$uuid.png"));
|
|
|
|
String author = p.itunes.author ?? p.author ?? '';
|
|
|
|
String provider = p.generator ?? '';
|
|
|
|
String link = p.link ?? '';
|
|
|
|
PodcastLocal podcastLocal = PodcastLocal(p.title, imageUrl, realUrl,
|
|
|
|
primaryColor, author, uuid, imagePath, provider, link,
|
|
|
|
description: p.description);
|
|
|
|
|
|
|
|
await dbHelper.savePodcastLocal(podcastLocal);
|
|
|
|
sendPort.send([item.title, item.url, 2, uuid, item.group]);
|
|
|
|
if (provider.contains('fireside')) {
|
|
|
|
FiresideData data = FiresideData(uuid, link);
|
|
|
|
try {
|
|
|
|
await data.fatchData();
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await dbHelper.savePodcastRss(p, uuid);
|
|
|
|
|
|
|
|
sendPort.send([item.title, item.url, 3, uuid]);
|
|
|
|
|
|
|
|
await Future.delayed(Duration(seconds: 2));
|
|
|
|
|
|
|
|
sendPort.send([item.title, item.url, 4]);
|
2020-07-16 15:55:41 +02:00
|
|
|
items.removeAt(0);
|
2020-07-14 17:45:45 +02:00
|
|
|
if (items.length > 0) {
|
|
|
|
await _subscribe(items.first);
|
|
|
|
} else
|
|
|
|
sendPort.send("done");
|
|
|
|
} else {
|
2020-07-16 12:21:19 +02:00
|
|
|
sendPort.send([item.title, realUrl, 5, checkUrl, item.group]);
|
2020-07-14 17:45:45 +02:00
|
|
|
await Future.delayed(Duration(seconds: 2));
|
|
|
|
sendPort.send([item.title, item.url, 4]);
|
2020-07-16 15:55:41 +02:00
|
|
|
items.removeAt(0);
|
2020-07-14 17:45:45 +02:00
|
|
|
if (items.length > 0) {
|
|
|
|
await _subscribe(items.first);
|
|
|
|
} else
|
|
|
|
sendPort.send("done");
|
|
|
|
}
|
2020-07-18 10:00:52 +02:00
|
|
|
} catch (e) {
|
2020-07-14 17:45:45 +02:00
|
|
|
print(e);
|
|
|
|
sendPort.send([item.title, item.url, 6]);
|
|
|
|
await Future.delayed(Duration(seconds: 2));
|
|
|
|
sendPort.send([item.title, item.url, 4]);
|
|
|
|
items.removeWhere((element) => element.url == item.url);
|
|
|
|
if (items.length > 0) {
|
|
|
|
await _subscribe(items.first);
|
|
|
|
} else
|
|
|
|
sendPort.send("done");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subReceivePort.distinct().listen((message) {
|
|
|
|
if (message is List<String>) {
|
|
|
|
items.add(SubscribeItem(message[0], message[1],
|
|
|
|
imgUrl: message[2], group: message[3]));
|
|
|
|
if (!_running) {
|
|
|
|
_subscribe(items.first);
|
|
|
|
_running = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|