diff --git a/app/src/main/java/com/cappielloantonio/play/database/dao/AlbumDao.java b/app/src/main/java/com/cappielloantonio/play/database/dao/AlbumDao.java index 763e9864..06656937 100644 --- a/app/src/main/java/com/cappielloantonio/play/database/dao/AlbumDao.java +++ b/app/src/main/java/com/cappielloantonio/play/database/dao/AlbumDao.java @@ -16,7 +16,6 @@ public interface AlbumDao { @Query("SELECT * FROM album") LiveData> getAll(); - // @Query("SELECT * FROM album WHERE artistId = :artistId;") @Query("SELECT album.* FROM album INNER JOIN album_artist_cross ON album.id = album_artist_cross.album_id AND album_artist_cross.artist_id = :artistId") LiveData> getArtistAlbums(String artistId); @@ -26,18 +25,12 @@ public interface AlbumDao { @Query("SELECT * FROM album WHERE title LIKE '%' || :name || '%'") LiveData> searchAlbum(String name); - @Query("SELECT EXISTS(SELECT * FROM album WHERE id = :id)") - boolean exist(String id); - @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(Album album); @Insert(onConflict = OnConflictStrategy.REPLACE) void insertAll(List albums); - @Delete - void delete(Album album); - @Query("SELECT title FROM album WHERE title LIKE :query || '%' OR title like '% ' || :query || '%' GROUP BY title LIMIT :number") List searchSuggestions(String query, int number); diff --git a/app/src/main/java/com/cappielloantonio/play/repository/AlbumRepository.java b/app/src/main/java/com/cappielloantonio/play/repository/AlbumRepository.java index 4168614a..8f558056 100644 --- a/app/src/main/java/com/cappielloantonio/play/repository/AlbumRepository.java +++ b/app/src/main/java/com/cappielloantonio/play/repository/AlbumRepository.java @@ -63,130 +63,6 @@ public class AlbumRepository { return suggestions; } - public boolean exist(Album album) { - boolean exist = false; - - ExistThreadSafe existThread = new ExistThreadSafe(albumDao, album); - Thread thread = new Thread(existThread); - thread.start(); - - try { - thread.join(); - exist = existThread.exist(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - return exist; - } - - public void insert(Album album) { - InsertThreadSafe insert = new InsertThreadSafe(albumDao, album); - Thread thread = new Thread(insert); - thread.start(); - } - - public void insertAll(ArrayList albums) { - InsertAllThreadSafe insertAll = new InsertAllThreadSafe(albumDao, albums); - Thread thread = new Thread(insertAll); - thread.start(); - } - - public void delete(Album album) { - DeleteThreadSafe delete = new DeleteThreadSafe(albumDao, album); - Thread thread = new Thread(delete); - thread.start(); - } - - public void deleteAll() { - DeleteAllThreadSafe delete = new DeleteAllThreadSafe(albumDao); - Thread thread = new Thread(delete); - thread.start(); - } - - public Album getAlbumByID(String id) { - Album album = null; - - GetAlbumByIDThreadSafe getAlbum = new GetAlbumByIDThreadSafe(albumDao, id); - Thread thread = new Thread(getAlbum); - thread.start(); - - try { - thread.join(); - album = getAlbum.getAlbum(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - return album; - } - - private static class ExistThreadSafe implements Runnable { - private AlbumDao albumDao; - private Album album; - private boolean exist = false; - - public ExistThreadSafe(AlbumDao albumDao, Album album) { - this.albumDao = albumDao; - this.album = album; - } - - @Override - public void run() { - exist = albumDao.exist(album.getId()); - } - - public boolean exist() { - return exist; - } - } - - private static class InsertThreadSafe implements Runnable { - private AlbumDao albumDao; - private Album album; - - public InsertThreadSafe(AlbumDao albumDao, Album album) { - this.albumDao = albumDao; - this.album = album; - } - - @Override - public void run() { - albumDao.insert(album); - } - } - - private static class InsertAllThreadSafe implements Runnable { - private AlbumDao albumDao; - private ArrayList albums; - - public InsertAllThreadSafe(AlbumDao albumDao, ArrayList albums) { - this.albumDao = albumDao; - this.albums = albums; - } - - @Override - public void run() { - albumDao.deleteAll(); - albumDao.insertAll(albums); - } - } - - private static class DeleteThreadSafe implements Runnable { - private AlbumDao albumDao; - private Album album; - - public DeleteThreadSafe(AlbumDao albumDao, Album album) { - this.albumDao = albumDao; - this.album = album; - } - - @Override - public void run() { - albumDao.delete(album); - } - } - private static class SearchSuggestionsThreadSafe implements Runnable { private AlbumDao albumDao; private String query; @@ -209,17 +85,27 @@ public class AlbumRepository { } } - private static class DeleteAllThreadSafe implements Runnable { - private AlbumDao albumDao; + public void insertAll(ArrayList albums) { + InsertAllThreadSafe insertAll = new InsertAllThreadSafe(albumDao, albums); + Thread thread = new Thread(insertAll); + thread.start(); + } - public DeleteAllThreadSafe(AlbumDao albumDao) { - this.albumDao = albumDao; + public Album getAlbumByID(String id) { + Album album = null; + + GetAlbumByIDThreadSafe getAlbum = new GetAlbumByIDThreadSafe(albumDao, id); + Thread thread = new Thread(getAlbum); + thread.start(); + + try { + thread.join(); + album = getAlbum.getAlbum(); + } catch (InterruptedException e) { + e.printStackTrace(); } - @Override - public void run() { - albumDao.deleteAll(); - } + return album; } private static class GetAlbumByIDThreadSafe implements Runnable { @@ -241,4 +127,20 @@ public class AlbumRepository { return album; } } + + private static class InsertAllThreadSafe implements Runnable { + private AlbumDao albumDao; + private ArrayList albums; + + public InsertAllThreadSafe(AlbumDao albumDao, ArrayList albums) { + this.albumDao = albumDao; + this.albums = albums; + } + + @Override + public void run() { + albumDao.deleteAll(); + albumDao.insertAll(albums); + } + } }