From ee738bc4c7ae5f6e4dcd391fb8bb470bd59f3afd Mon Sep 17 00:00:00 2001 From: eddyizm Date: Sat, 27 Sep 2025 15:37:59 -0700 Subject: [PATCH 1/4] 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" /> + + Date: Sat, 27 Sep 2025 21:52:04 -0700 Subject: [PATCH 2/4] chore: added dialog to starred artists sync --- .../tempo/ui/fragment/SettingsFragment.java | 18 +++++++++++++++++- .../viewmodel/StarredArtistsSyncViewModel.java | 1 - 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/SettingsFragment.java b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/SettingsFragment.java index aa33631c..602acc8c 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/SettingsFragment.java +++ b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/SettingsFragment.java @@ -42,6 +42,7 @@ import com.cappielloantonio.tempo.ui.dialog.DeleteDownloadStorageDialog; import com.cappielloantonio.tempo.ui.dialog.DownloadStorageDialog; import com.cappielloantonio.tempo.ui.dialog.StarredSyncDialog; import com.cappielloantonio.tempo.ui.dialog.StarredAlbumSyncDialog; +import com.cappielloantonio.tempo.ui.dialog.StarredArtistSyncDialog; import com.cappielloantonio.tempo.ui.dialog.StreamingCacheStorageDialog; import com.cappielloantonio.tempo.util.DownloadUtil; import com.cappielloantonio.tempo.util.Preferences; @@ -110,6 +111,7 @@ public class SettingsFragment extends PreferenceFragmentCompat { actionScan(); actionSyncStarredAlbums(); actionSyncStarredTracks(); + actionSyncStarredArtists(); actionChangeStreamingCacheStorage(); actionChangeDownloadStorage(); actionDeleteDownloadStorage(); @@ -296,7 +298,21 @@ public class SettingsFragment extends PreferenceFragmentCompat { return true; }); } - + + private void actionSyncStarredArtists() { + findPreference("sync_starred_artists_for_offline_use").setOnPreferenceChangeListener((preference, newValue) -> { + if (newValue instanceof Boolean) { + if ((Boolean) newValue) { + StarredArtistSyncDialog dialog = new StarredArtistSyncDialog(() -> { + ((SwitchPreference)preference).setChecked(false); + }); + dialog.show(activity.getSupportFragmentManager(), null); + } + } + return true; + }); + } + private void actionChangeStreamingCacheStorage() { findPreference("streaming_cache_storage").setOnPreferenceClickListener(preference -> { StreamingCacheStorageDialog dialog = new StreamingCacheStorageDialog(new DialogClickCallback() { diff --git a/app/src/main/java/com/cappielloantonio/tempo/viewmodel/StarredArtistsSyncViewModel.java b/app/src/main/java/com/cappielloantonio/tempo/viewmodel/StarredArtistsSyncViewModel.java index 724a7f9c..474cbe87 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/viewmodel/StarredArtistsSyncViewModel.java +++ b/app/src/main/java/com/cappielloantonio/tempo/viewmodel/StarredArtistsSyncViewModel.java @@ -72,7 +72,6 @@ public class StarredArtistsSyncViewModel extends AndroidViewModel { 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) { From a187ba1e75456525d2cc9103dbf12a064b805162 Mon Sep 17 00:00:00 2001 From: eddyizm Date: Sat, 27 Sep 2025 22:37:30 -0700 Subject: [PATCH 3/4] fix: moved api call back to artist repository after losing the thread. --- .../tempo/repository/ArtistRepository.java | 42 ++++++++++++++----- 1 file changed, 32 insertions(+), 10 deletions(-) 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 71b43fa9..4e06fad7 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/repository/ArtistRepository.java +++ b/app/src/main/java/com/cappielloantonio/tempo/repository/ArtistRepository.java @@ -31,16 +31,38 @@ public class ArtistRepository { 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<>()); - } - }); + // Get the artist info first, which contains the albums + App.getSubsonicClientInstance(false) + .getBrowsingClient() + .getArtist(artistId) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful() && response.body() != null && + response.body().getSubsonicResponse().getArtist() != null && + response.body().getSubsonicResponse().getArtist().getAlbums() != null) { + + List albums = response.body().getSubsonicResponse().getArtist().getAlbums(); + Log.d("ArtistSync", "Got albums directly: " + albums.size()); + + if (!albums.isEmpty()) { + fetchAllAlbumSongsWithCallback(albums, callback); + } else { + Log.d("ArtistSync", "No albums found in artist response"); + callback.onSongsCollected(new ArrayList<>()); + } + } else { + Log.d("ArtistSync", "Failed to get artist info"); + callback.onSongsCollected(new ArrayList<>()); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + Log.d("ArtistSync", "Error getting artist info: " + t.getMessage()); + callback.onSongsCollected(new ArrayList<>()); + } + }); } private void fetchAllAlbumSongsWithCallback(List albums, ArtistSongsCallback callback) { From 47380a79a576e9c4ccbc0d66a6efb1589116fd40 Mon Sep 17 00:00:00 2001 From: eddyizm Date: Sun, 28 Sep 2025 16:14:42 -0700 Subject: [PATCH 4/4] fix: added init on home tab and dialog, refactor and check for songs for albums/artists before displaying dialog --- .../ui/fragment/HomeTabMusicFragment.java | 168 +++++++++++++++--- .../tempo/viewmodel/HomeViewModel.java | 6 + .../res/layout/fragment_home_tab_music.xml | 92 ++++++++++ app/src/main/res/values/strings.xml | 10 ++ 4 files changed, 249 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/HomeTabMusicFragment.java b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/HomeTabMusicFragment.java index 4c47f0c9..bf6b7d6c 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/HomeTabMusicFragment.java +++ b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/HomeTabMusicFragment.java @@ -9,6 +9,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.PopupMenu; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -40,6 +41,7 @@ import com.cappielloantonio.tempo.service.MediaService; import com.cappielloantonio.tempo.subsonic.models.Child; import com.cappielloantonio.tempo.subsonic.models.Share; import com.cappielloantonio.tempo.subsonic.models.AlbumID3; +import com.cappielloantonio.tempo.subsonic.models.ArtistID3; import com.cappielloantonio.tempo.ui.activity.MainActivity; import com.cappielloantonio.tempo.ui.adapter.AlbumAdapter; import com.cappielloantonio.tempo.ui.adapter.AlbumHorizontalAdapter; @@ -116,6 +118,7 @@ public class HomeTabMusicFragment extends Fragment implements ClickCallback { initSyncStarredView(); initSyncStarredAlbumsView(); + initSyncStarredArtistsView(); initDiscoverSongSlideView(); initSimilarSongView(); initArtistRadio(); @@ -327,32 +330,12 @@ public class HomeTabMusicFragment extends Fragment implements ClickCallback { private void initSyncStarredAlbumsView() { if (Preferences.isStarredAlbumsSyncEnabled()) { - homeViewModel.getStarredAlbums(getViewLifecycleOwner()).observeForever(new Observer>() { + homeViewModel.getStarredAlbums(getViewLifecycleOwner()).observe(getViewLifecycleOwner(), new Observer>() { @Override public void onChanged(List albums) { - if (albums != null) { - DownloaderManager manager = DownloadUtil.getDownloadTracker(requireContext()); - List albumsToSync = new ArrayList<>(); - int albumCount = 0; - - for (AlbumID3 album : albums) { - boolean needsSync = false; - albumCount++; - albumsToSync.add(album.getName()); - } - - if (albumCount > 0) { - bind.homeSyncStarredAlbumsCard.setVisibility(View.VISIBLE); - String message = getResources().getQuantityString( - R.plurals.home_sync_starred_albums_count, - albumCount, - albumCount - ); - bind.homeSyncStarredAlbumsToSync.setText(message); - } + if (albums != null && !albums.isEmpty()) { + checkIfAlbumsNeedSync(albums); } - - homeViewModel.getStarredAlbums(getViewLifecycleOwner()).removeObserver(this); } }); } @@ -362,26 +345,157 @@ public class HomeTabMusicFragment extends Fragment implements ClickCallback { }); bind.homeSyncStarredAlbumsDownload.setOnClickListener(v -> { - homeViewModel.getAllStarredAlbumSongs().observeForever(new Observer>() { + homeViewModel.getAllStarredAlbumSongs().observe(getViewLifecycleOwner(), new Observer>() { @Override public void onChanged(List allSongs) { - if (allSongs != null) { + if (allSongs != null && !allSongs.isEmpty()) { DownloaderManager manager = DownloadUtil.getDownloadTracker(requireContext()); + int songsToDownload = 0; for (Child song : allSongs) { if (!manager.isDownloaded(song.getId())) { manager.download(MappingUtil.mapDownload(song), new Download(song)); + songsToDownload++; } } - } - homeViewModel.getAllStarredAlbumSongs().removeObserver(this); + if (songsToDownload > 0) { + Toast.makeText(requireContext(), + getResources().getQuantityString(R.plurals.songs_download_started, songsToDownload, songsToDownload), + Toast.LENGTH_SHORT).show(); + } + } + bind.homeSyncStarredAlbumsCard.setVisibility(View.GONE); } }); }); } + private void checkIfAlbumsNeedSync(List albums) { + homeViewModel.getAllStarredAlbumSongs().observe(getViewLifecycleOwner(), new Observer>() { + @Override + public void onChanged(List allSongs) { + if (allSongs != null) { + DownloaderManager manager = DownloadUtil.getDownloadTracker(requireContext()); + int songsToDownload = 0; + List albumsNeedingSync = new ArrayList<>(); + + for (AlbumID3 album : albums) { + boolean albumNeedsSync = false; + // Check if any songs from this album need downloading + for (Child song : allSongs) { + if (song.getAlbumId() != null && song.getAlbumId().equals(album.getId()) && + !manager.isDownloaded(song.getId())) { + songsToDownload++; + albumNeedsSync = true; + } + } + if (albumNeedsSync) { + albumsNeedingSync.add(album.getName()); + } + } + + if (songsToDownload > 0) { + bind.homeSyncStarredAlbumsCard.setVisibility(View.VISIBLE); + String message = getResources().getQuantityString( + R.plurals.home_sync_starred_albums_count, + albumsNeedingSync.size(), + albumsNeedingSync.size() + ); + bind.homeSyncStarredAlbumsToSync.setText(message); + } else { + bind.homeSyncStarredAlbumsCard.setVisibility(View.GONE); + } + } + } + }); + } + + private void initSyncStarredArtistsView() { + if (Preferences.isStarredArtistsSyncEnabled()) { + homeViewModel.getStarredArtists(getViewLifecycleOwner()).observe(getViewLifecycleOwner(), new Observer>() { + @Override + public void onChanged(List artists) { + if (artists != null && !artists.isEmpty()) { + checkIfArtistsNeedSync(artists); + } + } + }); + } + + bind.homeSyncStarredArtistsCancel.setOnClickListener(v -> { + bind.homeSyncStarredArtistsCard.setVisibility(View.GONE); + }); + + bind.homeSyncStarredArtistsDownload.setOnClickListener(v -> { + homeViewModel.getAllStarredArtistSongs().observe(getViewLifecycleOwner(), new Observer>() { + @Override + public void onChanged(List allSongs) { + if (allSongs != null && !allSongs.isEmpty()) { + DownloaderManager manager = DownloadUtil.getDownloadTracker(requireContext()); + int songsToDownload = 0; + + for (Child song : allSongs) { + if (!manager.isDownloaded(song.getId())) { + manager.download(MappingUtil.mapDownload(song), new Download(song)); + songsToDownload++; + } + } + + if (songsToDownload > 0) { + Toast.makeText(requireContext(), + getResources().getQuantityString(R.plurals.songs_download_started, songsToDownload, songsToDownload), + Toast.LENGTH_SHORT).show(); + } + } + + bind.homeSyncStarredArtistsCard.setVisibility(View.GONE); + } + }); + }); + } + + private void checkIfArtistsNeedSync(List artists) { + homeViewModel.getAllStarredArtistSongs().observe(getViewLifecycleOwner(), new Observer>() { + @Override + public void onChanged(List allSongs) { + if (allSongs != null) { + DownloaderManager manager = DownloadUtil.getDownloadTracker(requireContext()); + int songsToDownload = 0; + List artistsNeedingSync = new ArrayList<>(); + + for (ArtistID3 artist : artists) { + boolean artistNeedsSync = false; + // Check if any songs from this artist need downloading + for (Child song : allSongs) { + if (song.getArtistId() != null && song.getArtistId().equals(artist.getId()) && + !manager.isDownloaded(song.getId())) { + songsToDownload++; + artistNeedsSync = true; + } + } + if (artistNeedsSync) { + artistsNeedingSync.add(artist.getName()); + } + } + + if (songsToDownload > 0) { + bind.homeSyncStarredArtistsCard.setVisibility(View.VISIBLE); + String message = getResources().getQuantityString( + R.plurals.home_sync_starred_artists_count, + artistsNeedingSync.size(), + artistsNeedingSync.size() + ); + bind.homeSyncStarredArtistsToSync.setText(message); + } else { + bind.homeSyncStarredArtistsCard.setVisibility(View.GONE); + } + } + } + }); + } + private void initDiscoverSongSlideView() { if (homeViewModel.checkHomeSectorVisibility(Constants.HOME_SECTOR_DISCOVERY)) return; diff --git a/app/src/main/java/com/cappielloantonio/tempo/viewmodel/HomeViewModel.java b/app/src/main/java/com/cappielloantonio/tempo/viewmodel/HomeViewModel.java index 6477178c..2089ce20 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/viewmodel/HomeViewModel.java +++ b/app/src/main/java/com/cappielloantonio/tempo/viewmodel/HomeViewModel.java @@ -48,6 +48,7 @@ public class HomeViewModel extends AndroidViewModel { private final SharingRepository sharingRepository; private final StarredAlbumsSyncViewModel albumsSyncViewModel; + private final StarredArtistsSyncViewModel artistSyncViewModel; private final MutableLiveData> dicoverSongSample = new MutableLiveData<>(null); private final MutableLiveData> newReleasedAlbum = new MutableLiveData<>(null); @@ -85,6 +86,7 @@ public class HomeViewModel extends AndroidViewModel { sharingRepository = new SharingRepository(); albumsSyncViewModel = new StarredAlbumsSyncViewModel(application); + artistSyncViewModel = new StarredArtistsSyncViewModel(application); setOfflineFavorite(); } @@ -174,6 +176,10 @@ public class HomeViewModel extends AndroidViewModel { return albumsSyncViewModel.getAllStarredAlbumSongs(); } + public LiveData> getAllStarredArtistSongs() { + return artistSyncViewModel.getAllStarredArtistSongs(); + } + public LiveData> getStarredArtists(LifecycleOwner owner) { if (starredArtists.getValue() == null) { artistRepository.getStarredArtists(true, 20).observe(owner, starredArtists::postValue); diff --git a/app/src/main/res/layout/fragment_home_tab_music.xml b/app/src/main/res/layout/fragment_home_tab_music.xml index e9811da3..c516171c 100644 --- a/app/src/main/res/layout/fragment_home_tab_music.xml +++ b/app/src/main/res/layout/fragment_home_tab_music.xml @@ -198,6 +198,98 @@ + + + + + + + + + + + + + + + + + + + + + + + + Looks like there are some starred tracks to sync Sync Starred Albums Albums marked with a star will be available offline + Starred Artists Sync + You have starred artists with music not downloaded Best of Discovery Shuffle all @@ -447,6 +449,14 @@ %d album to sync %d albums to sync + + %d artist to sync + %d artists to sync + + + Downloading %d song + Downloading %d songs + Equalizer Reset Enable