Edit the download function to distinguish the downloads already performed from those still to be performed

This commit is contained in:
CappielloAntonio 2021-09-02 15:05:28 +02:00
parent 60adc11848
commit b3e3f95c8d
7 changed files with 114 additions and 48 deletions

View file

@ -7,6 +7,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.cappielloantonio.play.App; import com.cappielloantonio.play.App;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Song; import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.repository.DownloadRepository; import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.util.MappingUtil; import com.cappielloantonio.play.util.MappingUtil;
@ -68,12 +69,39 @@ public class DownloadTracker {
return download != null && download.state != Download.STATE_FAILED; return download != null && download.state != Download.STATE_FAILED;
} }
public boolean isDownloaded(List<Song> songs) {
for (Song song : songs) {
MediaItem mediaItem = MusicUtil.getMediaItemFromSong(song);
@Nullable Download download = downloads.get(checkNotNull(mediaItem.playbackProperties).uri);
if(download != null && download.state != Download.STATE_FAILED) {
return true;
}
}
return false;
}
@Nullable @Nullable
public DownloadRequest getDownloadRequest(String id, Uri uri) { public DownloadRequest getDownloadRequest(String id, Uri uri) {
return new DownloadRequest.Builder(id, uri).build(); return new DownloadRequest.Builder(id, uri).build();
} }
public void toggleDownload(List<Song> songs) { public void download(List<Song> songs) {
DownloadRepository downloadRepository = new DownloadRepository(App.getInstance());
for (Song song : songs) {
if(isDownloaded(song)) {
continue;
}
MediaItem mediaItem = MusicUtil.getMediaItemFromSong(song);
DownloadService.sendAddDownload(context, DownloaderService.class, getDownloadRequest(song.getId(), checkNotNull(mediaItem.playbackProperties).uri), false);
downloadRepository.insert(MappingUtil.mapToDownload(song));
}
}
public void remove(List<Song> songs) {
DownloadRepository downloadRepository = new DownloadRepository(App.getInstance()); DownloadRepository downloadRepository = new DownloadRepository(App.getInstance());
for (Song song : songs) { for (Song song : songs) {
@ -84,9 +112,6 @@ public class DownloadTracker {
if (download != null && download.state != Download.STATE_FAILED) { if (download != null && download.state != Download.STATE_FAILED) {
DownloadService.sendRemoveDownload(context, DownloaderService.class, download.request.id, false); DownloadService.sendRemoveDownload(context, DownloaderService.class, download.request.id, false);
downloadRepository.delete(MappingUtil.mapToDownload(song)); downloadRepository.delete(MappingUtil.mapToDownload(song));
} else {
DownloadService.sendAddDownload(context, DownloaderService.class, getDownloadRequest(song.getId(), mediaItem.playbackProperties.uri), false);
downloadRepository.insert(MappingUtil.mapToDownload(song));
} }
} }
} }

View file

@ -94,7 +94,7 @@ public class AlbumPageFragment extends Fragment {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_download_album: case R.id.action_download_album:
albumPageViewModel.getAlbumSongLiveList(requireActivity()).observe(requireActivity(), songs -> { albumPageViewModel.getAlbumSongLiveList(requireActivity()).observe(requireActivity(), songs -> {
DownloadUtil.getDownloadTracker(requireContext()).toggleDownload(songs); DownloadUtil.getDownloadTracker(requireContext()).download(songs);
}); });
return true; return true;
default: default:

View file

@ -1,6 +1,5 @@
package com.cappielloantonio.play.ui.fragment; package com.cappielloantonio.play.ui.fragment;
import android.graphics.PorterDuff;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@ -11,7 +10,6 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
@ -85,7 +83,7 @@ public class PlaylistPageFragment extends Fragment {
public boolean onOptionsItemSelected(@NonNull MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_download_playlist: case R.id.action_download_playlist:
DownloadUtil.getDownloadTracker(requireContext()).toggleDownload(playlistPageViewModel.getPlaylistSongLiveList().getValue()); DownloadUtil.getDownloadTracker(requireContext()).download(Objects.requireNonNull(playlistPageViewModel.getPlaylistSongLiveList().getValue()));
return true; return true;
default: default:
break; break;
@ -160,8 +158,6 @@ public class PlaylistPageFragment extends Fragment {
songHorizontalAdapter = new SongHorizontalAdapter(activity, requireContext(), true); songHorizontalAdapter = new SongHorizontalAdapter(activity, requireContext(), true);
bind.songRecyclerView.setAdapter(songHorizontalAdapter); bind.songRecyclerView.setAdapter(songHorizontalAdapter);
playlistPageViewModel.getPlaylistSongLiveList().observe(requireActivity(), songs -> { playlistPageViewModel.getPlaylistSongLiveList().observe(requireActivity(), songs -> songHorizontalAdapter.setItems(songs));
songHorizontalAdapter.setItems(songs);
});
} }
} }

View file

@ -1,7 +1,6 @@
package com.cappielloantonio.play.ui.fragment.bottomsheetdialog; package com.cappielloantonio.play.ui.fragment.bottomsheetdialog;
import android.os.Bundle; import android.os.Bundle;
import android.text.Html;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -26,7 +25,6 @@ import com.cappielloantonio.play.repository.AlbumRepository;
import com.cappielloantonio.play.repository.QueueRepository; import com.cappielloantonio.play.repository.QueueRepository;
import com.cappielloantonio.play.service.MusicPlayerRemote; import com.cappielloantonio.play.service.MusicPlayerRemote;
import com.cappielloantonio.play.ui.activity.MainActivity; import com.cappielloantonio.play.ui.activity.MainActivity;
import com.cappielloantonio.play.ui.fragment.dialog.RatingDialog;
import com.cappielloantonio.play.util.DownloadUtil; import com.cappielloantonio.play.util.DownloadUtil;
import com.cappielloantonio.play.util.MusicUtil; import com.cappielloantonio.play.util.MusicUtil;
import com.cappielloantonio.play.viewmodel.AlbumBottomSheetViewModel; import com.cappielloantonio.play.viewmodel.AlbumBottomSheetViewModel;
@ -126,43 +124,49 @@ public class AlbumBottomSheetDialog extends BottomSheetDialogFragment implements
}); });
TextView playNext = view.findViewById(R.id.play_next_text_view); TextView playNext = view.findViewById(R.id.play_next_text_view);
playNext.setOnClickListener(v -> { playNext.setOnClickListener(v -> albumBottomSheetViewModel.getAlbumTracks().observe(requireActivity(), songs -> {
albumBottomSheetViewModel.getAlbumTracks().observe(requireActivity(), songs -> { MusicPlayerRemote.playNext(songs);
MusicPlayerRemote.playNext(songs); ((MainActivity) requireActivity()).isBottomSheetInPeek(true);
((MainActivity) requireActivity()).isBottomSheetInPeek(true); dismissBottomSheet();
dismissBottomSheet(); }));
});
});
TextView addToQueue = view.findViewById(R.id.add_to_queue_text_view); TextView addToQueue = view.findViewById(R.id.add_to_queue_text_view);
addToQueue.setOnClickListener(v -> { addToQueue.setOnClickListener(v -> albumBottomSheetViewModel.getAlbumTracks().observe(requireActivity(), songs -> {
albumBottomSheetViewModel.getAlbumTracks().observe(requireActivity(), songs -> { MusicPlayerRemote.enqueue(songs);
MusicPlayerRemote.enqueue(songs); dismissBottomSheet();
dismissBottomSheet(); }));
});
});
TextView download = view.findViewById(R.id.download_text_view); TextView downloadAll = view.findViewById(R.id.download_all_text_view);
download.setOnClickListener(v -> { TextView removeAll = view.findViewById(R.id.remove_all_text_view);
albumBottomSheetViewModel.getAlbumTracks().observe(requireActivity(), songs -> {
DownloadUtil.getDownloadTracker(requireContext()).toggleDownload(songs); albumBottomSheetViewModel.getAlbumTracks().observe(requireActivity(), songs -> {
downloadAll.setOnClickListener(v -> {
DownloadUtil.getDownloadTracker(requireContext()).download(songs);
dismissBottomSheet(); dismissBottomSheet();
}); });
if (DownloadUtil.getDownloadTracker(requireContext()).isDownloaded(songs)) {
removeAll.setOnClickListener(v -> {
DownloadUtil.getDownloadTracker(requireContext()).remove(songs);
dismissBottomSheet();
});
} else {
removeAll.setVisibility(View.GONE);
}
}); });
TextView goToArtist = view.findViewById(R.id.go_to_artist_text_view); TextView goToArtist = view.findViewById(R.id.go_to_artist_text_view);
goToArtist.setOnClickListener(v -> { goToArtist.setOnClickListener(v -> albumBottomSheetViewModel.getArtist().observe(requireActivity(), artist -> {
albumBottomSheetViewModel.getArtist().observe(requireActivity(), artist -> { if (artist != null) {
if (artist != null) { Bundle bundle = new Bundle();
Bundle bundle = new Bundle(); bundle.putParcelable("artist_object", artist);
bundle.putParcelable("artist_object", artist); NavHostFragment.findNavController(this).navigate(R.id.artistPageFragment, bundle);
NavHostFragment.findNavController(this).navigate(R.id.artistPageFragment, bundle); } else
} else Toast.makeText(requireContext(), "Error retrieving artist", Toast.LENGTH_SHORT).show();
Toast.makeText(requireContext(), "Error retrieving artist", Toast.LENGTH_SHORT).show();
dismissBottomSheet(); dismissBottomSheet();
}); }));
});
} }
@Override @Override

