mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
Saved in database downloaded playlists
This commit is contained in:
parent
b41e3e641b
commit
d2ad0188b7
6 changed files with 16 additions and 32 deletions
|
|
@ -67,6 +67,11 @@ public class DownloaderTracker {
|
|||
return DownloadHelper.forMediaItem(context, mediaItem).getDownloadRequest(Util.getUtf8Bytes(checkNotNull(mediaItem.mediaId)));
|
||||
}
|
||||
|
||||
public boolean isDownloaded(Uri uri) {
|
||||
@Nullable Download download = downloads.get(uri);
|
||||
return download != null && download.state != Download.STATE_FAILED;
|
||||
}
|
||||
|
||||
@SuppressLint("UnsafeOptInUsageError")
|
||||
public boolean isDownloaded(MediaItem mediaItem) {
|
||||
@Nullable Download download = downloads.get(checkNotNull(mediaItem.localConfiguration).uri);
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ public class StarredSyncDialog extends DialogFragment {
|
|||
if (songs != null) {
|
||||
DownloadUtil.getDownloadTracker(context).download(
|
||||
MappingUtil.mapMediaItems(context, songs, false),
|
||||
MappingUtil.mapDownload(songs)
|
||||
MappingUtil.mapDownload(songs, null, null)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -110,7 +110,7 @@ public class AlbumPageFragment extends Fragment {
|
|||
if (isVisible() && getActivity() != null) {
|
||||
DownloadUtil.getDownloadTracker(requireContext()).download(
|
||||
MappingUtil.mapMediaItems(requireContext(), songs, false),
|
||||
MappingUtil.mapDownload(songs)
|
||||
MappingUtil.mapDownload(songs, null, null)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -107,12 +107,8 @@ public class PlaylistPageFragment extends Fragment {
|
|||
playlistPageViewModel.getPlaylistSongLiveList(requireActivity()).observe(requireActivity(), songs -> {
|
||||
if (isVisible() && getActivity() != null) {
|
||||
DownloadUtil.getDownloadTracker(requireContext()).download(
|
||||
MappingUtil.markPlaylistMediaItems(
|
||||
MappingUtil.mapMediaItems(requireContext(), songs, false),
|
||||
playlistPageViewModel.getPlaylist().getId(),
|
||||
playlistPageViewModel.getPlaylist().getName()
|
||||
),
|
||||
MappingUtil.mapDownload(songs)
|
||||
MappingUtil.mapMediaItems(requireContext(), songs, false),
|
||||
MappingUtil.mapDownload(songs, playlistPageViewModel.getPlaylist().getId(), playlistPageViewModel.getPlaylist().getName())
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -158,7 +158,7 @@ public class AlbumBottomSheetDialog extends BottomSheetDialogFragment implements
|
|||
|
||||
albumBottomSheetViewModel.getAlbumTracks().observe(requireActivity(), songs -> {
|
||||
List<MediaItem> mediaItems = MappingUtil.mapMediaItems(requireContext(), songs, false);
|
||||
List<Download> downloads = MappingUtil.mapDownload(songs);
|
||||
List<Download> downloads = MappingUtil.mapDownload(songs, null, null);
|
||||
|
||||
downloadAll.setOnClickListener(v -> {
|
||||
DownloadUtil.getDownloadTracker(requireContext()).download(mediaItems, downloads);
|
||||
|
|
|
|||
|
|
@ -173,11 +173,11 @@ public class MappingUtil {
|
|||
return playlists;
|
||||
}
|
||||
|
||||
public static ArrayList<Download> mapDownload(List<Song> songs) {
|
||||
public static ArrayList<Download> mapDownload(List<Song> songs, String playlistId, String playlistName) {
|
||||
ArrayList<Download> downloads = new ArrayList();
|
||||
|
||||
for (Song song : songs) {
|
||||
downloads.add(new Download(song, null, null));
|
||||
downloads.add(new Download(song, playlistId, playlistName));
|
||||
}
|
||||
|
||||
return downloads;
|
||||
|
|
@ -198,6 +198,8 @@ public class MappingUtil {
|
|||
}
|
||||
|
||||
public static MediaItem mapMediaItem(Context context, Song song, boolean stream) {
|
||||
boolean isDownloaded = DownloadUtil.getDownloadTracker(context).isDownloaded(MusicUtil.getSongDownloadUri(song));
|
||||
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("id", song.getId());
|
||||
bundle.putString("albumId", song.getAlbumId());
|
||||
|
|
@ -207,7 +209,7 @@ public class MappingUtil {
|
|||
.setMediaId(song.getId())
|
||||
.setMediaMetadata(
|
||||
new MediaMetadata.Builder()
|
||||
.setMediaUri(stream ? MusicUtil.getSongStreamUri(context, song) : MusicUtil.getSongDownloadUri(song))
|
||||
.setMediaUri(stream && !isDownloaded ? MusicUtil.getSongStreamUri(context, song) : MusicUtil.getSongDownloadUri(song))
|
||||
.setTitle(MusicUtil.getReadableString(song.getTitle()))
|
||||
.setTrackNumber(song.getTrackNumber())
|
||||
.setDiscNumber(song.getDiscNumber())
|
||||
|
|
@ -217,7 +219,7 @@ public class MappingUtil {
|
|||
.setExtras(bundle)
|
||||
.build()
|
||||
)
|
||||
.setUri(stream ? MusicUtil.getSongStreamUri(context, song) : MusicUtil.getSongDownloadUri(song))
|
||||
.setUri(stream && !isDownloaded ? MusicUtil.getSongStreamUri(context, song) : MusicUtil.getSongDownloadUri(song))
|
||||
.build();
|
||||
}
|
||||
|
||||
|
|
@ -230,23 +232,4 @@ public class MappingUtil {
|
|||
|
||||
return mediaItems;
|
||||
}
|
||||
|
||||
public static ArrayList<MediaItem> markPlaylistMediaItems(ArrayList<MediaItem> mediaItems, String playlistId, String playlistName) {
|
||||
ArrayList<MediaItem> toReturn = new ArrayList();
|
||||
|
||||
for(MediaItem mediaItem: mediaItems) {
|
||||
toReturn.add(markPlaylistMediaItem(mediaItem, playlistId, playlistName));
|
||||
}
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private static MediaItem markPlaylistMediaItem(MediaItem mediaItem, String playlistId, String playlistName) {
|
||||
if (mediaItem.mediaMetadata.extras != null) {
|
||||
mediaItem.mediaMetadata.extras.putString("playlistId", playlistId);
|
||||
mediaItem.mediaMetadata.extras.putString("playlistName", playlistName);
|
||||
}
|
||||
|
||||
return mediaItem;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue