Preparation to music streaming

This commit is contained in:
Antonio Cappiello 2020-12-08 11:12:44 +01:00
parent f837bb14e2
commit a28ad27288
23 changed files with 615 additions and 279 deletions

View file

@ -10,6 +10,7 @@ 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;
@ -18,6 +19,7 @@ public class QueueRepository {
private QueueDao queueDao;
private LiveData<List<Song>> listLiveQueue;
private LiveData<Song> liveLastPlayedSong;
public QueueRepository(Application application) {
AppDatabase database = AppDatabase.getInstance(application);
@ -29,6 +31,34 @@ public class QueueRepository {
return listLiveQueue;
}
public Song getSongByPosition(int position) {
Song song = null;
GetSongByPositionThreadSafe getSong = new GetSongByPositionThreadSafe(queueDao, position);
Thread thread = new Thread(getSong);
thread.start();
try {
thread.join();
song = getSong.getSong();
} catch (InterruptedException e) {
e.printStackTrace();
}
return song;
}
public LiveData<Song> getLiveLastPlayedSong() {
liveLastPlayedSong = queueDao.getLastPlayedSong();
return liveLastPlayedSong;
}
public void setLiveLastPlayedSong(Song song, int position) {
SetLastPlayedSongThreadSafe update = new SetLastPlayedSongThreadSafe(queueDao, song, position);
Thread thread = new Thread(update);
thread.start();
}
public List<Song> getSongs() {
List<Song> songs = new ArrayList<>();
@ -194,4 +224,44 @@ public class QueueRepository {
return songs;
}
}
private static class SetLastPlayedSongThreadSafe implements Runnable {
private QueueDao queueDao;
private Song song;
private int position;
public SetLastPlayedSongThreadSafe(QueueDao queueDao, Song song, int position) {
this.queueDao = queueDao;
this.song = song;
this.position = position;
}
@Override
public void run() {
if(song != null)
queueDao.setLastPlayedSong(song.getId(), Instant.now().toEpochMilli());
else
queueDao.setLastPlayedSong(position, Instant.now().toEpochMilli());
}
}
private static class GetSongByPositionThreadSafe implements Runnable {
private QueueDao queueDao;
private int position;
private Song song;
public GetSongByPositionThreadSafe(QueueDao queueDao, int position) {
this.queueDao = queueDao;
this.position = position;
}
@Override
public void run() {
song = queueDao.getSongByIndex(position);
}
public Song getSong() {
return song;
}
}
}

View file

@ -246,6 +246,23 @@ 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 ExistThreadSafe implements Runnable {
private SongDao songDao;
private Song song;
@ -459,4 +476,24 @@ 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;
}
}
}