View file

@ -54,7 +54,6 @@ public class SongBottomSheetDialog extends BottomSheetDialogFragment implements
songBottomSheetViewModel.setSong(song); songBottomSheetViewModel.setSong(song);
init(view); init(view);
initDownloadedUI(view.findViewById(R.id.download_text_view));
return view; return view;
} }
@ -149,10 +148,18 @@ public class SongBottomSheetDialog extends BottomSheetDialogFragment implements
TextView download = view.findViewById(R.id.download_text_view); TextView download = view.findViewById(R.id.download_text_view);
download.setOnClickListener(v -> { download.setOnClickListener(v -> {
DownloadUtil.getDownloadTracker(requireContext()).toggleDownload(Arrays.asList(song)); DownloadUtil.getDownloadTracker(requireContext()).download(Arrays.asList(song));
dismissBottomSheet(); dismissBottomSheet();
}); });
TextView remove = view.findViewById(R.id.remove_text_view);
remove.setOnClickListener(v -> {
DownloadUtil.getDownloadTracker(requireContext()).remove(Arrays.asList(song));
dismissBottomSheet();
});
initDownloadUI(download, remove);
TextView addToPlaylist = view.findViewById(R.id.add_to_playlist_text_view); TextView addToPlaylist = view.findViewById(R.id.add_to_playlist_text_view);
addToPlaylist.setOnClickListener(v -> { addToPlaylist.setOnClickListener(v -> {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
@ -203,11 +210,13 @@ public class SongBottomSheetDialog extends BottomSheetDialogFragment implements
dismiss(); dismiss();
} }
private void initDownloadedUI(TextView download) { private void initDownloadUI(TextView download, TextView remove) {
if (DownloadUtil.getDownloadTracker(requireContext()).isDownloaded(song)) { if (DownloadUtil.getDownloadTracker(requireContext()).isDownloaded(song)) {
download.setText("Remove"); download.setVisibility(View.GONE);
remove.setVisibility(View.VISIBLE);
} else { } else {
download.setText("Download"); download.setVisibility(View.VISIBLE);
remove.setVisibility(View.GONE);
} }
} }
} }

View file

@ -152,7 +152,7 @@
android:textSize="14sp" /> android:textSize="14sp" />
<TextView <TextView
android:id="@+id/download_text_view" android:id="@+id/download_all_text_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground" android:background="?attr/selectableItemBackground"
@ -162,7 +162,23 @@
android:paddingTop="12dp" android:paddingTop="12dp"
android:paddingEnd="20dp" android:paddingEnd="20dp"
android:paddingBottom="12dp" android:paddingBottom="12dp"
android:text="Scarica" android:text="Scarica tutte"
android:textColor="@color/titleTextColor"
android:textFontWeight="700"
android:textSize="14sp" />
<TextView
android:id="@+id/remove_all_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:fontFamily="@font/opensans"
android:paddingStart="20dp"
android:paddingTop="12dp"
android:paddingEnd="20dp"
android:paddingBottom="12dp"
android:text="Rimuovi tutte"
android:textColor="@color/titleTextColor" android:textColor="@color/titleTextColor"
android:textFontWeight="700" android:textFontWeight="700"
android:textSize="14sp" /> android:textSize="14sp" />

View file

@ -168,6 +168,22 @@
android:textFontWeight="700" android:textFontWeight="700"
android:textSize="14sp" /> android:textSize="14sp" />
<TextView
android:id="@+id/remove_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:fontFamily="@font/opensans"
android:paddingStart="20dp"
android:paddingTop="12dp"
android:paddingEnd="20dp"
android:paddingBottom="12dp"
android:text="Rimuovi"
android:textColor="@color/titleTextColor"
android:textFontWeight="700"
android:textSize="14sp" />
<TextView <TextView
android:id="@+id/add_to_playlist_text_view" android:id="@+id/add_to_playlist_text_view"
android:layout_width="match_parent" android:layout_width="match_parent"