Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.
Kotlin alternativeandroidandroid-applicationclientglassmorphisminnertubelibrespotliquid-glassspotifyspotify-connectspotifyapiyoutube-musicyoutube-playerytdlnis
Add the Connect device picker to the player
Lelonio30801509eeaa
diff --git a/.claude/settings.local.json b/.claude/settings.local.jsonindex c7dd206..e664eb6 100644--- a/.claude/settings.local.json+++ b/.claude/settings.local.json@@ -82,7 +82,9 @@"Bash(git -c user.name=Emanuele -c [email protected] commit -qm 'Seat the sheet on the measured tab bar *)","Bash(git -c user.name=Emanuele -c [email protected] commit -qm 'Give the player sheet its own background *)","Bash(cd \"/Users/emanuele/Progetto *)",- "Bash(git -c user.name=Emanuele -c [email protected] commit -qm 'Play through Spotify Connect *)"+ "Bash(git -c user.name=Emanuele -c [email protected] commit -qm 'Play through Spotify Connect *)",+ "Bash(./gradlew :app:compileDevKotlin)",+ "Bash(git -c user.name=Emanuele -c [email protected] commit -qm 'Add the Connect device picker to the player *)"]}}diff --git a/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin b/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.binindex 52bb80f..cc8ae6d 100644--- a/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin+++ b/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin@@ -2,27 +2,27 @@ C/C++ Structured Logljh/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/additional_project_files.txtCA-?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint �Ř��3 ����3i+?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint �����3 ����3ig-e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build.json �Ř��3� ����3n+e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build.json �����3� ����3nl-j/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build_mini.json �Ř��3� �����3[+j/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/android_gradle_build_mini.json �����3� �����3[Y-W/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja �Ř��3�� ����3_+W/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja �����3�� ����3_]-[/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja.txt �Ř��3d+[/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build.ninja.txt �����3db-`/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build_file_index.txt �Ř��3 @ ����3e+`/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/build_file_index.txt �����3 @ ����3ec-a/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json �Ř��3�� ����3i+a/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json �����3�� ����3ig-e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json.bin �Ř��3 �" ����3o+e/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/compile_commands.json.bin �����3 �" ����3om-k/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/metadata_generation_command.txt �Ř��3+k/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/metadata_generation_command.txt �����3� ����3b`-^/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/prefab_config.json �Ř��3 ( ����3g+^/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/prefab_config.json �����3 ( ����3ge-c/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/symbol_folder_index.txt �Ř��3 b ����3D+c/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/symbol_folder_index.txt �����3 b ����3DB-@/Users/emanuele/Progetto Spotify/app/src/main/cpp/CMakeLists.txt �Ř��3 � �Ȅ��3\ No newline at end of file+@/Users/emanuele/Progetto Spotify/app/src/main/cpp/CMakeLists.txt �����3 � �Ȅ��3\ No newline at end of filediff --git a/app/src/main/java/dev/emanuele/spot/data/SpotifyApi.kt b/app/src/main/java/dev/emanuele/spot/data/SpotifyApi.ktindex de51282..66a0382 100644--- a/app/src/main/java/dev/emanuele/spot/data/SpotifyApi.kt+++ b/app/src/main/java/dev/emanuele/spot/data/SpotifyApi.kt@@ -2,7 +2,9 @@ package dev.emanuele.spot.dataimport kotlinx.serialization.SerialNameimport kotlinx.serialization.Serializable+import retrofit2.http.Bodyimport retrofit2.http.GET+import retrofit2.http.PUTimport retrofit2.http.Pathimport retrofit2.http.Query@@ -81,6 +83,22 @@ interface SpotifyApi {@Query("limit") limit: Int = 20,): PageDto<AlbumDto>+ /**+ * Every device the account can currently play on.+ *+ * The Web API rather than the access point: librespot's Connect state keeps+ * the cluster — which is exactly this list — inside its own event loop, with+ * no public accessor, so reading it there would mean patching the crate.+ *+ * Needs `user-read-playback-state`.+ */+ @GET("v1/me/player/devices")+ suspend fun devices(): DevicesDto++ /** Moves playback to another device. Needs `user-modify-playback-state`. */+ @PUT("v1/me/player")+ suspend fun transferPlayback(@Body request: TransferRequestDto)+@GET("v1/search")suspend fun search(@Query("q") query: String,@@ -156,6 +174,31 @@ data class TopTracksDto(val tracks: List<TrackDto> = emptyList())@Serializabledata class NewReleasesDto(val albums: PageDto<AlbumDto>? = null)+@Serializable+data class DevicesDto(val devices: List<DeviceDto> = emptyList())++@Serializable+data class DeviceDto(+ /** Null for a device the account can see but not address, which is not playable. */+ val id: String? = null,+ val name: String,+ /** `Computer`, `Smartphone`, `Speaker`, `TV`, … */+ val type: String = "",+ @SerialName("is_active") val isActive: Boolean = false,+ @SerialName("is_restricted") val isRestricted: Boolean = false,+ @SerialName("volume_percent") val volumePercent: Int? = null,+)++@Serializable+data class TransferRequestDto(+ @SerialName("device_ids") val deviceIds: List<String>,+ /**+ * Keeps playing after the move. False would transfer *and* pause, which is+ * never what picking a device from a player means.+ */+ val play: Boolean = true,+)+@Serializabledata class PlaylistDto(val id: String,diff --git a/app/src/main/java/dev/emanuele/spot/ui/MainViewModel.kt b/app/src/main/java/dev/emanuele/spot/ui/MainViewModel.ktindex cdd3efa..109f067 100644--- a/app/src/main/java/dev/emanuele/spot/ui/MainViewModel.kt+++ b/app/src/main/java/dev/emanuele/spot/ui/MainViewModel.kt@@ -9,6 +9,7 @@ import dev.emanuele.spot.data.Catalogimport dev.emanuele.spot.data.CatalogPlaylistimport dev.emanuele.spot.data.CatalogTrackimport dev.emanuele.spot.data.SearchItem+import dev.emanuele.spot.data.TransferRequestDtoimport dev.emanuele.spot.data.SearchResultsimport dev.emanuele.spot.data.toCatalogTrackimport dev.emanuele.spot.data.toResults@@ -153,6 +154,94 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {}}+ /** The Spotify Connect device picker. */+ data class DevicesState(+ val open: Boolean = false,+ val loading: Boolean = false,+ val devices: List<SpotifyDevice> = emptyList(),+ val error: String? = null,+ )++ data class SpotifyDevice(+ val id: String,+ val name: String,+ val type: String,+ val isActive: Boolean,+ )++ private val _devices = MutableStateFlow(DevicesState())+ val devices: StateFlow<DevicesState> = _devices.asStateFlow()+ private var devicesJob: Job? = null++ fun openDevices() {+ _devices.value = _devices.value.copy(open = true)+ refreshDevices()+ }++ fun closeDevices() {+ _devices.value = _devices.value.copy(open = false)+ }++ /**+ * Reads the account's device list.+ *+ * Needs `user-read-playback-state`, which an application connected before+ * this feature existed was never asked for — hence the explicit message+ * rather than a bare 403.+ */+ fun refreshDevices() {+ devicesJob?.cancel()+ if (!container.webApi.isReady) {+ _devices.value = _devices.value.copy(+ loading = false,+ error = "Serve la tua applicazione Spotify: configurala in Cerca.",+ )+ return+ }++ _devices.value = _devices.value.copy(loading = true, error = null)+ devicesJob = viewModelScope.launch {+ runCatching { container.api.devices().devices }+ .onSuccess { list ->+ _devices.value = _devices.value.copy(+ loading = false,+ devices = list.mapNotNull { device ->+ SpotifyDevice(+ // A device with no id cannot be addressed, so it+ // is dropped rather than shown as a dead row.+ id = device.id ?: return@mapNotNull null,+ name = device.name,+ type = device.type,+ isActive = device.isActive,+ )+ },+ )+ }+ .onFailure {+ android.util.Log.e(TAG, "devices failed: ${chain(it)}", it)+ _devices.value = _devices.value.copy(+ loading = false,+ error = describe(it),+ )+ }+ }+ }++ /** Moves playback to another device, keeping it playing. */+ fun transferPlayback(deviceId: String) = viewModelScope.launch {+ runCatching { container.api.transferPlayback(TransferRequestDto(listOf(deviceId))) }+ .onSuccess {+ // Spotify reports the move a beat after acknowledging it, so the+ // list is re-read rather than edited optimistically.+ delay(TRANSFER_SETTLE_MS)+ refreshDevices()+ }+ .onFailure {+ android.util.Log.e(TAG, "transfer failed: ${chain(it)}", it)+ _devices.value = _devices.value.copy(error = describe(it))+ }+ }+private val _playlist = MutableStateFlow(PlaylistState())val playlist: StateFlow<PlaylistState> = _playlist.asStateFlow()@@ -252,7 +341,14 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {SpotifyOAuth.authorize(getApplication(),clientId = clientId,- scopes = listOf("user-top-read"),+ scopes = listOf(+ // The home feed's "artisti che ascolti".+ "user-top-read",+ // The Connect device picker: one to list them, one to move+ // playback.+ "user-read-playback-state",+ "user-modify-playback-state",+ ),)}.onSuccess {@@ -510,5 +606,8 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {const val PLAYLIST_LIMIT = 100const val SEARCH_DEBOUNCE_MS = 350L++ /** Spotify acknowledges a transfer before it has taken effect. */+ const val TRANSFER_SETTLE_MS = 700L}}diff --git a/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt b/app/src/main/java/dev/emanuele/spot/ui/SpotApp.ktindex 95350b5..3e32546 100644--- a/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt+++ b/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt@@ -137,6 +137,7 @@ fun SpotApp(val presets by viewModel.effectPresets.collectAsStateWithLifecycle()val feed by viewModel.feed.collectAsStateWithLifecycle()val playlistOrder by viewModel.playlistOrder.collectAsStateWithLifecycle()+ val devices by viewModel.devices.collectAsStateWithLifecycle()// Once, on the first composition that has a usable Web API session. The// ViewModel keeps what it fetched, so navigating away and back does not@@ -431,6 +432,11 @@ fun SpotApp(onDeletePreset = viewModel::deleteEffectPreset,backdrop = artBackdrop,canvas = canvas,+ devices = devices,+ onOpenDevices = viewModel::openDevices,+ onCloseDevices = viewModel::closeDevices,+ onRefreshDevices = viewModel::refreshDevices,+ onSelectDevice = { viewModel.transferPlayback(it) },)},)diff --git a/app/src/main/java/dev/emanuele/spot/ui/player/DevicePicker.kt b/app/src/main/java/dev/emanuele/spot/ui/player/DevicePicker.ktnew file mode 100644index 0000000..abdf96f--- /dev/null+++ b/app/src/main/java/dev/emanuele/spot/ui/player/DevicePicker.kt@@ -0,0 +1,192 @@+package dev.emanuele.spot.ui.player++import androidx.compose.foundation.background+import androidx.compose.foundation.clickable+import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.heightIn+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.rememberScrollState+import androidx.compose.foundation.shape.RoundedCornerShape+import androidx.compose.foundation.verticalScroll+import androidx.compose.material.icons.Icons+import androidx.compose.material.icons.filled.Computer+import androidx.compose.material.icons.filled.Refresh+import androidx.compose.material.icons.filled.Smartphone+import androidx.compose.material.icons.filled.Speaker+import androidx.compose.material.icons.filled.Tv+import androidx.compose.material3.CircularProgressIndicator+import androidx.compose.material3.Icon+import androidx.compose.material3.MaterialTheme+import androidx.compose.material3.Text+import androidx.compose.runtime.Composable+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.clip+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.graphics.vector.ImageVector+import androidx.compose.ui.text.style.TextOverflow+import androidx.compose.ui.unit.dp+import com.kyant.backdrop.Backdrop+import dev.emanuele.spot.ui.MainViewModel++/**+ * The Spotify Connect device list.+ *+ * The list comes from the Web API rather than from the engine: librespot keeps+ * the cluster — every device the account can see — inside the Connect event+ * loop with no public accessor, so reading it there would mean patching the+ * crate for something an endpoint already answers.+ *+ * Switching device is a real transfer, not a local mute: the audio moves, and+ * this app stops decoding. That is the whole point of Connect, and it is why+ * the active device is marked rather than assumed to be this one.+ */+@Composable+fun DevicePicker(+ state: MainViewModel.DevicesState,+ backdrop: Backdrop,+ onSelect: (String) -> Unit,+ onRefresh: () -> Unit,+ onDismiss: () -> Unit,+) {+ Box(+ Modifier+ .fillMaxSize()+ // Catches the tap that closes it, and dims what is behind so the+ // sheet reads as being in front rather than as another panel.+ .background(Color.Black.copy(alpha = 0.45f))+ .clickable(interactionSource = null, indication = null, onClick = onDismiss),+ contentAlignment = Alignment.BottomCenter,+ ) {+ GlassSurface(+ backdrop = backdrop,+ shape = RoundedCornerShape(28.dp),+ surfaceColor = GlassFilm,+ modifier = Modifier+ .padding(16.dp)+ .fillMaxWidth()+ .clip(RoundedCornerShape(28.dp))+ // Swallows taps so one inside the sheet does not dismiss it.+ .clickable(interactionSource = null, indication = null) {},+ ) {+ Column(Modifier.padding(vertical = 18.dp)) {+ Row(+ Modifier+ .fillMaxWidth()+ .padding(start = 22.dp, end = 14.dp, bottom = 10.dp),+ verticalAlignment = Alignment.CenterVertically,+ ) {+ Text(+ "Riproduci su",+ style = MaterialTheme.typography.titleLarge,+ color = GlassInk,+ modifier = Modifier.weight(1f),+ )+ Box(+ Modifier+ .size(36.dp)+ .clip(RoundedCornerShape(50))+ .clickable(onClick = onRefresh),+ contentAlignment = Alignment.Center,+ ) {+ Icon(+ Icons.Filled.Refresh,+ contentDescription = "Aggiorna",+ tint = GlassInkDim,+ modifier = Modifier.size(20.dp),+ )+ }+ }++ when {+ state.loading && state.devices.isEmpty() -> Box(+ Modifier+ .fillMaxWidth()+ .padding(vertical = 26.dp),+ contentAlignment = Alignment.Center,+ ) {+ CircularProgressIndicator(color = GlassInkDim, strokeWidth = 2.dp)+ }++ state.error != null -> Text(+ state.error,+ style = MaterialTheme.typography.bodyMedium,+ color = GlassInkDim,+ modifier = Modifier.padding(horizontal = 22.dp, vertical = 12.dp),+ )++ state.devices.isEmpty() -> Text(+ "Nessun dispositivo disponibile",+ style = MaterialTheme.typography.bodyMedium,+ color = GlassInkDim,+ modifier = Modifier.padding(horizontal = 22.dp, vertical = 12.dp),+ )++ else -> Column(+ Modifier+ .heightIn(max = 320.dp)+ .verticalScroll(rememberScrollState()),+ verticalArrangement = Arrangement.spacedBy(2.dp),+ ) {+ state.devices.forEach { device ->+ DeviceRow(device) { onSelect(device.id) }+ }+ }+ }+ }+ }+ }+}++@Composable+private fun DeviceRow(device: MainViewModel.SpotifyDevice, onClick: () -> Unit) {+ Row(+ Modifier+ .fillMaxWidth()+ .clickable(enabled = !device.isActive, onClick = onClick)+ .padding(horizontal = 22.dp, vertical = 12.dp),+ verticalAlignment = Alignment.CenterVertically,+ ) {+ Icon(+ iconFor(device.type),+ contentDescription = null,+ // The playing device is the accent's one job on this sheet: every+ // other row has to stay legible over whatever is behind the glass.+ tint = if (device.isActive) MaterialTheme.colorScheme.primary else GlassInkDim,+ modifier = Modifier.size(22.dp),+ )+ Column(+ Modifier+ .padding(start = 16.dp)+ .weight(1f),+ ) {+ Text(+ device.name,+ style = MaterialTheme.typography.titleMedium,+ color = if (device.isActive) MaterialTheme.colorScheme.primary else GlassInk,+ maxLines = 1,+ overflow = TextOverflow.Ellipsis,+ )+ Text(+ if (device.isActive) "In riproduzione" else device.type,+ style = MaterialTheme.typography.bodySmall,+ color = GlassInkDim,+ maxLines = 1,+ )+ }+ }+}++/** Spotify's device types, as far as they map onto icons worth telling apart. */+private fun iconFor(type: String): ImageVector = when (type.lowercase()) {+ "computer" -> Icons.Filled.Computer+ "smartphone", "tablet" -> Icons.Filled.Smartphone+ "tv", "castvideo" -> Icons.Filled.Tv+ else -> Icons.Filled.Speaker+}diff --git a/app/src/main/java/dev/emanuele/spot/ui/player/PlayerScreen.kt b/app/src/main/java/dev/emanuele/spot/ui/player/PlayerScreen.ktindex fe85b1e..287988a 100644--- a/app/src/main/java/dev/emanuele/spot/ui/player/PlayerScreen.kt+++ b/app/src/main/java/dev/emanuele/spot/ui/player/PlayerScreen.kt@@ -32,6 +32,7 @@ import androidx.compose.foundation.layout.systemBarsPaddingimport androidx.compose.foundation.shape.CircleShapeimport androidx.compose.foundation.shape.RoundedCornerShapeimport androidx.compose.material.icons.Icons+import androidx.compose.material.icons.filled.Devicesimport androidx.compose.material.icons.filled.KeyboardArrowDownimport androidx.compose.material.icons.filled.MoreHorizimport androidx.compose.material.icons.filled.Pause@@ -126,6 +127,12 @@ fun PlayerScreen(/** See MiniPlayer: the cover is shared with the bar this screen grew out of. */sharedScope: androidx.compose.animation.SharedTransitionScope? = null,animatedScope: androidx.compose.animation.AnimatedVisibilityScope? = null,+ /** The Spotify Connect device list, and what to do with it. */+ devices: dev.emanuele.spot.ui.MainViewModel.DevicesState,+ onOpenDevices: () -> Unit,+ onCloseDevices: () -> Unit,+ onRefreshDevices: () -> Unit,+ onSelectDevice: (String) -> Unit,) {var panel by remember { mutableStateOf(PlayerPanel.NONE) }@@ -213,6 +220,7 @@ fun PlayerScreen(backdrop = glassBackdrop,queueOpen = panel == PlayerPanel.QUEUE,onCollapse = onCollapse,+ onOpenDevices = onOpenDevices,onToggleQueue = {panel = if (panel == PlayerPanel.QUEUE) {PlayerPanel.NONE@@ -369,6 +377,19 @@ fun PlayerScreen(}}}++ // Over everything, including the transport: it is a modal choice, and+ // the controls underneath would be operating a device the user is in+ // the middle of changing.+ if (devices.open) {+ DevicePicker(+ state = devices,+ backdrop = glassBackdrop,+ onSelect = onSelectDevice,+ onRefresh = onRefreshDevices,+ onDismiss = onCloseDevices,+ )+ }}}@@ -377,6 +398,7 @@ private fun TopBar(backdrop: Backdrop,queueOpen: Boolean,onCollapse: () -> Unit,+ onOpenDevices: () -> Unit,onToggleQueue: () -> Unit,) {Row(@@ -394,6 +416,17 @@ private fun TopBar(textAlign = TextAlign.Center,modifier = Modifier.weight(1f),)+ // Connect gets the permanent slot rather than hiding behind "more":+ // moving playback to another speaker is the thing you reach for while+ // the player is open, and the queue already has its own tab below.+ GlassButton(backdrop, onClick = onOpenDevices) {+ Icon(+ Icons.Filled.Devices,+ contentDescription = "Dispositivi",+ tint = GlassInk,+ )+ }+ Spacer(Modifier.size(8.dp))GlassButton(backdrop, onClick = onToggleQueue) {Icon(Icons.Filled.MoreHoriz,