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) {
Bundle bundle = new Bundle();
bundle.putParcelable("album_object", albums.get(getBindingAdapterPosition()));
bundle.putBoolean("is_offline", false);
if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.searchFragment) {
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) {
Bundle bundle = new Bundle();
bundle.putParcelable("album_object", albums.get(getBindingAdapterPosition()));
bundle.putBoolean("is_offline", false);
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) {
Bundle bundle = new Bundle();
bundle.putParcelable("album_object", albums.get(getBindingAdapterPosition()));
bundle.putBoolean("is_offline", false);
if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.searchFragment) {
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 final LayoutInflater mInflater;
private final Context context;
private final boolean isOffline;
public AlbumHorizontalAdapter(Context context) {
public AlbumHorizontalAdapter(Context context, boolean isOffline) {
this.context = context;
this.mInflater = LayoutInflater.from(context);
this.albums = new ArrayList<>();
this.isOffline = isOffline;
}
@NonNull
@ -96,6 +98,7 @@ public class AlbumHorizontalAdapter extends RecyclerView.Adapter<AlbumHorizontal
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putParcelable("album_object", albums.get(getBindingAdapterPosition()));
bundle.putBoolean("is_offline", isOffline);
if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.homeFragment) {
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.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);
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) {
switch (item.getItemId()) {
case R.id.action_download_album:
albumPageViewModel.getAlbumSongLiveList().observe(requireActivity(), songs -> {
albumPageViewModel.getAlbumSongLiveList(requireActivity()).observe(requireActivity(), songs -> {
DownloadUtil.getDownloadTracker(requireContext()).toggleDownload(songs);
});
return true;
@ -106,6 +106,7 @@ public class AlbumPageFragment extends Fragment {
private void init() {
albumPageViewModel.setAlbum(getArguments().getParcelable("album_object"));
albumPageViewModel.setOffline(getArguments().getBoolean("is_offline"));
}
private void initAppBar() {
@ -146,7 +147,7 @@ public class AlbumPageFragment extends Fragment {
}
private void initMusicButton() {
albumPageViewModel.getAlbumSongLiveList().observe(requireActivity(), songs -> {
albumPageViewModel.getAlbumSongLiveList(requireActivity()).observe(requireActivity(), songs -> {
if (bind != null && !songs.isEmpty()) {
bind.albumPagePlayButton.setOnClickListener(v -> {
QueueRepository queueRepository = new QueueRepository(App.getInstance());
@ -193,7 +194,7 @@ public class AlbumPageFragment extends Fragment {
songHorizontalAdapter = new SongHorizontalAdapter(activity, requireContext(), false);
bind.songRecyclerView.setAdapter(songHorizontalAdapter);
albumPageViewModel.getAlbumSongLiveList().observe(requireActivity(), songs -> {
albumPageViewModel.getAlbumSongLiveList(requireActivity()).observe(requireActivity(), songs -> {
songHorizontalAdapter.setItems(songs);
});
}

View file

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

View file

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

View file

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

View file

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