From da7030bee1d78cab61f54ca512758e8e84029024 Mon Sep 17 00:00:00 2001 From: antonio Date: Sun, 12 Mar 2023 17:59:12 +0100 Subject: [PATCH] Fixed bulk live download when user chooses to sync starred tracks --- .../play/database/AppDatabase.java | 4 +-- .../play/database/dao/DownloadDao.java | 5 ++- .../cappielloantonio/play/model/Download.kt | 3 ++ .../play/repository/DownloadRepository.java | 33 +++++++++++++++---- .../play/service/DownloaderManager.java | 13 +++++--- .../play/service/DownloaderService.java | 6 ++++ .../ui/adapter/DownloadHorizontalAdapter.java | 10 ++++++ 7 files changed, 60 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/cappielloantonio/play/database/AppDatabase.java b/app/src/main/java/com/cappielloantonio/play/database/AppDatabase.java index 1659fd02..e7ef7ff5 100644 --- a/app/src/main/java/com/cappielloantonio/play/database/AppDatabase.java +++ b/app/src/main/java/com/cappielloantonio/play/database/AppDatabase.java @@ -20,9 +20,9 @@ import com.cappielloantonio.play.model.RecentSearch; import com.cappielloantonio.play.model.Server; @Database( - version = 61, + version = 62, entities = {Queue.class, Server.class, RecentSearch.class, Download.class, Chronology.class}, - autoMigrations = {@AutoMigration(from = 60, to = 61)} + autoMigrations = {@AutoMigration(from = 61, to = 62)} ) @TypeConverters({DateConverters.class}) public abstract class AppDatabase extends RoomDatabase { diff --git a/app/src/main/java/com/cappielloantonio/play/database/dao/DownloadDao.java b/app/src/main/java/com/cappielloantonio/play/database/dao/DownloadDao.java index 6d9ca49d..19f280c6 100644 --- a/app/src/main/java/com/cappielloantonio/play/database/dao/DownloadDao.java +++ b/app/src/main/java/com/cappielloantonio/play/database/dao/DownloadDao.java @@ -12,7 +12,7 @@ import java.util.List; @Dao public interface DownloadDao { - @Query("SELECT * FROM download ORDER BY album, track ASC") + @Query("SELECT * FROM download WHERE download_state = 1 ORDER BY artist, album, track ASC") LiveData> getAll(); @Insert(onConflict = OnConflictStrategy.REPLACE) @@ -21,6 +21,9 @@ public interface DownloadDao { @Insert(onConflict = OnConflictStrategy.REPLACE) void insertAll(List downloads); + @Query("UPDATE download SET download_state = 1 WHERE id = :id") + void update(String id); + @Query("DELETE FROM download WHERE id = :id") void delete(String id); diff --git a/app/src/main/java/com/cappielloantonio/play/model/Download.kt b/app/src/main/java/com/cappielloantonio/play/model/Download.kt index 800851db..dd507acd 100644 --- a/app/src/main/java/com/cappielloantonio/play/model/Download.kt +++ b/app/src/main/java/com/cappielloantonio/play/model/Download.kt @@ -17,6 +17,9 @@ class Download(@PrimaryKey override val id: String) : Child(id) { @ColumnInfo(name = "playlist_name") var playlistName: String? = null + @ColumnInfo(name = "download_state", defaultValue = "1") + var downloadState: Int = 0; + constructor(child: Child) : this(child.id) { parentId = child.parentId isDir = child.isDir diff --git a/app/src/main/java/com/cappielloantonio/play/repository/DownloadRepository.java b/app/src/main/java/com/cappielloantonio/play/repository/DownloadRepository.java index 5548d2d8..ae8b6c89 100644 --- a/app/src/main/java/com/cappielloantonio/play/repository/DownloadRepository.java +++ b/app/src/main/java/com/cappielloantonio/play/repository/DownloadRepository.java @@ -36,6 +36,27 @@ public class DownloadRepository { } } + public void update(String id) { + UpdateThreadSafe update = new UpdateThreadSafe(downloadDao, id); + Thread thread = new Thread(update); + thread.start(); + } + + private static class UpdateThreadSafe implements Runnable { + private final DownloadDao downloadDao; + private final String id; + + public UpdateThreadSafe(DownloadDao downloadDao, String id) { + this.downloadDao = downloadDao; + this.id = id; + } + + @Override + public void run() { + downloadDao.update(id); + } + } + public void insertAll(List downloads) { InsertAllThreadSafe insertAll = new InsertAllThreadSafe(downloadDao, downloads); Thread thread = new Thread(insertAll); @@ -76,24 +97,24 @@ public class DownloadRepository { } } - public void delete(Download download) { - DeleteThreadSafe delete = new DeleteThreadSafe(downloadDao, download); + public void delete(String id) { + DeleteThreadSafe delete = new DeleteThreadSafe(downloadDao, id); Thread thread = new Thread(delete); thread.start(); } private static class DeleteThreadSafe implements Runnable { private final DownloadDao downloadDao; - private final Download download; + private final String id; - public DeleteThreadSafe(DownloadDao downloadDao, Download download) { + public DeleteThreadSafe(DownloadDao downloadDao, String id) { this.downloadDao = downloadDao; - this.download = download; + this.id = id; } @Override public void run() { - downloadDao.delete(download.getId()); + downloadDao.delete(id); } } } diff --git a/app/src/main/java/com/cappielloantonio/play/service/DownloaderManager.java b/app/src/main/java/com/cappielloantonio/play/service/DownloaderManager.java index 0c15bf2b..6a3888e3 100644 --- a/app/src/main/java/com/cappielloantonio/play/service/DownloaderManager.java +++ b/app/src/main/java/com/cappielloantonio/play/service/DownloaderManager.java @@ -79,7 +79,7 @@ public class DownloaderManager { public void download(MediaItem mediaItem, com.cappielloantonio.play.model.Download download) { DownloadService.sendAddDownload(context, DownloaderService.class, buildDownloadRequest(mediaItem), false); - downloadDatabase(download); + insertDatabase(download); } public void download(List mediaItems, List downloads) { @@ -90,7 +90,6 @@ public class DownloaderManager { public void remove(MediaItem mediaItem, com.cappielloantonio.play.model.Download download) { DownloadService.sendRemoveDownload(context, DownloaderService.class, buildDownloadRequest(mediaItem).id, false); - removeDatabase(download); } public void remove(List mediaItems, List downloads) { @@ -114,11 +113,15 @@ public class DownloaderManager { return new DownloadRepository(); } - private static void downloadDatabase(com.cappielloantonio.play.model.Download download) { + public static void insertDatabase(com.cappielloantonio.play.model.Download download) { getDownloadRepository().insert(download); } - private static void removeDatabase(com.cappielloantonio.play.model.Download download) { - getDownloadRepository().delete(download); + public static void deleteDatabase(String id) { + getDownloadRepository().delete(id); + } + + public static void updateDatabase(String id) { + getDownloadRepository().update(id); } } diff --git a/app/src/main/java/com/cappielloantonio/play/service/DownloaderService.java b/app/src/main/java/com/cappielloantonio/play/service/DownloaderService.java index 72c4a119..182eecf3 100644 --- a/app/src/main/java/com/cappielloantonio/play/service/DownloaderService.java +++ b/app/src/main/java/com/cappielloantonio/play/service/DownloaderService.java @@ -69,6 +69,7 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa if (download.state == Download.STATE_COMPLETED) { notification = notificationHelper.buildDownloadCompletedNotification(context, R.drawable.ic_check_circle, null, Util.fromUtf8Bytes(download.request.data)); + DownloaderManager.updateDatabase(download.request.id); } else if (download.state == Download.STATE_FAILED) { notification = notificationHelper.buildDownloadFailedNotification(context, R.drawable.ic_error, null, Util.fromUtf8Bytes(download.request.data)); } else { @@ -77,5 +78,10 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa NotificationUtil.setNotification(context, nextNotificationId++, notification); } + + @Override + public void onDownloadRemoved(@NonNull DownloadManager downloadManager, Download download) { + DownloaderManager.deleteDatabase(download.request.id); + } } } diff --git a/app/src/main/java/com/cappielloantonio/play/ui/adapter/DownloadHorizontalAdapter.java b/app/src/main/java/com/cappielloantonio/play/ui/adapter/DownloadHorizontalAdapter.java index f9da5fdf..54620890 100644 --- a/app/src/main/java/com/cappielloantonio/play/ui/adapter/DownloadHorizontalAdapter.java +++ b/app/src/main/java/com/cappielloantonio/play/ui/adapter/DownloadHorizontalAdapter.java @@ -68,6 +68,16 @@ public class DownloadHorizontalAdapter extends RecyclerView.Adapter