Skip to content

Native Android client for Spotify, built with Jetpack Compose

A downloaded song picked in a fully downloaded list plays from its file, online too

kota-zoe032e1cb6689
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/LocalPlayPlan.kt b/base/app/src/main/java/com/music/spotui/data/offline/LocalPlayPlan.kt
new file mode 100644
index 00000000..8933c025
--- /dev/null
+++ b/base/app/src/main/java/com/music/spotui/data/offline/LocalPlayPlan.kt
@@ -0,0 +1,32 @@
+package com.music.spotui.data.offline
+
+/**
+ * Whether a song picked in a list plays from the phone (the downloaded file) or from the Web Player.
+ *
+ * Offline, or with the data saver on mobile data, the Web Player cannot stream: the list's
+ * downloaded songs play. Online, a downloaded song also plays from its file (the lossless one
+ * the user chose to keep) as long as the whole list is on the phone: the local queue holds only
+ * downloaded songs, so a list that is only partly downloaded would lose the others, and it keeps
+ * streaming from Spotify's own queue.
+ */
+object LocalPlayPlan {
+ /**
+ * The ids to play locally, in play order, or null when the Web Player plays.
+ * [ordered]: the list's track ids in the order to play them; [complete]: the whole list is loaded.
+ */
+ fun localIds(
+ ordered: List<String>,
+ picked: String,
+ complete: Boolean,
+ offline: Boolean,
+ dataSaver: Boolean,
+ isDownloaded: (String) -> Boolean,
+ ): List<String>? {
+ if (!isDownloaded(picked)) return null
+ val ids = ordered.filter { it.isNotBlank() }.distinct()
+ val downloaded = ids.filter(isDownloaded)
+ if (downloaded.isEmpty()) return null
+ if (offline || dataSaver) return downloaded
+ return if (complete && downloaded.size == ids.size) downloaded else null
+ }
+}
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/ContextScreen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/ContextScreen.kt
index deeb0e2f..b6bfd645 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/ContextScreen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/ContextScreen.kt
@@ -389,15 +389,21 @@ class ContextViewModel(
fun play(p: ContextPageModel, song: SongsModel, sort: TrackOrder = TrackOrder()) {
// No connection (the Web Player cannot stream), or the data saver on mobile data:
- // the page's downloaded songs play from the phone.
+ // the page's downloaded songs play from the phone. Online, a downloaded song also plays
+ // from its file when the whole list is downloaded (it used to stream anyway).
val app = Graph.app
- if (com.music.spotui.data.offline.OfflineStore.isDownloaded(song.spotifyTrackId) &&
- (com.music.spotui.data.DataSaver.active || !com.music.spotui.data.offline.OfflineStore.isOnline(app))) {
- val local = sort.apply(p.tracks).mapNotNull { com.music.spotui.data.offline.OfflineStore.tracks[it.spotifyTrackId] }.distinctBy { it.trackId }
+ val store = com.music.spotui.data.offline.OfflineStore
+ val limited = com.music.spotui.data.DataSaver.active || !store.isOnline(app)
+ val ids = com.music.spotui.data.offline.LocalPlayPlan.localIds(
+ sort.apply(p.tracks).map { it.spotifyTrackId }, song.spotifyTrackId, p.complete,
+ offline = !store.isOnline(app), dataSaver = com.music.spotui.data.DataSaver.active, isDownloaded = store::isDownloaded,
+ )
+ if (ids != null) {
+ val local = ids.mapNotNull { store.tracks[it] }
val at = local.indexOfFirst { it.trackId == song.spotifyTrackId }.coerceAtLeast(0)
- if (local.size < p.tracks.size)
+ if (limited && local.size < p.tracks.size)
android.widget.Toast.makeText(app, app.getString(R.string.offline_only_downloaded, local.size, p.tracks.size), android.widget.Toast.LENGTH_SHORT).show()
- com.music.spotui.di.LocalPlayer.play(local.map(com.music.spotui.data.offline.OfflineStore::toLocalTrack), at, context = p.name)
+ com.music.spotui.di.LocalPlayer.play(local.map(store::toLocalTrack), at, shuffle = state.shuffle.value, context = p.name)
return
}
state.updateQueue(p.tracks)
diff --git a/base/app/src/test/java/com/music/spotui/data/offline/LocalPlayPlanTest.kt b/base/app/src/test/java/com/music/spotui/data/offline/LocalPlayPlanTest.kt
new file mode 100644
index 00000000..f778cea6
--- /dev/null
+++ b/base/app/src/test/java/com/music/spotui/data/offline/LocalPlayPlanTest.kt
@@ -0,0 +1,28 @@
+package com.music.spotui.data.offline
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNull
+import org.junit.Test
+
+class LocalPlayPlanTest {
+ private val have = setOf("a", "b", "c")
+ private fun plan(ordered: List<String>, picked: String, complete: Boolean = true, offline: Boolean = false, saver: Boolean = false) =
+ LocalPlayPlan.localIds(ordered, picked, complete, offline, saver) { it in have }
+
+ @Test fun onlineFullyDownloadedListPlaysLocally() =
+ assertEquals(listOf("c", "a", "b"), plan(listOf("c", "a", "b", "a"), "a"))
+
+ @Test fun onlinePartlyDownloadedListStreams() = assertNull(plan(listOf("a", "b", "x"), "a"))
+
+ @Test fun onlineListNotFullyLoadedStreams() = assertNull(plan(listOf("a", "b"), "a", complete = false))
+
+ @Test fun onlinePickedSongNotDownloadedStreams() = assertNull(plan(listOf("a", "b", "x"), "x"))
+
+ @Test fun offlinePlaysTheDownloadedSongsOnly() = assertEquals(listOf("a", "b"), plan(listOf("a", "x", "b"), "a", offline = true))
+
+ @Test fun dataSaverPlaysTheDownloadedSongsOnly() = assertEquals(listOf("b", "a"), plan(listOf("b", "x", "a"), "a", saver = true))
+
+ @Test fun offlinePickedSongNotDownloadedIsLeftToTheCaller() = assertNull(plan(listOf("a", "x"), "x", offline = true))
+
+ @Test fun blankIdsAreIgnored() = assertEquals(listOf("a"), plan(listOf("a", ""), "a"))
+}