mirror of
https://github.com/antebudimir/tempus.git
synced 2026-04-15 16:27:26 +00:00
fix: checks preference and writes files externally, updates the ui (#312)
This commit is contained in:
commit
883d853129
2 changed files with 70 additions and 28 deletions
|
|
@ -23,6 +23,7 @@ import com.cappielloantonio.tempo.service.MediaManager;
|
||||||
import com.cappielloantonio.tempo.subsonic.models.Child;
|
import com.cappielloantonio.tempo.subsonic.models.Child;
|
||||||
import com.cappielloantonio.tempo.util.DownloadUtil;
|
import com.cappielloantonio.tempo.util.DownloadUtil;
|
||||||
import com.cappielloantonio.tempo.util.Constants;
|
import com.cappielloantonio.tempo.util.Constants;
|
||||||
|
import com.cappielloantonio.tempo.util.ExternalAudioReader;
|
||||||
import com.cappielloantonio.tempo.util.MusicUtil;
|
import com.cappielloantonio.tempo.util.MusicUtil;
|
||||||
import com.cappielloantonio.tempo.util.Preferences;
|
import com.cappielloantonio.tempo.util.Preferences;
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
|
|
@ -31,7 +32,9 @@ import com.google.common.util.concurrent.MoreExecutors;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
public class PlayerSongQueueAdapter extends RecyclerView.Adapter<PlayerSongQueueAdapter.ViewHolder> {
|
public class PlayerSongQueueAdapter extends RecyclerView.Adapter<PlayerSongQueueAdapter.ViewHolder> {
|
||||||
private static final String TAG = "PlayerSongQueueAdapter";
|
private static final String TAG = "PlayerSongQueueAdapter";
|
||||||
|
|
@ -39,7 +42,7 @@ public class PlayerSongQueueAdapter extends RecyclerView.Adapter<PlayerSongQueue
|
||||||
|
|
||||||
private ListenableFuture<MediaBrowser> mediaBrowserListenableFuture;
|
private ListenableFuture<MediaBrowser> mediaBrowserListenableFuture;
|
||||||
private List<Child> songs;
|
private List<Child> songs;
|
||||||
|
private final Map<String, Boolean> downloadStatusCache = new ConcurrentHashMap<>();
|
||||||
private String currentPlayingId;
|
private String currentPlayingId;
|
||||||
private boolean isPlaying;
|
private boolean isPlaying;
|
||||||
private List<Integer> currentPlayingPositions = Collections.emptyList();
|
private List<Integer> currentPlayingPositions = Collections.emptyList();
|
||||||
|
|
@ -80,7 +83,6 @@ public class PlayerSongQueueAdapter extends RecyclerView.Adapter<PlayerSongQueue
|
||||||
.build()
|
.build()
|
||||||
.thumbnail(thumbnail)
|
.thumbnail(thumbnail)
|
||||||
.into(holder.item.queueSongCoverImageView);
|
.into(holder.item.queueSongCoverImageView);
|
||||||
|
|
||||||
MediaManager.getCurrentIndex(mediaBrowserListenableFuture, new MediaIndexCallback() {
|
MediaManager.getCurrentIndex(mediaBrowserListenableFuture, new MediaIndexCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onRecovery(int index) {
|
public void onRecovery(int index) {
|
||||||
|
|
@ -96,16 +98,19 @@ public class PlayerSongQueueAdapter extends RecyclerView.Adapter<PlayerSongQueue
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
DownloaderManager downloaderManager = DownloadUtil.getDownloadTracker(holder.itemView.getContext());
|
boolean isDownloaded = false;
|
||||||
|
|
||||||
if (downloaderManager != null) {
|
if (Preferences.getDownloadDirectoryUri() == null) {
|
||||||
boolean isDownloaded = downloaderManager.isDownloaded(song.getId());
|
DownloaderManager downloaderManager = DownloadUtil.getDownloadTracker(holder.itemView.getContext());
|
||||||
|
if (downloaderManager != null) {
|
||||||
if (isDownloaded) {
|
isDownloaded = downloaderManager.isDownloaded(song.getId());
|
||||||
holder.item.downloadIndicatorIcon.setVisibility(View.VISIBLE);
|
|
||||||
} else {
|
|
||||||
holder.item.downloadIndicatorIcon.setVisibility(View.GONE);
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
isDownloaded = ExternalAudioReader.getUri(song) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isDownloaded) {
|
||||||
|
holder.item.downloadIndicatorIcon.setVisibility(View.VISIBLE);
|
||||||
} else {
|
} else {
|
||||||
holder.item.downloadIndicatorIcon.setVisibility(View.GONE);
|
holder.item.downloadIndicatorIcon.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
|
|
@ -169,7 +174,7 @@ public class PlayerSongQueueAdapter extends RecyclerView.Adapter<PlayerSongQueue
|
||||||
holder.item.coverArtOverlay.setVisibility(View.INVISIBLE);
|
holder.item.coverArtOverlay.setVisibility(View.INVISIBLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Child> getItems() {
|
public List<Child> getItems() {
|
||||||
return this.songs;
|
return this.songs;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ import androidx.recyclerview.widget.ItemTouchHelper;
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
|
import com.cappielloantonio.tempo.R;
|
||||||
import com.cappielloantonio.tempo.databinding.InnerFragmentPlayerQueueBinding;
|
import com.cappielloantonio.tempo.databinding.InnerFragmentPlayerQueueBinding;
|
||||||
import com.cappielloantonio.tempo.interfaces.ClickCallback;
|
import com.cappielloantonio.tempo.interfaces.ClickCallback;
|
||||||
import com.cappielloantonio.tempo.service.DownloaderManager;
|
import com.cappielloantonio.tempo.service.DownloaderManager;
|
||||||
|
|
@ -32,6 +33,8 @@ import com.cappielloantonio.tempo.ui.adapter.PlayerSongQueueAdapter;
|
||||||
import com.cappielloantonio.tempo.ui.dialog.PlaylistChooserDialog;
|
import com.cappielloantonio.tempo.ui.dialog.PlaylistChooserDialog;
|
||||||
import com.cappielloantonio.tempo.util.Constants;
|
import com.cappielloantonio.tempo.util.Constants;
|
||||||
import com.cappielloantonio.tempo.util.DownloadUtil;
|
import com.cappielloantonio.tempo.util.DownloadUtil;
|
||||||
|
import com.cappielloantonio.tempo.util.ExternalAudioReader;
|
||||||
|
import com.cappielloantonio.tempo.util.ExternalAudioWriter;
|
||||||
import com.cappielloantonio.tempo.util.MappingUtil;
|
import com.cappielloantonio.tempo.util.MappingUtil;
|
||||||
import com.cappielloantonio.tempo.util.Preferences;
|
import com.cappielloantonio.tempo.util.Preferences;
|
||||||
import com.cappielloantonio.tempo.viewmodel.PlaybackViewModel;
|
import com.cappielloantonio.tempo.viewmodel.PlaybackViewModel;
|
||||||
|
|
@ -384,28 +387,62 @@ public class PlayerQueueFragment extends Fragment implements ClickCallback {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<MediaItem> mediaItemsToDownload = MappingUtil.mapMediaItems(queueSongs);
|
int downloadCount = 0;
|
||||||
|
|
||||||
|
if (Preferences.getDownloadDirectoryUri() == null) {
|
||||||
|
List<MediaItem> mediaItemsToDownload = MappingUtil.mapMediaItems(queueSongs);
|
||||||
|
List<com.cappielloantonio.tempo.model.Download> downloadModels = new ArrayList<>();
|
||||||
|
|
||||||
List<com.cappielloantonio.tempo.model.Download> downloadModels = new ArrayList<>();
|
for (Child child : queueSongs) {
|
||||||
|
com.cappielloantonio.tempo.model.Download downloadModel =
|
||||||
|
new com.cappielloantonio.tempo.model.Download(child);
|
||||||
|
downloadModel.setArtist(child.getArtist());
|
||||||
|
downloadModel.setAlbum(child.getAlbum());
|
||||||
|
downloadModel.setCoverArtId(child.getCoverArtId());
|
||||||
|
downloadModels.add(downloadModel);
|
||||||
|
}
|
||||||
|
|
||||||
for (Child child : queueSongs) {
|
DownloaderManager downloaderManager = DownloadUtil.getDownloadTracker(requireContext());
|
||||||
com.cappielloantonio.tempo.model.Download downloadModel =
|
|
||||||
new com.cappielloantonio.tempo.model.Download(child);
|
|
||||||
downloadModel.setArtist(child.getArtist());
|
|
||||||
downloadModel.setAlbum(child.getAlbum());
|
|
||||||
downloadModel.setCoverArtId(child.getCoverArtId());
|
|
||||||
downloadModels.add(downloadModel);
|
|
||||||
}
|
|
||||||
|
|
||||||
DownloaderManager downloaderManager = DownloadUtil.getDownloadTracker(requireContext());
|
if (downloaderManager != null) {
|
||||||
|
downloaderManager.download(mediaItemsToDownload, downloadModels);
|
||||||
if (downloaderManager != null) {
|
downloadCount = queueSongs.size();
|
||||||
downloaderManager.download(mediaItemsToDownload, downloadModels);
|
Toast.makeText(requireContext(),
|
||||||
Toast.makeText(requireContext(), "Starting download of " + queueSongs.size() + " songs in the background.", Toast.LENGTH_SHORT).show();
|
getResources().getQuantityString(R.plurals.songs_download_started, downloadCount, downloadCount),
|
||||||
|
Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
|
new Handler().postDelayed(() -> {
|
||||||
|
if (playerSongQueueAdapter != null) {
|
||||||
|
playerSongQueueAdapter.notifyDataSetChanged();
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
} else {
|
||||||
|
Log.e(TAG, "DownloaderManager not initialized. Check DownloadUtil.");
|
||||||
|
Toast.makeText(requireContext(), "Download service unavailable.", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Log.e(TAG, "DownloaderManager not initialized. Check DownloadUtil.");
|
for (Child song : queueSongs) {
|
||||||
Toast.makeText(requireContext(), "Download service unavailable.", Toast.LENGTH_SHORT).show();
|
if (ExternalAudioReader.getUri(song) == null) {
|
||||||
|
ExternalAudioWriter.downloadToUserDirectory(requireContext(), song);
|
||||||
|
downloadCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (downloadCount > 0) {
|
||||||
|
Toast.makeText(requireContext(),
|
||||||
|
getResources().getQuantityString(R.plurals.songs_download_started, downloadCount, downloadCount),
|
||||||
|
Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
|
new Handler().postDelayed(() -> {
|
||||||
|
if (playerSongQueueAdapter != null) {
|
||||||
|
playerSongQueueAdapter.notifyDataSetChanged();
|
||||||
|
}
|
||||||
|
}, 2000);
|
||||||
|
} else {
|
||||||
|
Toast.makeText(requireContext(), "All songs already downloaded", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleFabMenu();
|
toggleFabMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue