Merge pull request #145 from GallowsDove/new-download-fix

fix: fix new download caching
This commit is contained in:
CappielloAntonio 2024-01-28 18:24:43 +01:00 committed by GitHub
commit 33aa38e885
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 17 deletions

View file

@ -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<MediaItem> 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);
}
}

View file

@ -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);
}
}
}