AlbumPageFragment now correctly retrieves information locally or from the server

This commit is contained in:
CappielloAntonio 2021-09-02 12:44:35 +02:00
parent eeae6bbce5
commit 589c3289d4
10 changed files with 37 additions and 12 deletions

View file

@ -93,6 +93,7 @@ public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.ViewHolder>
public void onClick(View view) { public void onClick(View view) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putParcelable("album_object", albums.get(getBindingAdapterPosition())); bundle.putParcelable("album_object", albums.get(getBindingAdapterPosition()));
bundle.putBoolean("is_offline", false);
if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.searchFragment) { if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.searchFragment) {
Navigation.findNavController(view).navigate(R.id.action_searchFragment_to_albumPageFragment, bundle); Navigation.findNavController(view).navigate(R.id.action_searchFragment_to_albumPageFragment, bundle);

View file

@ -91,6 +91,7 @@ public class AlbumArtistPageOrSimilarAdapter extends RecyclerView.Adapter<AlbumA
public void onClick(View view) { public void onClick(View view) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putParcelable("album_object", albums.get(getBindingAdapterPosition())); bundle.putParcelable("album_object", albums.get(getBindingAdapterPosition()));
bundle.putBoolean("is_offline", false);
Navigation.findNavController(view).navigate(R.id.albumPageFragment, bundle); Navigation.findNavController(view).navigate(R.id.albumPageFragment, bundle);
} }

View file

@ -135,6 +135,7 @@ public class AlbumCatalogueAdapter extends RecyclerView.Adapter<AlbumCatalogueAd
public void onClick(View view) { public void onClick(View view) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putParcelable("album_object", albums.get(getBindingAdapterPosition())); bundle.putParcelable("album_object", albums.get(getBindingAdapterPosition()));
bundle.putBoolean("is_offline", false);
if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.searchFragment) { if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.searchFragment) {
Navigation.findNavController(view).navigate(R.id.action_searchFragment_to_albumPageFragment, bundle); Navigation.findNavController(view).navigate(R.id.action_searchFragment_to_albumPageFragment, bundle);

View file

@ -28,11 +28,13 @@ public class AlbumHorizontalAdapter extends RecyclerView.Adapter<AlbumHorizontal
private List<Album> albums; private List<Album> albums;
private final LayoutInflater mInflater; private final LayoutInflater mInflater;
private final Context context; private final Context context;
private final boolean isOffline;
public AlbumHorizontalAdapter(Context context) { public AlbumHorizontalAdapter(Context context, boolean isOffline) {
this.context = context; this.context = context;
this.mInflater = LayoutInflater.from(context); this.mInflater = LayoutInflater.from(context);
this.albums = new ArrayList<>(); this.albums = new ArrayList<>();
this.isOffline = isOffline;
} }
@NonNull @NonNull
@ -96,6 +98,7 @@ public class AlbumHorizontalAdapter extends RecyclerView.Adapter<AlbumHorizontal
public void onClick(View view) { public void onClick(View view) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putParcelable("album_object", albums.get(getBindingAdapterPosition())); bundle.putParcelable("album_object", albums.get(getBindingAdapterPosition()));
bundle.putBoolean("is_offline", isOffline);
if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.homeFragment) { if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.homeFragment) {
Navigation.findNavController(view).navigate(R.id.action_homeFragment_to_albumPageFragment, bundle); Navigation.findNavController(view).navigate(R.id.action_homeFragment_to_albumPageFragment, bundle);

View file

@ -101,7 +101,10 @@ public class AlbumListPageFragment extends Fragment {
bind.albumListRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext())); bind.albumListRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
bind.albumListRecyclerView.setHasFixedSize(true); bind.albumListRecyclerView.setHasFixedSize(true);
albumHorizontalAdapter = new AlbumHorizontalAdapter(requireContext()); albumHorizontalAdapter = new AlbumHorizontalAdapter(requireContext(),
(albumListPageViewModel.title.equals(Album.DOWNLOADED) || albumListPageViewModel.title.equals(Album.FROM_ARTIST))
);
bind.albumListRecyclerView.setAdapter(albumHorizontalAdapter); bind.albumListRecyclerView.setAdapter(albumHorizontalAdapter);
albumListPageViewModel.getAlbumList(requireActivity()).observe(requireActivity(), albums -> albumHorizontalAdapter.setItems(albums)); albumListPageViewModel.getAlbumList(requireActivity()).observe(requireActivity(), albums -> albumHorizontalAdapter.setItems(albums));
} }

