Simplify queries to move song in playlist
This commit is contained in:
parent
19b19fe958
commit
e13d083a8c
3 changed files with 14 additions and 41 deletions
|
@ -197,14 +197,16 @@ interface Database {
|
||||||
@RewriteQueriesToDropUnusedColumns
|
@RewriteQueriesToDropUnusedColumns
|
||||||
fun songsWithContentLength(): Flow<List<DetailedSongWithContentLength>>
|
fun songsWithContentLength(): Flow<List<DetailedSongWithContentLength>>
|
||||||
|
|
||||||
@Query("UPDATE SongPlaylistMap SET position = position - 1 WHERE playlistId = :playlistId AND position >= :fromPosition")
|
@Query("""
|
||||||
fun decrementSongPositions(playlistId: Long, fromPosition: Int)
|
UPDATE SongPlaylistMap SET position =
|
||||||
|
CASE
|
||||||
@Query("UPDATE SongPlaylistMap SET position = position - 1 WHERE playlistId = :playlistId AND position >= :fromPosition AND position <= :toPosition")
|
WHEN position < :fromPosition THEN position + 1
|
||||||
fun decrementSongPositions(playlistId: Long, fromPosition: Int, toPosition: Int)
|
WHEN position > :fromPosition THEN position - 1
|
||||||
|
ELSE :toPosition
|
||||||
@Query("UPDATE SongPlaylistMap SET position = position + 1 WHERE playlistId = :playlistId AND position >= :fromPosition AND position <= :toPosition")
|
END
|
||||||
fun incrementSongPositions(playlistId: Long, fromPosition: Int, toPosition: Int)
|
WHERE playlistId = :playlistId AND position BETWEEN MIN(:fromPosition,:toPosition) and MAX(:fromPosition,:toPosition)
|
||||||
|
""")
|
||||||
|
fun move(playlistId: Long, fromPosition: Int, toPosition: Int)
|
||||||
|
|
||||||
@Query("DELETE FROM SongPlaylistMap WHERE playlistId = :id")
|
@Query("DELETE FROM SongPlaylistMap WHERE playlistId = :id")
|
||||||
fun clearPlaylist(id: Long)
|
fun clearPlaylist(id: Long)
|
||||||
|
|
|
@ -132,17 +132,8 @@ fun InPlaylistMediaItemMenu(
|
||||||
onDismiss = onDismiss,
|
onDismiss = onDismiss,
|
||||||
onRemoveFromPlaylist = {
|
onRemoveFromPlaylist = {
|
||||||
transaction {
|
transaction {
|
||||||
Database.delete(
|
Database.move(playlistId, positionInPlaylist, Int.MAX_VALUE)
|
||||||
SongPlaylistMap(
|
Database.delete(SongPlaylistMap(song.id, playlistId, Int.MAX_VALUE))
|
||||||
songId = song.id,
|
|
||||||
playlistId = playlistId,
|
|
||||||
position = positionInPlaylist
|
|
||||||
)
|
|
||||||
)
|
|
||||||
Database.decrementSongPositions(
|
|
||||||
playlistId = playlistId,
|
|
||||||
fromPosition = positionInPlaylist + 1
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
modifier = modifier
|
modifier = modifier
|
||||||
|
|
|
@ -92,28 +92,8 @@ fun LocalPlaylistScreen(playlistId: Long) {
|
||||||
lazyListState = lazyListState,
|
lazyListState = lazyListState,
|
||||||
key = playlistWithSongs.songs,
|
key = playlistWithSongs.songs,
|
||||||
onDragEnd = { fromIndex, toIndex ->
|
onDragEnd = { fromIndex, toIndex ->
|
||||||
transaction {
|
query {
|
||||||
if (fromIndex > toIndex) {
|
Database.move(playlistWithSongs.playlist.id, fromIndex, toIndex)
|
||||||
Database.incrementSongPositions(
|
|
||||||
playlistId = playlistWithSongs.playlist.id,
|
|
||||||
fromPosition = toIndex,
|
|
||||||
toPosition = fromIndex - 1
|
|
||||||
)
|
|
||||||
} else if (fromIndex < toIndex) {
|
|
||||||
Database.decrementSongPositions(
|
|
||||||
playlistId = playlistWithSongs.playlist.id,
|
|
||||||
fromPosition = fromIndex + 1,
|
|
||||||
toPosition = toIndex
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
Database.update(
|
|
||||||
SongPlaylistMap(
|
|
||||||
songId = playlistWithSongs.songs[fromIndex].id,
|
|
||||||
playlistId = playlistWithSongs.playlist.id,
|
|
||||||
position = toIndex
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
extraItemCount = 1
|
extraItemCount = 1
|
||||||
|
|
Loading…
Reference in a new issue