Modified the queue update logic

This commit is contained in:
CappielloAntonio 2021-12-30 13:47:07 +01:00
parent 747431423e
commit 88d8a8c620
4 changed files with 87 additions and 16 deletions

View file

@ -20,7 +20,7 @@ import com.cappielloantonio.play.model.Server;
@SuppressLint("RestrictedApi") @SuppressLint("RestrictedApi")
@Database( @Database(
version = 26, version = 28,
entities = {Queue.class, Server.class, RecentSearch.class, Download.class, Playlist.class} entities = {Queue.class, Server.class, RecentSearch.class, Download.class, Playlist.class}
// autoMigrations = { @AutoMigration(from = 23, to = 24) } // autoMigrations = { @AutoMigration(from = 23, to = 24) }
) )

View file

@ -7,6 +7,7 @@ import androidx.room.OnConflictStrategy;
import androidx.room.Query; import androidx.room.Query;
import com.cappielloantonio.play.model.Queue; import com.cappielloantonio.play.model.Queue;
import com.cappielloantonio.play.model.Server;
import java.util.List; import java.util.List;
@ -19,7 +20,10 @@ public interface QueueDao {
List<Queue> getAllSimple(); List<Queue> getAllSimple();
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<Queue> songQueueObject); void insert(Queue songQueueObject);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<Queue> songQueueObjects);
@Query("DELETE FROM queue WHERE queue.track_order=:position") @Query("DELETE FROM queue WHERE queue.track_order=:position")
void deleteByPosition(int position); void deleteByPosition(int position);

View file

@ -6,7 +6,7 @@ import androidx.room.PrimaryKey;
@Entity(tableName = "queue") @Entity(tableName = "queue")
public class Queue { public class Queue {
@PrimaryKey @PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "track_order") @ColumnInfo(name = "track_order")
private int trackOrder; private int trackOrder;
@ -37,8 +37,7 @@ public class Queue {
@ColumnInfo(name = "last_play") @ColumnInfo(name = "last_play")
private long lastPlay; private long lastPlay;
public Queue(int trackOrder, String songID, String title, String albumId, String albumName, String artistId, String artistName, String primary, long duration, long lastPlay) { public Queue(String songID, String title, String albumId, String albumName, String artistId, String artistName, String primary, long duration, long lastPlay) {
this.trackOrder = trackOrder;
this.songID = songID; this.songID = songID;
this.title = title; this.title = title;
this.albumId = albumId; this.albumId = albumId;

View file

@ -5,13 +5,10 @@ import android.app.Application;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import com.cappielloantonio.play.database.AppDatabase; import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.DownloadDao;
import com.cappielloantonio.play.database.dao.QueueDao; import com.cappielloantonio.play.database.dao.QueueDao;
import com.cappielloantonio.play.model.Download;
import com.cappielloantonio.play.model.Queue; import com.cappielloantonio.play.model.Queue;
import com.cappielloantonio.play.model.Song; import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.util.MappingUtil; import com.cappielloantonio.play.util.MappingUtil;
import com.cappielloantonio.play.util.QueueUtil;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
@ -48,19 +45,75 @@ public class QueueRepository {
return songs; return songs;
} }
public void insertAll(List<Song> songs) { public void insert(Song song, boolean reset) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(queueDao, songs); try {
Thread thread = new Thread(insertAll); if(reset) {
thread.start(); Thread delete = new Thread(new DeleteAllThreadSafe(queueDao));
delete.start();
delete.join();
}
Thread insert = new Thread(new InsertThreadSafe(queueDao, song));
insert.start();
insert.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
public void insertAllAndStartNew(List<Song> songs) { public void insertAll(List<Song> songs, boolean reset) {
try { try {
final Thread delete = new Thread(new DeleteAllThreadSafe(queueDao)); if(reset) {
final Thread insertAll = new Thread(new InsertAllThreadSafe(queueDao, songs)); Thread delete = new Thread(new DeleteAllThreadSafe(queueDao));
delete.start();
delete.join();
}
Thread insertAll = new Thread(new InsertAllThreadSafe(queueDao, songs));
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void insertImmediatelyAfter(Song song, int afterIndex) {
try {
GetSongsThreadSafe getSongsThreadSafe = new GetSongsThreadSafe(queueDao);
Thread getSongsThread = new Thread(getSongsThreadSafe);
getSongsThread.start();
getSongsThread.join();
List<Song> songs = getSongsThreadSafe.getSongs();
songs.add(afterIndex, song);
Thread delete = new Thread(new DeleteAllThreadSafe(queueDao));
delete.start(); delete.start();
delete.join(); delete.join();
Thread insertAll = new Thread(new InsertAllThreadSafe(queueDao, songs));
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void insertAllImmediatelyAfter(List<Song> toAdd, int afterIndex) {
try {
GetSongsThreadSafe getSongsThreadSafe = new GetSongsThreadSafe(queueDao);
Thread getSongsThread = new Thread(getSongsThreadSafe);
getSongsThread.start();
getSongsThread.join();
List<Song> songs = getSongsThreadSafe.getSongs();
songs.addAll(afterIndex, toAdd);
Thread delete = new Thread(new DeleteAllThreadSafe(queueDao));
delete.start();
delete.join();
Thread insertAll = new Thread(new InsertAllThreadSafe(queueDao, songs));
insertAll.start(); insertAll.start();
insertAll.join(); insertAll.join();
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -121,6 +174,21 @@ public class QueueRepository {
} }
} }
private static class InsertThreadSafe implements Runnable {
private final QueueDao queueDao;
private final Song song;
public InsertThreadSafe(QueueDao queueDao, Song song) {
this.queueDao = queueDao;
this.song = song;
}
@Override
public void run() {
queueDao.insert(MappingUtil.mapSongToQueue(song));
}
}
private static class InsertAllThreadSafe implements Runnable { private static class InsertAllThreadSafe implements Runnable {
private final QueueDao queueDao; private final QueueDao queueDao;
private final List<Song> songs; private final List<Song> songs;
@ -132,7 +200,7 @@ public class QueueRepository {
@Override @Override
public void run() { public void run() {
queueDao.insertAll(QueueUtil.getQueueElementsFromSongs(songs)); queueDao.insertAll(MappingUtil.mapSongsToQueue(songs));
} }
} }