Add player state persistence concept

This commit is contained in:
vfsfitvnm
2022-06-12 12:19:23 +02:00
parent a0e42473e6
commit e8e69549c6
9 changed files with 564 additions and 10 deletions

View File

@@ -2,10 +2,12 @@ package it.vfsfitvnm.vimusic
import android.content.Context
import android.database.Cursor
import android.os.Parcel
import androidx.media3.common.MediaItem
import androidx.room.*
import androidx.sqlite.db.SupportSQLiteQuery
import it.vfsfitvnm.vimusic.models.*
import kotlinx.coroutines.flow.Flow
import java.io.ByteArrayOutputStream
@Dao
@@ -83,6 +85,15 @@ interface Database {
@Insert(onConflict = OnConflictStrategy.ABORT)
fun insert(song: Song): Long
@Insert(onConflict = OnConflictStrategy.ABORT)
fun insertQueuedMediaItems(queuedMediaItems: List<QueuedMediaItem>)
@Query("SELECT * FROM QueuedMediaItem")
fun queuedMediaItems(): List<QueuedMediaItem>
@Query("DELETE FROM QueuedMediaItem")
fun clearQueuedMediaItems()
@Update
fun update(song: Song)
@@ -114,14 +125,18 @@ interface Database {
@androidx.room.Database(
entities = [
Song::class, SongInPlaylist::class, Playlist::class, Info::class, SongWithAuthors::class, SearchQuery::class
Song::class, SongInPlaylist::class, Playlist::class, Info::class, SongWithAuthors::class, SearchQuery::class, QueuedMediaItem::class
],
views = [
SortedSongInPlaylist::class
],
version = 1,
exportSchema = true
version = 2,
exportSchema = true,
autoMigrations = [
AutoMigration(from = 1, to = 2)
]
)
@TypeConverters(Converters::class)
abstract class DatabaseInitializer protected constructor() : RoomDatabase() {
abstract val database: Database
@@ -139,6 +154,37 @@ abstract class DatabaseInitializer protected constructor() : RoomDatabase() {
}
}
@TypeConverters
object Converters {
// TODO: temporary
@TypeConverter
fun mediaItemFromByteArray(value: ByteArray?): MediaItem? {
return value?.let { byteArray ->
val parcel = Parcel.obtain()
parcel.unmarshall(byteArray, 0, byteArray.size)
parcel.setDataPosition(0);
val pb = parcel.readBundle(MediaItem::class.java.classLoader)
parcel.recycle()
pb?.let {
MediaItem.CREATOR.fromBundle(pb)
}
}
}
// TODO: temporary
@TypeConverter
fun mediaItemToByteArray(mediaItem: MediaItem?): ByteArray? {
return mediaItem?.toBundle()?.let { persistableBundle ->
val parcel = Parcel.obtain()
parcel.writeBundle(persistableBundle)
parcel.marshall().also {
parcel.recycle()
}
}
}
}
val Database.internal: RoomDatabase
get() = DatabaseInitializer.Instance