play an offline context by reference and filter its window

This commit is contained in:
austinried
2026-10-04 00:30:04 +00:00
parent 3e042cb380
commit 554b7ef25d
11 changed files with 110 additions and 116 deletions
@@ -483,29 +483,6 @@ interface LibraryDao {
albumId: String,
): Flow<List<Song>>
@Query(
"SELECT songs.id FROM songs " +
"JOIN song_downloads sd ON sd.sourceId = songs.sourceId AND sd.songId = songs.id AND sd.status = 'Completed' " +
"WHERE songs.sourceId = :sourceId AND songs.albumId = :albumId " +
"ORDER BY songs.disc, songs.track, songs.id",
)
suspend fun downloadedAlbumSongIds(
sourceId: Long,
albumId: String,
): List<String>
@Query(
"SELECT playlist_songs.songId FROM playlist_songs " +
"JOIN song_downloads sd ON sd.sourceId = playlist_songs.sourceId " +
"AND sd.songId = playlist_songs.songId AND sd.status = 'Completed' " +
"WHERE playlist_songs.sourceId = :sourceId AND playlist_songs.playlistId = :playlistId " +
"ORDER BY playlist_songs.position",
)
suspend fun downloadedPlaylistSongIds(
sourceId: Long,
playlistId: String,
): List<String>
@Query("$PLAYLIST_SONGS_SELECT AND $DOWNLOADED_SONG$PLAYLIST_SONGS_ORDER")
fun playlistSongsDownloaded(
sourceId: Long,
@@ -208,6 +208,15 @@ class LibraryRepository(
db.libraryDao().songsByAlbum(sourceId, albumId)
}
suspend fun albumSongOrdinal(
sourceId: Long,
albumId: String,
songId: String,
): Long {
val index = db.queueDao().albumSongIds(sourceId, albumId).indexOf(songId)
return if (index < 0) 0L else index.toLong()
}
fun playlist(
sourceId: Long,
playlistId: String,
@@ -250,19 +250,6 @@ class QueueRepository(
songId: String,
) = QueueEntry(position = 0, sourceId = sourceId, kind = QueueKind.Song, refId = songId)
suspend fun downloadedSongIds(
sourceId: Long,
kind: QueueKind,
refId: String,
): List<String> {
val library = db.libraryDao()
return when (kind) {
QueueKind.Album -> library.downloadedAlbumSongIds(sourceId, refId)
QueueKind.Playlist -> library.downloadedPlaylistSongIds(sourceId, refId)
QueueKind.Song -> emptyList()
}
}
suspend fun sourceName(
kind: QueueKind,
sourceId: Long,
@@ -146,7 +146,6 @@ class PlaybackController(
private val offline: Boolean get() = sourceRepository.offline.value
private var windowPositions = LongArray(0)
private var windowFiltered = false
private var contextOverride: QueueContext? = null
init {
scope.launch {
@@ -242,48 +241,9 @@ class PlaybackController(
entry: QueueEntry,
start: Long,
disableShuffle: Boolean = false,
) {
if (!offline) {
contextOverride = null
play(listOf(entry), start, disableShuffle)
return
}
scope.launch {
val entries = downloadedEntries(entry)
if (entries.isEmpty()) {
showMessage("Nothing downloaded for offline playback")
return@launch
}
contextOverride = QueueContext(entry.kind, entry.sourceId, entry.refId)
play(entries, start, disableShuffle = true)
}
}
) = play(listOf(entry), start, disableShuffle)
private fun shuffleContext(entry: QueueEntry) {
if (!offline) {
contextOverride = null
shufflePlay(listOf(entry))
return
}
scope.launch {
val entries = downloadedEntries(entry)
if (entries.isEmpty()) {
showMessage("Nothing downloaded for offline playback")
return@launch
}
contextOverride = QueueContext(entry.kind, entry.sourceId, entry.refId)
shufflePlay(entries)
}
}
private suspend fun downloadedEntries(entry: QueueEntry): List<QueueEntry> {
if (entry.kind == QueueKind.Song) {
return if (downloads.localUri(entry.refId) != null) listOf(entry) else emptyList()
}
return queueRepository
.downloadedSongIds(entry.sourceId, entry.kind, entry.refId)
.map { QueueEntry(position = 0, sourceId = entry.sourceId, kind = QueueKind.Song, refId = it) }
}
private fun shuffleContext(entry: QueueEntry) = shufflePlay(listOf(entry))
private fun shufflePlay(entries: List<QueueEntry>) {
if (player == null) {
@@ -1024,7 +984,6 @@ class PlaybackController(
windowEnd = -1
windowPositions = LongArray(0)
windowFiltered = false
contextOverride = null
lastPosition = null
lastEdit = null
player?.run {
@@ -1393,7 +1352,6 @@ class PlaybackController(
}
private fun contextAt(position: Long?): QueueContext? {
contextOverride?.let { return it }
val snapshot = snapshot ?: return null
val play = position?.let { snapshot.anchorContextPlay(it) } ?: return null
val entry = snapshot.locateContext(play)?.first ?: return null
@@ -137,10 +137,10 @@ fun AlbumDetailScreen(
artwork: ArtworkColors?,
downloads: Map<String, SongDownload> = emptyMap(),
onBack: () -> Unit,
onSongClick: (Int) -> Unit,
onSongClick: (String) -> Unit,
onSongLongClick: (MenuTarget) -> Unit = {},
onShuffle: () -> Unit = {},
onPlay: () -> Unit = { onSongClick(0) },
onPlay: () -> Unit = {},
downloadStatus: ListDownloadStatus = ListDownloadStatus(),
onDownloadAction: (BulkDownloadAction) -> Unit = {},
onMore: () -> Unit = {},
@@ -204,7 +204,7 @@ fun AlbumDetailScreen(
},
content = { rowModifier ->
var lastDisc: Long? = null
songs.forEachIndexed { index, song ->
songs.forEach { song ->
val disc = song.disc ?: 1L
val discLabel = discLabels[disc]
if (disc != lastDisc && (multiDisc || discLabel != null)) {
@@ -221,7 +221,7 @@ fun AlbumDetailScreen(
download = downloads[song.id],
modifier =
rowModifier.combinedClickable(
onClick = { onSongClick(index) },
onClick = { onSongClick(song.id) },
onLongClick = {
onSongLongClick(MenuTarget.Song(song, coverArt(album?.coverArt, true), downloads[song.id]))
},
@@ -79,9 +79,10 @@ class AlbumDetailViewModel(
}
}
fun play(startIndex: Int) {
fun play(songId: String) {
viewModelScope.launch {
playbackController.playAlbum(sourceId.first(), albumId, startIndex.toLong())
val id = sourceId.first()
playbackController.playAlbum(id, albumId, libraryRepository.albumSongOrdinal(id, albumId, songId))
}
}
@@ -113,10 +113,10 @@ fun PlaylistDetailScreen(
downloadStatus: ListDownloadStatus = ListDownloadStatus(),
onDownloadAction: (BulkDownloadAction) -> Unit = {},
onBack: () -> Unit,
onSongClick: (Int) -> Unit,
onSongClick: (Long) -> Unit,
onSongLongClick: (MenuTarget) -> Unit = {},
onShuffle: () -> Unit = {},
onPlay: () -> Unit = { onSongClick(0) },
onPlay: () -> Unit = {},
onMore: () -> Unit = {},
playingSongId: String? = null,
modifier: Modifier = Modifier,
@@ -155,7 +155,7 @@ fun PlaylistDetailScreen(
download = downloads[item.song.id],
modifier =
rowModifier.combinedClickable(
onClick = { onSongClick(index) },
onClick = { onSongClick(item.position) },
onLongClick = {
onSongLongClick(MenuTarget.Song(item.song, coverArt(item.coverArt, true), downloads[item.song.id]))
},
@@ -81,9 +81,9 @@ class PlaylistDetailViewModel(
thumbnail: Boolean,
): CoverArtRef? = sourceRepository.coverArt(coverArt, thumbnail)
fun play(startIndex: Int) {
fun play(position: Long) {
viewModelScope.launch {
playbackController.playPlaylist(sourceId.first(), playlistId, startIndex.toLong())
playbackController.playPlaylist(sourceId.first(), playlistId, position)
}
}