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

7
.idea/misc.xml generated
View file

@ -4,8 +4,12 @@
<option name="filePathToZoomLevelMap">
<map>
<entry key="app/src/main/res/drawable-v24/ic_add.xml" value="0.27685185185185185" />
<entry key="app/src/main/res/drawable-v24/ic_download.xml" value="0.27685185185185185" />
<entry key="app/src/main/res/drawable-v24/ic_launcher_foreground.xml" value="0.2814814814814815" />
<entry key="app/src/main/res/drawable/ic_close.xml" value="0.27685185185185185" />
<entry key="app/src/main/res/drawable/ic_download_for_offline.xml" value="0.27685185185185185" />
<entry key="app/src/main/res/drawable/ic_downloading.xml" value="0.2722222222222222" />
<entry key="app/src/main/res/drawable/ic_drag_handle.xml" value="0.27685185185185185" />
<entry key="app/src/main/res/drawable/ic_file_download.xml" value="0.2722222222222222" />
<entry key="app/src/main/res/drawable/ic_launcher_background.xml" value="0.2814814814814815" />
<entry key="app/src/main/res/drawable/ic_play.xml" value="0.27685185185185185" />
@ -24,7 +28,8 @@
<entry key="app/src/main/res/layout/fragment_login.xml" value="0.3166496424923391" />
<entry key="app/src/main/res/layout/item_home_discover_song.xml" value="0.3166496424923391" />
<entry key="app/src/main/res/layout/item_horizontal_album.xml" value="0.3166496424923391" />
<entry key="app/src/main/res/layout/item_horizontal_track.xml" value="0.1" />
<entry key="app/src/main/res/layout/item_horizontal_track.xml" value="4.0" />
<entry key="app/src/main/res/layout/item_library_album.xml" value="0.3229166666666667" />
<entry key="app/src/main/res/layout/item_login_server.xml" value="0.25" />
<entry key="app/src/main/res/layout/item_player_now_playing_song.xml" value="0.3229166666666667" />
<entry key="app/src/main/res/layout/item_player_queue_song.xml" value="0.3229166666666667" />

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");

View file

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/titleTextColor"
android:pathData="M16.59,9H15V4c0,-0.55 -0.45,-1 -1,-1h-4c-0.55,0 -1,0.45 -1,1v5H7.41c-0.89,0 -1.34,1.08 -0.71,1.71l4.59,4.59c0.39,0.39 1.02,0.39 1.41,0l4.59,-4.59c0.63,-0.63 0.19,-1.71 -0.7,-1.71zM5,19c0,0.55 0.45,1 1,1h12c0.55,0 1,-0.45 1,-1s-0.45,-1 -1,-1H6c-0.55,0 -1,0.45 -1,1z"/>
</vector>

View file

@ -83,9 +83,8 @@
android:id="@+id/search_result_dowanload_indicator_image_view"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_gravity="center"
android:layout_margin="8dp"
android:background="@drawable/ic_download_for_offline"
android:background="@drawable/ic_download"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/search_result_song_more_button"
@ -99,7 +98,6 @@
android:layout_marginEnd="12dp"
android:background="@drawable/ic_more_vert"
android:foreground="?android:attr/selectableItemBackgroundBorderless"
android:gravity="center_vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />