From 0d329aff6483f7c7872f528c109d94a76cd4245c Mon Sep 17 00:00:00 2001 From: eddyizm Date: Wed, 17 Dec 2025 22:27:00 -0800 Subject: [PATCH] fix: checks preferecen and writes files externally, updates the ui --- .../ui/adapter/PlayerSongQueueAdapter.java | 27 ++++--- .../ui/fragment/PlayerQueueFragment.java | 71 ++++++++++++++----- 2 files changed, 70 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/cappielloantonio/tempo/ui/adapter/PlayerSongQueueAdapter.java b/app/src/main/java/com/cappielloantonio/tempo/ui/adapter/PlayerSongQueueAdapter.java index 14269b1c..f34990e0 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/ui/adapter/PlayerSongQueueAdapter.java +++ b/app/src/main/java/com/cappielloantonio/tempo/ui/adapter/PlayerSongQueueAdapter.java @@ -23,6 +23,7 @@ import com.cappielloantonio.tempo.service.MediaManager; import com.cappielloantonio.tempo.subsonic.models.Child; import com.cappielloantonio.tempo.util.DownloadUtil; import com.cappielloantonio.tempo.util.Constants; +import com.cappielloantonio.tempo.util.ExternalAudioReader; import com.cappielloantonio.tempo.util.MusicUtil; import com.cappielloantonio.tempo.util.Preferences; 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.Collections; import java.util.List; +import java.util.Map; import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; public class PlayerSongQueueAdapter extends RecyclerView.Adapter { private static final String TAG = "PlayerSongQueueAdapter"; @@ -39,7 +42,7 @@ public class PlayerSongQueueAdapter extends RecyclerView.Adapter mediaBrowserListenableFuture; private List songs; - + private final Map downloadStatusCache = new ConcurrentHashMap<>(); private String currentPlayingId; private boolean isPlaying; private List currentPlayingPositions = Collections.emptyList(); @@ -80,7 +83,6 @@ public class PlayerSongQueueAdapter extends RecyclerView.Adapter getItems() { return this.songs; } diff --git a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/PlayerQueueFragment.java b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/PlayerQueueFragment.java index b2b13d90..f1c10371 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/PlayerQueueFragment.java +++ b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/PlayerQueueFragment.java @@ -21,6 +21,7 @@ import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import com.cappielloantonio.tempo.R; import com.cappielloantonio.tempo.databinding.InnerFragmentPlayerQueueBinding; import com.cappielloantonio.tempo.interfaces.ClickCallback; 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.util.Constants; 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.Preferences; import com.cappielloantonio.tempo.viewmodel.PlaybackViewModel; @@ -384,28 +387,62 @@ public class PlayerQueueFragment extends Fragment implements ClickCallback { return; } - List mediaItemsToDownload = MappingUtil.mapMediaItems(queueSongs); + int downloadCount = 0; + + if (Preferences.getDownloadDirectoryUri() == null) { + List mediaItemsToDownload = MappingUtil.mapMediaItems(queueSongs); + List downloadModels = new ArrayList<>(); - List 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) { - 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()); - DownloaderManager downloaderManager = DownloadUtil.getDownloadTracker(requireContext()); - - if (downloaderManager != null) { - downloaderManager.download(mediaItemsToDownload, downloadModels); - Toast.makeText(requireContext(), "Starting download of " + queueSongs.size() + " songs in the background.", Toast.LENGTH_SHORT).show(); + if (downloaderManager != null) { + downloaderManager.download(mediaItemsToDownload, downloadModels); + downloadCount = queueSongs.size(); + Toast.makeText(requireContext(), + 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 { - Log.e(TAG, "DownloaderManager not initialized. Check DownloadUtil."); - Toast.makeText(requireContext(), "Download service unavailable.", Toast.LENGTH_SHORT).show(); + for (Child song : queueSongs) { + 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(); }