Modern Apps git

← Modern-Apps.git

music: play assistant album requests in disc/track order

Commit
bbce76c2fdbbf3de6aaf4afa8552fe22d9ccd246
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:25 UTC (28 days ago)
Parents
078b364
Symptom: asking the assistant to play an album played its tracks
alphabetically, and a playlist in library title order, while tapping
the same album or playlist in the app played it in proper order.

Root cause: PlayIntent filtered the title-sorted library list and
queued it as-is. The album screen and MusicLibraryTree both sort
albums by disc/track; the intent path never did, and for playlists it
turned the stored matching order into a set and lost it.

Fix: extract the queue construction into orderForPlayback - albums
sorted by disc, track, then title (matching MusicLibraryTree), and
playlists mapped in their stored matching order, skipping ids no
longer in the library. Song and artist behaviour is unchanged.

Verification: new PlayIntentOrderTest pins album, playlist, stale-id
and unknown-type cases; module tests run after this series.

Diff

music/src/main/java/com/vayunmathur/music/intents/PlayIntent.kt
diff --git a/music/src/main/java/com/vayunmathur/music/intents/PlayIntent.kt b/music/src/main/java/com/vayunmathur/music/intents/PlayIntent.ktindex ec7def3cca..9f183de91d 100644--- a/music/src/main/java/com/vayunmathur/music/intents/PlayIntent.kt+++ b/music/src/main/java/com/vayunmathur/music/intents/PlayIntent.kt@@ -2,6 +2,7 @@ package com.vayunmathur.music.intents  import com.vayunmathur.library.intents.music.PlayMusicData import com.vayunmathur.library.util.AssistantIntent+import com.vayunmathur.music.data.Music import com.vayunmathur.music.data.MusicRepository import com.vayunmathur.music.data.TYPE_MUSIC_PLAYLIST import com.vayunmathur.music.platform.PlaybackManager@@ -17,19 +18,41 @@ class PlayIntent: AssistantIntent<PlayMusicData, Unit>(serializer<PlayMusicData>          val allMusic = repo.getAllMusic() -        val songsToPlay = when (input.type) {-            "song" -> allMusic.filter { it.id == input.id }-            "album" -> allMusic.filter { it.albumId == input.id }-            "artist" -> allMusic.filter { it.artistId == input.id }-            "playlist" -> {-                val songIds = repo.getFromRight(input.id, TYPE_MUSIC_PLAYLIST).toSet()-                allMusic.filter { it.id in songIds }-            }-            else -> emptyList()+        val playlistIds = if (input.type == "playlist") {+            repo.getFromRight(input.id, TYPE_MUSIC_PLAYLIST)+        } else {+            emptyList()         }+        val songsToPlay = orderForPlayback(input.type, input.id, allMusic, playlistIds)          if (songsToPlay.isNotEmpty()) {             pm.playSong(songsToPlay, 0)         }     } }++/**+ * The queue an assistant "play X" request should start, in playback order.+ *+ * The library list is title-sorted (it backs the songs tab), so filtering+ * it directly played an album alphabetically instead of in disc/track+ * order - the album screen and the car tree both sort albums by+ * disc/track - and played a playlist in library order instead of the+ * order its matchings are stored in.+ */+internal fun orderForPlayback(+    type: String,+    id: Long,+    allMusic: List<Music>,+    playlistIds: List<Long> = emptyList(),+): List<Music> = when (type) {+    "song" -> allMusic.filter { it.id == id }+    "album" -> allMusic.filter { it.albumId == id }+        .sortedWith(compareBy({ it.discNumber }, { it.trackNumber }, { it.title.lowercase() }))+    "artist" -> allMusic.filter { it.artistId == id }+    "playlist" -> {+        val byId = allMusic.associateBy { it.id }+        playlistIds.mapNotNull { byId[it] }+    }+    else -> emptyList()+}
music/src/test/java/com/vayunmathur/music/intents/PlayIntentOrderTest.kt
diff --git a/music/src/test/java/com/vayunmathur/music/intents/PlayIntentOrderTest.kt b/music/src/test/java/com/vayunmathur/music/intents/PlayIntentOrderTest.ktnew file mode 100644index 0000000000..ef4996fec3--- /dev/null+++ b/music/src/test/java/com/vayunmathur/music/intents/PlayIntentOrderTest.kt@@ -0,0 +1,58 @@+package com.vayunmathur.music.intents++import com.vayunmathur.music.data.Music+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++/**+ * Pins the playback order of assistant "play X" queues: albums play in+ * disc/track order (not the library's title order) and playlists play in+ * their stored matching order.+ */+class PlayIntentOrderTest {++    private fun song(+        id: Long,+        title: String,+        albumId: Long = 7,+        disc: Int = 1,+        track: Int = 0,+    ) = Music(+        id = id, title = title, artist = "Artist", artistId = 1, album = "Album",+        albumId = albumId, uri = "content://media/$id", duration = 180_000,+        trackNumber = track, year = 2020, discNumber = disc,+    )++    // Title-sorted, as the library hands it over: B-side before the opener.+    private val library = listOf(+        song(3, "Bravo", disc = 1, track = 2),+        song(2, "Alpha", disc = 2, track = 1),+        song(1, "Zulu", disc = 1, track = 1),+        song(9, "Other album", albumId = 8),+    )++    @Test+    fun albumPlaysInDiscTrackOrderNotTitleOrder() {+        val queue = orderForPlayback("album", 7, library)+        assertEquals(listOf(1L, 3L, 2L), queue.map { it.id })+    }++    @Test+    fun playlistPlaysInStoredMatchingOrder() {+        val queue = orderForPlayback("playlist", 5, library, playlistIds = listOf(2L, 1L))+        assertEquals(listOf(2L, 1L), queue.map { it.id })+    }++    @Test+    fun playlistSkipsIdsNoLongerInTheLibrary() {+        val queue = orderForPlayback("playlist", 5, library, playlistIds = listOf(99L, 1L))+        assertEquals(listOf(1L), queue.map { it.id })+    }++    @Test+    fun songAndUnknownTypesAreUnchanged() {+        assertEquals(listOf(9L), orderForPlayback("song", 9, library).map { it.id })+        assertTrue(orderForPlayback("genre", 1, library).isEmpty())+    }+}