Fix instant mix overriding added value in songs

This commit is contained in:
CappielloAntonio 2021-04-19 11:20:01 +02:00
parent 7576fbb75b
commit 308ddf6c7c
9 changed files with 124 additions and 75 deletions

View file

@ -6,6 +6,7 @@ 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;
@ -17,11 +18,13 @@ import java.util.List;
public class QueueRepository {
private static final String TAG = "QueueRepository";
private SongDao songDao;
private QueueDao queueDao;
private LiveData<List<Song>> listLiveQueue;
public QueueRepository(Application application) {
AppDatabase database = AppDatabase.getInstance(application);
songDao = database.songDao();
queueDao = database.queueDao();
}
@ -59,6 +62,26 @@ public class QueueRepository {
thread.start();
}
public List<Song> insertMix(ArrayList<Song> media) {
List<String> IDs = QueueUtil.getIDsFromSongs(media);
List<Song> mix = new ArrayList<>();
GetSongsByIDThreadSafe getSongsByIDThreadSafe = new GetSongsByIDThreadSafe(songDao, IDs);
Thread thread = new Thread(getSongsByIDThreadSafe);
thread.start();
try {
thread.join();
mix = QueueUtil.orderSongByIdList(IDs, getSongsByIDThreadSafe.getSongs());
insertAllAndStartNew(mix);
} catch (InterruptedException e) {
e.printStackTrace();
}
return mix;
}
public void insertAllAndStartNew(List<Song> songs) {
try {
final Thread delete = new Thread(new DeleteAllThreadSafe(queueDao));
@ -218,4 +241,24 @@ public class QueueRepository {
return songs;
}
}
private static class GetSongsByIDThreadSafe implements Runnable {
private SongDao songDao;
private List<String> IDs;
private List<Song> songs;
public GetSongsByIDThreadSafe(SongDao songDao, List<String> IDs) {
this.songDao = songDao;
this.IDs = IDs;
}
@Override
public void run() {
songs = songDao.getSongsByID(IDs);
}
public List<Song> getSongs() {
return songs;
}
}
}

View file

@ -46,11 +46,6 @@ public class SongRepository {
return searchListLiveSongs;
}
public LiveData<Song> getLiveDataSong(String id) {
liveDataSong = songDao.getOne(id);
return liveDataSong;
}
public LiveData<List<Song>> getListLiveRecentlyAddedSampleSong(int number) {
listLiveSampleRecentlyAddedSongs = songDao.getRecentlyAddedSample(number);
return listLiveSampleRecentlyAddedSongs;
@ -240,9 +235,11 @@ public class SongRepository {
}
public void increasePlayCount(Song song) {
Log.i(TAG, "increasePlayCount: " + song.getId());
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();
@ -250,6 +247,7 @@ public class SongRepository {
}
public void setFavoriteStatus(Song song) {
// UpdateFavoriteThreadSafe update = new UpdateFavoriteThreadSafe(songDao, song);
UpdateThreadSafe update = new UpdateThreadSafe(songDao, song);
Thread thread = new Thread(update);
thread.start();
@ -296,23 +294,6 @@ public class SongRepository {
return sample;
}
public Song getSongByID(String id) {
Song song = null;
GetSongByIDThreadSafe songByID = new GetSongByIDThreadSafe(songDao, id);
Thread thread = new Thread(songByID);
thread.start();
try {
thread.join();
song = songByID.getSong();
} catch (InterruptedException e) {
e.printStackTrace();
}
return song;
}
private static class GetSongsByAlbumIDThreadSafe implements Runnable {
private SongDao songDao;
private String albumID;
@ -439,6 +420,36 @@ public class SongRepository {
}
}
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;
@ -568,24 +579,4 @@ public class SongRepository {
return decades;
}
}
private static class GetSongByIDThreadSafe implements Runnable {
private SongDao songDao;
private String id;
private Song song;
public GetSongByIDThreadSafe(SongDao songDao, String id) {
this.songDao = songDao;
this.id = id;
}
@Override
public void run() {
song = songDao.getSongByID(id);
}
public Song getSong() {
return song;
}
}
}