diff --git a/app/src/main/java/com/cappielloantonio/play/database/AppDatabase.java b/app/src/main/java/com/cappielloantonio/play/database/AppDatabase.java index f4c04df4..a347d4bb 100644 --- a/app/src/main/java/com/cappielloantonio/play/database/AppDatabase.java +++ b/app/src/main/java/com/cappielloantonio/play/database/AppDatabase.java @@ -20,7 +20,7 @@ import com.cappielloantonio.play.model.Server; @SuppressLint("RestrictedApi") @Database( - version = 26, + version = 28, entities = {Queue.class, Server.class, RecentSearch.class, Download.class, Playlist.class} // autoMigrations = { @AutoMigration(from = 23, to = 24) } ) diff --git a/app/src/main/java/com/cappielloantonio/play/database/dao/QueueDao.java b/app/src/main/java/com/cappielloantonio/play/database/dao/QueueDao.java index 60080020..31970c8c 100644 --- a/app/src/main/java/com/cappielloantonio/play/database/dao/QueueDao.java +++ b/app/src/main/java/com/cappielloantonio/play/database/dao/QueueDao.java @@ -7,6 +7,7 @@ import androidx.room.OnConflictStrategy; import androidx.room.Query; import com.cappielloantonio.play.model.Queue; +import com.cappielloantonio.play.model.Server; import java.util.List; @@ -19,7 +20,10 @@ public interface QueueDao { List getAllSimple(); @Insert(onConflict = OnConflictStrategy.REPLACE) - void insertAll(List songQueueObject); + void insert(Queue songQueueObject); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insertAll(List songQueueObjects); @Query("DELETE FROM queue WHERE queue.track_order=:position") void deleteByPosition(int position); diff --git a/app/src/main/java/com/cappielloantonio/play/model/Queue.java b/app/src/main/java/com/cappielloantonio/play/model/Queue.java index 5245220a..b960658d 100644 --- a/app/src/main/java/com/cappielloantonio/play/model/Queue.java +++ b/app/src/main/java/com/cappielloantonio/play/model/Queue.java @@ -6,7 +6,7 @@ import androidx.room.PrimaryKey; @Entity(tableName = "queue") public class Queue { - @PrimaryKey + @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "track_order") private int trackOrder; @@ -37,8 +37,7 @@ public class Queue { @ColumnInfo(name = "last_play") 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) { - this.trackOrder = trackOrder; + public Queue(String songID, String title, String albumId, String albumName, String artistId, String artistName, String primary, long duration, long lastPlay) { this.songID = songID; this.title = title; this.albumId = albumId; diff --git a/app/src/main/java/com/cappielloantonio/play/repository/QueueRepository.java b/app/src/main/java/com/cappielloantonio/play/repository/QueueRepository.java index ac2a487a..2824d204 100644 --- a/app/src/main/java/com/cappielloantonio/play/repository/QueueRepository.java +++ b/app/src/main/java/com/cappielloantonio/play/repository/QueueRepository.java @@ -5,13 +5,10 @@ import android.app.Application; import androidx.lifecycle.LiveData; import com.cappielloantonio.play.database.AppDatabase; -import com.cappielloantonio.play.database.dao.DownloadDao; import com.cappielloantonio.play.database.dao.QueueDao; -import com.cappielloantonio.play.model.Download; import com.cappielloantonio.play.model.Queue; import com.cappielloantonio.play.model.Song; import com.cappielloantonio.play.util.MappingUtil; -import com.cappielloantonio.play.util.QueueUtil; import java.time.Instant; import java.util.ArrayList; @@ -48,19 +45,75 @@ public class QueueRepository { return songs; } - public void insertAll(List songs) { - InsertAllThreadSafe insertAll = new InsertAllThreadSafe(queueDao, songs); - Thread thread = new Thread(insertAll); - thread.start(); + public void insert(Song song, boolean reset) { + try { + if(reset) { + 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 songs) { + public void insertAll(List songs, boolean reset) { try { - final Thread delete = new Thread(new DeleteAllThreadSafe(queueDao)); - final Thread insertAll = new Thread(new InsertAllThreadSafe(queueDao, songs)); + if(reset) { + 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 songs = getSongsThreadSafe.getSongs(); + songs.add(afterIndex, song); + + 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 insertAllImmediatelyAfter(List toAdd, int afterIndex) { + try { + GetSongsThreadSafe getSongsThreadSafe = new GetSongsThreadSafe(queueDao); + Thread getSongsThread = new Thread(getSongsThreadSafe); + getSongsThread.start(); + getSongsThread.join(); + + List 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.join(); } 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 final QueueDao queueDao; private final List songs; @@ -132,7 +200,7 @@ public class QueueRepository { @Override public void run() { - queueDao.insertAll(QueueUtil.getQueueElementsFromSongs(songs)); + queueDao.insertAll(MappingUtil.mapSongsToQueue(songs)); } }