From 375501f28291e7b6c662f74afba011c39c1790dc Mon Sep 17 00:00:00 2001 From: GallowsDove Date: Tue, 16 Jan 2024 19:58:45 +0100 Subject: [PATCH] fix: Fix new download caching --- .../tempo/service/DownloaderManager.java | 35 +++++++++++-------- .../tempo/service/DownloaderService.java | 6 ++-- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderManager.java b/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderManager.java index 66f13910..0465ed02 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderManager.java +++ b/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderManager.java @@ -61,19 +61,11 @@ public class DownloaderManager { } public boolean isDownloaded(MediaItem mediaItem) { - @Nullable Download download = downloads.get(mediaItem.mediaId); - return download != null && download.state != Download.STATE_FAILED; + return isDownloaded(mediaItem.mediaId); } public boolean areDownloaded(List mediaItems) { - for (MediaItem mediaItem : mediaItems) { - @Nullable Download download = downloads.get(mediaItem.mediaId); - if (download != null && download.state != Download.STATE_FAILED) { - return true; - } - } - - return false; + return mediaItems.stream().anyMatch(this::isDownloaded); } public void download(MediaItem mediaItem, com.cappielloantonio.tempo.model.Download download) { @@ -91,6 +83,7 @@ public class DownloaderManager { public void remove(MediaItem mediaItem, com.cappielloantonio.tempo.model.Download download) { DownloadService.sendRemoveDownload(context, DownloaderService.class, buildDownloadRequest(mediaItem).id, false); + downloads.remove(download.getId()); deleteDatabase(download.getId()); } @@ -126,19 +119,31 @@ public class DownloaderManager { return new DownloadRepository(); } - public static void insertDatabase(com.cappielloantonio.tempo.model.Download download) { + private static void insertDatabase(com.cappielloantonio.tempo.model.Download download) { getDownloadRepository().insert(download); } - public static void deleteDatabase(String id) { + private static void deleteDatabase(String id) { getDownloadRepository().delete(id); } - public static void deleteAllDatabase() { + private static void deleteAllDatabase() { getDownloadRepository().deleteAll(); } - public static void updateDatabase(String id) { + private static void updateDatabase(String id) { getDownloadRepository().update(id); } -} + + public void updateRequestDownload(Download download) { + updateDatabase(download.request.id); + if (download.state == Download.STATE_COMPLETED) { + downloads.put(download.request.id, download); + } + } + + public void removeRequestDownload(Download download) { + deleteDatabase(download.request.id); + downloads.remove(download.request.id); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java b/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java index e34867a0..d75f16a4 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java +++ b/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java @@ -53,6 +53,7 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa private static final class TerminalStateNotificationHelper implements DownloadManager.Listener { private final Context context; private final DownloadNotificationHelper notificationHelper; + private final DownloaderManager downloaderManager; private final Notification successfulDownloadGroupNotification; private final Notification failedDownloadGroupNotification; @@ -65,6 +66,7 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa public TerminalStateNotificationHelper(Context context, DownloadNotificationHelper notificationHelper, int firstNotificationId) { this.context = context.getApplicationContext(); this.notificationHelper = notificationHelper; + this.downloaderManager = DownloadUtil.getDownloadTracker(context); nextNotificationId = firstNotificationId; successfulDownloadGroupNotification = DownloadUtil.buildGroupSummaryNotification( @@ -95,7 +97,7 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa notification = notificationHelper.buildDownloadCompletedNotification(context, R.drawable.ic_check_circle, null, DownloaderManager.getDownloadNotificationMessage(download.request.id)); notification = Notification.Builder.recoverBuilder(context, notification).setGroup(DownloadUtil.DOWNLOAD_NOTIFICATION_SUCCESSFUL_GROUP).build(); NotificationUtil.setNotification(this.context, successfulDownloadGroupNotificationId, successfulDownloadGroupNotification); - DownloaderManager.updateDatabase(download.request.id); + downloaderManager.updateRequestDownload(download); } else if (download.state == Download.STATE_FAILED) { notification = notificationHelper.buildDownloadFailedNotification(context, R.drawable.ic_error, null, DownloaderManager.getDownloadNotificationMessage(download.request.id)); notification = Notification.Builder.recoverBuilder(context, notification).setGroup(DownloadUtil.DOWNLOAD_NOTIFICATION_FAILED_GROUP).build(); @@ -109,7 +111,7 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa @Override public void onDownloadRemoved(@NonNull DownloadManager downloadManager, Download download) { - DownloaderManager.deleteDatabase(download.request.id); + downloaderManager.removeRequestDownload(download); } } }