Fixed bulk live download when user chooses to sync starred tracks

This commit is contained in:
antonio 2023-03-12 17:59:12 +01:00
parent 3f6f3ab06a
commit da7030bee1
7 changed files with 60 additions and 14 deletions

View file

@ -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 {

View file

@ -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<List<Download>> getAll();
@Insert(onConflict = OnConflictStrategy.REPLACE)
@ -21,6 +21,9 @@ public interface DownloadDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<Download> 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);

View file

@ -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

View file

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

View file

@ -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<MediaItem> mediaItems, List<com.cappielloantonio.play.model.Download> 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<MediaItem> mediaItems, List<com.cappielloantonio.play.model.Download> 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);
}
}

View file

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

View file

@ -68,6 +68,16 @@ public class DownloadHorizontalAdapter extends RecyclerView.Adapter<DownloadHori
return songs.get(id);
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ItemHorizontalDownloadBinding item;