mirror of
https://github.com/CappielloAntonio/tempo.git
synced 2026-02-01 23:13:36 +00:00
Add playlist catalogue page and item
This commit is contained in:
@@ -6,7 +6,9 @@ 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;
|
||||
@@ -65,4 +67,41 @@ public class PlaylistRepository {
|
||||
playlistDao.deleteAll();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Playlist> getRandomSample(int number) {
|
||||
List<Playlist> sample = new ArrayList<>();
|
||||
|
||||
PickRandomThreadSafe randomThread = new PickRandomThreadSafe(playlistDao, 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 PlaylistDao playlistDao;
|
||||
private int elementNumber;
|
||||
private List<Playlist> sample;
|
||||
|
||||
public PickRandomThreadSafe(PlaylistDao playlistDao, int number) {
|
||||
this.playlistDao = playlistDao;
|
||||
this.elementNumber = number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
sample = playlistDao.random(elementNumber);
|
||||
}
|
||||
|
||||
public List<Playlist> getSample() {
|
||||
return sample;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user