Add Room for playlist
Added DAO Queries and Able insert songs to playlist
This commit is contained in:
parent
6ace96708b
commit
b5e07a31d8
16 changed files with 140 additions and 66 deletions
|
@ -4,6 +4,10 @@ import androidx.room.*
|
|||
|
||||
@Dao
|
||||
interface PlaylistDao {
|
||||
|
||||
@Query("SELECT * FROM PlaylistEntity WHERE playlist_name = :name")
|
||||
suspend fun checkPlaylistExists(name: String): List<PlaylistEntity>
|
||||
|
||||
@Insert
|
||||
suspend fun createPlaylist(playlistEntity: PlaylistEntity)
|
||||
|
||||
|
@ -16,4 +20,7 @@ interface PlaylistDao {
|
|||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertSongs(songEntities: List<SongEntity>)
|
||||
|
||||
@Query("SELECT * FROM SongEntity WHERE playlist_creator_id = :playlistName AND song_id = :songId")
|
||||
suspend fun checkSongExistsWithPlaylistName(playlistName: String, songId: Int): List<SongEntity>
|
||||
}
|
|
@ -7,7 +7,7 @@ import androidx.room.RoomDatabase
|
|||
|
||||
@Database(
|
||||
entities = [PlaylistEntity::class, SongEntity::class],
|
||||
version = 4,
|
||||
version = 7,
|
||||
exportSchema = false
|
||||
)
|
||||
abstract class PlaylistDatabase : RoomDatabase() {
|
||||
|
|
|
@ -9,7 +9,10 @@ import kotlinx.android.parcel.Parcelize
|
|||
@Entity
|
||||
@Parcelize
|
||||
class PlaylistEntity(
|
||||
@PrimaryKey
|
||||
@ColumnInfo(name = "playlist_name")
|
||||
val playlistName: String
|
||||
) : Parcelable
|
||||
) : Parcelable {
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
@ColumnInfo(name = "playlist_id")
|
||||
var playListId: Int = 0
|
||||
}
|
|
@ -6,7 +6,7 @@ import androidx.room.Relation
|
|||
data class PlaylistWithSongs(
|
||||
@Embedded val playlistEntity: PlaylistEntity,
|
||||
@Relation(
|
||||
parentColumn = "playlist_name",
|
||||
entityColumn = "playlist_creator_name"
|
||||
parentColumn = "playlist_id",
|
||||
entityColumn = "playlist_creator_id"
|
||||
) val songs: List<SongEntity>
|
||||
)
|
|
@ -2,8 +2,39 @@ package code.name.monkey.retromusic.db
|
|||
|
||||
import androidx.annotation.WorkerThread
|
||||
|
||||
class RoomPlaylistRepository(private val playlistDao: PlaylistDao) {
|
||||
|
||||
interface RoomPlaylistRepository {
|
||||
suspend fun createPlaylist(playlistEntity: PlaylistEntity)
|
||||
suspend fun checkPlaylistExists(playlistName: String): List<PlaylistEntity>
|
||||
suspend fun playlists(): List<PlaylistEntity>
|
||||
suspend fun playlistWithSongs(): List<PlaylistWithSongs>
|
||||
suspend fun insertSongs(songs: List<SongEntity>)
|
||||
}
|
||||
|
||||
class RealRoomPlaylistRepository(private val playlistDao: PlaylistDao) : RoomPlaylistRepository {
|
||||
@WorkerThread
|
||||
override suspend fun createPlaylist(playlistEntity: PlaylistEntity) =
|
||||
playlistDao.createPlaylist(playlistEntity)
|
||||
|
||||
@WorkerThread
|
||||
suspend fun getPlaylistWithSongs(): List<PlaylistWithSongs> = playlistDao.playlistsWithSong()
|
||||
override suspend fun checkPlaylistExists(playlistName: String): List<PlaylistEntity> =
|
||||
playlistDao.checkPlaylistExists(playlistName)
|
||||
|
||||
@WorkerThread
|
||||
override suspend fun playlists(): List<PlaylistEntity> = playlistDao.playlists()
|
||||
|
||||
@WorkerThread
|
||||
override suspend fun playlistWithSongs(): List<PlaylistWithSongs> =
|
||||
playlistDao.playlistsWithSong()
|
||||
|
||||
@WorkerThread
|
||||
override suspend fun insertSongs(songs: List<SongEntity>) {
|
||||
/* val tempList = ArrayList<SongEntity>(songs)
|
||||
val existingSongs = songs.map {
|
||||
playlistDao.checkSongExistsWithPlaylistName(it.playlistCreatorName, it.songId)
|
||||
}.first()
|
||||
println("Existing ${existingSongs.size}")
|
||||
tempList.removeAll(existingSongs)*/
|
||||
playlistDao.insertSongs(songs)
|
||||
}
|
||||
}
|
|
@ -8,8 +8,8 @@ import androidx.room.PrimaryKey
|
|||
class SongEntity(
|
||||
@ColumnInfo(name = "song_id")
|
||||
val songId: Int,
|
||||
@ColumnInfo(name = "playlist_creator_name")
|
||||
val playlistCreatorName: String
|
||||
@ColumnInfo(name = "playlist_creator_id")
|
||||
val playlistCreatorId: Int
|
||||
) {
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
@ColumnInfo(name = "song_key")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue