Removed unused methods from song repository

This commit is contained in:
CappielloAntonio 2021-04-19 15:10:53 +02:00
parent 4a02a49ac1
commit ff1f4ef106
3 changed files with 235 additions and 383 deletions

View file

@ -18,9 +18,6 @@ public interface SongDao {
@Query("SELECT * FROM song")
LiveData<List<Song>> getAll();
@Query("SELECT * FROM song WHERE id = :id")
LiveData<Song> getOne(String id);
@Query("SELECT * FROM song")
List<Song> getAllList();
@ -36,22 +33,18 @@ public interface SongDao {
@Query("SELECT * FROM song WHERE play_count != 0 ORDER BY play_count DESC LIMIT :number")
LiveData<List<Song>> getMostPlayedSample(int number);
// @Query("SELECT * FROM song WHERE artistId = :artistID ORDER BY play_count DESC LIMIT :number")
@Query("SELECT song.* FROM song INNER JOIN song_artist_cross ON song.id = song_artist_cross.song_id AND song_artist_cross.artist_id = :artistID ORDER BY play_count DESC LIMIT :number")
LiveData<List<Song>> getArtistTopSongsSample(String artistID, int number);
// @Query("SELECT * FROM song WHERE artistId = :artistID ORDER BY play_count DESC")
@Query("SELECT song.* FROM song INNER JOIN song_artist_cross ON song.id = song_artist_cross.song_id AND song_artist_cross.artist_id = :artistID ORDER BY play_count DESC")
LiveData<List<Song>> getArtistTopSongs(String artistID);
// @Query("SELECT * FROM song WHERE artistId = :artistID ORDER BY RANDOM() LIMIT :number")
@Query("SELECT song.* FROM song INNER JOIN song_artist_cross ON song.id = song_artist_cross.song_id AND song_artist_cross.artist_id = :artistID ORDER BY RANDOM() LIMIT :number")
List<Song> getArtistRandomSongs(String artistID, int number);
@Query("SELECT * FROM song WHERE albumId = :albumID ORDER BY trackNumber ASC")
LiveData<List<Song>> getLiveAlbumSong(String albumID);
// @Query("SELECT * FROM song WHERE albumId = :albumID ORDER BY trackNumber ASC")
@Query("SELECT song.* FROM song INNER JOIN playlist_song_cross ON song.id = playlist_song_cross.song_id AND playlist_song_cross.playlist_id = :playlistID")
LiveData<List<Song>> getLivePlaylistSong(String playlistID);
@ -70,33 +63,15 @@ public interface SongDao {
@Query("SELECT * FROM song WHERE favorite = 1")
LiveData<List<Song>> getFavoriteSong();
@Query("SELECT * FROM song WHERE id = :id")
Song getSongByID(String id);
@Query("SELECT EXISTS(SELECT * FROM song WHERE id = :id)")
boolean exist(String id);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Song song);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<Song> songs);
@Delete
void delete(Song song);
@Query("DELETE FROM song")
void deleteAll();
@Update
void update(Song song);
@Query("UPDATE song SET play_count = :playCount AND last_play = :lastPlay WHERE id = :id")
void updatePlayCount(String id, int playCount, long lastPlay);
@Query("UPDATE song SET favorite = :isFavorite WHERE id = :id")
void updateFavorite(String id, boolean isFavorite);
@Query("SELECT * FROM song WHERE id IN (:ids)")
List<Song> getSongsByID(List<String> ids);

View file

@ -21,7 +21,6 @@ public class SongRepository {
private SongDao songDao;
private SongGenreCrossDao songGenreCrossDao;
private LiveData<List<Song>> searchListLiveSongs;
private LiveData<Song> liveDataSong;
private LiveData<List<Song>> listLiveSampleRecentlyAddedSongs;
private LiveData<List<Song>> listLiveSampleRecentlyPlayedSongs;
private LiveData<List<Song>> listLiveSampleMostPlayedSongs;
@ -34,7 +33,6 @@ public class SongRepository {
private LiveData<List<Song>> listLiveSampleFavoritesSong;
private LiveData<List<Song>> listLiveFavoritesSong;
public SongRepository(Application application) {
AppDatabase database = AppDatabase.getInstance(application);
songDao = database.songDao();
@ -93,6 +91,28 @@ public class SongRepository {
return songs;
}
private static class GetRandomSongsByArtistIDThreadSafe implements Runnable {
private SongDao songDao;
private String artistID;
private int limit;
private List<Song> songs = new ArrayList<>();
public GetRandomSongsByArtistIDThreadSafe(SongDao songDao, String artistID, int limit) {
this.songDao = songDao;
this.artistID = artistID;
this.limit = limit;
}
@Override
public void run() {
songs = songDao.getArtistRandomSongs(artistID, limit);
}
public List<Song> getSongs() {
return songs;
}
}
public LiveData<List<Song>> getAlbumListLiveSong(String albumID) {
listLiveAlbumSongs = songDao.getLiveAlbumSong(albumID);
return listLiveAlbumSongs;
@ -124,175 +144,6 @@ public class SongRepository {
return songs;
}
public LiveData<List<Song>> getFilteredListLiveSong(ArrayList<String> filters) {
listLiveFilteredSongs = songDao.getFilteredSong(filters);
return listLiveFilteredSongs;
}
public List<String> getSearchSuggestion(String query) {
List<String> suggestions = new ArrayList<>();
SearchSuggestionsThreadSafe suggestionsThread = new SearchSuggestionsThreadSafe(songDao, query, 5);
Thread thread = new Thread(suggestionsThread);
thread.start();
try {
thread.join();
suggestions = suggestionsThread.getSuggestions();
} catch (InterruptedException e) {
e.printStackTrace();
}
return suggestions;
}
/*
* Funzione che ritorna l'intero set di canzoni.
* Utilizzato per l'aggiornamento del catalogo.
*/
public List<Song> getCatalogue() {
List<Song> catalogue = new ArrayList<>();
GetCatalogueThreadSafe getCatalogueThread = new GetCatalogueThreadSafe(songDao);
Thread thread = new Thread(getCatalogueThread);
thread.start();
try {
thread.join();
catalogue = getCatalogueThread.getCatalogue();
} catch (InterruptedException e) {
e.printStackTrace();
}
return catalogue;
}
public List<Integer> getYearList() {
List<Integer> years = new ArrayList<>();
GetYearListThreadSafe getYearListThreadSafe = new GetYearListThreadSafe(songDao);
Thread thread = new Thread(getYearListThreadSafe);
thread.start();
try {
thread.join();
years = getYearListThreadSafe.getYearList();
} catch (InterruptedException e) {
e.printStackTrace();
}
return years;
}
public LiveData<List<Song>> getSongByYearListLive(int year) {
listLiveSongByYear = songDao.getSongsByYear(year);
return listLiveSongByYear;
}
public LiveData<List<Song>> getListLiveFavoritesSampleSong(int number) {
listLiveSampleFavoritesSong = songDao.getFavoriteSongSample(number);
return listLiveSampleFavoritesSong;
}
public LiveData<List<Song>> getListLiveFavoritesSong() {
listLiveFavoritesSong = songDao.getFavoriteSong();
return listLiveFavoritesSong;
}
public boolean exist(Song song) {
boolean exist = false;
ExistThreadSafe existThread = new ExistThreadSafe(songDao, song);
Thread thread = new Thread(existThread);
thread.start();
try {
thread.join();
exist = existThread.exist();
} catch (InterruptedException e) {
e.printStackTrace();
}
return exist;
}
public void insert(Song song) {
InsertThreadSafe insert = new InsertThreadSafe(songDao, song);
Thread thread = new Thread(insert);
thread.start();
}
public void insertAll(ArrayList<Song> songs) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(songDao, songGenreCrossDao, songs);
Thread thread = new Thread(insertAll);
thread.start();
}
public void delete(Song song) {
DeleteThreadSafe delete = new DeleteThreadSafe(songDao, song);
Thread thread = new Thread(delete);
thread.start();
}
public void increasePlayCount(Song song) {
boolean isIncreased = song.nowPlaying();
if(isIncreased) {
// UpdatePlayCountThreadSafe update = new UpdatePlayCountThreadSafe(songDao, song);
UpdateThreadSafe update = new UpdateThreadSafe(songDao, song);
Thread thread = new Thread(update);
thread.start();
}
}
public void setFavoriteStatus(Song song) {
// UpdateFavoriteThreadSafe update = new UpdateFavoriteThreadSafe(songDao, song);
UpdateThreadSafe update = new UpdateThreadSafe(songDao, song);
Thread thread = new Thread(update);
thread.start();
}
public void getAll() {
GetCatalogueThreadSafe catalogue = new GetCatalogueThreadSafe(songDao);
Thread thread = new Thread(catalogue);
thread.start();
}
public void insertSongPerGenre(ArrayList<SongGenreCross> songGenreCrosses) {
InsertPerGenreThreadSafe insertPerGenre = new InsertPerGenreThreadSafe(songGenreCrossDao, songGenreCrosses);
Thread thread = new Thread(insertPerGenre);
thread.start();
}
public void deleteAllSong() {
DeleteAllSongThreadSafe delete = new DeleteAllSongThreadSafe(songDao);
Thread thread = new Thread(delete);
thread.start();
}
public void deleteAllSongGenreCross() {
DeleteAllSongGenreCrossThreadSafe delete = new DeleteAllSongGenreCrossThreadSafe(songGenreCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
public List<Song> getRandomSample(int number) {
List<Song> sample = new ArrayList<>();
PickRandomThreadSafe randomThread = new PickRandomThreadSafe(songDao, number);
Thread thread = new Thread(randomThread);
thread.start();
try {
thread.join();
sample = randomThread.getSample();
} catch (InterruptedException e) {
e.printStackTrace();
}
return sample;
}
private static class GetSongsByAlbumIDThreadSafe implements Runnable {
private SongDao songDao;
private String albumID;
@ -313,160 +164,26 @@ public class SongRepository {
}
}
private static class GetRandomSongsByArtistIDThreadSafe implements Runnable {
private SongDao songDao;
private String artistID;
private int limit;
private List<Song> songs = new ArrayList<>();
public GetRandomSongsByArtistIDThreadSafe(SongDao songDao, String artistID, int limit) {
this.songDao = songDao;
this.artistID = artistID;
this.limit = limit;
}
@Override
public void run() {
songs = songDao.getArtistRandomSongs(artistID, limit);
}
public List<Song> getSongs() {
return songs;
}
public LiveData<List<Song>> getFilteredListLiveSong(ArrayList<String> filters) {
listLiveFilteredSongs = songDao.getFilteredSong(filters);
return listLiveFilteredSongs;
}
private static class ExistThreadSafe implements Runnable {
private SongDao songDao;
private Song song;
private boolean exist = false;
public List<String> getSearchSuggestion(String query) {
List<String> suggestions = new ArrayList<>();
public ExistThreadSafe(SongDao songDao, Song song) {
this.songDao = songDao;
this.song = song;
SearchSuggestionsThreadSafe suggestionsThread = new SearchSuggestionsThreadSafe(songDao, query, 5);
Thread thread = new Thread(suggestionsThread);
thread.start();
try {
thread.join();
suggestions = suggestionsThread.getSuggestions();
} catch (InterruptedException e) {
e.printStackTrace();
}
@Override
public void run() {
exist = songDao.exist(song.getId());
}
public boolean exist() {
return exist;
}
}
private static class InsertThreadSafe implements Runnable {
private SongDao songDao;
private Song song;
public InsertThreadSafe(SongDao songDao, Song song) {
this.songDao = songDao;
this.song = song;
}
@Override
public void run() {
songDao.insert(song);
}
}
private static class InsertAllThreadSafe implements Runnable {
private SongDao songDao;
private SongGenreCrossDao songGenreCrossDao;
private ArrayList<Song> songs;
public InsertAllThreadSafe(SongDao songDao, SongGenreCrossDao songGenreCrossDao, ArrayList<Song> songs) {
this.songDao = songDao;
this.songGenreCrossDao = songGenreCrossDao;
this.songs = songs;
}
@Override
public void run() {
songDao.deleteAll();
songGenreCrossDao.deleteAll();
songDao.insertAll(songs);
}
}
private static class DeleteThreadSafe implements Runnable {
private SongDao songDao;
private Song song;
public DeleteThreadSafe(SongDao songDao, Song song) {
this.songDao = songDao;
this.song = song;
}
@Override
public void run() {
songDao.delete(song);
}
}
private static class UpdateThreadSafe implements Runnable {
private SongDao songDao;
private Song song;
public UpdateThreadSafe(SongDao songDao, Song song) {
this.songDao = songDao;
this.song = song;
}
@Override
public void run() {
songDao.update(song);
}
}
private static class UpdatePlayCountThreadSafe implements Runnable {
private SongDao songDao;
private Song song;
public UpdatePlayCountThreadSafe(SongDao songDao, Song song) {
this.songDao = songDao;
this.song = song;
}
@Override
public void run() {
songDao.updatePlayCount(song.getId(), song.getPlayCount(), song.getLastPlay());
}
}
private static class UpdateFavoriteThreadSafe implements Runnable {
private SongDao songDao;
private Song song;
public UpdateFavoriteThreadSafe(SongDao songDao, Song song) {
this.songDao = songDao;
this.song = song;
}
@Override
public void run() {
songDao.updateFavorite(song.getId(), song.isFavorite());
}
}
private static class PickRandomThreadSafe implements Runnable {
private SongDao songDao;
private int elementNumber;
private List<Song> sample;
public PickRandomThreadSafe(SongDao songDao, int number) {
this.songDao = songDao;
this.elementNumber = number;
}
@Override
public void run() {
sample = songDao.random(elementNumber);
}
public List<Song> getSample() {
return sample;
}
return suggestions;
}
private static class SearchSuggestionsThreadSafe implements Runnable {
@ -491,6 +208,27 @@ public class SongRepository {
}
}
/*
* Funzione che ritorna l'intero set di canzoni.
* Utilizzato per l'aggiornamento del catalogo.
*/
public List<Song> getCatalogue() {
List<Song> catalogue = new ArrayList<>();
GetCatalogueThreadSafe getCatalogueThread = new GetCatalogueThreadSafe(songDao);
Thread thread = new Thread(getCatalogueThread);
thread.start();
try {
thread.join();
catalogue = getCatalogueThread.getCatalogue();
} catch (InterruptedException e) {
e.printStackTrace();
}
return catalogue;
}
private static class GetCatalogueThreadSafe implements Runnable {
private SongDao songDao;
private List<Song> catalogue = new ArrayList<>();
@ -509,45 +247,21 @@ public class SongRepository {
}
}
private static class InsertPerGenreThreadSafe implements Runnable {
private SongGenreCrossDao songGenreCrossDao;
private ArrayList<SongGenreCross> cross;
public List<Integer> getYearList() {
List<Integer> years = new ArrayList<>();
public InsertPerGenreThreadSafe(SongGenreCrossDao songGenreCrossDao, ArrayList<SongGenreCross> cross) {
this.songGenreCrossDao = songGenreCrossDao;
this.cross = cross;
GetYearListThreadSafe getYearListThreadSafe = new GetYearListThreadSafe(songDao);
Thread thread = new Thread(getYearListThreadSafe);
thread.start();
try {
thread.join();
years = getYearListThreadSafe.getYearList();
} catch (InterruptedException e) {
e.printStackTrace();
}
@Override
public void run() {
songGenreCrossDao.insertAll(cross);
}
}
private static class DeleteAllSongThreadSafe implements Runnable {
private SongDao songDao;
public DeleteAllSongThreadSafe(SongDao songDao) {
this.songDao = songDao;
}
@Override
public void run() {
songDao.deleteAll();
}
}
private static class DeleteAllSongGenreCrossThreadSafe implements Runnable {
private SongGenreCrossDao songGenreCrossDao;
public DeleteAllSongGenreCrossThreadSafe(SongGenreCrossDao songGenreCrossDao) {
this.songGenreCrossDao = songGenreCrossDao;
}
@Override
public void run() {
songGenreCrossDao.deleteAll();
}
return years;
}
private static class GetYearListThreadSafe implements Runnable {
@ -578,4 +292,169 @@ public class SongRepository {
return decades;
}
}
public LiveData<List<Song>> getSongByYearListLive(int year) {
listLiveSongByYear = songDao.getSongsByYear(year);
return listLiveSongByYear;
}
public LiveData<List<Song>> getListLiveFavoritesSampleSong(int number) {
listLiveSampleFavoritesSong = songDao.getFavoriteSongSample(number);
return listLiveSampleFavoritesSong;
}
public LiveData<List<Song>> getListLiveFavoritesSong() {
listLiveFavoritesSong = songDao.getFavoriteSong();
return listLiveFavoritesSong;
}
public void insertAll(ArrayList<Song> songs) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(songDao, songGenreCrossDao, songs);
Thread thread = new Thread(insertAll);
thread.start();
}
private static class InsertAllThreadSafe implements Runnable {
private SongDao songDao;
private SongGenreCrossDao songGenreCrossDao;
private ArrayList<Song> songs;
public InsertAllThreadSafe(SongDao songDao, SongGenreCrossDao songGenreCrossDao, ArrayList<Song> songs) {
this.songDao = songDao;
this.songGenreCrossDao = songGenreCrossDao;
this.songs = songs;
}
@Override
public void run() {
songDao.deleteAll();
songGenreCrossDao.deleteAll();
songDao.insertAll(songs);
}
}
public void increasePlayCount(Song song) {
if(song.nowPlaying()) {
UpdateThreadSafe update = new UpdateThreadSafe(songDao, song);
Thread thread = new Thread(update);
thread.start();
}
}
public void setFavoriteStatus(Song song) {
UpdateThreadSafe update = new UpdateThreadSafe(songDao, song);
Thread thread = new Thread(update);
thread.start();
}
private static class UpdateThreadSafe implements Runnable {
private SongDao songDao;
private Song song;
public UpdateThreadSafe(SongDao songDao, Song song) {
this.songDao = songDao;
this.song = song;
}
@Override
public void run() {
songDao.update(song);
}
}
public void insertSongPerGenre(ArrayList<SongGenreCross> songGenreCrosses) {
InsertPerGenreThreadSafe insertPerGenre = new InsertPerGenreThreadSafe(songGenreCrossDao, songGenreCrosses);
Thread thread = new Thread(insertPerGenre);
thread.start();
}
private static class InsertPerGenreThreadSafe implements Runnable {
private SongGenreCrossDao songGenreCrossDao;
private ArrayList<SongGenreCross> cross;
public InsertPerGenreThreadSafe(SongGenreCrossDao songGenreCrossDao, ArrayList<SongGenreCross> cross) {
this.songGenreCrossDao = songGenreCrossDao;
this.cross = cross;
}
@Override
public void run() {
songGenreCrossDao.insertAll(cross);
}
}
public void deleteAllSong() {
DeleteAllSongThreadSafe delete = new DeleteAllSongThreadSafe(songDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllSongThreadSafe implements Runnable {
private SongDao songDao;
public DeleteAllSongThreadSafe(SongDao songDao) {
this.songDao = songDao;
}
@Override
public void run() {
songDao.deleteAll();
}
}
public void deleteAllSongGenreCross() {
DeleteAllSongGenreCrossThreadSafe delete = new DeleteAllSongGenreCrossThreadSafe(songGenreCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllSongGenreCrossThreadSafe implements Runnable {
private SongGenreCrossDao songGenreCrossDao;
public DeleteAllSongGenreCrossThreadSafe(SongGenreCrossDao songGenreCrossDao) {
this.songGenreCrossDao = songGenreCrossDao;
}
@Override
public void run() {
songGenreCrossDao.deleteAll();
}
}
public List<Song> getRandomSample(int number) {
List<Song> sample = new ArrayList<>();
PickRandomThreadSafe randomThread = new PickRandomThreadSafe(songDao, number);
Thread thread = new Thread(randomThread);
thread.start();
try {
thread.join();
sample = randomThread.getSample();
} catch (InterruptedException e) {
e.printStackTrace();
}
return sample;
}
private static class PickRandomThreadSafe implements Runnable {
private SongDao songDao;
private int elementNumber;
private List<Song> sample;
public PickRandomThreadSafe(SongDao songDao, int number) {
this.songDao = songDao;
this.elementNumber = number;
}
@Override
public void run() {
sample = songDao.random(elementNumber);
}
public List<Song> getSample() {
return sample;
}
}
}

View file

@ -30,8 +30,6 @@ public class PlaylistPageViewModel extends AndroidViewModel {
}
public LiveData<List<Song>> getPlaylistSongList() {
// Prendere le canzoni di ciascuna playlist
Log.i(TAG, "getPlaylistSongList: " + playlist.getId());
songList = songRepository.getPlaylistLiveSong(playlist.getId());
return songList;
}