replace queue store with room database
This commit is contained in:
parent
bb2793dbea
commit
44c1bb63b6
10 changed files with 150 additions and 324 deletions
|
|
@ -0,0 +1,18 @@
|
|||
package com.dkanada.gramophone.database;
|
||||
|
||||
import androidx.room.RoomDatabase;
|
||||
|
||||
import com.dkanada.gramophone.model.Song;
|
||||
|
||||
@androidx.room.Database(
|
||||
entities = {
|
||||
Song.class,
|
||||
QueueSong.class
|
||||
},
|
||||
version = 1,
|
||||
exportSchema = false
|
||||
)
|
||||
public abstract class JellyDatabase extends RoomDatabase {
|
||||
public abstract QueueSongDao queueSongDao();
|
||||
public abstract SongDao songDao();
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package com.dkanada.gramophone.database;
|
||||
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.ForeignKey;
|
||||
|
||||
import com.dkanada.gramophone.model.Song;
|
||||
|
||||
@Entity(
|
||||
tableName = "queueSongs",
|
||||
primaryKeys = {
|
||||
"index",
|
||||
"queue"
|
||||
}
|
||||
)
|
||||
public class QueueSong {
|
||||
public int index;
|
||||
|
||||
public int queue;
|
||||
|
||||
@ForeignKey(
|
||||
entity = Song.class,
|
||||
parentColumns = {"id"},
|
||||
childColumns = {"songId"},
|
||||
onDelete = ForeignKey.CASCADE
|
||||
)
|
||||
public String songId;
|
||||
|
||||
public QueueSong(String songId, int index, int queue) {
|
||||
this.songId = songId;
|
||||
|
||||
this.index = index;
|
||||
this.queue = queue;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
package com.dkanada.gramophone.database;
|
||||
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.OnConflictStrategy;
|
||||
import androidx.room.Query;
|
||||
|
||||
import com.dkanada.gramophone.App;
|
||||
import com.dkanada.gramophone.model.Song;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Dao
|
||||
public abstract class QueueSongDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
public abstract void insertQueueSongs(List<QueueSong> queueSongs);
|
||||
|
||||
@Query("DELETE FROM queueSongs")
|
||||
public abstract void deleteQueueSongs();
|
||||
|
||||
@Query("SELECT * from queueSongs WHERE queue = :queue ORDER BY `index`")
|
||||
public abstract List<QueueSong> getQueueSongs(int queue);
|
||||
|
||||
public List<Song> getQueue(int queue) {
|
||||
List<QueueSong> queueSongs = getQueueSongs(queue);
|
||||
List<Song> songs = new ArrayList<>();
|
||||
|
||||
for (QueueSong queueSong : queueSongs) {
|
||||
Song song = App.getDatabase().songDao().getSong(queueSong.songId);
|
||||
if (song != null) songs.add(song);
|
||||
}
|
||||
|
||||
return songs;
|
||||
}
|
||||
|
||||
public void setQueue(List<Song> songs, int queue) {
|
||||
List<QueueSong> queueSongs = new ArrayList<>();
|
||||
for (int i = 0; i < songs.size(); i++) {
|
||||
queueSongs.add(new QueueSong(songs.get(i).id, i, queue));
|
||||
}
|
||||
|
||||
insertQueueSongs(queueSongs);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package com.dkanada.gramophone.database;
|
||||
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.OnConflictStrategy;
|
||||
import androidx.room.Query;
|
||||
|
||||
import com.dkanada.gramophone.model.Song;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Dao
|
||||
public interface SongDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
void insertSongs(List<Song> songs);
|
||||
|
||||
@Query("DELETE FROM songs")
|
||||
void deleteSongs();
|
||||
|
||||
@Query("SELECT * FROM songs WHERE id = :id")
|
||||
Song getSong(String id);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue