Implementation of download navigation for artists and albums

This commit is contained in:
CappielloAntonio 2021-08-30 12:38:43 +02:00
parent c5f39cf9ee
commit 86d46f7537
16 changed files with 98 additions and 53 deletions

View file

@ -101,6 +101,8 @@ public class AlbumHorizontalAdapter extends RecyclerView.Adapter<AlbumHorizontal
Navigation.findNavController(view).navigate(R.id.action_homeFragment_to_albumPageFragment, bundle);
} else if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.albumListPageFragment) {
Navigation.findNavController(view).navigate(R.id.action_albumListPageFragment_to_albumPageFragment, bundle);
} else if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.downloadFragment) {
Navigation.findNavController(view).navigate(R.id.action_downloadFragment_to_songListPageFragment, bundle);
}
}

View file

@ -107,6 +107,8 @@ public class ArtistHorizontalAdapter extends RecyclerView.Adapter<ArtistHorizont
Navigation.findNavController(view).navigate(R.id.action_homeFragment_to_artistPageFragment, bundle);
} else if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.artistListPageFragment) {
Navigation.findNavController(view).navigate(R.id.action_artistListPageFragment_to_artistPageFragment, bundle);
} else if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.downloadFragment) {
Navigation.findNavController(view).navigate(R.id.action_downloadFragment_to_albumListPageFragment, bundle);
}
}

View file

