2020-04-18 06:48:02 +02:00
|
|
|
import 'dart:isolate';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_isolate/flutter_isolate.dart';
|
|
|
|
import 'package:tsacdop/local_storage/key_value_storage.dart';
|
|
|
|
import 'package:tsacdop/local_storage/sqflite_localpodcast.dart';
|
|
|
|
|
2020-05-06 18:50:32 +02:00
|
|
|
import '../type/podcastlocal.dart';
|
2020-04-18 06:48:02 +02:00
|
|
|
|
|
|
|
enum RefreshState { none, fetch, error }
|
|
|
|
|
|
|
|
class RefreshItem {
|
|
|
|
String title;
|
|
|
|
RefreshState refreshState;
|
|
|
|
RefreshItem(this.title, this.refreshState);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _listen() {
|
|
|
|
receivePort.distinct().listen((message) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> start() async {
|
|
|
|
if (!_created) {
|
|
|
|
_complete = false;
|
|
|
|
_createIsolate();
|
|
|
|
_listen();
|
|
|
|
_created = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dispose() {
|
|
|
|
refreshIsolate?.kill();
|
|
|
|
refreshIsolate = null;
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> refreshIsolateEntryPoint(SendPort sendPort) async {
|
2020-04-22 20:10:57 +02:00
|
|
|
KeyValueStorage refreshstorage = KeyValueStorage(refreshdateKey);
|
|
|
|
await refreshstorage.saveInt(DateTime.now().millisecondsSinceEpoch);
|
2020-04-18 06:48:02 +02:00
|
|
|
var dbHelper = DBHelper();
|
|
|
|
List<PodcastLocal> podcastList = await dbHelper.getPodcastLocalAll();
|
2020-04-22 20:10:57 +02:00
|
|
|
//int i = 0;
|
|
|
|
await Future.forEach<PodcastLocal>(podcastList, (podcastLocal) async {
|
2020-04-18 06:48:02 +02:00
|
|
|
sendPort.send([podcastLocal.title, 1]);
|
2020-04-22 20:10:57 +02:00
|
|
|
await dbHelper.updatePodcastRss(podcastLocal);
|
|
|
|
print('Refresh ' + podcastLocal.title);
|
2020-04-18 06:48:02 +02:00
|
|
|
});
|
2020-04-22 20:10:57 +02:00
|
|
|
|
|
|
|
// KeyValueStorage refreshcountstorage = KeyValueStorage('refreshcount');
|
|
|
|
// await refreshcountstorage.saveInt(i);
|
2020-04-18 06:48:02 +02:00
|
|
|
sendPort.send("done");
|
|
|
|
}
|