This commit is contained in:
stonega 2021-01-03 01:33:45 +08:00
parent 54268cf8b9
commit ea611e9798
2 changed files with 25 additions and 38 deletions

View File

@ -30,7 +30,7 @@ void autoDownloadCallback(String id, DownloadTaskStatus status, int progress) {
//For background auto downlaod //For background auto downlaod
class AutoDownloader { class AutoDownloader {
DBHelper dbHelper = DBHelper(); final DBHelper _dbHelper = DBHelper();
final List<EpisodeTask> _episodeTasks = []; final List<EpisodeTask> _episodeTasks = [];
final Completer _completer = Completer(); final Completer _completer = Completer();
AutoDownloader() { AutoDownloader() {
@ -53,10 +53,8 @@ class AutoDownloader {
for (var episodeTask in _episodeTasks) { for (var episodeTask in _episodeTasks) {
if (episodeTask.taskId == id) { if (episodeTask.taskId == id) {
// episodeTask.status = status; episodeTask.status = status;
// episodeTask.progress = progress; episodeTask.progress = progress;
episodeTask =
episodeTask.copyWith(progress: progress, status: status);
if (status == DownloadTaskStatus.complete) { if (status == DownloadTaskStatus.complete) {
_saveMediaId(episodeTask); _saveMediaId(episodeTask);
} else if (status == DownloadTaskStatus.failed) { } else if (status == DownloadTaskStatus.failed) {
@ -90,7 +88,7 @@ class AutoDownloader {
var fileStat = await File( var fileStat = await File(
path.join(completeTask.first.savedDir, completeTask.first.filename)) path.join(completeTask.first.savedDir, completeTask.first.filename))
.stat(); .stat();
await dbHelper.saveMediaId(episodeTask.episode.enclosureUrl, filePath, await _dbHelper.saveMediaId(episodeTask.episode.enclosureUrl, filePath,
episodeTask.taskId, fileStat.size); episodeTask.taskId, fileStat.size);
_episodeTasks.removeWhere((element) => _episodeTasks.removeWhere((element) =>
element.episode.enclosureUrl == episodeTask.episode.enclosureUrl); element.episode.enclosureUrl == episodeTask.episode.enclosureUrl);
@ -135,7 +133,7 @@ class AutoDownloader {
//For download episode inside app //For download episode inside app
class DownloadState extends ChangeNotifier { class DownloadState extends ChangeNotifier {
DBHelper dbHelper = DBHelper(); final DBHelper _dbHelper = DBHelper();
List<EpisodeTask> _episodeTasks = []; List<EpisodeTask> _episodeTasks = [];
List<EpisodeTask> get episodeTasks => _episodeTasks; List<EpisodeTask> get episodeTasks => _episodeTasks;
@ -155,7 +153,7 @@ class DownloadState extends ChangeNotifier {
_episodeTasks = []; _episodeTasks = [];
var dbHelper = DBHelper(); var dbHelper = DBHelper();
var tasks = await FlutterDownloader.loadTasks(); var tasks = await FlutterDownloader.loadTasks();
if (tasks.length != 0) { if (tasks.isNotEmpty) {
for (var task in tasks) { for (var task in tasks) {
var episode = await dbHelper.getRssItemWithUrl(task.url); var episode = await dbHelper.getRssItemWithUrl(task.url);
if (episode == null) { if (episode == null) {
@ -215,10 +213,8 @@ class DownloadState extends ChangeNotifier {
for (var episodeTask in _episodeTasks) { for (var episodeTask in _episodeTasks) {
if (episodeTask.taskId == id) { if (episodeTask.taskId == id) {
//episodeTask.status = status; episodeTask.status = status;
//episodeTask.progress = progress; episodeTask.progress = progress;
episodeTask =
episodeTask.copyWith(progress: progress, status: status);
if (status == DownloadTaskStatus.complete) { if (status == DownloadTaskStatus.complete) {
_saveMediaId(episodeTask).then((value) { _saveMediaId(episodeTask).then((value) {
notifyListeners(); notifyListeners();
@ -232,7 +228,7 @@ class DownloadState extends ChangeNotifier {
} }
Future _saveMediaId(EpisodeTask episodeTask) async { Future _saveMediaId(EpisodeTask episodeTask) async {
episodeTask = episodeTask.copyWith(status: DownloadTaskStatus.complete); episodeTask.status = DownloadTaskStatus.complete;
final completeTask = await FlutterDownloader.loadTasksWithRawQuery( final completeTask = await FlutterDownloader.loadTasksWithRawQuery(
query: "SELECT * FROM task WHERE task_id = '${episodeTask.taskId}'"); query: "SELECT * FROM task WHERE task_id = '${episodeTask.taskId}'");
var filePath = var filePath =
@ -240,10 +236,10 @@ class DownloadState extends ChangeNotifier {
var fileStat = await File( var fileStat = await File(
path.join(completeTask.first.savedDir, completeTask.first.filename)) path.join(completeTask.first.savedDir, completeTask.first.filename))
.stat(); .stat();
dbHelper.saveMediaId(episodeTask.episode.enclosureUrl, filePath, _dbHelper.saveMediaId(episodeTask.episode.enclosureUrl, filePath,
episodeTask.taskId, fileStat.size); episodeTask.taskId, fileStat.size);
var episode = var episode =
await dbHelper.getRssItemWithUrl(episodeTask.episode.enclosureUrl); await _dbHelper.getRssItemWithUrl(episodeTask.episode.enclosureUrl);
_removeTask(episodeTask.episode); _removeTask(episodeTask.episode);
_episodeTasks.add(EpisodeTask(episode, episodeTask.taskId, _episodeTasks.add(EpisodeTask(episode, episodeTask.taskId,
progress: 100, status: DownloadTaskStatus.complete)); progress: 100, status: DownloadTaskStatus.complete));
@ -271,8 +267,7 @@ class DownloadState extends ChangeNotifier {
} }
Future startTask(EpisodeBrief episode, {bool showNotification = true}) async { Future startTask(EpisodeBrief episode, {bool showNotification = true}) async {
var dbHelper = DBHelper(); var isDownloaded = await _dbHelper.isDownloaded(episode.enclosureUrl);
var isDownloaded = await dbHelper.isDownloaded(episode.enclosureUrl);
if (!isDownloaded) { if (!isDownloaded) {
final dir = await _getDownloadDirectory(); final dir = await _getDownloadDirectory();
var localPath = var localPath =
@ -300,7 +295,7 @@ class DownloadState extends ChangeNotifier {
openFileFromNotification: false, openFileFromNotification: false,
); );
_episodeTasks.add(EpisodeTask(episode, taskId)); _episodeTasks.add(EpisodeTask(episode, taskId));
await dbHelper.saveDownloaded(episode.enclosureUrl, taskId); await _dbHelper.saveDownloaded(episode.enclosureUrl, taskId);
notifyListeners(); notifyListeners();
} }
} }
@ -320,7 +315,7 @@ class DownloadState extends ChangeNotifier {
var index = _episodeTasks.indexOf(task); var index = _episodeTasks.indexOf(task);
_episodeTasks[index] = task.copyWith(taskId: newTaskId); _episodeTasks[index] = task.copyWith(taskId: newTaskId);
notifyListeners(); notifyListeners();
await dbHelper.saveDownloaded(episode.enclosureUrl, newTaskId); await _dbHelper.saveDownloaded(episode.enclosureUrl, newTaskId);
} }
Future retryTask(EpisodeBrief episode) async { Future retryTask(EpisodeBrief episode) async {
@ -330,7 +325,7 @@ class DownloadState extends ChangeNotifier {
var index = _episodeTasks.indexOf(task); var index = _episodeTasks.indexOf(task);
_episodeTasks[index] = task.copyWith(taskId: newTaskId); _episodeTasks[index] = task.copyWith(taskId: newTaskId);
notifyListeners(); notifyListeners();
await dbHelper.saveDownloaded(episode.enclosureUrl, newTaskId); await _dbHelper.saveDownloaded(episode.enclosureUrl, newTaskId);
} }
Future removeTask(EpisodeBrief episode) async { Future removeTask(EpisodeBrief episode) async {
@ -343,12 +338,11 @@ class DownloadState extends ChangeNotifier {
var task = episodeToTask(episode); var task = episodeToTask(episode);
await FlutterDownloader.remove( await FlutterDownloader.remove(
taskId: task.taskId, shouldDeleteContent: true); taskId: task.taskId, shouldDeleteContent: true);
await dbHelper.delDownloaded(episode.enclosureUrl); await _dbHelper.delDownloaded(episode.enclosureUrl);
for (var episodeTask in _episodeTasks) { for (var episodeTask in _episodeTasks) {
if (episodeTask.taskId == task.taskId) { if (episodeTask.taskId == task.taskId) {
//episodeTask.status = DownloadTaskStatus.undefined; episodeTask.status = DownloadTaskStatus.undefined;
episodeTask = episodeTask.copyWith(status: DownloadTaskStatus.undefined);
} }
notifyListeners(); notifyListeners();
} }
@ -372,7 +366,7 @@ class DownloadState extends ChangeNotifier {
var deadline = DateTime.now() var deadline = DateTime.now()
.subtract(Duration(days: autoDelete)) .subtract(Duration(days: autoDelete))
.millisecondsSinceEpoch; .millisecondsSinceEpoch;
var episodes = await dbHelper.getOutdatedEpisode(deadline, var episodes = await _dbHelper.getOutdatedEpisode(deadline,
deletePlayed: deletePlayed); deletePlayed: deletePlayed);
if (episodes.isNotEmpty) { if (episodes.isNotEmpty) {
for (var episode in episodes) { for (var episode in episodes) {

View File

@ -1,25 +1,18 @@
import 'package:flutter_downloader/flutter_downloader.dart'; import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:equatable/equatable.dart';
import 'episodebrief.dart'; import 'episodebrief.dart';
class EpisodeTask extends Equatable { class EpisodeTask {
final String taskId; final String taskId;
final EpisodeBrief episode; final EpisodeBrief episode;
final int progress; int progress;
final DownloadTaskStatus status; DownloadTaskStatus status;
EpisodeTask( EpisodeTask(this.episode, this.taskId,
this.episode, {this.progress = 0, this.status = DownloadTaskStatus.undefined});
this.taskId, {
this.progress = 0,
this.status = DownloadTaskStatus.undefined,
});
EpisodeTask copyWith({String taskId, int progress, DownloadTaskStatus status}) { EpisodeTask copyWith(
{String taskId, int progress, DownloadTaskStatus status}) {
return EpisodeTask(episode, taskId ?? this.taskId, return EpisodeTask(episode, taskId ?? this.taskId,
progress: progress ?? this.progress, status: status ?? this.status); progress: progress ?? this.progress, status: status ?? this.status);
} }
@override
List<Object> get props => [taskId];
} }