mirror of
https://github.com/CappielloAntonio/tempo.git
synced 2025-12-24 00:18:02 -05:00
Edit the download function to distinguish the downloads already performed from those still to be performed
This commit is contained in:
@@ -7,6 +7,7 @@ import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.cappielloantonio.play.App;
|
||||
import com.cappielloantonio.play.model.Album;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
import com.cappielloantonio.play.repository.DownloadRepository;
|
||||
import com.cappielloantonio.play.util.MappingUtil;
|
||||
@@ -68,12 +69,39 @@ public class DownloadTracker {
|
||||
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
|
||||
public DownloadRequest getDownloadRequest(String id, Uri uri) {
|
||||
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());
|
||||
|
||||
for (Song song : songs) {
|
||||
@@ -84,9 +112,6 @@ public class DownloadTracker {
|
||||
if (download != null && download.state != Download.STATE_FAILED) {
|
||||
DownloadService.sendRemoveDownload(context, DownloaderService.class, download.request.id, false);
|
||||
downloadRepository.delete(MappingUtil.mapToDownload(song));
|
||||
} else {
|
||||
DownloadService.sendAddDownload(context, DownloaderService.class, getDownloadRequest(song.getId(), mediaItem.playbackProperties.uri), false);
|
||||
downloadRepository.insert(MappingUtil.mapToDownload(song));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ public class AlbumPageFragment extends Fragment {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_download_album:
|
||||
albumPageViewModel.getAlbumSongLiveList(requireActivity()).observe(requireActivity(), songs -> {
|
||||
DownloadUtil.getDownloadTracker(requireContext()).toggleDownload(songs);
|
||||
DownloadUtil.getDownloadTracker(requireContext()).download(songs);
|
||||
});
|
||||
return true;
|
||||
default:
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.cappielloantonio.play.ui.fragment;
|
||||
|
||||
import android.graphics.PorterDuff;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
@@ -11,7 +10,6 @@ import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
@@ -85,7 +83,7 @@ public class PlaylistPageFragment extends Fragment {
|
||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_download_playlist:
|
||||
DownloadUtil.getDownloadTracker(requireContext()).toggleDownload(playlistPageViewModel.getPlaylistSongLiveList().getValue());
|
||||
DownloadUtil.getDownloadTracker(requireContext()).download(Objects.requireNonNull(playlistPageViewModel.getPlaylistSongLiveList().getValue()));
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
@@ -160,8 +158,6 @@ public class PlaylistPageFragment extends Fragment {
|
||||
songHorizontalAdapter = new SongHorizontalAdapter(activity, requireContext(), true);
|
||||
bind.songRecyclerView.setAdapter(songHorizontalAdapter);
|
||||
|
||||
playlistPageViewModel.getPlaylistSongLiveList().observe(requireActivity(), songs -> {
|
||||
songHorizontalAdapter.setItems(songs);
|
||||
});
|
||||
playlistPageViewModel.getPlaylistSongLiveList().observe(requireActivity(), songs -> songHorizontalAdapter.setItems(songs));
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.cappielloantonio.play.ui.fragment.bottomsheetdialog;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.text.Html;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
@@ -26,7 +25,6 @@ import com.cappielloantonio.play.repository.AlbumRepository;
|
||||
import com.cappielloantonio.play.repository.QueueRepository;
|
||||
import com.cappielloantonio.play.service.MusicPlayerRemote;
|
||||
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.MusicUtil;
|
||||
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);
|
||||
playNext.setOnClickListener(v -> {
|
||||
albumBottomSheetViewModel.getAlbumTracks().observe(requireActivity(), songs -> {
|
||||
MusicPlayerRemote.playNext(songs);
|
||||
((MainActivity) requireActivity()).isBottomSheetInPeek(true);
|
||||
dismissBottomSheet();
|
||||
});
|
||||
});
|
||||
playNext.setOnClickListener(v -> albumBottomSheetViewModel.getAlbumTracks().observe(requireActivity(), songs -> {
|
||||
MusicPlayerRemote.playNext(songs);
|
||||
((MainActivity) requireActivity()).isBottomSheetInPeek(true);
|
||||
dismissBottomSheet();
|
||||
}));
|
||||
|
||||
TextView addToQueue = view.findViewById(R.id.add_to_queue_text_view);
|
||||
addToQueue.setOnClickListener(v -> {
|
||||
albumBottomSheetViewModel.getAlbumTracks().observe(requireActivity(), songs -> {
|
||||
MusicPlayerRemote.enqueue(songs);
|
||||
dismissBottomSheet();
|
||||
});
|
||||
});
|
||||
addToQueue.setOnClickListener(v -> albumBottomSheetViewModel.getAlbumTracks().observe(requireActivity(), songs -> {
|
||||
MusicPlayerRemote.enqueue(songs);
|
||||
dismissBottomSheet();
|
||||
}));
|
||||
|
||||
TextView download = view.findViewById(R.id.download_text_view);
|
||||
download.setOnClickListener(v -> {
|
||||
albumBottomSheetViewModel.getAlbumTracks().observe(requireActivity(), songs -> {
|
||||
DownloadUtil.getDownloadTracker(requireContext()).toggleDownload(songs);
|
||||
TextView downloadAll = view.findViewById(R.id.download_all_text_view);
|
||||
TextView removeAll = view.findViewById(R.id.remove_all_text_view);
|
||||
|
||||
albumBottomSheetViewModel.getAlbumTracks().observe(requireActivity(), songs -> {
|
||||
|
||||
downloadAll.setOnClickListener(v -> {
|
||||
DownloadUtil.getDownloadTracker(requireContext()).download(songs);
|
||||
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);
|
||||
goToArtist.setOnClickListener(v -> {
|
||||
albumBottomSheetViewModel.getArtist().observe(requireActivity(), artist -> {
|
||||
if (artist != null) {
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putParcelable("artist_object", artist);
|
||||
NavHostFragment.findNavController(this).navigate(R.id.artistPageFragment, bundle);
|
||||
} else
|
||||
Toast.makeText(requireContext(), "Error retrieving artist", Toast.LENGTH_SHORT).show();
|
||||
goToArtist.setOnClickListener(v -> albumBottomSheetViewModel.getArtist().observe(requireActivity(), artist -> {
|
||||
if (artist != null) {
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putParcelable("artist_object", artist);
|
||||
NavHostFragment.findNavController(this).navigate(R.id.artistPageFragment, bundle);
|
||||
} else
|
||||
Toast.makeText(requireContext(), "Error retrieving artist", Toast.LENGTH_SHORT).show();
|
||||
|
||||
dismissBottomSheet();
|
||||
});
|
||||
});
|
||||
dismissBottomSheet();
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -54,7 +54,6 @@ public class SongBottomSheetDialog extends BottomSheetDialogFragment implements
|
||||
songBottomSheetViewModel.setSong(song);
|
||||
|
||||
init(view);
|
||||
initDownloadedUI(view.findViewById(R.id.download_text_view));
|
||||
|
||||
return view;
|
||||
}
|
||||
@@ -149,10 +148,18 @@ public class SongBottomSheetDialog extends BottomSheetDialogFragment implements
|
||||
|
||||
TextView download = view.findViewById(R.id.download_text_view);
|
||||
download.setOnClickListener(v -> {
|
||||
DownloadUtil.getDownloadTracker(requireContext()).toggleDownload(Arrays.asList(song));
|
||||
DownloadUtil.getDownloadTracker(requireContext()).download(Arrays.asList(song));
|
||||
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);
|
||||
addToPlaylist.setOnClickListener(v -> {
|
||||
Bundle bundle = new Bundle();
|
||||
@@ -203,11 +210,13 @@ public class SongBottomSheetDialog extends BottomSheetDialogFragment implements
|
||||
dismiss();
|
||||
}
|
||||
|
||||
private void initDownloadedUI(TextView download) {
|
||||
private void initDownloadUI(TextView download, TextView remove) {
|
||||
if (DownloadUtil.getDownloadTracker(requireContext()).isDownloaded(song)) {
|
||||
download.setText("Remove");
|
||||
download.setVisibility(View.GONE);
|
||||
remove.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
download.setText("Download");
|
||||
download.setVisibility(View.VISIBLE);
|
||||
remove.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -152,7 +152,7 @@
|
||||
android:textSize="14sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/download_text_view"
|
||||
android:id="@+id/download_all_text_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?attr/selectableItemBackground"
|
||||
@@ -162,7 +162,23 @@
|
||||
android:paddingTop="12dp"
|
||||
android:paddingEnd="20dp"
|
||||
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:textFontWeight="700"
|
||||
android:textSize="14sp" />
|
||||
|
||||
@@ -168,6 +168,22 @@
|
||||
android:textFontWeight="700"
|
||||
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
|
||||
android:id="@+id/add_to_playlist_text_view"
|
||||
android:layout_width="match_parent"
|
||||
|
||||
Reference in New Issue
Block a user