Fix downloaded item implementation

This commit is contained in:
CappielloAntonio 2021-08-11 12:11:14 +02:00
parent 93d61be594
commit b24903fd47
9 changed files with 25 additions and 22 deletions

View file

@ -21,6 +21,7 @@ import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.repository.QueueRepository;
import com.cappielloantonio.play.service.MusicPlayerRemote;
import com.cappielloantonio.play.ui.activity.MainActivity;
import com.cappielloantonio.play.util.DownloadUtil;
import com.cappielloantonio.play.util.MusicUtil;
import java.util.ArrayList;
@ -57,7 +58,7 @@ public class SongHorizontalAdapter extends RecyclerView.Adapter<SongHorizontalAd
holder.songArtist.setText(MusicUtil.getReadableString(song.getArtistName()));
holder.songDuration.setText(MusicUtil.getReadableDurationString(song.getDuration(), false));
if (song.isOffline()) {
if (DownloadUtil.getDownloadTracker(context).isDownloaded(song)) {
holder.downloadIndicator.setVisibility(View.VISIBLE);
} else {
holder.downloadIndicator.setVisibility(View.GONE);

View file

@ -42,7 +42,6 @@ public class Song implements Parcelable {
private long added;
private int playCount;
private long lastPlay;
private boolean offline;
public Song() {
this.id = UUID.randomUUID().toString();
@ -68,7 +67,6 @@ public class Song implements Parcelable {
this.added = child.getCreated().getTime();
this.playCount = 0;
this.lastPlay = 0;
this.offline = false;
}
public Song(Queue queue) {
@ -173,10 +171,6 @@ public class Song implements Parcelable {
return lastPlay;
}
public boolean isOffline() {
return offline;
}
public void setId(String id) {
this.id = id;
}
@ -257,10 +251,6 @@ public class Song implements Parcelable {
this.playCount = playCount;
}
public void setOffline(boolean offline) {
this.offline = offline;
}
/*
Log.i(TAG, "increasePlayCount: " + isIncreased);
* Incremento il numero di ascolti solo se ho ascoltato la canzone da più tempo di:
@ -328,7 +318,6 @@ public class Song implements Parcelable {
dest.writeLong(this.added);
dest.writeInt(this.playCount);
dest.writeLong(this.lastPlay);
dest.writeBoolean(this.offline);
}
@SuppressLint("NewApi")
@ -353,7 +342,6 @@ public class Song implements Parcelable {
this.added = in.readLong();
this.playCount = in.readInt();
this.lastPlay = in.readLong();
this.offline = in.readBoolean();
}
public static final Creator<Song> CREATOR = new Creator<Song>() {

View file

@ -43,10 +43,12 @@ public class DownloadTracker {
public DownloadTracker(Context context, HttpDataSource.Factory httpDataSourceFactory, DownloadManager downloadManager) {
this.context = context.getApplicationContext();
this.httpDataSourceFactory = httpDataSourceFactory;
listeners = new CopyOnWriteArraySet<>();
downloads = new HashMap<>();
downloadIndex = downloadManager.getDownloadIndex();
trackSelectorParameters = DownloadHelper.getDefaultTrackSelectorParameters(context);
downloadManager.addListener(new DownloadManagerListener());
loadDownloads();
}
@ -80,11 +82,9 @@ public class DownloadTracker {
@Nullable Download download = downloads.get(checkNotNull(mediaItem.playbackProperties).uri);
if (download != null && download.state != Download.STATE_FAILED) {
song.setOffline(false);
DownloadService.sendRemoveDownload(context, DownloaderService.class, download.request.id, false);
downloadRepository.delete(MappingUtil.mapToDownload(song));
} else {
song.setOffline(true);
DownloadService.sendAddDownload(context, DownloaderService.class, getDownloadRequest(song.getId(), mediaItem.playbackProperties.uri), false);
downloadRepository.insert(MappingUtil.mapToDownload(song));
}

View file

@ -44,7 +44,7 @@ public class DownloaderService extends DownloadService {
@Override
@NonNull
protected Notification getForegroundNotification(@NonNull List<Download> downloads) {
return DownloadUtil.getDownloadNotificationHelper(this).buildProgressNotification(this, R.drawable.ic_downloading, null, null, downloads);
return DownloadUtil.getDownloadNotificationHelper(this).buildProgressNotification(this, R.drawable.ic_download, null, null, downloads);
}
private static final class TerminalStateNotificationHelper implements DownloadManager.Listener {

View file

@ -92,7 +92,9 @@ public class AlbumPageFragment extends Fragment {
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_download_album:
DownloadUtil.getDownloadTracker(requireContext()).toggleDownload(albumPageViewModel.getAlbumSongLiveList().getValue());
albumPageViewModel.getAlbumSongLiveList().observe(requireActivity(), songs -> {
DownloadUtil.getDownloadTracker(requireContext()).toggleDownload(songs);
});
return true;
default:
break;

View file

@ -178,7 +178,7 @@ public class SongBottomSheetDialog extends BottomSheetDialogFragment implements
}
private void initDownloadedUI() {
if (song.isOffline()) {
if (DownloadUtil.getDownloadTracker(requireContext()).isDownloaded(song)) {
download.setText("Remove");
} else {
download.setText("Download");