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.

Ask the library what you are looking at, not how it is sorted

Leloniodfcd49bbb3eb
diff --git a/app/src/main/java/dev/lelonio/square/SquareApplication.kt b/app/src/main/java/dev/lelonio/square/SquareApplication.kt
index 9875522..29e6e6e 100644
--- a/app/src/main/java/dev/lelonio/square/SquareApplication.kt
+++ b/app/src/main/java/dev/lelonio/square/SquareApplication.kt
@@ -59,6 +59,10 @@ class SquareApplication : Application() {
val pinnedPlaylists: dev.lelonio.square.data.PinnedPlaylistStore by lazy {
dev.lelonio.square.data.PinnedPlaylistStore(this)
}
+ /** How the library was left looking: grid or list, and the sort. */
+ val libraryView: dev.lelonio.square.data.LibraryViewStore by lazy {
+ dev.lelonio.square.data.LibraryViewStore(this)
+ }
val preferences: PreferencesStore by lazy { PreferencesStore(this) }
/** Which file the engine asks Spotify for. */
diff --git a/app/src/main/java/dev/lelonio/square/data/LibraryViewStore.kt b/app/src/main/java/dev/lelonio/square/data/LibraryViewStore.kt
new file mode 100644
index 0000000..e2e914c
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/data/LibraryViewStore.kt
@@ -0,0 +1,45 @@
+package dev.lelonio.square.data
+
+import android.content.Context
+
+/**
+ * How the listener left the library looking.
+ *
+ * A grid and a list are not two ways of showing the same thing to the same
+ * person: whoever wants rows wants them every time, and having to say so again
+ * on every visit is the app forgetting something it was told. Kept on the
+ * device, like the pins, because no service has an opinion about it.
+ */
+class LibraryViewStore(context: Context) {
+
+ private val prefs = context.applicationContext
+ .getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
+
+ /** True for the grid, which is what a library opens as until told otherwise. */
+ var grid: Boolean
+ get() = prefs.getBoolean(KEY_GRID, true)
+ set(value) {
+ prefs.edit().putBoolean(KEY_GRID, value).apply()
+ }
+
+ /** The sort, by its own key; unknown values fall back to the default. */
+ var order: String?
+ get() = prefs.getString(KEY_ORDER, null)
+ set(value) {
+ prefs.edit().putString(KEY_ORDER, value).apply()
+ }
+
+ /** Whether the sort runs backwards; the direction is part of the sort. */
+ var descending: Boolean
+ get() = prefs.getBoolean(KEY_DESC, false)
+ set(value) {
+ prefs.edit().putBoolean(KEY_DESC, value).apply()
+ }
+
+ private companion object {
+ const val FILE_NAME = "square_library_view"
+ const val KEY_GRID = "grid"
+ const val KEY_ORDER = "order"
+ const val KEY_DESC = "descending"
+ }
+}
diff --git a/app/src/main/java/dev/lelonio/square/data/SpotifyApi.kt b/app/src/main/java/dev/lelonio/square/data/SpotifyApi.kt
index db60501..16cfee5 100644
--- a/app/src/main/java/dev/lelonio/square/data/SpotifyApi.kt
+++ b/app/src/main/java/dev/lelonio/square/data/SpotifyApi.kt
@@ -38,6 +38,13 @@ interface SpotifyApi {
@Query("offset") offset: Int = 0,
): PageDto<SavedTrackDto>
+ /** The albums the account has saved, which the library shows beside playlists. */
+ @GET("v1/me/albums")
+ suspend fun savedAlbums(
+ @Query("limit") limit: Int = 50,
+ @Query("offset") offset: Int = 0,
+ ): PageDto<SavedAlbumDto>
+
@GET("v1/me/playlists")
suspend fun playlists(
@Query("limit") limit: Int = 50,
@@ -422,6 +429,13 @@ data class ArtistCursorPageDto(
@Serializable
data class CursorsDto(val after: String? = null)
+/** One row of `me/albums`: the album, plus when it was saved. */
+@Serializable
+data class SavedAlbumDto(
+ val album: AlbumDto,
+ @SerialName("added_at") val addedAt: String? = null,
+)
+
@Serializable
data class AlbumDto(
val id: String? = null,
diff --git a/app/src/main/java/dev/lelonio/square/ui/library/LibraryScreen.kt b/app/src/main/java/dev/lelonio/square/ui/library/LibraryScreen.kt
index b3cfd49..427dfe9 100644
--- a/app/src/main/java/dev/lelonio/square/ui/library/LibraryScreen.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/library/LibraryScreen.kt
@@ -43,6 +43,7 @@ import dev.lelonio.square.ui.glass.backdrop.Backdrop
import dev.lelonio.square.R
import dev.lelonio.square.data.CatalogPlaylist
import dev.lelonio.square.data.sortedByRecentlyOpened
+import dev.lelonio.square.data.withLocalFilesFirst
import dev.lelonio.square.data.withPinnedFirst
import dev.lelonio.square.ui.MainViewModel
import dev.lelonio.square.ui.components.Artwork
@@ -52,10 +53,22 @@ import dev.lelonio.square.ui.player.GlassFilm
import dev.lelonio.square.ui.theme.Ink
import dev.lelonio.square.ui.theme.InkDim
import dev.lelonio.square.ui.theme.softShadow
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.ui.platform.LocalContext
+import androidx.compose.ui.layout.onGloballyPositioned
+import androidx.compose.ui.layout.boundsInRoot
+import dev.lelonio.square.ui.components.CHOICE_MENU_WIDTH
+import dev.lelonio.square.ui.components.GlassChoiceItem
+import dev.lelonio.square.ui.components.GlassMenuRule
+import dev.lelonio.square.ui.components.GlassChoiceMenu
+import dev.lelonio.square.ui.glass.backdrop.backdrops.layerBackdrop
+import dev.lelonio.square.ui.glass.backdrop.backdrops.rememberCombinedBackdrop
+import dev.lelonio.square.ui.glass.backdrop.backdrops.rememberLayerBackdrop
import com.adamglin.PhosphorIcons
import com.adamglin.phosphoricons.Fill
import com.adamglin.phosphoricons.fill.PushPin
import com.adamglin.phosphoricons.Regular
+import com.adamglin.phosphoricons.regular.ArrowsDownUp
import com.adamglin.phosphoricons.regular.ListBullets
import com.adamglin.phosphoricons.regular.Plus
import com.adamglin.phosphoricons.regular.SquaresFour
@@ -64,10 +77,25 @@ import com.adamglin.phosphoricons.regular.SquaresFour
private enum class Layout { GRID, LIST }
/** What the library is sorted by. */
-private enum class Order(@StringRes val label: Int) {
- RECENT(R.string.recently_opened),
- NAME(R.string.name),
- ADDED(R.string.spotify_order),
+private enum class Order(val key: String, @StringRes val label: Int) {
+ RECENT("recent", R.string.recently_opened),
+ NAME("name", R.string.name),
+ ADDED("added", R.string.spotify_order),
+}
+
+/**
+ * Which kind of thing the library is showing.
+ *
+ * The chips used to be the sort, which put the three least-used controls in the
+ * most prominent place on the screen and left no way at all to say "only
+ * albums". The sort is a menu now; the chips answer the question a library is
+ * actually asked, which is what am I looking at.
+ */
+private enum class Filter(@StringRes val label: Int, @StringRes val count: Int) {
+ ALL(R.string.library_all, R.string.item_count),
+ PLAYLISTS(R.string.playlists, R.string.playlist_count),
+ ARTISTS(R.string.artists, R.string.artist_count),
+ ALBUMS(R.string.albums, R.string.album_count),
}
/**
@@ -99,6 +127,8 @@ fun LibraryScreen(
/** The artists the account follows; empty leaves the shelf out entirely. */
artists: List<dev.lelonio.square.data.SearchItem> = emptyList(),
onOpenArtist: (dev.lelonio.square.data.SearchItem) -> Unit = {},
+ /** The albums the account has saved; see MainViewModel.savedAlbums. */
+ albums: List<CatalogPlaylist> = emptyList(),
backdrop: Backdrop,
) {
when (state) {
@@ -133,33 +163,93 @@ fun LibraryScreen(
}
is MainViewModel.UiState.Ready -> {
- var layout by remember { mutableStateOf(Layout.GRID) }
- var order by remember { mutableStateOf(Order.RECENT) }
+ // Both are the listener's own decision about their library rather
+ // than a mode of this screen, so they are read from where they were
+ // left and written as they change; see LibraryViewStore.
+ val appContext = LocalContext.current.applicationContext
+ val view = remember(appContext) {
+ (appContext as dev.lelonio.square.SquareApplication).libraryView
+ }
+ var layout by remember { mutableStateOf(if (view.grid) Layout.GRID else Layout.LIST) }
+ var order by remember {
+ mutableStateOf(Order.entries.firstOrNull { it.key == view.order } ?: Order.RECENT)
+ }
+ var filter by remember { mutableStateOf(Filter.ALL) }
+ var sortOpen by remember { mutableStateOf(false) }
+ // Recorded so the menu has the tiles behind it to blur, rather than
+ // the page colour: over a flat fill glass has nothing to bend and
+ // comes out looking like a hole. Safe to record — the menu is drawn
+ // outside the grid, so nothing in this layer samples it.
+ val listBackdrop = rememberLayerBackdrop()
+ var descending by remember { mutableStateOf(view.descending) }
+ val onOrderChosen: (Order) -> Unit = {
+ order = it
+ view.order = it.key
+ }
+ var sortAnchor by remember { mutableStateOf(androidx.compose.ui.unit.IntOffset.Zero) }
+ val density = androidx.compose.ui.platform.LocalDensity.current
+
+ val artistItems = remember(artists) {
+ artists.map { artist ->
+ CatalogPlaylist(
+ uri = artist.uri,
+ name = artist.title,
+ artworkUrl = artist.artworkUrl,
+ )
+ }
+ }
- val playlists = remember(state.playlists, playlistOrder, pinned, order) {
+ // What the chips are asking for, before any sorting.
+ val shown = remember(state.playlists, albums, artistItems, filter) {
+ when (filter) {
+ Filter.ALL -> state.playlists + albums
+ Filter.PLAYLISTS -> state.playlists
+ Filter.ALBUMS -> albums
+ Filter.ARTISTS -> artistItems
+ }
+ }
+
+ val playlists = remember(shown, playlistOrder, pinned, order, descending) {
when (order) {
- Order.RECENT -> state.playlists.sortedByRecentlyOpened(playlistOrder)
- Order.NAME -> state.playlists.sortedWith(
+ Order.RECENT -> shown.sortedByRecentlyOpened(playlistOrder)
+ Order.NAME -> shown.sortedWith(
compareBy(String.CASE_INSENSITIVE_ORDER) { it.name },
)
// The order the account added them, which is what the
// rootlist arrives in.
- Order.ADDED -> state.playlists
+ Order.ADDED -> shown
}
+ // The direction belongs to the sort rather than beside it:
+ // reversing "recently opened" is "least recently", and a
+ // separate control for that would be a second sort.
+ .let { if (descending) it.reversed() else it }
// Pinning outranks the sort: it is the one instruction the
// listener gave about this list themselves.
.withPinnedFirst(pinned)
+ // And the phone's own music outranks that, because it is
+ // not a playlist competing for a place: it is a fixed shelf
+ // of the library, and one nobody has opened yet would
+ // otherwise sit sixtieth among lists they have.
+ .withLocalFilesFirst()
}
+ Box(Modifier.fillMaxSize()) {
Column(Modifier.fillMaxSize()) {
Header(
count = playlists.size,
layout = layout,
order = order,
+ filter = filter,
backdrop = backdrop,
topPadding = contentPadding.calculateTopPadding(),
- onLayout = { layout = it },
- onOrder = { order = it },
+ onLayout = {
+ layout = it
+ view.grid = it == Layout.GRID
+ },
+ onOrder = { onOrderChosen(it) },
+ onFilter = { filter = it },
+ onSort = { sortOpen = true },
+ onSortAnchor = { sortAnchor = it },
canEdit = canEdit,
onCreatePlaylist = onCreatePlaylist,
)
@@ -177,9 +267,11 @@ fun LibraryScreen(
contentPadding = listPadding,
horizontalArrangement = Arrangement.spacedBy(14.dp),
verticalArrangement = Arrangement.spacedBy(18.dp),
- modifier = Modifier.fillMaxSize(),
+ modifier = Modifier
+ .fillMaxSize()
+ .layerBackdrop(listBackdrop),
) {
- if (artists.isNotEmpty()) {
+ if (artists.isNotEmpty() && filter == Filter.ALL) {
item(span = { GridItemSpan(maxLineSpan) }, key = "artists") {
ArtistShelf(artists, onOpenArtist)
}
@@ -189,7 +281,7 @@ fun LibraryScreen(
GridTile(
playlist,
pinned = playlist.uri in pinned,
- onClick = { onOpenPlaylist(playlist) },
+ onClick = { open(playlist, artists, onOpenPlaylist, onOpenArtist) },
onLongClick = if (canEdit) {
{ onPlaylistMenu(playlist) }
} else {
@@ -202,9 +294,11 @@ fun LibraryScreen(
Layout.LIST -> LazyColumn(
contentPadding = listPadding,
verticalArrangement = Arrangement.spacedBy(4.dp),
- modifier = Modifier.fillMaxSize(),
+ modifier = Modifier
+ .fillMaxSize()
+ .layerBackdrop(listBackdrop),
) {
- if (artists.isNotEmpty()) {
+ if (artists.isNotEmpty() && filter == Filter.ALL) {
item(key = "artists") { ArtistShelf(artists, onOpenArtist) }
}
@@ -212,7 +306,7 @@ fun LibraryScreen(
ListRow(
playlist,
pinned = playlist.uri in pinned,
- onClick = { onOpenPlaylist(playlist) },
+ onClick = { open(playlist, artists, onOpenPlaylist, onOpenArtist) },
onLongClick = if (canEdit) {
{ onPlaylistMenu(playlist) }
} else {
@@ -223,19 +317,85 @@ fun LibraryScreen(
}
}
}
+
+ // The app's own menu rather than Material's card: see GlassMenu.
+ // The same one the track sort on a playlist page opens, for the
+ // same reason — a menu that arrives from another design system is
+ // the one surface on screen that says so.
+ GlassChoiceMenu(
+ visible = sortOpen,
+ anchor = sortAnchor.leftOf(density),
+ backdrop = rememberCombinedBackdrop(backdrop, listBackdrop),
+ onDismiss = { sortOpen = false },
+ ) {
+ Order.entries.forEach { entry ->
+ GlassChoiceItem(
+ stringResource(entry.label),
+ selected = entry == order,
+ ) {
+ sortOpen = false
+ onOrderChosen(entry)
+ }
+ }
+ GlassMenuRule()
+
+ // Deliberately leaves the menu open: the direction is the one
+ // choice people flip back and forth to compare, and reopening
+ // for each flip makes a sort feel heavy.
+ GlassChoiceItem(
+ stringResource(R.string.sort_descending),
+ selected = descending,
+ ) {
+ descending = !descending
+ view.descending = descending
+ }
+ }
+ }
}
}
}
+/** Where a menu hanging off the sort button's right edge goes. */
+private fun androidx.compose.ui.unit.IntOffset.leftOf(
+ density: androidx.compose.ui.unit.Density,
+): androidx.compose.ui.unit.IntOffset {
+ val width = with(density) {
+ CHOICE_MENU_WIDTH.roundToPx()
+ }
+ val gap = with(density) { 8.dp.roundToPx() }
+ return androidx.compose.ui.unit.IntOffset((x - width + gap).coerceAtLeast(gap), y + gap)
+}
+
+/**
+ * Opens whatever the tile stands for.
+ *
+ * The grid holds playlists, albums and, under the artists chip, artists. The
+ * first two are track lists and open the same way; an artist is a page of its
+ * own, and the only thing that knows which is which is the URI.
+ */
+private fun open(
+ item: CatalogPlaylist,
+ artists: List<dev.lelonio.square.data.SearchItem>,
+ onOpenPlaylist: (CatalogPlaylist) -> Unit,
+ onOpenArtist: (dev.lelonio.square.data.SearchItem) -> Unit,
+) {
+ val artist = artists.firstOrNull { it.uri == item.uri }
+ if (artist != null) onOpenArtist(artist) else onOpenPlaylist(item)
+}
+
@Composable
private fun Header(
count: Int,
layout: Layout,
order: Order,
+ filter: Filter,
backdrop: Backdrop,
topPadding: Dp,
onLayout: (Layout) -> Unit,
onOrder: (Order) -> Unit,
+ onFilter: (Filter) -> Unit,
+ onSort: () -> Unit,
+ onSortAnchor: (androidx.compose.ui.unit.IntOffset) -> Unit,
canEdit: Boolean,
onCreatePlaylist: () -> Unit,
) {
@@ -252,8 +412,11 @@ private fun Header(
) {
Column(Modifier.weight(1f)) {
Text(stringResource(R.string.library), style = MaterialTheme.typography.displayLarge)
+ // Counting whatever the chips are showing, and saying so: the
+ // same number labelled "playlists" under the albums chip is a
+ // line that contradicts the screen it sits on.
Text(
- stringResource(R.string.playlist_count, count),
+ stringResource(filter.count, count),
style = MaterialTheme.typography.bodySmall,
color = InkDim,
)
@@ -301,18 +464,20 @@ private fun Header(
}
}
- // The sort is three chips rather than a menu: there are only three, and
- // a menu hides which one is active behind a tap.
+ // The chips say what is being shown; the sort sits at the end of the
+ // same row as a menu, because it is a choice among three that is made
+ // once and then left alone.
Row(
Modifier
.fillMaxWidth()
.padding(start = 20.dp, end = 20.dp, top = 14.dp, bottom = 6.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
+ verticalAlignment = Alignment.CenterVertically,
) {
- Order.entries.forEach { entry ->
- val selected = entry == order
+ Filter.entries.forEach { entry ->
+ val selected = entry == filter
LiquidButton(
- onClick = { onOrder(entry) },
+ onClick = { onFilter(entry) },
backdrop = backdrop,
flat = true,
contentHeight = 36.dp,
@@ -327,6 +492,32 @@ private fun Header(
)
}
}
+
+ Spacer(Modifier.weight(1f))
+
+ LiquidButton(
+ onClick = onSort,
+ backdrop = backdrop,
+ flat = true,
+ contentHeight = 36.dp,
+ contentPadding = 12.dp,
+ modifier = Modifier.onGloballyPositioned {
+ val bounds = it.boundsInRoot()
+ onSortAnchor(
+ androidx.compose.ui.unit.IntOffset(
+ bounds.right.toInt(),
+ bounds.bottom.toInt(),
+ ),
+ )
+ },
+ ) {
+ Icon(
+ PhosphorIcons.Regular.ArrowsDownUp,
+ contentDescription = stringResource(R.string.sort),
+ tint = InkDim,
+ modifier = Modifier.size(18.dp),
+ )
+ }
}
}
}