Add Room database for custom playlist
Here we go again! Doing custom playlist db
This commit is contained in:
parent
662a5eea35
commit
6ace96708b
13 changed files with 250 additions and 7 deletions
|
@ -0,0 +1,19 @@
|
|||
package code.name.monkey.retromusic.db
|
||||
|
||||
import androidx.room.*
|
||||
|
||||
@Dao
|
||||
interface PlaylistDao {
|
||||
@Insert
|
||||
suspend fun createPlaylist(playlistEntity: PlaylistEntity)
|
||||
|
||||
@Query("SELECT * FROM PlaylistEntity")
|
||||
suspend fun playlists(): List<PlaylistEntity>
|
||||
|
||||
@Transaction
|
||||
@Query("SELECT * FROM PlaylistEntity")
|
||||
suspend fun playlistsWithSong(): List<PlaylistWithSongs>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertSongs(songEntities: List<SongEntity>)
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package code.name.monkey.retromusic.db
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
|
||||
@Database(
|
||||
entities = [PlaylistEntity::class, SongEntity::class],
|
||||
version = 4,
|
||||
exportSchema = false
|
||||
)
|
||||
abstract class PlaylistDatabase : RoomDatabase() {
|
||||
abstract fun playlistDao(): PlaylistDao
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var INSTANCE: PlaylistDatabase? = null
|
||||
|
||||
fun getDatabase(
|
||||
context: Context
|
||||
): PlaylistDatabase {
|
||||
return INSTANCE ?: synchronized(this) {
|
||||
val instance = Room.databaseBuilder(
|
||||
context.applicationContext,
|
||||
PlaylistDatabase::class.java,
|
||||
"playlists.db"
|
||||
).fallbackToDestructiveMigration().build()
|
||||
INSTANCE = instance
|
||||
instance
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package code.name.monkey.retromusic.db
|
||||
|
||||
import android.os.Parcelable
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import kotlinx.android.parcel.Parcelize
|
||||
|
||||
@Entity
|
||||
@Parcelize
|
||||
class PlaylistEntity(
|
||||
@PrimaryKey
|
||||
@ColumnInfo(name = "playlist_name")
|
||||
val playlistName: String
|
||||
) : Parcelable
|
|
@ -0,0 +1,12 @@
|
|||
package code.name.monkey.retromusic.db
|
||||
|
||||
import androidx.room.Embedded
|
||||
import androidx.room.Relation
|
||||
|
||||
data class PlaylistWithSongs(
|
||||
@Embedded val playlistEntity: PlaylistEntity,
|
||||
@Relation(
|
||||
parentColumn = "playlist_name",
|
||||
entityColumn = "playlist_creator_name"
|
||||
) val songs: List<SongEntity>
|
||||
)
|
|
@ -0,0 +1,9 @@
|
|||
package code.name.monkey.retromusic.db
|
||||
|
||||
import androidx.annotation.WorkerThread
|
||||
|
||||
class RoomPlaylistRepository(private val playlistDao: PlaylistDao) {
|
||||
|
||||
@WorkerThread
|
||||
suspend fun getPlaylistWithSongs(): List<PlaylistWithSongs> = playlistDao.playlistsWithSong()
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package code.name.monkey.retromusic.db
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity
|
||||
class SongEntity(
|
||||
@ColumnInfo(name = "song_id")
|
||||
val songId: Int,
|
||||
@ColumnInfo(name = "playlist_creator_name")
|
||||
val playlistCreatorName: String
|
||||
) {
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
@ColumnInfo(name = "song_key")
|
||||
var songPrimaryKey: Long = 0
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue