Imports optimization and entries rearranged

This commit is contained in:
CappielloAntonio 2021-04-27 11:01:02 +02:00
parent 3c2837e096
commit 65e47f61ef
63 changed files with 987 additions and 1087 deletions

View file

@ -2,19 +2,10 @@ package com.cappielloantonio.play.repository;
import android.app.Application;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.AlbumArtistCrossDao;
import com.cappielloantonio.play.database.dao.QueueDao;
import com.cappielloantonio.play.database.dao.SongArtistCrossDao;
import com.cappielloantonio.play.model.AlbumArtistCross;
import com.cappielloantonio.play.model.Queue;
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.model.SongArtistCross;
import com.cappielloantonio.play.util.QueueUtil;
import java.util.ArrayList;
import java.util.List;
public class AlbumArtistRepository {
@ -41,6 +32,12 @@ public class AlbumArtistRepository {
}
}
public void deleteAll() {
DeleteAllAlbumArtistCrossThreadSafe delete = new DeleteAllAlbumArtistCrossThreadSafe(albumArtistCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class InsertAllThreadSafe implements Runnable {
private AlbumArtistCrossDao albumArtistCrossDao;
private List<AlbumArtistCross> crosses;
@ -56,12 +53,6 @@ public class AlbumArtistRepository {
}
}
public void deleteAll() {
DeleteAllAlbumArtistCrossThreadSafe delete = new DeleteAllAlbumArtistCrossThreadSafe(albumArtistCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllAlbumArtistCrossThreadSafe implements Runnable {
private AlbumArtistCrossDao albumArtistCrossDao;

View file

@ -63,6 +63,35 @@ public class AlbumRepository {
return suggestions;
}
public void insertAll(ArrayList<Album> albums) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(albumDao, albums);
Thread thread = new Thread(insertAll);
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 SearchSuggestionsThreadSafe implements Runnable {
private AlbumDao albumDao;
private String query;
@ -85,18 +114,6 @@ public class AlbumRepository {
}
}
public void insertAll(ArrayList<Album> albums) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(albumDao, albums);
Thread thread = new Thread(insertAll);
thread.start();
}
public void deleteAll() {
DeleteAllThreadSafe delete = new DeleteAllThreadSafe(albumDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllThreadSafe implements Runnable {
private AlbumDao albumDao;
@ -110,23 +127,6 @@ public class AlbumRepository {
}
}
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 GetAlbumByIDThreadSafe implements Runnable {
private Album album;
private AlbumDao albumDao;

View file

@ -55,6 +55,35 @@ public class ArtistRepository {
return suggestions;
}
public void insertAll(ArrayList<Artist> artists) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(artistDao, artists);
Thread thread = new Thread(insertAll);
thread.start();
}
public void deleteAll() {
DeleteAllThreadSafe delete = new DeleteAllThreadSafe(artistDao);
Thread thread = new Thread(delete);
thread.start();
}
public Artist getArtistByID(String id) {
Artist artist = null;
GetArtistByIDThreadSafe getArtist = new GetArtistByIDThreadSafe(artistDao, id);
Thread thread = new Thread(getArtist);
thread.start();
try {
thread.join();
artist = getArtist.getArtist();
} catch (InterruptedException e) {
e.printStackTrace();
}
return artist;
}
private static class SearchSuggestionsThreadSafe implements Runnable {
private ArtistDao artistDao;
private String query;
@ -77,12 +106,6 @@ public class ArtistRepository {
}
}
public void insertAll(ArrayList<Artist> artists) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(artistDao, artists);
Thread thread = new Thread(insertAll);
thread.start();
}
private static class InsertAllThreadSafe implements Runnable {
private ArtistDao artistDao;
private ArrayList<Artist> artists;
@ -99,12 +122,6 @@ public class ArtistRepository {
}
}
public void deleteAll() {
DeleteAllThreadSafe delete = new DeleteAllThreadSafe(artistDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllThreadSafe implements Runnable {
private ArtistDao artistDao;
@ -118,23 +135,6 @@ public class ArtistRepository {
}
}
public Artist getArtistByID(String id) {
Artist artist = null;
GetArtistByIDThreadSafe getArtist = new GetArtistByIDThreadSafe(artistDao, id);
Thread thread = new Thread(getArtist);
thread.start();
try {
thread.join();
artist = getArtist.getArtist();
} catch (InterruptedException e) {
e.printStackTrace();
}
return artist;
}
private static class GetArtistByIDThreadSafe implements Runnable {
private Artist artist;
private ArtistDao artistDao;

View file

@ -5,10 +5,8 @@ import android.app.Application;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.ArtistDao;
import com.cappielloantonio.play.database.dao.GenreDao;
import com.cappielloantonio.play.database.dao.SongGenreCrossDao;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Genre;
import java.util.ArrayList;
@ -47,73 +45,25 @@ public class GenreRepository {
try {
thread.join();
list = getGenreListThread.getList();
}
catch (InterruptedException e) {
} catch (InterruptedException e) {
e.printStackTrace();
}
return list;
}
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;
}
}
public void insertAll(ArrayList<Genre> genres) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(genreDao, genres);
Thread thread = new Thread(insertAll);
thread.start();
}
private static class InsertAllThreadSafe implements Runnable {
private GenreDao genreDao;
private ArrayList<Genre> genres;
public InsertAllThreadSafe(GenreDao genreDao, ArrayList<Genre> genres) {
this.genreDao = genreDao;
this.genres = genres;
}
@Override
public void run() {
genreDao.deleteAll();
genreDao.insertAll(genres);
}
}
public void deleteAll() {
DeleteAllGenreThreadSafe delete = new DeleteAllGenreThreadSafe(genreDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllGenreThreadSafe implements Runnable {
private GenreDao genreDao;
public DeleteAllGenreThreadSafe(GenreDao genreDao) {
this.genreDao = genreDao;
}
@Override
public void run() {
genreDao.deleteAll();
}
}
public LiveData<List<Genre>> searchListLiveGenre(String name, int limit) {
searchListLiveGenre = genreDao.searchGenre(name, limit);
return searchListLiveGenre;
@ -136,6 +86,53 @@ public class GenreRepository {
return suggestions;
}
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 InsertAllThreadSafe implements Runnable {
private GenreDao genreDao;
private ArrayList<Genre> genres;
public InsertAllThreadSafe(GenreDao genreDao, ArrayList<Genre> genres) {
this.genreDao = genreDao;
this.genres = genres;
}
@Override
public void run() {
genreDao.deleteAll();
genreDao.insertAll(genres);
}
}
private static class DeleteAllGenreThreadSafe implements Runnable {
private GenreDao genreDao;
public DeleteAllGenreThreadSafe(GenreDao genreDao) {
this.genreDao = genreDao;
}
@Override
public void run() {
genreDao.deleteAll();
}
}
private static class SearchSuggestionsThreadSafe implements Runnable {
private GenreDao genreDao;
private String query;

View file

@ -6,9 +6,7 @@ import androidx.lifecycle.LiveData;
import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.PlaylistDao;
import com.cappielloantonio.play.database.dao.SongDao;
import com.cappielloantonio.play.model.Playlist;
import com.cappielloantonio.play.model.Song;
import java.util.ArrayList;
import java.util.List;
@ -33,41 +31,12 @@ public class PlaylistRepository {
thread.start();
}
private static class InsertAllThreadSafe implements Runnable {
private PlaylistDao playlistDao;
private ArrayList<Playlist> playlists;
public InsertAllThreadSafe(PlaylistDao playlistDao, ArrayList<Playlist> playlists) {
this.playlistDao = playlistDao;
this.playlists = playlists;
}
@Override
public void run() {
playlistDao.deleteAll();
playlistDao.insertAll(playlists);
}
}
public void deleteAll() {
DeleteAllThreadSafe delete = new DeleteAllThreadSafe(playlistDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllThreadSafe implements Runnable {
private PlaylistDao playlistDao;
public DeleteAllThreadSafe(PlaylistDao playlistDao) {
this.playlistDao = playlistDao;
}
@Override
public void run() {
playlistDao.deleteAll();
}
}
public List<Playlist> getRandomSample(int number) {
List<Playlist> sample = new ArrayList<>();
@ -85,6 +54,35 @@ public class PlaylistRepository {
return sample;
}
private static class InsertAllThreadSafe implements Runnable {
private PlaylistDao playlistDao;
private ArrayList<Playlist> playlists;
public InsertAllThreadSafe(PlaylistDao playlistDao, ArrayList<Playlist> playlists) {
this.playlistDao = playlistDao;
this.playlists = playlists;
}
@Override
public void run() {
playlistDao.deleteAll();
playlistDao.insertAll(playlists);
}
}
private static class DeleteAllThreadSafe implements Runnable {
private PlaylistDao playlistDao;
public DeleteAllThreadSafe(PlaylistDao playlistDao) {
this.playlistDao = playlistDao;
}
@Override
public void run() {
playlistDao.deleteAll();
}
}
private static class PickRandomThreadSafe implements Runnable {
private PlaylistDao playlistDao;
private int elementNumber;

View file

@ -4,9 +4,7 @@ import android.app.Application;
import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.PlaylistSongCrossDao;
import com.cappielloantonio.play.database.dao.SongArtistCrossDao;
import com.cappielloantonio.play.model.PlaylistSongCross;
import com.cappielloantonio.play.model.SongArtistCross;
import java.util.List;
@ -26,6 +24,12 @@ public class PlaylistSongRepository {
thread.start();
}
public void deleteAll() {
DeleteAllPlaylistSongCrossThreadSafe delete = new DeleteAllPlaylistSongCrossThreadSafe(playlistSongCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class InsertAllThreadSafe implements Runnable {
private PlaylistSongCrossDao playlistSongCrossDao;
private List<PlaylistSongCross> crosses;
@ -41,12 +45,6 @@ public class PlaylistSongRepository {
}
}
public void deleteAll() {
DeleteAllPlaylistSongCrossThreadSafe delete = new DeleteAllPlaylistSongCrossThreadSafe(playlistSongCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllPlaylistSongCrossThreadSafe implements Runnable {
private PlaylistSongCrossDao playlistSongCrossDao;

View file

@ -7,11 +7,9 @@ import androidx.lifecycle.LiveData;
import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.QueueDao;
import com.cappielloantonio.play.database.dao.SongDao;
import com.cappielloantonio.play.model.Queue;
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.util.QueueUtil;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
@ -50,24 +48,6 @@ public class QueueRepository {
return songs;
}
private static class GetSongsThreadSafe implements Runnable {
private QueueDao queueDao;
private List<Song> songs;
public GetSongsThreadSafe(QueueDao queueDao) {
this.queueDao = queueDao;
}
@Override
public void run() {
songs = queueDao.getAllSimple();
}
public List<Song> getSongs() {
return songs;
}
}
public void insertAll(List<Song> songs) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(queueDao, songs);
Thread thread = new Thread(insertAll);
@ -94,6 +74,67 @@ public class QueueRepository {
return mix;
}
public void insertAllAndStartNew(List<Song> songs) {
try {
final Thread delete = new Thread(new DeleteAllThreadSafe(queueDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(queueDao, songs));
delete.start();
delete.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void deleteByPosition(int position) {
DeleteByPositionThreadSafe delete = new DeleteByPositionThreadSafe(queueDao, position);
Thread thread = new Thread(delete);
thread.start();
}
public void deleteAll() {
DeleteAllThreadSafe delete = new DeleteAllThreadSafe(queueDao);
Thread thread = new Thread(delete);
thread.start();
}
public int count() {
int count = 0;
CountThreadSafe countThread = new CountThreadSafe(queueDao);
Thread thread = new Thread(countThread);
thread.start();
try {
thread.join();
count = countThread.getCount();
} catch (InterruptedException e) {
e.printStackTrace();
}
return count;
}
private static class GetSongsThreadSafe implements Runnable {
private QueueDao queueDao;
private List<Song> songs;
public GetSongsThreadSafe(QueueDao queueDao) {
this.queueDao = queueDao;
}
@Override
public void run() {
songs = queueDao.getAllSimple();
}
public List<Song> getSongs() {
return songs;
}
}
private static class GetSongsByIDThreadSafe implements Runnable {
private SongDao songDao;
private List<String> IDs;
@ -114,20 +155,6 @@ public class QueueRepository {
}
}
public void insertAllAndStartNew(List<Song> songs) {
try {
final Thread delete = new Thread(new DeleteAllThreadSafe(queueDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(queueDao, songs));
delete.start();
delete.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static class InsertAllThreadSafe implements Runnable {
private QueueDao queueDao;
private List<Song> songs;
@ -143,17 +170,11 @@ public class QueueRepository {
}
}
public void deleteByPosition(int position) {
DeleteByPositionThreadSafe delete = new DeleteByPositionThreadSafe(queueDao, position);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteByPositionThreadSafe implements Runnable {
private QueueDao queueDao;
private int position;
public DeleteByPositionThreadSafe(QueueDao queueDao,int position) {
public DeleteByPositionThreadSafe(QueueDao queueDao, int position) {
this.queueDao = queueDao;
this.position = position;
}
@ -164,12 +185,6 @@ public class QueueRepository {
}
}
public void deleteAll() {
DeleteAllThreadSafe delete = new DeleteAllThreadSafe(queueDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllThreadSafe implements Runnable {
private QueueDao queueDao;
@ -183,23 +198,6 @@ public class QueueRepository {
}
}
public int count() {
int count = 0;
CountThreadSafe countThread = new CountThreadSafe(queueDao);
Thread thread = new Thread(countThread);
thread.start();
try {
thread.join();
count = countThread.getCount();
} catch (InterruptedException e) {
e.printStackTrace();
}
return count;
}
private static class CountThreadSafe implements Runnable {
private QueueDao queueDao;
private int count = 0;

View file

@ -2,8 +2,6 @@ package com.cappielloantonio.play.repository;
import android.app.Application;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.RecentSearchDao;
import com.cappielloantonio.play.model.RecentSearch;
@ -31,6 +29,29 @@ public class RecentSearchRepository {
thread.start();
}
public void deleteAll() {
DeleteAllThreadSafe delete = new DeleteAllThreadSafe(recentSearchDao);
Thread thread = new Thread(delete);
thread.start();
}
public List<String> getRecentSearchSuggestion(int limit) {
List<String> recent = new ArrayList<>();
RecentThreadSafe suggestionsThread = new RecentThreadSafe(recentSearchDao, limit);
Thread thread = new Thread(suggestionsThread);
thread.start();
try {
thread.join();
recent = suggestionsThread.getRecent();
} catch (InterruptedException e) {
e.printStackTrace();
}
return recent;
}
private static class DeleteThreadSafe implements Runnable {
private RecentSearchDao recentSearchDao;
private RecentSearch recentSearch;
@ -61,12 +82,6 @@ public class RecentSearchRepository {
}
}
public void deleteAll() {
DeleteAllThreadSafe delete = new DeleteAllThreadSafe(recentSearchDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllThreadSafe implements Runnable {
private RecentSearchDao recentSearchDao;
@ -80,23 +95,6 @@ public class RecentSearchRepository {
}
}
public List<String> getRecentSearchSuggestion(int limit) {
List<String> recent = new ArrayList<>();
RecentThreadSafe suggestionsThread = new RecentThreadSafe(recentSearchDao,limit);
Thread thread = new Thread(suggestionsThread);
thread.start();
try {
thread.join();
recent = suggestionsThread.getRecent();
} catch (InterruptedException e) {
e.printStackTrace();
}
return recent;
}
private static class RecentThreadSafe implements Runnable {
private RecentSearchDao recentSearchDao;
private int limit;

View file

@ -3,11 +3,7 @@ package com.cappielloantonio.play.repository;
import android.app.Application;
import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.AlbumArtistCrossDao;
import com.cappielloantonio.play.database.dao.SongArtistCrossDao;
import com.cappielloantonio.play.database.dao.SongGenreCrossDao;
import com.cappielloantonio.play.model.AlbumArtistCross;
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.model.SongArtistCross;
import java.util.List;
@ -36,6 +32,12 @@ public class SongArtistRepository {
}
}
public void deleteAll() {
DeleteAllSongArtistCrossThreadSafe delete = new DeleteAllSongArtistCrossThreadSafe(songArtistCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class InsertAllThreadSafe implements Runnable {
private SongArtistCrossDao songArtistCrossDao;
private List<SongArtistCross> crosses;
@ -51,12 +53,6 @@ public class SongArtistRepository {
}
}
public void deleteAll() {
DeleteAllSongArtistCrossThreadSafe delete = new DeleteAllSongArtistCrossThreadSafe(songArtistCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllSongArtistCrossThreadSafe implements Runnable {
private SongArtistCrossDao songArtistCrossDao;

View file

@ -98,28 +98,6 @@ 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;
@ -144,33 +122,13 @@ public class SongRepository {
e.printStackTrace();
}
if(randomOrder) {
if (randomOrder) {
Collections.shuffle(songs);
}
return songs;
}
private static class GetSongsByAlbumIDThreadSafe implements Runnable {
private SongDao songDao;
private String albumID;
private List<Song> songs = new ArrayList<>();
public GetSongsByAlbumIDThreadSafe(SongDao songDao, String albumID) {
this.songDao = songDao;
this.albumID = albumID;
}
@Override
public void run() {
songs = songDao.getAlbumSong(albumID);
}
public List<Song> getSongs() {
return songs;
}
}
public LiveData<List<Song>> getFilteredListLiveSong(ArrayList<String> filters) {
listLiveFilteredSongs = songDao.getFilteredSong(filters);
return listLiveFilteredSongs;
@ -193,28 +151,6 @@ public class SongRepository {
return suggestions;
}
private static class SearchSuggestionsThreadSafe implements Runnable {
private SongDao songDao;
private String query;
private int number;
private List<String> suggestions = new ArrayList<>();
public SearchSuggestionsThreadSafe(SongDao songDao, String query, int number) {
this.songDao = songDao;
this.query = query;
this.number = number;
}
@Override
public void run() {
suggestions = songDao.searchSuggestions(query, number);
}
public List<String> getSuggestions() {
return suggestions;
}
}
/*
* Funzione che ritorna l'intero set di canzoni.
* Utilizzato per l'aggiornamento del catalogo.
@ -236,24 +172,6 @@ public class SongRepository {
return catalogue;
}
private static class GetCatalogueThreadSafe implements Runnable {
private SongDao songDao;
private List<Song> catalogue = new ArrayList<>();
public GetCatalogueThreadSafe(SongDao songDao) {
this.songDao = songDao;
}
@Override
public void run() {
catalogue = songDao.getAllList();
}
public List<Song> getCatalogue() {
return catalogue;
}
}
public List<Integer> getYearList() {
List<Integer> years = new ArrayList<>();
@ -271,35 +189,6 @@ public class SongRepository {
return years;
}
private static class GetYearListThreadSafe implements Runnable {
private SongDao songDao;
private List<Integer> years = new ArrayList<>();
private List<Integer> decades = new ArrayList<>();
public GetYearListThreadSafe(SongDao songDao) {
this.songDao = songDao;
}
@Override
public void run() {
years = songDao.getYearList();
for(int year : years) {
if(!decades.contains(year - year % 10)) {
decades.add(year);
}
}
}
public List<Integer> getYearList() {
return years;
}
public List<Integer> getDecadeList() {
return decades;
}
}
public LiveData<List<Song>> getSongByYearListLive(int year) {
listLiveSongByYear = songDao.getSongsByYear(year);
return listLiveSongByYear;
@ -331,6 +220,178 @@ public class SongRepository {
thread.start();
}
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();
}
public void setOfflineStatus(Song song) {
UpdateThreadSafe update = new UpdateThreadSafe(songDao, song);
Thread thread = new Thread(update);
thread.start();
}
public void setAllOffline() {
SetAllOfflineThreadSafe update = new SetAllOfflineThreadSafe(songDao);
Thread thread = new Thread(update);
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 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;
}
}
private static class GetSongsByAlbumIDThreadSafe implements Runnable {
private SongDao songDao;
private String albumID;
private List<Song> songs = new ArrayList<>();
public GetSongsByAlbumIDThreadSafe(SongDao songDao, String albumID) {
this.songDao = songDao;
this.albumID = albumID;
}
@Override
public void run() {
songs = songDao.getAlbumSong(albumID);
}
public List<Song> getSongs() {
return songs;
}
}
private static class SearchSuggestionsThreadSafe implements Runnable {
private SongDao songDao;
private String query;
private int number;
private List<String> suggestions = new ArrayList<>();
public SearchSuggestionsThreadSafe(SongDao songDao, String query, int number) {
this.songDao = songDao;
this.query = query;
this.number = number;
}
@Override
public void run() {
suggestions = songDao.searchSuggestions(query, number);
}
public List<String> getSuggestions() {
return suggestions;
}
}
private static class GetCatalogueThreadSafe implements Runnable {
private SongDao songDao;
private List<Song> catalogue = new ArrayList<>();
public GetCatalogueThreadSafe(SongDao songDao) {
this.songDao = songDao;
}
@Override
public void run() {
catalogue = songDao.getAllList();
}
public List<Song> getCatalogue() {
return catalogue;
}
}
private static class GetYearListThreadSafe implements Runnable {
private SongDao songDao;
private List<Integer> years = new ArrayList<>();
private List<Integer> decades = new ArrayList<>();
public GetYearListThreadSafe(SongDao songDao) {
this.songDao = songDao;
}
@Override
public void run() {
years = songDao.getYearList();
for (int year : years) {
if (!decades.contains(year - year % 10)) {
decades.add(year);
}
}
}
public List<Integer> getYearList() {
return years;
}
public List<Integer> getDecadeList() {
return decades;
}
}
private static class InsertAllThreadSafe implements Runnable {
private SongDao songDao;
private SongGenreCrossDao songGenreCrossDao;
@ -350,26 +411,6 @@ public class SongRepository {
}
}
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();
}
public void setOfflineStatus(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;
@ -385,12 +426,6 @@ public class SongRepository {
}
}
public void setAllOffline() {
SetAllOfflineThreadSafe update = new SetAllOfflineThreadSafe(songDao);
Thread thread = new Thread(update);
thread.start();
}
private static class SetAllOfflineThreadSafe implements Runnable {
private SongDao songDao;
@ -404,12 +439,6 @@ public class SongRepository {
}
}
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;
@ -425,12 +454,6 @@ public class SongRepository {
}
}
public void deleteAllSong() {
DeleteAllSongThreadSafe delete = new DeleteAllSongThreadSafe(songDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllSongThreadSafe implements Runnable {
private SongDao songDao;
@ -444,12 +467,6 @@ public class SongRepository {
}
}
public void deleteAllSongGenreCross() {
DeleteAllSongGenreCrossThreadSafe delete = new DeleteAllSongGenreCrossThreadSafe(songGenreCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllSongGenreCrossThreadSafe implements Runnable {
private SongGenreCrossDao songGenreCrossDao;
@ -463,23 +480,6 @@ public class SongRepository {
}
}
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;