feat: add pinned column to Playlist with migration

Made-with: Cursor
This commit is contained in:
cottongin
2026-03-11 16:25:56 -04:00
parent cf57d22dc5
commit eb111fce35
5 changed files with 23 additions and 6 deletions

View File

@@ -15,7 +15,7 @@ class RadioApplication : Application() {
val database: RadioDatabase by lazy {
Room.databaseBuilder(this, RadioDatabase::class.java, "radio_database")
.addCallback(SomaFmSeedData)
.addMigrations(RadioDatabase.MIGRATION_1_2, RadioDatabase.MIGRATION_2_3, RadioDatabase.MIGRATION_3_4, RadioDatabase.MIGRATION_4_5)
.addMigrations(RadioDatabase.MIGRATION_1_2, RadioDatabase.MIGRATION_2_3, RadioDatabase.MIGRATION_3_4, RadioDatabase.MIGRATION_4_5, RadioDatabase.MIGRATION_5_6)
.build()
}

View File

@@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.Flow
@Dao
interface PlaylistDao {
@Query("SELECT * FROM playlists ORDER BY starred DESC, sortOrder ASC")
@Query("SELECT * FROM playlists ORDER BY pinned DESC, sortOrder ASC")
fun getAllPlaylists(): Flow<List<Playlist>>
@Query("SELECT * FROM playlists WHERE id = :id")
@@ -30,4 +30,10 @@ interface PlaylistDao {
@Query("UPDATE playlists SET starred = :starred WHERE id = :id")
suspend fun toggleStarred(id: Long, starred: Boolean)
@Query("UPDATE playlists SET pinned = :pinned WHERE id = :id")
suspend fun updatePinned(id: Long, pinned: Boolean)
@Query("UPDATE playlists SET name = :name WHERE id = :id")
suspend fun rename(id: Long, name: String)
}

View File

@@ -22,7 +22,7 @@ import xyz.cottongin.radio247.data.model.StationStream
StationStream::class,
StationPreference::class
],
version = 5,
version = 6,
exportSchema = true
)
abstract class RadioDatabase : RoomDatabase() {
@@ -80,5 +80,12 @@ abstract class RadioDatabase : RoomDatabase() {
db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS index_station_preferences_stationId ON station_preferences(stationId)")
}
}
val MIGRATION_5_6 = object : Migration(5, 6) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE playlists ADD COLUMN pinned INTEGER NOT NULL DEFAULT 0")
db.execSQL("UPDATE playlists SET pinned = 1 WHERE isBuiltIn = 1")
}
}
}
}

View File

@@ -76,14 +76,17 @@ object SomaFmSeedData : RoomDatabase.Callback() {
* Inserts the SomaFM playlist and all stations.
* @param includeIsHidden true for v3+ schema, false for MIGRATION_1_2.
* @param includeStreams true for v4+ schema (fresh install), false otherwise.
* @param includePinned true for v6+ schema (fresh install), false for migrations before MIGRATION_5_6.
*/
fun seedStations(
db: SupportSQLiteDatabase,
includeIsHidden: Boolean = true,
includeStreams: Boolean = true
includeStreams: Boolean = true,
includePinned: Boolean = false
) {
val (pinnedCol, pinnedVal) = if (includePinned) ", pinned" to ", 1" else "" to ""
db.execSQL(
"INSERT INTO playlists (name, sortOrder, starred, isBuiltIn) VALUES ('SomaFM', 0, 0, 1)"
"INSERT INTO playlists (name, sortOrder, starred, isBuiltIn$pinnedCol) VALUES ('SomaFM', 0, 0, 1$pinnedVal)"
)
val cursor = db.query("SELECT last_insert_rowid()")
cursor.moveToFirst()

View File

@@ -9,5 +9,6 @@ data class Playlist(
val name: String,
val sortOrder: Int = 0,
val starred: Boolean = false,
val isBuiltIn: Boolean = false
val isBuiltIn: Boolean = false,
val pinned: Boolean = false
)