mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
Save in the database what has been downloaded
This commit is contained in:
parent
81e9b5c3d9
commit
f416bfda8f
9 changed files with 59 additions and 19 deletions
|
|
@ -18,6 +18,11 @@ import androidx.media3.exoplayer.offline.DownloadManager;
|
|||
import androidx.media3.exoplayer.offline.DownloadRequest;
|
||||
import androidx.media3.exoplayer.offline.DownloadService;
|
||||
|
||||
import com.cappielloantonio.play.App;
|
||||
import com.cappielloantonio.play.repository.DownloadRepository;
|
||||
import com.cappielloantonio.play.repository.QueueRepository;
|
||||
import com.cappielloantonio.play.util.MappingUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
|
@ -81,24 +86,26 @@ public class DownloaderTracker {
|
|||
}
|
||||
|
||||
@SuppressLint("UnsafeOptInUsageError")
|
||||
public void download(MediaItem mediaItem) {
|
||||
public void download(MediaItem mediaItem, com.cappielloantonio.play.model.Download download) {
|
||||
DownloadService.sendAddDownload(context, DownloaderService.class, buildDownloadRequest(mediaItem), false);
|
||||
downloadDatabase(download);
|
||||
}
|
||||
|
||||
public void download(List<MediaItem> mediaItems) {
|
||||
for (MediaItem mediaItem : mediaItems) {
|
||||
download(mediaItem);
|
||||
public void download(List<MediaItem> mediaItems, List<com.cappielloantonio.play.model.Download> downloads) {
|
||||
for (int counter = 0; counter < mediaItems.size(); counter++) {
|
||||
download(mediaItems.get(counter), downloads.get(counter));
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("UnsafeOptInUsageError")
|
||||
public void remove(MediaItem mediaItem) {
|
||||
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) {
|
||||
for (MediaItem mediaItem : mediaItems) {
|
||||
remove(mediaItem);
|
||||
public void remove(List<MediaItem> mediaItems, List<com.cappielloantonio.play.model.Download> downloads) {
|
||||
for (int counter = 0; counter < mediaItems.size(); counter++) {
|
||||
remove(mediaItems.get(counter), downloads.get(counter));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -131,4 +138,16 @@ public class DownloaderTracker {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static DownloadRepository getDownloadRepository() {
|
||||
return new DownloadRepository(App.getInstance());
|
||||
}
|
||||
|
||||
private static void downloadDatabase(com.cappielloantonio.play.model.Download download) {
|
||||
getDownloadRepository().insert(download);
|
||||
}
|
||||
|
||||
private static void removeDatabase(com.cappielloantonio.play.model.Download download) {
|
||||
getDownloadRepository().delete(download);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,7 +63,10 @@ public class StarredSyncDialog extends DialogFragment {
|
|||
((AlertDialog) Objects.requireNonNull(getDialog())).getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
|
||||
starredSyncViewModel.getStarredTracks(requireActivity()).observe(requireActivity(), songs -> {
|
||||
if (songs != null) {
|
||||
DownloadUtil.getDownloadTracker(context).download(MappingUtil.mapMediaItems(context, songs, false));
|
||||
DownloadUtil.getDownloadTracker(context).download(
|
||||
MappingUtil.mapMediaItems(context, songs, false),
|
||||
MappingUtil.mapDownload(songs)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -108,7 +108,10 @@ public class AlbumPageFragment extends Fragment {
|
|||
if (item.getItemId() == R.id.action_download_album) {
|
||||
albumPageViewModel.getAlbumSongLiveList(requireActivity()).observe(requireActivity(), songs -> {
|
||||
if (isVisible() && getActivity() != null) {
|
||||
DownloadUtil.getDownloadTracker(requireContext()).download(MappingUtil.mapMediaItems(requireContext(), songs, false));
|
||||
DownloadUtil.getDownloadTracker(requireContext()).download(
|
||||
MappingUtil.mapMediaItems(requireContext(), songs, false),
|
||||
MappingUtil.mapDownload(songs)
|
||||
);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -111,7 +111,8 @@ public class PlaylistPageFragment extends Fragment {
|
|||
MappingUtil.mapMediaItems(requireContext(), songs, false),
|
||||
playlistPageViewModel.getPlaylist().getId(),
|
||||
playlistPageViewModel.getPlaylist().getName()
|
||||
)
|
||||
),
|
||||
MappingUtil.mapDownload(songs)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import com.cappielloantonio.play.R;
|
|||
import com.cappielloantonio.play.glide.CustomGlideRequest;
|
||||
import com.cappielloantonio.play.interfaces.MediaCallback;
|
||||
import com.cappielloantonio.play.model.Album;
|
||||
import com.cappielloantonio.play.model.Download;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
import com.cappielloantonio.play.repository.AlbumRepository;
|
||||
import com.cappielloantonio.play.service.MediaManager;
|
||||
|
|
@ -157,15 +158,16 @@ 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);
|
||||
|
||||
downloadAll.setOnClickListener(v -> {
|
||||
DownloadUtil.getDownloadTracker(requireContext()).download(mediaItems);
|
||||
DownloadUtil.getDownloadTracker(requireContext()).download(mediaItems, downloads);
|
||||
dismissBottomSheet();
|
||||
});
|
||||
|
||||
if (DownloadUtil.getDownloadTracker(requireContext()).areDownloaded(mediaItems)) {
|
||||
removeAll.setOnClickListener(v -> {
|
||||
DownloadUtil.getDownloadTracker(requireContext()).remove(mediaItems);
|
||||
DownloadUtil.getDownloadTracker(requireContext()).remove(mediaItems, downloads);
|
||||
dismissBottomSheet();
|
||||
});
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -157,13 +157,19 @@ public class SongBottomSheetDialog extends BottomSheetDialogFragment implements
|
|||
|
||||
TextView download = view.findViewById(R.id.download_text_view);
|
||||
download.setOnClickListener(v -> {
|
||||
DownloadUtil.getDownloadTracker(requireContext()).download(MappingUtil.mapMediaItem(requireContext(), song, false));
|
||||
DownloadUtil.getDownloadTracker(requireContext()).download(
|
||||
MappingUtil.mapMediaItem(requireContext(), song, false),
|
||||
MappingUtil.mapDownload(song, null, null)
|
||||
);
|
||||
dismissBottomSheet();
|
||||
});
|
||||
|
||||
TextView remove = view.findViewById(R.id.remove_text_view);
|
||||
remove.setOnClickListener(v -> {
|
||||
DownloadUtil.getDownloadTracker(requireContext()).remove(MappingUtil.mapMediaItem(requireContext(), song, false));
|
||||
DownloadUtil.getDownloadTracker(requireContext()).remove(
|
||||
MappingUtil.mapMediaItem(requireContext(), song, false),
|
||||
MappingUtil.mapDownload(song, null, null)
|
||||
);
|
||||
dismissBottomSheet();
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -173,7 +173,7 @@ public class MappingUtil {
|
|||
return playlists;
|
||||
}
|
||||
|
||||
public static ArrayList<Download> mapToDownload(List<Song> songs) {
|
||||
public static ArrayList<Download> mapDownload(List<Song> songs) {
|
||||
ArrayList<Download> downloads = new ArrayList();
|
||||
|
||||
for (Song song : songs) {
|
||||
|
|
@ -183,7 +183,7 @@ public class MappingUtil {
|
|||
return downloads;
|
||||
}
|
||||
|
||||
public static Download mapToDownload(Song song, String playlistId, String playlistName) {
|
||||
public static Download mapDownload(Song song, String playlistId, String playlistName) {
|
||||
return new Download(song, playlistId, playlistName);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -57,7 +57,10 @@ public class PlayerBottomSheetViewModel extends AndroidViewModel {
|
|||
song.setFavorite(true);
|
||||
|
||||
if (PreferenceUtil.getInstance(context).isStarredSyncEnabled()) {
|
||||
DownloadUtil.getDownloadTracker(context).download(MappingUtil.mapMediaItem(context, song, false));
|
||||
DownloadUtil.getDownloadTracker(context).download(
|
||||
MappingUtil.mapMediaItem(context, song, false),
|
||||
MappingUtil.mapDownload(song, null, null)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,7 +49,10 @@ public class SongBottomSheetViewModel extends AndroidViewModel {
|
|||
song.setFavorite(true);
|
||||
|
||||
if (PreferenceUtil.getInstance(context).isStarredSyncEnabled()) {
|
||||
DownloadUtil.getDownloadTracker(context).download(MappingUtil.mapMediaItem(context, song, false));
|
||||
DownloadUtil.getDownloadTracker(context).download(
|
||||
MappingUtil.mapMediaItem(context, song, false),
|
||||
MappingUtil.mapDownload(song, null, null)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue