2020-08-10 20:41:22 +02:00
|
|
|
import 'dart:developer' as developer;
|
2020-04-18 06:48:02 +02:00
|
|
|
import 'dart:isolate';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_isolate/flutter_isolate.dart';
|
|
|
|
|
2020-06-10 09:42:40 +02:00
|
|
|
import '../local_storage/key_value_storage.dart';
|
|
|
|
import '../local_storage/sqflite_localpodcast.dart';
|
2020-04-18 06:48:02 +02:00
|
|
|
|
2020-07-18 11:52:31 +02:00
|
|
|
enum RefreshState { none, fetch, error, artwork }
|
2020-04-18 06:48:02 +02:00
|
|
|
|
|
|
|
class RefreshItem {
|
|
|
|
String title;
|
|
|
|
RefreshState refreshState;
|
2020-07-18 11:52:31 +02:00
|
|
|
bool artwork;
|
|
|
|
RefreshItem(this.title, this.refreshState, {this.artwork = false});
|
2020-04-18 06:48:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class RefreshWorker extends ChangeNotifier {
|
|
|
|
FlutterIsolate refreshIsolate;
|
|
|
|
ReceivePort receivePort;
|
|
|
|
SendPort refreshSendPort;
|
|
|
|
|
|
|
|
RefreshItem _currentRefreshItem = RefreshItem('', RefreshState.none);
|
|
|
|
bool _complete = false;
|
|
|
|
RefreshItem get currentRefreshItem => _currentRefreshItem;
|
|
|
|
bool get complete => _complete;
|
|
|
|
|
|
|
|
bool _created = false;
|
|
|
|
|
|
|
|
Future<void> _createIsolate() async {
|
|
|
|
receivePort = ReceivePort();
|
|
|
|
refreshIsolate = await FlutterIsolate.spawn(
|
|
|
|
refreshIsolateEntryPoint, receivePort.sendPort);
|
|
|
|
}
|
|
|
|
|
2020-09-26 16:26:25 +02:00
|
|
|
void _listen(List<String> podcasts) {
|
2020-04-18 06:48:02 +02:00
|
|
|
receivePort.distinct().listen((message) {
|
2020-09-26 16:26:25 +02:00
|
|
|
if (message is SendPort) {
|
|
|
|
refreshSendPort = message;
|
|
|
|
refreshSendPort.send(podcasts);
|
|
|
|
}
|
2020-04-18 06:48:02 +02:00
|
|
|
if (message is List) {
|
|
|
|
_currentRefreshItem =
|
|
|
|
RefreshItem(message[0], RefreshState.values[message[1]]);
|
|
|
|
notifyListeners();
|
|
|
|
} else if (message is String && message == "done") {
|
|
|
|
_currentRefreshItem = RefreshItem('', RefreshState.none);
|
|
|
|
_complete = true;
|
|
|
|
notifyListeners();
|
|
|
|
refreshIsolate?.kill();
|
|
|
|
refreshIsolate = null;
|
|
|
|
_created = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-26 16:26:25 +02:00
|
|
|
Future<void> start(List<String> podcasts) async {
|
2020-04-18 06:48:02 +02:00
|
|
|
if (!_created) {
|
2020-09-30 18:15:41 +02:00
|
|
|
if (podcasts.isEmpty) {
|
|
|
|
final refreshstorage = KeyValueStorage(refreshdateKey);
|
|
|
|
await refreshstorage.saveInt(DateTime.now().millisecondsSinceEpoch);
|
|
|
|
}
|
2020-04-18 06:48:02 +02:00
|
|
|
_complete = false;
|
2020-09-26 16:26:25 +02:00
|
|
|
await _createIsolate();
|
|
|
|
_listen(podcasts);
|
2020-04-18 06:48:02 +02:00
|
|
|
_created = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dispose() {
|
|
|
|
refreshIsolate?.kill();
|
|
|
|
refreshIsolate = null;
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> refreshIsolateEntryPoint(SendPort sendPort) async {
|
2020-09-26 16:26:25 +02:00
|
|
|
var refreshReceivePort = ReceivePort();
|
|
|
|
sendPort.send(refreshReceivePort.sendPort);
|
|
|
|
var _dbHelper = DBHelper();
|
|
|
|
|
|
|
|
Future<void> _refreshAll(List<String> podcasts) async {
|
|
|
|
var podcastList;
|
|
|
|
if (podcasts.isEmpty) {
|
|
|
|
podcastList = await _dbHelper.getPodcastLocalAll(updateOnly: true);
|
|
|
|
} else {
|
|
|
|
podcastList = await _dbHelper.getPodcastLocal(podcasts, updateOnly: true);
|
|
|
|
}
|
|
|
|
for (var podcastLocal in podcastList) {
|
|
|
|
sendPort.send([podcastLocal.title, 1]);
|
|
|
|
var updateCount = await _dbHelper.updatePodcastRss(podcastLocal);
|
|
|
|
developer.log('Refresh ${podcastLocal.title}$updateCount');
|
|
|
|
}
|
|
|
|
sendPort.send("done");
|
2020-07-13 09:41:59 +02:00
|
|
|
}
|
2020-09-26 16:26:25 +02:00
|
|
|
|
|
|
|
refreshReceivePort.distinct().listen((message) {
|
|
|
|
if (message is List<dynamic>) {
|
|
|
|
_refreshAll(message);
|
|
|
|
}
|
|
|
|
});
|
2020-04-18 06:48:02 +02:00
|
|
|
}
|