Tweak Database.kt code

This commit is contained in:
vfsfitvnm 2022-10-05 16:22:04 +02:00
parent ed7ee1b6c0
commit 98939b6ac1

View file

@ -313,21 +313,12 @@ interface Database {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(searchQuery: SearchQuery)
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(info: Artist): Long
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(info: Album): Long
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(playlist: Playlist): Long
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(songPlaylistMap: SongPlaylistMap): Long
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(songAlbumMap: SongAlbumMap): Long
@Insert(onConflict = OnConflictStrategy.ABORT)
fun insert(songArtistMap: SongArtistMap): Long
@ -340,6 +331,12 @@ interface Database {
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insertSongPlaylistMaps(songPlaylistMaps: List<SongPlaylistMap>)
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(album: Album, songAlbumMap: SongAlbumMap)
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(artists: List<Artist>, songArtistMaps: List<SongArtistMap>)
@Transaction
fun insert(mediaItem: MediaItem, block: (Song) -> Song = { it }) {
val song = Song(
@ -353,62 +350,34 @@ interface Database {
}
mediaItem.mediaMetadata.extras?.getString("albumId")?.let { albumId ->
Album(
id = albumId,
title = mediaItem.mediaMetadata.albumTitle?.toString(),
year = null,
authorsText = null,
thumbnailUrl = null,
shareUrl = null,
timestamp = null,
).also(::insert)
upsert(
SongAlbumMap(
songId = song.id,
albumId = albumId,
position = null
)
insert(
Album(id = albumId, title = mediaItem.mediaMetadata.albumTitle?.toString()),
SongAlbumMap(songId = song.id, albumId = albumId, position = null)
)
}
mediaItem.mediaMetadata.extras?.getStringArrayList("artistNames")?.let { artistNames ->
mediaItem.mediaMetadata.extras?.getStringArrayList("artistIds")?.let { artistIds ->
artistNames.mapIndexed { index, artistName ->
Artist(
id = artistIds[index],
name = artistName,
thumbnailUrl = null,
info = null,
timestamp = null
).also(::insert)
if (artistNames.size == artistIds.size) {
insert(
artistNames.mapIndexed { index, artistName ->
Artist(id = artistIds[index], name = artistName)
},
artistIds.map { artistId ->
SongArtistMap(songId = song.id, artistId = artistId)
}
)
}
}
}?.forEach { artist ->
insert(
SongArtistMap(
songId = song.id,
artistId = artist.id
)
)
}
}
@Update
fun update(song: Song)
@Update
fun update(artist: Artist)
@Update
fun update(album: Album)
@Update
fun update(songAlbumMap: SongAlbumMap)
@Update
fun update(songPlaylistMap: SongPlaylistMap)
@Update
fun update(playlist: Playlist)
@ -427,9 +396,6 @@ interface Database {
@Delete
fun delete(playlist: Playlist)
@Delete
fun delete(playlist: Album)
@Delete
fun delete(songPlaylistMap: SongPlaylistMap)
}