From 47380a79a576e9c4ccbc0d66a6efb1589116fd40 Mon Sep 17 00:00:00 2001 From: eddyizm Date: Sun, 28 Sep 2025 16:14:42 -0700 Subject: [PATCH] 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