adding an Artists database table for caching them

This commit is contained in:
tibbi 2021-05-10 21:21:23 +02:00
parent 1386bd68c1
commit 2c882dd66b
3 changed files with 48 additions and 8 deletions

View file

@ -11,16 +11,18 @@ import com.simplemobiletools.musicplayer.extensions.getAllInitialTracks
import com.simplemobiletools.musicplayer.extensions.playlistDAO
import com.simplemobiletools.musicplayer.helpers.ALL_TRACKS_PLAYLIST_ID
import com.simplemobiletools.musicplayer.helpers.RoomHelper
import com.simplemobiletools.musicplayer.interfaces.ArtistsDao
import com.simplemobiletools.musicplayer.interfaces.PlaylistsDao
import com.simplemobiletools.musicplayer.interfaces.QueueItemsDao
import com.simplemobiletools.musicplayer.interfaces.SongsDao
import com.simplemobiletools.musicplayer.models.Artist
import com.simplemobiletools.musicplayer.models.Playlist
import com.simplemobiletools.musicplayer.models.QueueItem
import com.simplemobiletools.musicplayer.models.Track
import com.simplemobiletools.musicplayer.objects.MyExecutor
import java.util.concurrent.Executors
@Database(entities = [(Track::class), (Playlist::class), QueueItem::class], version = 6)
@Database(entities = [Track::class, Playlist::class, QueueItem::class, Artist::class], version = 7)
abstract class SongsDatabase : RoomDatabase() {
abstract fun SongsDao(): SongsDao
@ -29,6 +31,8 @@ abstract class SongsDatabase : RoomDatabase() {
abstract fun QueueItemsDao(): QueueItemsDao
abstract fun ArtistsDao(): ArtistsDao
companion object {
private var db: SongsDatabase? = null
@ -51,6 +55,7 @@ abstract class SongsDatabase : RoomDatabase() {
.addMigrations(MIGRATION_3_4)
.addMigrations(MIGRATION_4_5)
.addMigrations(MIGRATION_5_6)
.addMigrations(MIGRATION_6_7)
.build()
}
}
@ -75,10 +80,10 @@ abstract class SongsDatabase : RoomDatabase() {
override fun migrate(database: SupportSQLiteDatabase) {
database.apply {
execSQL("CREATE TABLE songs_new (media_store_id INTEGER NOT NULL, title TEXT NOT NULL, artist TEXT NOT NULL, path TEXT NOT NULL, duration INTEGER NOT NULL, " +
"album TEXT NOT NULL, playlist_id INTEGER NOT NULL, PRIMARY KEY(path, playlist_id))")
"album TEXT NOT NULL, playlist_id INTEGER NOT NULL, PRIMARY KEY(path, playlist_id))")
execSQL("INSERT INTO songs_new (media_store_id, title, artist, path, duration, album, playlist_id) " +
"SELECT media_store_id, title, artist, path, duration, album, playlist_id FROM songs")
"SELECT media_store_id, title, artist, path, duration, album, playlist_id FROM songs")
execSQL("DROP TABLE songs")
execSQL("ALTER TABLE songs_new RENAME TO songs")
@ -106,10 +111,10 @@ abstract class SongsDatabase : RoomDatabase() {
override fun migrate(database: SupportSQLiteDatabase) {
database.apply {
execSQL("CREATE TABLE songs_new (media_store_id INTEGER NOT NULL, title TEXT NOT NULL, artist TEXT NOT NULL, path TEXT NOT NULL, duration INTEGER NOT NULL, " +
"album TEXT NOT NULL, cover_art TEXT default '' NOT NULL, playlist_id INTEGER NOT NULL, track_id INTEGER NOT NULL DEFAULT 0, PRIMARY KEY(media_store_id, playlist_id))")
"album TEXT NOT NULL, cover_art TEXT default '' NOT NULL, playlist_id INTEGER NOT NULL, track_id INTEGER NOT NULL DEFAULT 0, PRIMARY KEY(media_store_id, playlist_id))")
execSQL("INSERT OR IGNORE INTO songs_new (media_store_id, title, artist, path, duration, album, cover_art, playlist_id, track_id) " +
"SELECT media_store_id, title, artist, path, duration, album, cover_art, playlist_id, track_id FROM songs")
"SELECT media_store_id, title, artist, path, duration, album, cover_art, playlist_id, track_id FROM songs")
execSQL("DROP TABLE songs")
execSQL("ALTER TABLE songs_new RENAME TO tracks")
@ -122,10 +127,10 @@ abstract class SongsDatabase : RoomDatabase() {
override fun migrate(database: SupportSQLiteDatabase) {
database.apply {
execSQL("CREATE TABLE tracks_new (`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `media_store_id` INTEGER NOT NULL, `title` TEXT NOT NULL, `artist` TEXT NOT NULL, `path` TEXT NOT NULL, `duration` INTEGER NOT NULL, " +
"`album` TEXT NOT NULL, `cover_art` TEXT default '' NOT NULL, `playlist_id` INTEGER NOT NULL, `track_id` INTEGER NOT NULL DEFAULT 0)")
"`album` TEXT NOT NULL, `cover_art` TEXT default '' NOT NULL, `playlist_id` INTEGER NOT NULL, `track_id` INTEGER NOT NULL DEFAULT 0)")
execSQL("INSERT OR IGNORE INTO tracks_new (media_store_id, title, artist, path, duration, album, cover_art, playlist_id, track_id) " +
"SELECT media_store_id, title, artist, path, duration, album, cover_art, playlist_id, track_id FROM tracks")
"SELECT media_store_id, title, artist, path, duration, album, cover_art, playlist_id, track_id FROM tracks")
execSQL("DROP TABLE tracks")
execSQL("ALTER TABLE tracks_new RENAME TO tracks")
@ -134,5 +139,14 @@ abstract class SongsDatabase : RoomDatabase() {
}
}
}
private val MIGRATION_6_7 = object : Migration(6, 7) {
override fun migrate(database: SupportSQLiteDatabase) {
database.apply {
execSQL("CREATE TABLE `artists` (`id` INTEGER NOT NULL PRIMARY KEY, `title` TEXT NOT NULL, `album_cnt` INTEGER NOT NULL, `track_cnt` INTEGER NOT NULL, `album_art_id` INTEGER NOT NULL)")
execSQL("CREATE UNIQUE INDEX IF NOT EXISTS `index_artists_id` ON `artists` (`id`)")
}
}
}
}
}

View file

@ -0,0 +1,16 @@
package com.simplemobiletools.musicplayer.interfaces
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.simplemobiletools.musicplayer.models.Artist
@Dao
interface ArtistsDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(artist: Artist): Long
@Query("SELECT * FROM artists")
fun getAll(): List<Artist>
}

View file

@ -1,12 +1,22 @@
package com.simplemobiletools.musicplayer.models
import android.provider.MediaStore
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import com.simplemobiletools.commons.helpers.AlphanumericComparator
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.musicplayer.helpers.PLAYER_SORT_BY_ALBUM_COUNT
import com.simplemobiletools.musicplayer.helpers.PLAYER_SORT_BY_TITLE
data class Artist(val id: Long, val title: String, var albumCnt: Int, var trackCnt: Int, var albumArtId: Long) : Comparable<Artist> {
@Entity(tableName = "artists", indices = [(Index(value = ["id"], unique = true))])
data class Artist(
@PrimaryKey(autoGenerate = true) val id: Long,
@ColumnInfo(name = "title") val title: String,
@ColumnInfo(name = "album_cnt") var albumCnt: Int,
@ColumnInfo(name = "track_cnt") var trackCnt: Int,
@ColumnInfo(name = "album_art_id") var albumArtId: Long) : Comparable<Artist> {
companion object {
var sorting = 0
}