From ee738bc4c7ae5f6e4dcd391fb8bb470bd59f3afd Mon Sep 17 00:00:00 2001 From: eddyizm Date: Sat, 27 Sep 2025 15:37:59 -0700 Subject: [PATCH] feat: download starred artists. --- app/build.gradle | 2 +- .../tempo/repository/ArtistRepository.java | 62 +++++++++++- .../ui/dialog/StarredArtistSyncDialog.java | 88 +++++++++++++++++ .../ArtistBottomSheetDialog.java | 4 +- .../tempo/util/Preferences.kt | 13 +++ .../viewmodel/ArtistBottomSheetViewModel.java | 48 ++++++++-- .../StarredArtistsSyncViewModel.java | 95 +++++++++++++++++++ .../res/layout/dialog_starred_artist_sync.xml | 14 +++ app/src/main/res/values/strings.xml | 6 +- app/src/main/res/xml/global_preferences.xml | 6 ++ 10 files changed, 325 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/com/cappielloantonio/tempo/ui/dialog/StarredArtistSyncDialog.java create mode 100644 app/src/main/java/com/cappielloantonio/tempo/viewmodel/StarredArtistsSyncViewModel.java create mode 100644 app/src/main/res/layout/dialog_starred_artist_sync.xml diff --git a/app/build.gradle b/app/build.gradle index d0393e45..9bdd17cc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,7 +11,7 @@ android { targetSdk 35 versionCode 32 - versionName '3.15.0' + versionName '3.15.1' testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' javaCompileOptions { diff --git a/app/src/main/java/com/cappielloantonio/tempo/repository/ArtistRepository.java b/app/src/main/java/com/cappielloantonio/tempo/repository/ArtistRepository.java index f39dbffa..71b43fa9 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/repository/ArtistRepository.java +++ b/app/src/main/java/com/cappielloantonio/tempo/repository/ArtistRepository.java @@ -2,10 +2,12 @@ package com.cappielloantonio.tempo.repository; import androidx.annotation.NonNull; import androidx.lifecycle.MutableLiveData; +import android.util.Log; import com.cappielloantonio.tempo.App; import com.cappielloantonio.tempo.subsonic.base.ApiResponse; import com.cappielloantonio.tempo.subsonic.models.ArtistID3; +import com.cappielloantonio.tempo.subsonic.models.AlbumID3; import com.cappielloantonio.tempo.subsonic.models.ArtistInfo2; import com.cappielloantonio.tempo.subsonic.models.Child; import com.cappielloantonio.tempo.subsonic.models.IndexID3; @@ -13,12 +15,70 @@ import com.cappielloantonio.tempo.subsonic.models.IndexID3; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public class ArtistRepository { + private final AlbumRepository albumRepository; + + public ArtistRepository() { + this.albumRepository = new AlbumRepository(); + } + + public void getArtistAllSongs(String artistId, ArtistSongsCallback callback) { + Log.d("ArtistSync", "Getting albums for artist: " + artistId); + + // Use AlbumRepository to get all albums by this artist + albumRepository.getArtistAlbums(artistId).observeForever(albums -> { + Log.d("ArtistSync", "Got albums: " + (albums != null ? albums.size() : 0)); + if (albums != null && !albums.isEmpty()) { + fetchAllAlbumSongsWithCallback(albums, callback); + } else { + Log.d("ArtistSync", "No albums found"); + callback.onSongsCollected(new ArrayList<>()); + } + }); + } + + private void fetchAllAlbumSongsWithCallback(List albums, ArtistSongsCallback callback) { + if (albums == null || albums.isEmpty()) { + Log.d("ArtistSync", "No albums to process"); + callback.onSongsCollected(new ArrayList<>()); + return; + } + + List allSongs = new ArrayList<>(); + AtomicInteger remainingAlbums = new AtomicInteger(albums.size()); + Log.d("ArtistSync", "Processing " + albums.size() + " albums"); + + for (AlbumID3 album : albums) { + Log.d("ArtistSync", "Getting tracks for album: " + album.getName()); + MutableLiveData> albumTracks = albumRepository.getAlbumTracks(album.getId()); + albumTracks.observeForever(songs -> { + Log.d("ArtistSync", "Got " + (songs != null ? songs.size() : 0) + " songs from album"); + if (songs != null) { + allSongs.addAll(songs); + } + albumTracks.removeObservers(null); + + int remaining = remainingAlbums.decrementAndGet(); + Log.d("ArtistSync", "Remaining albums: " + remaining); + + if (remaining == 0) { + Log.d("ArtistSync", "All albums processed. Total songs: " + allSongs.size()); + callback.onSongsCollected(allSongs); + } + }); + } + } + + public interface ArtistSongsCallback { + void onSongsCollected(List songs); + } + public MutableLiveData> getStarredArtists(boolean random, int size) { MutableLiveData> starredArtists = new MutableLiveData<>(new ArrayList<>()); @@ -89,7 +149,7 @@ public class ArtistRepository { } /* - * Metodo che mi restituisce le informazioni essenzionali dell'artista (cover, numero di album...) + * Method that returns essential artist information (cover, album number, etc.) */ public void getArtistInfo(List artists, MutableLiveData> list) { List liveArtists = list.getValue(); diff --git a/app/src/main/java/com/cappielloantonio/tempo/ui/dialog/StarredArtistSyncDialog.java b/app/src/main/java/com/cappielloantonio/tempo/ui/dialog/StarredArtistSyncDialog.java new file mode 100644 index 00000000..448ca072 --- /dev/null +++ b/app/src/main/java/com/cappielloantonio/tempo/ui/dialog/StarredArtistSyncDialog.java @@ -0,0 +1,88 @@ +package com.cappielloantonio.tempo.ui.dialog; + +import android.app.Dialog; +import android.content.Context; +import android.os.Bundle; +import android.widget.Button; + +import androidx.annotation.NonNull; +import androidx.annotation.OptIn; +import androidx.fragment.app.DialogFragment; +import androidx.lifecycle.ViewModelProvider; +import androidx.media3.common.util.UnstableApi; + +import com.cappielloantonio.tempo.R; +import com.cappielloantonio.tempo.databinding.DialogStarredArtistSyncBinding; +import com.cappielloantonio.tempo.model.Download; +import com.cappielloantonio.tempo.util.DownloadUtil; +import com.cappielloantonio.tempo.util.MappingUtil; +import com.cappielloantonio.tempo.util.Preferences; +import com.cappielloantonio.tempo.viewmodel.StarredArtistsSyncViewModel; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; + +import java.util.stream.Collectors; + +@OptIn(markerClass = UnstableApi.class) +public class StarredArtistSyncDialog extends DialogFragment { + private StarredArtistsSyncViewModel starredArtistsSyncViewModel; + + private Runnable onCancel; + + public StarredArtistSyncDialog(Runnable onCancel) { + this.onCancel = onCancel; + } + + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + DialogStarredArtistSyncBinding bind = DialogStarredArtistSyncBinding.inflate(getLayoutInflater()); + + starredArtistsSyncViewModel = new ViewModelProvider(requireActivity()).get(StarredArtistsSyncViewModel.class); + + return new MaterialAlertDialogBuilder(getActivity()) + .setView(bind.getRoot()) + .setTitle(R.string.starred_artist_sync_dialog_title) + .setPositiveButton(R.string.starred_sync_dialog_positive_button, null) + .setNeutralButton(R.string.starred_sync_dialog_neutral_button, null) + .setNegativeButton(R.string.starred_sync_dialog_negative_button, null) + .create(); + } + + @Override + public void onResume() { + super.onResume(); + setButtonAction(requireContext()); + } + + private void setButtonAction(Context context) { + androidx.appcompat.app.AlertDialog dialog = (androidx.appcompat.app.AlertDialog) getDialog(); + + if (dialog != null) { + Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE); + positiveButton.setOnClickListener(v -> { + starredArtistsSyncViewModel.getStarredArtistSongs(requireActivity()).observe(this, allSongs -> { + if (allSongs != null && !allSongs.isEmpty()) { + DownloadUtil.getDownloadTracker(context).download( + MappingUtil.mapDownloads(allSongs), + allSongs.stream().map(Download::new).collect(Collectors.toList()) + ); + } + dialog.dismiss(); + }); + }); + + Button neutralButton = dialog.getButton(Dialog.BUTTON_NEUTRAL); + neutralButton.setOnClickListener(v -> { + Preferences.setStarredArtistsSyncEnabled(true); + dialog.dismiss(); + }); + + Button negativeButton = dialog.getButton(Dialog.BUTTON_NEGATIVE); + negativeButton.setOnClickListener(v -> { + Preferences.setStarredArtistsSyncEnabled(false); + if (onCancel != null) onCancel.run(); + dialog.dismiss(); + }); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/bottomsheetdialog/ArtistBottomSheetDialog.java b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/bottomsheetdialog/ArtistBottomSheetDialog.java index f3c9d490..78fc943e 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/bottomsheetdialog/ArtistBottomSheetDialog.java +++ b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/bottomsheetdialog/ArtistBottomSheetDialog.java @@ -66,7 +66,7 @@ public class ArtistBottomSheetDialog extends BottomSheetDialogFragment implement super.onStop(); } - // TODO Utilizzare il viewmodel come tramite ed evitare le chiamate dirette + // TODO Use the viewmodel as a conduit and avoid direct calls private void init(View view) { ImageView coverArtist = view.findViewById(R.id.artist_cover_image_view); CustomGlideRequest.Builder @@ -81,7 +81,7 @@ public class ArtistBottomSheetDialog extends BottomSheetDialogFragment implement ToggleButton favoriteToggle = view.findViewById(R.id.button_favorite); favoriteToggle.setChecked(artistBottomSheetViewModel.getArtist().getStarred() != null); favoriteToggle.setOnClickListener(v -> { - artistBottomSheetViewModel.setFavorite(); + artistBottomSheetViewModel.setFavorite(requireContext()); }); TextView playRadio = view.findViewById(R.id.play_radio_text_view); diff --git a/app/src/main/java/com/cappielloantonio/tempo/util/Preferences.kt b/app/src/main/java/com/cappielloantonio/tempo/util/Preferences.kt index 92cb30cd..f4188719 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/util/Preferences.kt +++ b/app/src/main/java/com/cappielloantonio/tempo/util/Preferences.kt @@ -37,6 +37,7 @@ object Preferences { private const val WIFI_ONLY = "wifi_only" private const val DATA_SAVING_MODE = "data_saving_mode" private const val SERVER_UNREACHABLE = "server_unreachable" + private const val SYNC_STARRED_ARTISTS_FOR_OFFLINE_USE = "sync_starred_artists_for_offline_use" private const val SYNC_STARRED_ALBUMS_FOR_OFFLINE_USE = "sync_starred_albums_for_offline_use" private const val SYNC_STARRED_TRACKS_FOR_OFFLINE_USE = "sync_starred_tracks_for_offline_use" private const val QUEUE_SYNCING = "queue_syncing" @@ -303,6 +304,18 @@ object Preferences { .apply() } + @JvmStatic + fun isStarredArtistsSyncEnabled(): Boolean { + return App.getInstance().preferences.getBoolean(SYNC_STARRED_ARTISTS_FOR_OFFLINE_USE, false) + } + + @JvmStatic + fun setStarredArtistsSyncEnabled(isStarredSyncEnabled: Boolean) { + App.getInstance().preferences.edit().putBoolean( + SYNC_STARRED_ARTISTS_FOR_OFFLINE_USE, isStarredSyncEnabled + ).apply() + } + @JvmStatic fun isStarredAlbumsSyncEnabled(): Boolean { return App.getInstance().preferences.getBoolean(SYNC_STARRED_ALBUMS_FOR_OFFLINE_USE, false) diff --git a/app/src/main/java/com/cappielloantonio/tempo/viewmodel/ArtistBottomSheetViewModel.java b/app/src/main/java/com/cappielloantonio/tempo/viewmodel/ArtistBottomSheetViewModel.java index 08ae3681..2c008d80 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/viewmodel/ArtistBottomSheetViewModel.java +++ b/app/src/main/java/com/cappielloantonio/tempo/viewmodel/ArtistBottomSheetViewModel.java @@ -1,17 +1,25 @@ package com.cappielloantonio.tempo.viewmodel; import android.app.Application; - +import android.content.Context; +import android.util.Log; import androidx.annotation.NonNull; import androidx.lifecycle.AndroidViewModel; +import com.cappielloantonio.tempo.model.Download; import com.cappielloantonio.tempo.interfaces.StarCallback; import com.cappielloantonio.tempo.repository.ArtistRepository; import com.cappielloantonio.tempo.repository.FavoriteRepository; import com.cappielloantonio.tempo.subsonic.models.ArtistID3; +import com.cappielloantonio.tempo.subsonic.models.Child; import com.cappielloantonio.tempo.util.NetworkUtil; +import com.cappielloantonio.tempo.util.DownloadUtil; +import com.cappielloantonio.tempo.util.MappingUtil; +import com.cappielloantonio.tempo.util.Preferences; import java.util.Date; +import java.util.stream.Collectors; +import java.util.List; public class ArtistBottomSheetViewModel extends AndroidViewModel { private final ArtistRepository artistRepository; @@ -34,7 +42,7 @@ public class ArtistBottomSheetViewModel extends AndroidViewModel { this.artist = artist; } - public void setFavorite() { + public void setFavorite(Context context) { if (artist.getStarred() != null) { if (NetworkUtil.isOffline()) { removeFavoriteOffline(); @@ -43,9 +51,9 @@ public class ArtistBottomSheetViewModel extends AndroidViewModel { } } else { if (NetworkUtil.isOffline()) { - setFavoriteOffline(); + setFavoriteOffline(context); } else { - setFavoriteOnline(); + setFavoriteOnline(context); } } } @@ -59,7 +67,6 @@ public class ArtistBottomSheetViewModel extends AndroidViewModel { favoriteRepository.unstar(null, null, artist.getId(), new StarCallback() { @Override public void onError() { - // artist.setStarred(new Date()); favoriteRepository.starLater(null, null, artist.getId(), false); } }); @@ -67,20 +74,45 @@ public class ArtistBottomSheetViewModel extends AndroidViewModel { artist.setStarred(null); } - private void setFavoriteOffline() { + private void setFavoriteOffline(Context context) { favoriteRepository.starLater(null, null, artist.getId(), true); artist.setStarred(new Date()); } - private void setFavoriteOnline() { + private void setFavoriteOnline(Context context) { favoriteRepository.star(null, null, artist.getId(), new StarCallback() { @Override public void onError() { - // artist.setStarred(null); favoriteRepository.starLater(null, null, artist.getId(), true); } }); artist.setStarred(new Date()); + + Log.d("ArtistSync", "Checking preference: " + Preferences.isStarredArtistsSyncEnabled()); + + if (Preferences.isStarredArtistsSyncEnabled()) { + Log.d("ArtistSync", "Starting artist sync for: " + artist.getName()); + + artistRepository.getArtistAllSongs(artist.getId(), new ArtistRepository.ArtistSongsCallback() { + @Override + public void onSongsCollected(List songs) { + Log.d("ArtistSync", "Callback triggered with songs: " + (songs != null ? songs.size() : 0)); + if (songs != null && !songs.isEmpty()) { + Log.d("ArtistSync", "Starting download of " + songs.size() + " songs"); + DownloadUtil.getDownloadTracker(context).download( + MappingUtil.mapDownloads(songs), + songs.stream().map(Download::new).collect(Collectors.toList()) + ); + Log.d("ArtistSync", "Download started successfully"); + } else { + Log.d("ArtistSync", "No songs to download"); + } + } + }); + } else { + Log.d("ArtistSync", "Artist sync preference is disabled"); + } } + /// } diff --git a/app/src/main/java/com/cappielloantonio/tempo/viewmodel/StarredArtistsSyncViewModel.java b/app/src/main/java/com/cappielloantonio/tempo/viewmodel/StarredArtistsSyncViewModel.java new file mode 100644 index 00000000..724a7f9c --- /dev/null +++ b/app/src/main/java/com/cappielloantonio/tempo/viewmodel/StarredArtistsSyncViewModel.java @@ -0,0 +1,95 @@ +package com.cappielloantonio.tempo.viewmodel; + +import android.app.Application; +import android.app.Activity; + +import androidx.annotation.NonNull; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LifecycleOwner; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.Observer; +import androidx.lifecycle.MutableLiveData; + +import com.cappielloantonio.tempo.repository.ArtistRepository; +import com.cappielloantonio.tempo.subsonic.models.ArtistID3; +import com.cappielloantonio.tempo.subsonic.models.Child; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicInteger; + +public class StarredArtistsSyncViewModel extends AndroidViewModel { + private final ArtistRepository artistRepository; + + private final MutableLiveData> starredArtists = new MutableLiveData<>(null); + private final MutableLiveData> starredArtistSongs = new MutableLiveData<>(null); + + public StarredArtistsSyncViewModel(@NonNull Application application) { + super(application); + artistRepository = new ArtistRepository(); + } + + public LiveData> getStarredArtists(LifecycleOwner owner) { + artistRepository.getStarredArtists(false, -1).observe(owner, starredArtists::postValue); + return starredArtists; + } + + public LiveData> getAllStarredArtistSongs() { + artistRepository.getStarredArtists(false, -1).observeForever(new Observer>() { + @Override + public void onChanged(List artists) { + if (artists != null && !artists.isEmpty()) { + collectAllArtistSongs(artists, starredArtistSongs::postValue); + } else { + starredArtistSongs.postValue(new ArrayList<>()); + } + artistRepository.getStarredArtists(false, -1).removeObserver(this); + } + }); + + return starredArtistSongs; + } + + public LiveData> getStarredArtistSongs(Activity activity) { + artistRepository.getStarredArtists(false, -1).observe((LifecycleOwner) activity, artists -> { + if (artists != null && !artists.isEmpty()) { + collectAllArtistSongs(artists, starredArtistSongs::postValue); + } else { + starredArtistSongs.postValue(new ArrayList<>()); + } + }); + return starredArtistSongs; + } + + private void collectAllArtistSongs(List artists, ArtistSongsCallback callback) { + if (artists == null || artists.isEmpty()) { + callback.onSongsCollected(new ArrayList<>()); + return; + } + + List allSongs = new ArrayList<>(); + AtomicInteger remainingArtists = new AtomicInteger(artists.size()); + + for (ArtistID3 artist : artists) { + // Use the new callback-based method + artistRepository.getArtistAllSongs(artist.getId(), new ArtistRepository.ArtistSongsCallback() { + @Override + public void onSongsCollected(List songs) { + if (songs != null) { + allSongs.addAll(songs); + } + + int remaining = remainingArtists.decrementAndGet(); + if (remaining == 0) { + callback.onSongsCollected(allSongs); + } + } + }); + } + } + + private interface ArtistSongsCallback { + void onSongsCollected(List songs); + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_starred_artist_sync.xml b/app/src/main/res/layout/dialog_starred_artist_sync.xml new file mode 100644 index 00000000..ca41742e --- /dev/null +++ b/app/src/main/res/layout/dialog_starred_artist_sync.xml @@ -0,0 +1,14 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 40febf58..126f027c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -346,6 +346,8 @@ Priority given to the transcoding mode. If set to \"Direct play\" the bitrate of the file will not be changed. Download transcoded media. If enabled, the download endpoint will not be used, but the following settings. \n\n If \"Transcode format for donwloads\" is set to \"Direct download\" the bitrate of the file will not be changed. When the file is transcoded on the fly, the client usually does not show the track length. It is possible to request the servers that support the functionality to estimate the duration of the track being played, but the response times may take longer. + If enabled, starred artists will be downloaded for offline use. + Sync starred artists for offline use If enabled, starred albums will be downloaded for offline use. Sync starred albums for offline use If enabled, starred tracks will be downloaded for offline use. @@ -403,6 +405,8 @@ Continue and download Downloading starred tracks may require a large amount of data. Sync starred tracks + Downloading starred artists may require a large amount of data. + Sync starred artists Downloading starred albums may require a large amount of data. Sync starred albums For the changes to take effect, restart the app. @@ -410,7 +414,7 @@ Select storage option External Internal - https://buymeacoffee.com/a.cappiello + https://ko-fi.com/eddyizm Album Artist Bit depth diff --git a/app/src/main/res/xml/global_preferences.xml b/app/src/main/res/xml/global_preferences.xml index 18cf4470..e8d262d7 100644 --- a/app/src/main/res/xml/global_preferences.xml +++ b/app/src/main/res/xml/global_preferences.xml @@ -150,6 +150,12 @@ android:summary="@string/settings_sync_starred_albums_for_offline_use_summary" android:key="sync_starred_albums_for_offline_use" /> + +