diff --git a/lib/local_storage/sqflite_localpodcast.dart b/lib/local_storage/sqflite_localpodcast.dart index ce59cfb..994fa42 100644 --- a/lib/local_storage/sqflite_localpodcast.dart +++ b/lib/local_storage/sqflite_localpodcast.dart @@ -1,5 +1,3 @@ -import 'dart:io'; - import 'package:sqflite/sqflite.dart'; import 'dart:async'; import 'package:path/path.dart'; @@ -861,36 +859,32 @@ class DBHelper { // } // return episodes; //} - Future> getOutdatedEpisode(int days) async { + Future> getOutdatedEpisode(int deadline) async { var dbClient = await database; List episodes = []; - if (days > 0) { - int deadline = - DateTime.now().subtract(Duration(days: days)).millisecondsSinceEpoch; - List list = await dbClient - .rawQuery("""SELECT E.title, E.enclosure_url, E.enclosure_length, + List list = await dbClient + .rawQuery("""SELECT E.title, E.enclosure_url, E.enclosure_length, E.milliseconds, P.title as feed_title, E.duration, E.explicit, E.liked, E.downloaded, P.imagePath, P.primaryColor, E.media_id, E.is_new, P.skip_seconds FROM Episodes E INNER JOIN PodcastLocal P ON E.feed_id = P.id WHERE E.download_date < ? AND E.enclosure_url != E.media_id ORDER BY E.milliseconds DESC""", [deadline]); - for (int x = 0; x < list.length; x++) { - episodes.add(EpisodeBrief( - list[x]['title'], - list[x]['enclosure_url'], - list[x]['enclosure_length'], - list[x]['milliseconds'], - list[x]['feed_title'], - list[x]['primaryColor'], - list[x]['liked'], - list[x]['downloaded'], - list[x]['duration'], - list[x]['explicit'], - list[x]['imagePath'], - list[x]['media_id'], - list[x]['is_new'], - list[x]['skip_seconds'])); - } + for (int x = 0; x < list.length; x++) { + episodes.add(EpisodeBrief( + list[x]['title'], + list[x]['enclosure_url'], + list[x]['enclosure_length'], + list[x]['milliseconds'], + list[x]['feed_title'], + list[x]['primaryColor'], + list[x]['liked'], + list[x]['downloaded'], + list[x]['duration'], + list[x]['explicit'], + list[x]['imagePath'], + list[x]['media_id'], + list[x]['is_new'], + list[x]['skip_seconds'])); } return episodes; } @@ -1101,26 +1095,35 @@ class DBHelper { Future saveDownloaded(String url, String id) async { var dbClient = await database; int milliseconds = DateTime.now().millisecondsSinceEpoch; - int count = await dbClient.rawUpdate( - "UPDATE Episodes SET downloaded = ?, download_date = ? WHERE enclosure_url = ?", - [id, milliseconds, url]); + int count; + await dbClient.transaction((txn) async { + count = await txn.rawUpdate( + "UPDATE Episodes SET downloaded = ?, download_date = ? WHERE enclosure_url = ?", + [id, milliseconds, url]); + }); return count; } Future saveMediaId(String url, String path, String id, int size) async { var dbClient = await database; int milliseconds = DateTime.now().millisecondsSinceEpoch; - int count = await dbClient.rawUpdate( - "UPDATE Episodes SET enclosure_length = ?, media_id = ?, download_date = ?, downloaded = ? WHERE enclosure_url = ?", - [size, path, milliseconds, id, url]); + int count; + await dbClient.transaction((txn) async { + count = await txn.rawUpdate( + "UPDATE Episodes SET enclosure_length = ?, media_id = ?, download_date = ?, downloaded = ? WHERE enclosure_url = ?", + [size, path, milliseconds, id, url]); + }); return count; } Future delDownloaded(String url) async { var dbClient = await database; - int count = await dbClient.rawUpdate( - "UPDATE Episodes SET downloaded = 'ND', media_id = ? WHERE enclosure_url = ?", - [url, url]); + int count; + await dbClient.transaction((txn) async { + count = await txn.rawUpdate( + "UPDATE Episodes SET downloaded = 'ND', media_id = ? WHERE enclosure_url = ?", + [url, url]); + }); print('Deleted ' + url); return count; } diff --git a/lib/state/download_state.dart b/lib/state/download_state.dart index 9750ae1..374e25d 100644 --- a/lib/state/download_state.dart +++ b/lib/state/download_state.dart @@ -225,7 +225,6 @@ class DownloadState extends ChangeNotifier { } EpisodeTask episodeToTask(EpisodeBrief episode) { - print(_episodeTasks.first.episode.description); return _episodeTasks .firstWhere((task) => task.episode.enclosureUrl == episode.enclosureUrl, orElse: () { @@ -322,8 +321,7 @@ class DownloadState extends ChangeNotifier { } _removeTask(EpisodeBrief episode) { - _episodeTasks.removeWhere( - (element) => element.episode.enclosureUrl == episode.enclosureUrl); + _episodeTasks.removeWhere((element) => element.episode == episode); } _autoDelete() async { @@ -333,11 +331,21 @@ class DownloadState extends ChangeNotifier { if (autoDelete == 0) await autoDeleteStorage.saveInt(30); else if (autoDelete > 0) { - List episodes = - await dbHelper.getOutdatedEpisode(autoDelete); + int deadline = DateTime.now() + .subtract(Duration(days: autoDelete)) + .millisecondsSinceEpoch; + List episodes = await dbHelper.getOutdatedEpisode(deadline); if (episodes.length > 0) { - await Future.forEach(episodes, (episode) => delTask(episode)); + await Future.forEach( + episodes, (episode) async => await delTask(episode)); } + final tasks = await FlutterDownloader.loadTasksWithRawQuery( + query: + 'SELECT * FROM task WHERE time_created < $deadline AND status = 3'); + await Future.forEach( + tasks, + (task) async => FlutterDownloader.remove( + taskId: task.taskId, shouldDeleteContent: true)); } } } diff --git a/lib/type/episodebrief.dart b/lib/type/episodebrief.dart index 704ab32..2454b09 100644 --- a/lib/type/episodebrief.dart +++ b/lib/type/episodebrief.dart @@ -1,3 +1,5 @@ +import 'dart:ui'; + import 'package:intl/intl.dart'; import 'package:audio_service/audio_service.dart'; @@ -83,11 +85,11 @@ class EpisodeBrief { } @override - bool operator == (Object episode) => + bool operator ==(Object episode) => episode is EpisodeBrief && episode.title == title && episode.enclosureUrl == enclosureUrl; @override - int get hashCode => enclosureUrl.hashCode + title.hashCode; + int get hashCode => hashValues(enclosureUrl, title); } diff --git a/lib/type/podcastlocal.dart b/lib/type/podcastlocal.dart index a37123b..955a347 100644 --- a/lib/type/podcastlocal.dart +++ b/lib/type/podcastlocal.dart @@ -1,3 +1,5 @@ +import 'dart:ui'; + class PodcastLocal { final String title; final String imageUrl; @@ -24,5 +26,5 @@ class PodcastLocal { podcastLocal.id == id; @override - int get hashCode => id.hashCode + rssUrl.hashCode; + int get hashCode => hashValues(id, rssUrl); }