View file

@ -93,7 +93,7 @@ public class AlbumPageFragment extends Fragment {
public boolean onOptionsItemSelected(@NonNull MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_download_album: case R.id.action_download_album:
albumPageViewModel.getAlbumSongLiveList().observe(requireActivity(), songs -> { albumPageViewModel.getAlbumSongLiveList(requireActivity()).observe(requireActivity(), songs -> {
DownloadUtil.getDownloadTracker(requireContext()).toggleDownload(songs); DownloadUtil.getDownloadTracker(requireContext()).toggleDownload(songs);
}); });
return true; return true;
@ -106,6 +106,7 @@ public class AlbumPageFragment extends Fragment {
private void init() { private void init() {
albumPageViewModel.setAlbum(getArguments().getParcelable("album_object")); albumPageViewModel.setAlbum(getArguments().getParcelable("album_object"));
albumPageViewModel.setOffline(getArguments().getBoolean("is_offline"));
} }
private void initAppBar() { private void initAppBar() {
@ -146,7 +147,7 @@ public class AlbumPageFragment extends Fragment {
} }
private void initMusicButton() { private void initMusicButton() {
albumPageViewModel.getAlbumSongLiveList().observe(requireActivity(), songs -> { albumPageViewModel.getAlbumSongLiveList(requireActivity()).observe(requireActivity(), songs -> {
if (bind != null && !songs.isEmpty()) { if (bind != null && !songs.isEmpty()) {
bind.albumPagePlayButton.setOnClickListener(v -> { bind.albumPagePlayButton.setOnClickListener(v -> {
QueueRepository queueRepository = new QueueRepository(App.getInstance()); QueueRepository queueRepository = new QueueRepository(App.getInstance());
@ -193,7 +194,7 @@ public class AlbumPageFragment extends Fragment {
songHorizontalAdapter = new SongHorizontalAdapter(activity, requireContext(), false); songHorizontalAdapter = new SongHorizontalAdapter(activity, requireContext(), false);
bind.songRecyclerView.setAdapter(songHorizontalAdapter); bind.songRecyclerView.setAdapter(songHorizontalAdapter);
albumPageViewModel.getAlbumSongLiveList().observe(requireActivity(), songs -> { albumPageViewModel.getAlbumSongLiveList(requireActivity()).observe(requireActivity(), songs -> {
songHorizontalAdapter.setItems(songs); songHorizontalAdapter.setItems(songs);
}); });
} }

View file

@ -182,7 +182,7 @@ public class DownloadFragment extends Fragment {
private void initDownloadedAlbumView() { private void initDownloadedAlbumView() {
bind.downloadedAlbumRecyclerView.setHasFixedSize(true); bind.downloadedAlbumRecyclerView.setHasFixedSize(true);
downloadedAlbumAdapter = new AlbumHorizontalAdapter(requireContext()); downloadedAlbumAdapter = new AlbumHorizontalAdapter(requireContext(), true);
bind.downloadedAlbumRecyclerView.setAdapter(downloadedAlbumAdapter); bind.downloadedAlbumRecyclerView.setAdapter(downloadedAlbumAdapter);
downloadViewModel.getDownloadedAlbums(requireActivity(), 20).observe(requireActivity(), albums -> { downloadViewModel.getDownloadedAlbums(requireActivity(), 20).observe(requireActivity(), albums -> {
if (albums == null) { if (albums == null) {

View file

@ -367,7 +367,7 @@ public class HomeFragment extends Fragment {
private void initStarredAlbumsView() { private void initStarredAlbumsView() {
bind.starredAlbumsRecyclerView.setHasFixedSize(true); bind.starredAlbumsRecyclerView.setHasFixedSize(true);
starredAlbumAdapter = new AlbumHorizontalAdapter(requireContext()); starredAlbumAdapter = new AlbumHorizontalAdapter(requireContext(), false);
bind.starredAlbumsRecyclerView.setAdapter(starredAlbumAdapter); bind.starredAlbumsRecyclerView.setAdapter(starredAlbumAdapter);
homeViewModel.getStarredAlbums(requireActivity()).observe(requireActivity(), albums -> { homeViewModel.getStarredAlbums(requireActivity()).observe(requireActivity(), albums -> {
if (albums == null) { if (albums == null) {

View file

@ -146,7 +146,7 @@ public class LibraryFragment extends Fragment {
private void initNewReleasesView() { private void initNewReleasesView() {
bind.newReleasesRecyclerView.setHasFixedSize(true); bind.newReleasesRecyclerView.setHasFixedSize(true);
newRelesesAlbumAdapter = new AlbumHorizontalAdapter(requireContext()); newRelesesAlbumAdapter = new AlbumHorizontalAdapter(requireContext(), false);
bind.newReleasesRecyclerView.setAdapter(newRelesesAlbumAdapter); bind.newReleasesRecyclerView.setAdapter(newRelesesAlbumAdapter);
libraryViewModel.getRecentlyReleasedAlbums(requireActivity()).observe(requireActivity(), albums -> { libraryViewModel.getRecentlyReleasedAlbums(requireActivity()).observe(requireActivity(), albums -> {
if (albums == null) { if (albums == null) {

View file

@ -3,6 +3,7 @@ package com.cappielloantonio.play.viewmodel;
import android.app.Application; import android.app.Application;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
@ -12,28 +13,38 @@ import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Song; import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.repository.AlbumRepository; import com.cappielloantonio.play.repository.AlbumRepository;
import com.cappielloantonio.play.repository.ArtistRepository; import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.repository.SongRepository; import com.cappielloantonio.play.repository.SongRepository;
import com.cappielloantonio.play.util.MappingUtil;
import com.cappielloantonio.play.util.MusicUtil;
import java.util.List; import java.util.List;
public class AlbumPageViewModel extends AndroidViewModel { public class AlbumPageViewModel extends AndroidViewModel {
private AlbumRepository albumRepository; private AlbumRepository albumRepository;
private ArtistRepository artistRepository; private ArtistRepository artistRepository;
private DownloadRepository downloadRepository;
private LiveData<List<Song>> songLiveList = new MutableLiveData<>(); private MutableLiveData<List<Song>> songLiveList = new MutableLiveData<>();
private LiveData<Album> albumInfo = new MutableLiveData<>();
private Album album; private Album album;
private boolean isOffline;
public AlbumPageViewModel(@NonNull Application application) { public AlbumPageViewModel(@NonNull Application application) {
super(application); super(application);
albumRepository = new AlbumRepository(application); albumRepository = new AlbumRepository(application);
artistRepository = new ArtistRepository(application); artistRepository = new ArtistRepository(application);
downloadRepository = new DownloadRepository(application);
} }
public LiveData<List<Song>> getAlbumSongLiveList() { public LiveData<List<Song>> getAlbumSongLiveList(FragmentActivity activity) {
if(isOffline) {
downloadRepository.getLiveDownloadFromAlbum(album.getId()).observe(activity, downloads -> songLiveList.postValue(MappingUtil.mapDownloadToSong(downloads)));
} else {
songLiveList = albumRepository.getAlbumTracks(album.getId()); songLiveList = albumRepository.getAlbumTracks(album.getId());
}
return songLiveList; return songLiveList;
} }
@ -45,6 +56,10 @@ public class AlbumPageViewModel extends AndroidViewModel {
this.album = album; this.album = album;
} }
public void setOffline(boolean offline) {
isOffline = offline;
}
public LiveData<Artist> getArtist() { public LiveData<Artist> getArtist() {
return artistRepository.getArtistInfo(album.getArtistId()); return artistRepository.getArtistInfo(album.getArtistId());
} }