Add song/genre sync

This commit is contained in:
Antonio Cappiello 2020-11-24 10:52:00 +01:00
parent b2c269a051
commit 76037e487b
47 changed files with 703 additions and 89 deletions

View file

@ -6,20 +6,24 @@ import androidx.lifecycle.LiveData;
import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.GenreDao;
import com.cappielloantonio.play.database.dao.SongGenreCrossDao;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Genre;
import com.cappielloantonio.play.model.SongGenreCross;
import java.util.ArrayList;
import java.util.List;
public class GenreRepository {
private GenreDao genreDao;
private SongGenreCrossDao songGenreCrossDao;
private LiveData<List<Genre>> listLiveGenres;
private LiveData<List<Genre>> listLiveAlbumGenre;
public GenreRepository(Application application) {
AppDatabase database = AppDatabase.getInstance(application);
genreDao = database.genreDao();
songGenreCrossDao = database.songGenreCrossDao();
}
public LiveData<List<Genre>> getListLiveGenres() {
@ -32,6 +36,24 @@ public class GenreRepository {
return listLiveAlbumGenre;
}
public List<Genre> getListGenre() {
List<Genre> list = null;
GetGenreListThreadSafe getGenreListThread = new GetGenreListThreadSafe(genreDao);
Thread thread = new Thread(getGenreListThread);
thread.start();
try {
thread.join();
list = getGenreListThread.getList();
}
catch (InterruptedException e) {
e.printStackTrace();
}
return list;
}
public boolean exist(Genre genre) {
boolean exist = false;
@ -62,12 +84,36 @@ public class GenreRepository {
thread.start();
}
public void insertPerGenre(ArrayList<SongGenreCross> songGenreCrosses) {
InsertPerGenreThreadSafe insertPerGenre = new InsertPerGenreThreadSafe(songGenreCrossDao, songGenreCrosses);
Thread thread = new Thread(insertPerGenre);
thread.start();
}
public void delete(Genre genre) {
DeleteThreadSafe delete = new DeleteThreadSafe(genreDao, genre);
Thread thread = new Thread(delete);
thread.start();
}
private static class GetGenreListThreadSafe implements Runnable {
private GenreDao genreDao;
private List<Genre> list = null;
public GetGenreListThreadSafe(GenreDao genreDao) {
this.genreDao = genreDao;
}
@Override
public void run() {
list = genreDao.getGenreList();
}
public List<Genre> getList() {
return list;
}
}
private static class ExistThreadSafe implements Runnable {
private GenreDao genreDao;
private Genre genre;
@ -118,6 +164,21 @@ public class GenreRepository {
}
}
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);
}
}
private static class DeleteThreadSafe implements Runnable {
private GenreDao genreDao;
private Genre genre;

View file

@ -20,6 +20,7 @@ public class SongRepository {
private LiveData<List<Song>> listLiveSampleMostPlayedSongs;
private LiveData<List<Song>> listLiveSampleArtistTopSongs;
private LiveData<List<Song>> listLiveAlbumSongs;
private LiveData<List<Song>> listLiveSongByGenre;
public SongRepository(Application application) {
@ -32,26 +33,36 @@ public class SongRepository {
return searchListLiveSongs;
}
public LiveData<List<Song>> getListLiveRecentlyAddedSampleSong() {
listLiveSampleRecentlyAddedSongs = songDao.getRecentlyAddedSample(20);
public LiveData<List<Song>> getListLiveRecentlyAddedSampleSong(int number) {
listLiveSampleRecentlyAddedSongs = songDao.getRecentlyAddedSample(number);
return listLiveSampleRecentlyAddedSongs;
}
public LiveData<List<Song>> getListLiveRecentlyPlayedSampleSong() {
listLiveSampleRecentlyPlayedSongs = songDao.getRecentlyPlayedSample(20);
public LiveData<List<Song>> getListLiveRecentlyPlayedSampleSong(int number) {
listLiveSampleRecentlyPlayedSongs = songDao.getRecentlyPlayedSample(number);
return listLiveSampleRecentlyPlayedSongs;
}
public LiveData<List<Song>> getListLiveMostPlayedSampleSong() {
listLiveSampleMostPlayedSongs = songDao.getMostPlayedSample(20);
public LiveData<List<Song>> getListLiveMostPlayedSampleSong(int number) {
listLiveSampleMostPlayedSongs = songDao.getMostPlayedSample(number);
return listLiveSampleMostPlayedSongs;
}
public LiveData<List<Song>> getArtistListLiveTopSong(String artistID) {
public LiveData<List<Song>> getListLiveSongByGenre(String genreID) {
listLiveSongByGenre = songDao.getSongByGenre(genreID);
return listLiveSongByGenre;
}
public LiveData<List<Song>> getArtistListLiveTopSongSample(String artistID) {
listLiveSampleArtistTopSongs = songDao.getArtistTopSongsSample(artistID, 5);
return listLiveSampleArtistTopSongs;
}
public LiveData<List<Song>> getArtistListLiveTopSong(String artistID) {
listLiveSampleArtistTopSongs = songDao.getArtistTopSongs(artistID);
return listLiveSampleArtistTopSongs;
}
public LiveData<List<Song>> getAlbumListLiveSong(String albumID) {
listLiveAlbumSongs = songDao.getAlbumSong(albumID);
return listLiveAlbumSongs;