Modified sql queries used to retrieving downloaded artists and albums

This commit is contained in:
CappielloAntonio 2021-08-31 09:11:42 +02:00
parent 7d6ad5737f
commit ac584974c6
4 changed files with 15 additions and 20 deletions

View file

@ -15,6 +15,12 @@ public interface DownloadDao {
@Query("SELECT * FROM download WHERE server=:server")
LiveData<List<Download>> getAll(String server);
@Query("SELECT * FROM download WHERE server=:server GROUP BY artistName LIMIT :size")
LiveData<List<Download>> getSampleArtist(int size, String server);
@Query("SELECT * FROM download WHERE server=:server GROUP BY albumName LIMIT :size")
LiveData<List<Download>> getSampleAlbum(int size, String server);
@Query("SELECT * FROM download WHERE server=:server LIMIT :size")
LiveData<List<Download>> getSample(int size, String server);

View file

@ -10,7 +10,6 @@ import com.cappielloantonio.play.database.dao.DownloadDao;
import com.cappielloantonio.play.model.Download;
import com.cappielloantonio.play.util.PreferenceUtil;
import java.util.ArrayList;
import java.util.List;
public class DownloadRepository {
@ -27,8 +26,11 @@ public class DownloadRepository {
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>> getLiveDownloadSample(int size, boolean isArtist, boolean isAlbum, boolean isTrack) {
if (isArtist) return downloadDao.getSampleArtist(size, PreferenceUtil.getInstance(App.getInstance()).getServerId());
else if (isAlbum) return downloadDao.getSampleAlbum(size, PreferenceUtil.getInstance(App.getInstance()).getServerId());
else if (isTrack) return downloadDao.getSample(size, PreferenceUtil.getInstance(App.getInstance()).getServerId());
else return downloadDao.getSample(size, PreferenceUtil.getInstance(App.getInstance()).getServerId());
}
public LiveData<List<Download>> getLiveDownloadFromArtist(String artistId) {

View file

@ -8,10 +8,8 @@ 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;

View file

@ -10,16 +10,11 @@ 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.DownloadRepository;
import com.cappielloantonio.play.util.MappingUtil;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;
public class DownloadViewModel extends AndroidViewModel {
private static final String TAG = "HomeViewModel";
@ -37,25 +32,19 @@ public class DownloadViewModel extends AndroidViewModel {
}
public LiveData<List<Artist>> getDownloadedArtists(LifecycleOwner owner, int size) {
downloadRepository.getLiveDownloadSample(size).observe(owner, downloads -> {
List<Download> unique = downloads
.stream()
.collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Download::getArtistName))), ArrayList::new)
);
downloadedArtistSample.postValue(MappingUtil.mapDownloadToArtist(unique));
downloadRepository.getLiveDownloadSample(size, true, false, false).observe(owner, downloads -> {
downloadedArtistSample.postValue(MappingUtil.mapDownloadToArtist(downloads));
});
return downloadedArtistSample;
}
public LiveData<List<Album>> getDownloadedAlbums(LifecycleOwner owner, int size) {
downloadRepository.getLiveDownloadSample(size).observe(owner, downloads -> downloadedAlbumSample.postValue(MappingUtil.mapDownloadToAlbum(downloads)));
downloadRepository.getLiveDownloadSample(size, false, true, false).observe(owner, downloads -> downloadedAlbumSample.postValue(MappingUtil.mapDownloadToAlbum(downloads)));
return downloadedAlbumSample;
}
public LiveData<List<Song>> getDownloadedTracks(LifecycleOwner owner, int size) {
downloadRepository.getLiveDownloadSample(size).observe(owner, downloads -> downloadedTrackSample.postValue(MappingUtil.mapDownloadToSong(downloads)));
downloadRepository.getLiveDownloadSample(size, false, false, true).observe(owner, downloads -> downloadedTrackSample.postValue(MappingUtil.mapDownloadToSong(downloads)));
return downloadedTrackSample;
}
}