Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

A draggable scrollbar, and sorting by when tracks were added

Lelonio220da4c7d4e5
diff --git a/.claude/settings.local.json b/.claude/settings.local.json
index 774f5d4..31bc2dd 100644
--- a/.claude/settings.local.json
+++ b/.claude/settings.local.json
@@ -111,7 +111,8 @@
"Bash(git commit -q -m 'Load whole playlists, a batch at a time *)",
"Bash(PATH=/Users/emanuele/.cargo/bin:__TRACKED_VAR__ cargo check --quiet)",
"Bash(git commit -q -m 'Cap concurrent track lookups and retry the dropped ones *)",
- "Bash(git commit -q -m 'Read playlists through the Web API, and keep them *)"
+ "Bash(git commit -q -m 'Read playlists through the Web API, and keep them *)",
+ "Bash(git commit -q -m 'A draggable scrollbar, and sorting by when tracks were added *)"
]
}
}
diff --git a/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin b/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/configure_fingerprint.bin
index 92db97c..83d38e8 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 Logl
j
h/Users/emanuele/Progetto Spotify/app/.cxx/RelWithDebInfo/151m4dr6/arm64-v8a/additional_project_files.txtC
A
-?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint  �����3  ����3i
+?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint  ����3  ����3i
g
-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� ����3n
l
-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  ����3d
b
-`/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 @ ����3e
c
-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�� ����3i
g
-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 �" ����3o
m
-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  ( ����3g
e
-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 ����3D
B
-@/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 file
diff --git a/app/src/main/java/dev/emanuele/spot/data/Catalog.kt b/app/src/main/java/dev/emanuele/spot/data/Catalog.kt
index f5685af..5b8add1 100644
--- a/app/src/main/java/dev/emanuele/spot/data/Catalog.kt
+++ b/app/src/main/java/dev/emanuele/spot/data/Catalog.kt
@@ -27,6 +27,14 @@ data class CatalogTrack(
val durationMs: Long = 0,
val explicit: Boolean = false,
val artworkUrl: String? = null,
+ /**
+ * When the track was added to the playlist it was read from, ISO-8601.
+ *
+ * Only the Web API knows this — the access point's context has no such
+ * field — so it is null for anything resolved through the fallback path,
+ * and sorting by it then leaves the list in playlist order.
+ */
+ val addedAt: String? = null,
)
/**
@@ -57,7 +65,7 @@ data class CatalogPlaylist(
)
/** Bridges a Web API track into the model the UI and the queue already use. */
-fun TrackDto.toCatalogTrack(): CatalogTrack = CatalogTrack(
+fun TrackDto.toCatalogTrack(addedAt: String? = null): CatalogTrack = CatalogTrack(
uri = uri,
name = name,
artist = artists.joinToString(", ") { it.name },
@@ -65,6 +73,7 @@ fun TrackDto.toCatalogTrack(): CatalogTrack = CatalogTrack(
durationMs = durationMs,
explicit = explicit,
artworkUrl = album?.images?.firstOrNull()?.url,
+ addedAt = addedAt,
)
/**
diff --git a/app/src/main/java/dev/emanuele/spot/data/SpotifyApi.kt b/app/src/main/java/dev/emanuele/spot/data/SpotifyApi.kt
index f13c4d4..3d07e14 100644
--- a/app/src/main/java/dev/emanuele/spot/data/SpotifyApi.kt
+++ b/app/src/main/java/dev/emanuele/spot/data/SpotifyApi.kt
@@ -149,6 +149,8 @@ data class SavedTrackDto(
data class PlaylistTrackDto(
/** Null for episodes and for tracks removed from the catalogue. */
val track: TrackDto? = null,
+ /** ISO-8601, and absent on playlists old enough to predate the field. */
+ @SerialName("added_at") val addedAt: String? = null,
)
@Serializable
diff --git a/app/src/main/java/dev/emanuele/spot/ui/MainViewModel.kt b/app/src/main/java/dev/emanuele/spot/ui/MainViewModel.kt
index b223c0e..009e641 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/MainViewModel.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/MainViewModel.kt
@@ -691,7 +691,7 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
while (true) {
val page = container.api.playlistTracks(id, limit = WEB_API_PAGE, offset = offset)
// Episodes and delisted tracks come back as a null track.
- loaded += page.items.mapNotNull { it.track?.toCatalogTrack() }
+ loaded += page.items.mapNotNull { it.track?.toCatalogTrack(it.addedAt) }
offset += page.items.size
if (showProgress) {
_playlist.value = base.copy(
diff --git a/app/src/main/java/dev/emanuele/spot/ui/components/ScrollBar.kt b/app/src/main/java/dev/emanuele/spot/ui/components/ScrollBar.kt
new file mode 100644
index 0000000..99be1e6
--- /dev/null
+++ b/app/src/main/java/dev/emanuele/spot/ui/components/ScrollBar.kt
@@ -0,0 +1,133 @@
+package dev.emanuele.spot.ui.components
+
+import androidx.compose.animation.core.animateFloatAsState
+import androidx.compose.animation.core.tween
+import androidx.compose.foundation.background
+import androidx.compose.foundation.gestures.detectVerticalDragGestures
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.BoxWithConstraints
+import androidx.compose.foundation.layout.fillMaxHeight
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.width
+import androidx.compose.foundation.lazy.LazyListState
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.derivedStateOf
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.rememberCoroutineScope
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.clip
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.graphics.graphicsLayer
+import androidx.compose.ui.input.pointer.pointerInput
+import androidx.compose.ui.platform.LocalDensity
+import androidx.compose.ui.unit.Dp
+import androidx.compose.ui.unit.dp
+import kotlinx.coroutines.launch
+import kotlin.math.roundToInt
+
+/**
+ * A scroll position indicator that is also a way to move.
+ *
+ * A thousand-track playlist is the case this exists for: flinging through it
+ * takes a dozen throws and tells you nothing about where you are. Dragging the
+ * thumb crosses the whole list in one gesture.
+ *
+ * Position is measured in items rather than in pixels. A lazy list only knows
+ * the height of what is currently on screen, so a pixel-accurate bar would need
+ * every row measured up front — which is exactly what a lazy list exists to
+ * avoid. Rows here are near enough the same height that counting them is
+ * indistinguishable from measuring them, and it stays correct with the header
+ * and the album strip in the list.
+ *
+ * Hidden on short lists, where it would be furniture with nothing to do.
+ */
+@Composable
+fun LazyScrollBar(
+ state: LazyListState,
+ modifier: Modifier = Modifier,
+ /** Below this many rows the list is short enough to scroll by hand. */
+ minItems: Int = 40,
+ thumbHeight: Dp = 52.dp,
+ color: Color = Color.White.copy(alpha = 0.55f),
+) {
+ val density = LocalDensity.current
+ val scope = rememberCoroutineScope()
+ var dragging by remember { mutableStateOf(false) }
+
+ val enabled by remember { derivedStateOf { state.layoutInfo.totalItemsCount >= minItems } }
+ if (!enabled) return
+
+ // Visible while the list is moving and for a moment after, or for as long as
+ // it is being dragged. A permanent bar over the artwork is noise; one that
+ // appears when you scroll is an answer to the question you just asked.
+ val alpha by animateFloatAsState(
+ targetValue = if (state.isScrollInProgress || dragging) 1f else 0f,
+ animationSpec = tween(if (state.isScrollInProgress || dragging) 120 else 500),
+ label = "scrollbarAlpha",
+ )
+
+ BoxWithConstraints(
+ modifier
+ .fillMaxHeight()
+ .width(THUMB_WIDTH + TOUCH_MARGIN * 2)
+ .pointerInput(state) {
+ detectVerticalDragGestures(
+ onDragStart = { down ->
+ dragging = true
+ scope.launch { state.scrollToItem(itemFor(down.y, size.height, state)) }
+ },
+ onDragEnd = { dragging = false },
+ onDragCancel = { dragging = false },
+ ) { change, _ ->
+ change.consume()
+ scope.launch {
+ state.scrollToItem(itemFor(change.position.y, size.height, state))
+ }
+ }
+ },
+ ) {
+ val trackPx = with(density) { maxHeight.toPx() }
+ val thumbPx = with(density) { thumbHeight.toPx() }
+
+ Box(
+ Modifier
+ .padding(horizontal = TOUCH_MARGIN)
+ .width(THUMB_WIDTH)
+ .height(thumbHeight)
+ // Read here rather than in composition: scrolling then moves the
+ // thumb without recomposing the list it is drawn over.
+ .graphicsLayer {
+ this.alpha = alpha
+ translationY = (trackPx - thumbPx) * state.progress()
+ }
+ .clip(RoundedCornerShape(50))
+ .background(color),
+ )
+ }
+}
+
+/** How far down the list is, 0 to 1, counted in items. */
+private fun LazyListState.progress(): Float {
+ val info = layoutInfo
+ val last = info.totalItemsCount - info.visibleItemsInfo.size
+ if (last <= 0) return 0f
+ return (firstVisibleItemIndex.toFloat() / last).coerceIn(0f, 1f)
+}
+
+/** Which item a touch at [y] within a bar [height] tall is asking for. */
+private fun itemFor(y: Float, height: Int, state: LazyListState): Int {
+ val info = state.layoutInfo
+ val last = (info.totalItemsCount - info.visibleItemsInfo.size).coerceAtLeast(0)
+ val fraction = (y / height.toFloat()).coerceIn(0f, 1f)
+ return (fraction * last).roundToInt().coerceIn(0, last)
+}
+
+private val THUMB_WIDTH = 4.dp
+
+/** Widens the target without widening the mark; 4dp of bar is not grabbable. */
+private val TOUCH_MARGIN = 10.dp
diff --git a/app/src/main/java/dev/emanuele/spot/ui/library/PlaylistScreen.kt b/app/src/main/java/dev/emanuele/spot/ui/library/PlaylistScreen.kt
index bbb30af..eadf049 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/library/PlaylistScreen.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/library/PlaylistScreen.kt
@@ -20,6 +20,7 @@ import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
+import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.CircularProgressIndicator
@@ -49,6 +50,7 @@ import dev.emanuele.spot.data.CatalogTrack
import dev.emanuele.spot.ui.MainViewModel
import com.kyant.backdrop.Backdrop
import dev.emanuele.spot.ui.components.Artwork
+import dev.emanuele.spot.ui.components.LazyScrollBar
import dev.emanuele.spot.ui.components.SwipeToQueue
import dev.emanuele.spot.ui.glass.LiquidButton
import dev.emanuele.spot.ui.theme.rememberArtworkColor
@@ -73,6 +75,7 @@ enum class TrackSort(val label: String) {
ORIGINAL("Ordine della playlist"),
TITLE("Titolo"),
ARTIST("Artista"),
+ ADDED("Aggiunti di recente"),
DURATION("Durata"),
}
@@ -128,7 +131,10 @@ fun PlaylistScreen(
// No top content padding: the hero runs under the status bar, which is
// the whole point of the layout — the picture is the top of the screen,
// not something sitting below a gap.
+ val listState = rememberLazyListState()
+
LazyColumn(
+ state = listState,
contentPadding = PaddingValues(bottom = contentPadding.calculateBottomPadding()),
) {
item(contentType = "header") {
@@ -268,6 +274,16 @@ fun PlaylistScreen(
}
}
+ LazyScrollBar(
+ state = listState,
+ modifier = Modifier
+ .align(Alignment.CenterEnd)
+ .padding(
+ top = contentPadding.calculateTopPadding(),
+ bottom = contentPadding.calculateBottomPadding(),
+ ),
+ )
+
// Floating rather than a top bar: the list scrolls under it, so the
// picture stays uninterrupted.
LiquidButton(
@@ -674,6 +690,10 @@ private fun TrackSort.comparator(): Comparator<CatalogTrack> = when (this) {
TrackSort.ORIGINAL -> Comparator { _, _ -> 0 }
TrackSort.TITLE -> compareBy(String.CASE_INSENSITIVE_ORDER) { it.name }
TrackSort.ARTIST -> compareBy(String.CASE_INSENSITIVE_ORDER) { it.artist }
+ // Most recent first, and anything without a date last rather than first:
+ // a null here means the list came from the access point, which does not
+ // carry the field, and those belong at the end instead of on top.
+ TrackSort.ADDED -> compareByDescending(nullsFirst()) { it.addedAt }
TrackSort.DURATION -> compareBy { it.durationMs }
}