From ff1f4ef106be7ae68945136bdd309ac56530f67a Mon Sep 17 00:00:00 2001 From: CappielloAntonio Date: Mon, 19 Apr 2021 15:10:53 +0200 Subject: [PATCH] Removed unused methods from song repository --- .../play/database/dao/SongDao.java | 25 - .../play/repository/SongRepository.java | 591 +++++++----------- .../play/viewmodel/PlaylistPageViewModel.java | 2 - 3 files changed, 235 insertions(+), 383 deletions(-) diff --git a/app/src/main/java/com/cappielloantonio/play/database/dao/SongDao.java b/app/src/main/java/com/cappielloantonio/play/database/dao/SongDao.java index 601bc60b..f16f970d 100644 --- a/app/src/main/java/com/cappielloantonio/play/database/dao/SongDao.java +++ b/app/src/main/java/com/cappielloantonio/play/database/dao/SongDao.java @@ -18,9 +18,6 @@ public interface SongDao { @Query("SELECT * FROM song") LiveData> getAll(); - @Query("SELECT * FROM song WHERE id = :id") - LiveData getOne(String id); - @Query("SELECT * FROM song") List getAllList(); @@ -36,22 +33,18 @@ public interface SongDao { @Query("SELECT * FROM song WHERE play_count != 0 ORDER BY play_count DESC LIMIT :number") LiveData> 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> 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> 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 getArtistRandomSongs(String artistID, int number); @Query("SELECT * FROM song WHERE albumId = :albumID ORDER BY trackNumber ASC") LiveData> 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> getLivePlaylistSong(String playlistID); @@ -70,33 +63,15 @@ public interface SongDao { @Query("SELECT * FROM song WHERE favorite = 1") LiveData> 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 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 getSongsByID(List ids); diff --git a/app/src/main/java/com/cappielloantonio/play/repository/SongRepository.java b/app/src/main/java/com/cappielloantonio/play/repository/SongRepository.java index 9bb5bbe0..04534e1b 100644 --- a/app/src/main/java/com/cappielloantonio/play/repository/SongRepository.java +++ b/app/src/main/java/com/cappielloantonio/play/repository/SongRepository.java @@ -21,7 +21,6 @@ public class SongRepository { private SongDao songDao; private SongGenreCrossDao songGenreCrossDao; private LiveData> searchListLiveSongs; - private LiveData liveDataSong; private LiveData> listLiveSampleRecentlyAddedSongs; private LiveData> listLiveSampleRecentlyPlayedSongs; private LiveData> listLiveSampleMostPlayedSongs; @@ -34,7 +33,6 @@ public class SongRepository { private LiveData> listLiveSampleFavoritesSong; private LiveData> 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 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 getSongs() { + return songs; + } + } + public LiveData> getAlbumListLiveSong(String albumID) { listLiveAlbumSongs = songDao.getLiveAlbumSong(albumID); return listLiveAlbumSongs; @@ -124,175 +144,6 @@ public class SongRepository { return songs; } - public LiveData> getFilteredListLiveSong(ArrayList filters) { - listLiveFilteredSongs = songDao.getFilteredSong(filters); - return listLiveFilteredSongs; - } - - public List getSearchSuggestion(String query) { - List 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 getCatalogue() { - List 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 getYearList() { - List 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> getSongByYearListLive(int year) { - listLiveSongByYear = songDao.getSongsByYear(year); - return listLiveSongByYear; - } - - public LiveData> getListLiveFavoritesSampleSong(int number) { - listLiveSampleFavoritesSong = songDao.getFavoriteSongSample(number); - return listLiveSampleFavoritesSong; - } - - public LiveData> 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 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 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 getRandomSample(int number) { - List 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 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 getSongs() { - return songs; - } + public LiveData> getFilteredListLiveSong(ArrayList 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 getSearchSuggestion(String query) { + List 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 songs; - - public InsertAllThreadSafe(SongDao songDao, SongGenreCrossDao songGenreCrossDao, ArrayList 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 sample; - - public PickRandomThreadSafe(SongDao songDao, int number) { - this.songDao = songDao; - this.elementNumber = number; - } - - @Override - public void run() { - sample = songDao.random(elementNumber); - } - - public List 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 getCatalogue() { + List 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 catalogue = new ArrayList<>(); @@ -509,45 +247,21 @@ public class SongRepository { } } - private static class InsertPerGenreThreadSafe implements Runnable { - private SongGenreCrossDao songGenreCrossDao; - private ArrayList cross; + public List getYearList() { + List years = new ArrayList<>(); - public InsertPerGenreThreadSafe(SongGenreCrossDao songGenreCrossDao, ArrayList 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> getSongByYearListLive(int year) { + listLiveSongByYear = songDao.getSongsByYear(year); + return listLiveSongByYear; + } + + public LiveData> getListLiveFavoritesSampleSong(int number) { + listLiveSampleFavoritesSong = songDao.getFavoriteSongSample(number); + return listLiveSampleFavoritesSong; + } + + public LiveData> getListLiveFavoritesSong() { + listLiveFavoritesSong = songDao.getFavoriteSong(); + return listLiveFavoritesSong; + } + + public void insertAll(ArrayList 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 songs; + + public InsertAllThreadSafe(SongDao songDao, SongGenreCrossDao songGenreCrossDao, ArrayList 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 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 cross; + + public InsertPerGenreThreadSafe(SongGenreCrossDao songGenreCrossDao, ArrayList 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 getRandomSample(int number) { + List 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 sample; + + public PickRandomThreadSafe(SongDao songDao, int number) { + this.songDao = songDao; + this.elementNumber = number; + } + + @Override + public void run() { + sample = songDao.random(elementNumber); + } + + public List getSample() { + return sample; + } + } } diff --git a/app/src/main/java/com/cappielloantonio/play/viewmodel/PlaylistPageViewModel.java b/app/src/main/java/com/cappielloantonio/play/viewmodel/PlaylistPageViewModel.java index 23c5116e..5d77a4a0 100644 --- a/app/src/main/java/com/cappielloantonio/play/viewmodel/PlaylistPageViewModel.java +++ b/app/src/main/java/com/cappielloantonio/play/viewmodel/PlaylistPageViewModel.java @@ -30,8 +30,6 @@ public class PlaylistPageViewModel extends AndroidViewModel { } public LiveData> getPlaylistSongList() { - // Prendere le canzoni di ciascuna playlist - Log.i(TAG, "getPlaylistSongList: " + playlist.getId()); songList = songRepository.getPlaylistLiveSong(playlist.getId()); return songList; }