@ -15,7 +15,7 @@ import com.cappielloantonio.play.model.Queue;
import com.cappielloantonio.play.model.RecentSearch;
import com.cappielloantonio.play.model.Server;
@Database(entities = {Queue.class, Server.class, RecentSearch.class, Download.class}, version = 15, exportSchema = false)
@Database(entities = {Queue.class, Server.class, RecentSearch.class, Download.class}, version = 16, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
private static final String TAG = "AppDatabase";

View file

@ -12,12 +12,18 @@ import java.util.List;
@Dao
public interface DownloadDao {
@Query("SELECT * FROM download")
List<Download> getAll();
@Query("SELECT * FROM download WHERE server=:server")
LiveData<List<Download>> getAll(String server);
@Query("SELECT * FROM download WHERE server=:server LIMIT :size")
LiveData<List<Download>> getSample(int size, String server);
@Query("SELECT * FROM download WHERE server=:server AND artistId=:artistId")
LiveData<List<Download>> getAllFromArtist(String server, String artistId);
@Query("SELECT * FROM download WHERE server=:server AND albumId=:albumId")
LiveData<List<Download>> getAllFromAlbum(String server, String albumId);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Download download);

View file

@ -18,7 +18,9 @@ public class Album implements Parcelable {
public static final String RECENTLY_PLAYED = "RECENTLY_PLAYED";
public static final String MOST_PLAYED = "MOST_PLAYED";
public static final String RECENTLY_ADDED = "RECENTLY_ADDED";
public static final String DOWNLOADED = "DOWNLOADED";
public static final String STARRED = "STARRED";
public static final String FROM_ARTIST = "FROM_ARTIST";
private String id;
private String title;

View file

@ -17,6 +17,7 @@ import java.util.List;
public class Artist implements Parcelable {
private static final String TAG = "Artist";
public static final String DOWNLOADED = "DOWNLOADED";
public static final String STARRED = "STARRED";
private List<Genre> genres;

View file

@ -20,6 +20,7 @@ public class Song implements Parcelable {
public static final String BY_YEAR = "BY_YEAR";
public static final String STARRED = "STARRED";
public static final String DOWNLOADED = "DOWNLOADED";
public static final String FROM_ALBUM = "FROM_ALBUM";
private String id;
private String title;

View file

@ -23,45 +23,22 @@ public class DownloadRepository {
downloadDao = database.downloadDao();
}
public List<Download> getLiveDownload() {
List<Download> downloads = new ArrayList<>();
GetDownloadThreadSafe getDownloads = new GetDownloadThreadSafe(downloadDao);
Thread thread = new Thread(getDownloads);
thread.start();
try {
thread.join();
downloads = getDownloads.getDownloads();
} catch (InterruptedException e) {
e.printStackTrace();
}
return downloads;
}
private static class GetDownloadThreadSafe implements Runnable {
private final DownloadDao downloadDao;
private List<Download> downloads;
public GetDownloadThreadSafe(DownloadDao downloadDao) {
this.downloadDao = downloadDao;
}
@Override
public void run() {
downloads = downloadDao.getAll();
}
public List<Download> getDownloads() {
return downloads;
}
public LiveData<List<Download>> getLiveDownload() {
return downloadDao.getAll(PreferenceUtil.getInstance(App.getInstance()).getServerId());
}
public LiveData<List<Download>> getLiveDownloadSample(int size) {
return downloadDao.getSample(size, PreferenceUtil.getInstance(App.getInstance()).getServerId());
}
public LiveData<List<Download>> getLiveDownloadFromArtist(String artistId) {
return downloadDao.getAllFromArtist(PreferenceUtil.getInstance(App.getInstance()).getServerId(), artistId);
}
public LiveData<List<Download>> getLiveDownloadFromAlbum(String albumId) {
return downloadDao.getAllFromAlbum(PreferenceUtil.getInstance(App.getInstance()).getServerId(), albumId);
}
public void insert(Download download) {
InsertThreadSafe insert = new InsertThreadSafe(downloadDao, download);
Thread thread = new Thread(insert);

View file

@ -65,6 +65,13 @@ public class AlbumListPageFragment extends Fragment {
} else if (getArguments().getString(Album.STARRED) != null) {
albumListPageViewModel.title = Album.STARRED;
bind.pageTitleLabel.setText("Starred albums");
} else if (getArguments().getString(Album.DOWNLOADED) != null) {
albumListPageViewModel.title = Album.DOWNLOADED;
bind.pageTitleLabel.setText("Downloaded albums");
} else if (getArguments().getParcelable("artist_object") != null) {
albumListPageViewModel.artist = getArguments().getParcelable("artist_object");
albumListPageViewModel.title = Album.FROM_ARTIST;
bind.pageTitleLabel.setText(albumListPageViewModel.artist.getName());
}
}

View file

@ -14,6 +14,7 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import com.cappielloantonio.play.adapter.ArtistHorizontalAdapter;
import com.cappielloantonio.play.databinding.FragmentArtistListPageBinding;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.ui.activity.MainActivity;
import com.cappielloantonio.play.viewmodel.ArtistListPageViewModel;
@ -53,9 +54,12 @@ public class ArtistListPageFragment extends Fragment {
}
private void init() {
if (getArguments().getString(Album.STARRED) != null) {
artistListPageViewModel.title = Album.STARRED;
if (getArguments().getString(Artist.STARRED) != null) {
artistListPageViewModel.title = Artist.STARRED;
bind.pageTitleLabel.setText("Starred artists");
} else if (getArguments().getString(Artist.DOWNLOADED) != null) {
artistListPageViewModel.title = Artist.DOWNLOADED;
bind.pageTitleLabel.setText("Downloaded artists");
}
}
@ -86,6 +90,6 @@ public class ArtistListPageFragment extends Fragment {
artistHorizontalAdapter = new ArtistHorizontalAdapter(requireContext());
bind.artistListRecyclerView.setAdapter(artistHorizontalAdapter);
artistListPageViewModel.getArtistList().observe(requireActivity(), artists -> artistHorizontalAdapter.setItems(artists));
artistListPageViewModel.getArtistList(requireActivity()).observe(requireActivity(), artists -> artistHorizontalAdapter.setItems(artists));
}
}

View file

@ -123,13 +123,13 @@ public class DownloadFragment extends Fragment {
private void init() {
bind.downloadedArtistTextViewClickable.setOnClickListener(v -> {
Bundle bundle = new Bundle();
bundle.putString(Song.DOWNLOADED, Song.DOWNLOADED);
bundle.putString(Artist.DOWNLOADED, Artist.DOWNLOADED);
activity.navController.navigate(R.id.action_downloadFragment_to_artistListPageFragment, bundle);
});
bind.downloadedAlbumTextViewClickable.setOnClickListener(v -> {
Bundle bundle = new Bundle();
bundle.putString(Song.DOWNLOADED, Song.DOWNLOADED);
bundle.putString(Album.DOWNLOADED, Album.DOWNLOADED);
activity.navController.navigate(R.id.action_downloadFragment_to_albumListPageFragment, bundle);
});

View file

@ -93,6 +93,10 @@ public class SongListPageFragment extends Fragment {
} else if (getArguments().getString(Song.DOWNLOADED) != null) {
songListPageViewModel.title = Song.DOWNLOADED;
bind.pageTitleLabel.setText("Downloaded");
} else if (getArguments().getParcelable("album_object") != null) {
songListPageViewModel.album = getArguments().getParcelable("album_object");
songListPageViewModel.title = Song.FROM_ALBUM;
bind.pageTitleLabel.setText(songListPageViewModel.album.getTitle());
}
}
@ -116,7 +120,7 @@ public class SongListPageFragment extends Fragment {
}
private void initButtons() {
songListPageViewModel.getSongList().observe(requireActivity(), songs -> {
songListPageViewModel.getSongList(requireActivity()).observe(requireActivity(), songs -> {
if (bind != null) {
bind.songListShuffleImageView.setOnClickListener(v -> {
Collections.shuffle(songs);
@ -139,6 +143,6 @@ public class SongListPageFragment extends Fragment {
songHorizontalAdapter = new SongHorizontalAdapter(activity, requireContext());
bind.songListRecyclerView.setAdapter(songHorizontalAdapter);
songListPageViewModel.getSongList().observe(requireActivity(), songs -> songHorizontalAdapter.setItems(songs));
songListPageViewModel.getSongList(requireActivity()).observe(requireActivity(), songs -> songHorizontalAdapter.setItems(songs));
}
}

View file

@ -9,16 +9,21 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.repository.AlbumRepository;
import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.util.MappingUtil;
import java.util.ArrayList;
import java.util.List;
public class AlbumListPageViewModel extends AndroidViewModel {
private AlbumRepository albumRepository;
private DownloadRepository downloadRepository;
public String title;
public Artist artist;
private MutableLiveData<List<Album>> albumList;
@ -26,30 +31,41 @@ public class AlbumListPageViewModel extends AndroidViewModel {
super(application);
albumRepository = new AlbumRepository(application);
downloadRepository = new DownloadRepository(application);
}
public LiveData<List<Album>> getAlbumList(FragmentActivity activity) {
albumList = new MutableLiveData<>(new ArrayList<>());
switch (title) {
case Song.RECENTLY_PLAYED:
case Album.RECENTLY_PLAYED:
albumRepository.getAlbums("recent", 500).observe(activity, albums -> {
albumList.setValue(albums);
});
break;
case Song.MOST_PLAYED:
case Album.MOST_PLAYED:
albumRepository.getAlbums("frequent", 500).observe(activity, albums -> {
albumList.setValue(albums);
});
break;
case Song.RECENTLY_ADDED:
case Album.RECENTLY_ADDED:
albumRepository.getAlbums("newest", 500).observe(activity, albums -> {
albumList.setValue(albums);
});
break;
case Song.STARRED:
case Album.STARRED:
albumList = albumRepository.getStarredAlbums();
break;
case Album.DOWNLOADED:
downloadRepository.getLiveDownload().observe(activity, downloads -> {
albumList.setValue(MappingUtil.mapDownloadToAlbum(downloads));
});
break;
case Album.FROM_ARTIST:
downloadRepository.getLiveDownloadFromArtist(artist.getId()).observe(activity, downloads -> {
albumList.setValue(MappingUtil.mapDownloadToAlbum(downloads));
});
break;
}
return albumList;

View file

@ -3,19 +3,25 @@ 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;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Download;
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.util.MappingUtil;
import java.util.ArrayList;
import java.util.List;
public class ArtistListPageViewModel extends AndroidViewModel {
private ArtistRepository artistRepository;
private DownloadRepository downloadRepository;
public String title;
@ -25,15 +31,21 @@ public class ArtistListPageViewModel extends AndroidViewModel {
super(application);
artistRepository = new ArtistRepository(application);
downloadRepository = new DownloadRepository(application);
}
public LiveData<List<Artist>> getArtistList() {
public LiveData<List<Artist>> getArtistList(FragmentActivity activity) {
artistList = new MutableLiveData<>(new ArrayList<>());
switch (title) {
case Song.STARRED:
case Artist.STARRED:
artistList = artistRepository.getStarredArtists();
break;
case Artist.DOWNLOADED:
downloadRepository.getLiveDownload().observe(activity, downloads -> {
artistList.setValue(MappingUtil.mapDownloadToArtist(downloads));
});
break;
}
return artistList;

View file

@ -4,10 +4,12 @@ import android.app.Application;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Genre;
import com.cappielloantonio.play.model.Song;
@ -27,6 +29,9 @@ public class SongListPageViewModel extends AndroidViewModel {
public String title;
public Genre genre;
public Artist artist;
public Album album;
private MutableLiveData<List<Song>> songList;
public ArrayList<String> filters = new ArrayList<>();
public ArrayList<String> filterNames = new ArrayList<>();
@ -41,8 +46,8 @@ public class SongListPageViewModel extends AndroidViewModel {
downloadRepository = new DownloadRepository(application);
}
public LiveData<List<Song>> getSongList() {
MutableLiveData<List<Song>> songList = new MutableLiveData<>(new ArrayList<>());
public LiveData<List<Song>> getSongList(FragmentActivity activity) {
songList = new MutableLiveData<>(new ArrayList<>());
switch (title) {
case Song.RECENTLY_PLAYED:
@ -70,7 +75,14 @@ public class SongListPageViewModel extends AndroidViewModel {
songList = songRepository.getStarredSongs(false, -1);
break;
case Song.DOWNLOADED:
songList.setValue(MappingUtil.mapDownloadToSong(downloadRepository.getLiveDownload()));
downloadRepository.getLiveDownload().observe(activity, downloads -> {
songList.setValue(MappingUtil.mapDownloadToSong(downloads));
});
break;
case Song.FROM_ALBUM:
downloadRepository.getLiveDownloadFromAlbum(album.getId()).observe(activity, downloads -> {
songList.setValue(MappingUtil.mapDownloadToSong(downloads));
});
break;
}

View file

@ -33,7 +33,6 @@
app:popUpTo="@id/loginFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/homeFragment"
android:name="com.cappielloantonio.play.ui.fragment.HomeFragment"