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

@ -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;
}
}