Skip to content

Native Android client for Spotify, built with Jetpack Compose

Podcasts chip: a New episodes row opens the episodes not played yet of the followed shows, newest first, in both looks (the latest three of each show, read four shows at a time through the show page's own episodes query, kept for half an hour), a tap opening the episode [skip ci]

kota-zo7976b1437a5a
diff --git a/base/app/src/main/java/com/music/spotui/data/api/Api.kt b/base/app/src/main/java/com/music/spotui/data/api/Api.kt
index 77b43a86..1f908f28 100644
--- a/base/app/src/main/java/com/music/spotui/data/api/Api.kt
+++ b/base/app/src/main/java/com/music/spotui/data/api/Api.kt
@@ -68,6 +68,14 @@ class Api constructor(
const val EPISODES_ID = "your-episodes"
/** Prefix of the library id of a followed show in the Podcasts chip (a plain show id otherwise). */
const val SHOW_PREFIX = "show:"
+ /** "New episodes" (unplayed episodes of the followed shows), first of the Podcasts chip's shows. */
+ const val NEW_EPISODES_ID = "new-episodes"
+ /** Episodes asked for each followed show by the new episodes list, and the most it keeps. */
+ private const val NEW_PER_SHOW = 3
+ private const val NEW_EPISODES_MAX = 60
+ /** The new episodes list is asked for again only after this long (the shows are many queries). */
+ private const val NEW_EPISODES_TTL_MS = 30 * 60_000L
+ @Volatile var newEpisodes: Pair<Long, List<SongsModel>>? = null
/** "Recently played": Spotify's own list for the account, pinned in the library. */
const val RECENT_ID = "recently-played"
/** Pages of 50 asked for a show's episodes / chapters, at most (500 rows). */
@@ -91,7 +99,7 @@ class Api constructor(
fun clear() {
PlaylistFresh.drop(null)
albums = null; artists = null; home = null; library = null
- libraryRoot = null; folders.clear()
+ libraryRoot = null; folders.clear(); newEpisodes = null
facets.clear()
LikedCache.tracks = null
ArtistCache.map.clear()
@@ -329,7 +337,31 @@ class Api constructor(
}
offset = page.nextOffset ?: break
}
- return out
+ if (out.isEmpty()) return out
+ val news = LibraryEntry(NEW_EPISODES_ID, context.getString(R.string.new_episodes_title), context.getString(R.string.new_episodes_subtitle),
+ out.first().coverUri, isPlaylist = true)
+ return listOf(news) + out
+ }
+
+ /**
+ * The episodes not played yet of the shows the user follows, newest first: the show page's own
+ * episodes query, asked for the latest few of each show, four shows at a time. Kept for half
+ * an hour, so opening the screen again asks nothing.
+ */
+ suspend fun getNewEpisodes(): List<SongsModel> {
+ if (!ready()) return emptyList()
+ newEpisodes?.let { (at, list) -> if (System.currentTimeMillis() - at < NEW_EPISODES_TTL_MS) return list }
+ val shows = getFollowedShows().filter { it.spotifyId.startsWith(SHOW_PREFIX) }.map { it.spotifyId.removePrefix(SHOW_PREFIX) }
+ val gate = kotlinx.coroutines.sync.Semaphore(PLAYLIST_PARALLEL)
+ val found = coroutineScope {
+ shows.map { id ->
+ async(Dispatchers.IO) { gate.withPermit { runCatching { api.podcasts.episodes("spotify:show:$id", 0, NEW_PER_SHOW).items }.getOrDefault(emptyList()) } }
+ }.map { it.await() }.flatten()
+ }
+ val list = found.map(::episodeSong).filter { !it.played && it.releaseDate.isNotBlank() }
+ .sortedByDescending { it.releaseDate }.take(NEW_EPISODES_MAX)
+ if (list.isNotEmpty()) newEpisodes = System.currentTimeMillis() to list
+ return list
}
/** The user's top artists. */
diff --git a/base/app/src/main/java/com/music/spotui/ui/immersive/pages/ImmersiveDiscoverScreens.kt b/base/app/src/main/java/com/music/spotui/ui/immersive/pages/ImmersiveDiscoverScreens.kt
index 2774853f..58628906 100644
--- a/base/app/src/main/java/com/music/spotui/ui/immersive/pages/ImmersiveDiscoverScreens.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/immersive/pages/ImmersiveDiscoverScreens.kt
@@ -170,6 +170,30 @@ fun ImmersiveEpisodesScreen(navController: NavController) {
}
}
+/** "New episodes": the episodes not played yet of the followed shows, newest first; a tap opens the episode. */
+@Composable
+fun ImmersiveNewEpisodesScreen(navController: NavController) {
+ val episodes by produceState<List<com.music.spotui.data.entity.SongsModel>?>(null) { value = Graph.api.getNewEpisodes() }
+ ImmersivePage(title = stringResource(R.string.new_episodes_title), onBack = { navController.popBackStack() }) {
+ val list = episodes
+ when {
+ list == null -> immersiveSongListSkeleton(count = 7)
+ list.isEmpty() -> item(key = "empty") { ImmersiveMessageState(stringResource(R.string.new_episodes_empty)) }
+ else -> items(list, key = { it.url }) { e ->
+ Column(immersiveItemPlacement()) {
+ ImmersiveListRow(
+ title = e.title,
+ subtitle = e.singer + " • " + e.releaseDate.take(10),
+ imageUrl = e.coverUri,
+ round = false,
+ onClick = { navController.navigate(com.music.spotui.ui.navigation.episodeRoute(e.url.removePrefix("episode:"), e.title)) },
+ )
+ }
+ }
+ }
+ }
+}
+
private fun episodeMinutesLabel(min: Long) = if (min >= 60) "${min / 60} h ${min % 60} min" else "$min min"
/**
diff --git a/base/app/src/main/java/com/music/spotui/ui/immersive/screens/ImmersiveLibraryScreen.kt b/base/app/src/main/java/com/music/spotui/ui/immersive/screens/ImmersiveLibraryScreen.kt
index 474cbdb7..5cbec0ff 100644
--- a/base/app/src/main/java/com/music/spotui/ui/immersive/screens/ImmersiveLibraryScreen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/immersive/screens/ImmersiveLibraryScreen.kt
@@ -446,7 +446,7 @@ internal fun Modifier.immersiveBleed(inset: Dp): Modifier = layout { measurable,
/** Albums, playlists and folders start loading under the finger. */
private fun prefetchLibraryEntry(entry: LibraryEntry) = when {
entry.isFolder || entry.spotifyId.startsWith(LOCAL_PLAYLIST_PREFIX) || entry.spotifyId.startsWith(Api.HomeCache.SHOW_PREFIX) -> Unit
- entry.spotifyId == Api.HomeCache.LOCAL_FILES_ID || entry.spotifyId == Api.HomeCache.DOWNLOADS_ID || entry.spotifyId == Api.HomeCache.EPISODES_ID -> Unit
+ entry.spotifyId == Api.HomeCache.LOCAL_FILES_ID || entry.spotifyId == Api.HomeCache.DOWNLOADS_ID || entry.spotifyId == Api.HomeCache.EPISODES_ID || entry.spotifyId == Api.HomeCache.NEW_EPISODES_ID -> Unit
entry.spotifyId == Api.HomeCache.LIKED_SONGS_ID -> ContextViewModel.prefetch("liked", "")
entry.isPlaylist -> ContextViewModel.prefetch("playlist", entry.spotifyId)
else -> ContextViewModel.prefetch("album", entry.spotifyId)
@@ -460,6 +460,7 @@ private fun openLibraryEntry(entry: LibraryEntry, navController: NavController)
else if (entry.spotifyId == Api.HomeCache.LOCAL_FILES_ID) navController.navigate(Routes.LocalFiles.route)
else if (entry.spotifyId == Api.HomeCache.LIKED_SONGS_ID) navController.navigate(Routes.Liked.route)
else if (entry.spotifyId == Api.HomeCache.EPISODES_ID) navController.navigate(Routes.Episodes.route)
+ else if (entry.spotifyId == Api.HomeCache.NEW_EPISODES_ID) navController.navigate(Routes.NewEpisodes.route)
else if (entry.isPlaylist) navController.navigate(playlistRoute(entry.spotifyId, entry.name))
else navController.navigate(albumRoute(entry.name, entry.artists, entry.spotifyId))
}
diff --git a/base/app/src/main/java/com/music/spotui/ui/navigation/MyNavHost.kt b/base/app/src/main/java/com/music/spotui/ui/navigation/MyNavHost.kt
index 956d3fbc..2b27be2d 100644
--- a/base/app/src/main/java/com/music/spotui/ui/navigation/MyNavHost.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/navigation/MyNavHost.kt
@@ -160,6 +160,14 @@ fun MyNavHost(
if (isImmersive()) com.music.spotui.ui.immersive.pages.ImmersiveEpisodesScreen(navHostController) else com.music.spotui.ui.screens.EpisodesScreen(navHostController)
}
+ page(Routes.NewEpisodes.route) {
+ LaunchedEffect(playerState) {
+ bottomBarState.value = true
+ bottomBarPlayerState.value = playerState != ""
+ }
+ if (isImmersive()) com.music.spotui.ui.immersive.pages.ImmersiveNewEpisodesScreen(navHostController) else com.music.spotui.ui.screens.NewEpisodesScreen(navHostController)
+ }
+
page(Routes.Recap.route) {
LaunchedEffect(playerState) {
bottomBarState.value = true
diff --git a/base/app/src/main/java/com/music/spotui/ui/navigation/Routes.kt b/base/app/src/main/java/com/music/spotui/ui/navigation/Routes.kt
index 862c43db..9ea85c08 100644
--- a/base/app/src/main/java/com/music/spotui/ui/navigation/Routes.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/navigation/Routes.kt
@@ -29,6 +29,7 @@ sealed class Routes(
object Category : Routes(0, "Category", "category")
object NewReleases : Routes(0, "NewReleases", "newreleases")
object Episodes : Routes(0, "Episodes", "episodes")
+ object NewEpisodes : Routes(0, "NewEpisodes", "newepisodes")
object Recap : Routes(0, "Recap", "recap")
object Recent : Routes(0, "Recent", "recent")
object Friends : Routes(0, "Friends", "friends")
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/DiscoverScreens.kt b/base/app/src/main/java/com/music/spotui/ui/screens/DiscoverScreens.kt
index eee97c84..760ed1a3 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/DiscoverScreens.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/DiscoverScreens.kt
@@ -211,6 +211,37 @@ fun EpisodesScreen(navController: NavController) {
}
}
+/** "New episodes": the episodes not played yet of the followed shows, newest first; a tap opens the episode. */
+@OptIn(ExperimentalGlideComposeApi::class)
+@Composable
+fun NewEpisodesScreen(navController: NavController) {
+ val episodes by produceState<List<com.music.spotui.data.entity.SongsModel>?>(null) { value = Graph.api.getNewEpisodes() }
+ Column(Modifier.fillMaxSize().background(Background).statusBarsPadding()) {
+ ScreenHeader(stringResource(R.string.new_episodes_title), navController)
+ when (val list = episodes) {
+ null -> Loading()
+ else -> if (list.isEmpty()) Text(stringResource(R.string.new_episodes_empty), color = Subtle, fontSize = 14.sp, modifier = Modifier.padding(20.dp))
+ else LazyColumn(contentPadding = PaddingValues(bottom = com.music.spotui.ui.components.bottomBarsSpace())) {
+ items(list, key = { it.url }) { e ->
+ Row(
+ verticalAlignment = Alignment.CenterVertically,
+ modifier = Modifier.fillMaxWidth().clickable {
+ navController.navigate(com.music.spotui.ui.navigation.episodeRoute(e.url.removePrefix("episode:"), e.title))
+ }.padding(16.dp, 8.dp),
+ ) {
+ GlideImage(transition = CrossFade, model = e.coverUri, contentDescription = null, contentScale = ContentScale.Crop,
+ modifier = Modifier.size(56.dp).clip(RoundedCornerShape(6.dp)))
+ Column(Modifier.padding(start = 12.dp).weight(1f)) {
+ Text(e.title, color = AppText, fontSize = 15.sp, maxLines = 2, overflow = TextOverflow.Ellipsis)
+ Text(e.singer + " • " + e.releaseDate.take(10), color = Subtle, fontSize = 12.sp, maxLines = 1, overflow = TextOverflow.Ellipsis)
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
@Composable
private fun formatMinutes(min: Long) = if (min >= 60) "${min / 60} h ${min % 60} min" else "$min min"
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/LibraryScreeen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/LibraryScreeen.kt
index 79b37536..dd3e087d 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/LibraryScreeen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/LibraryScreeen.kt
@@ -619,7 +619,7 @@ const val LOCAL_PLAYLIST_PREFIX = "local-playlist:"
private fun prefetchLibraryEntry(entry: LibraryEntry) = when {
LibraryEdits.isPending(entry.spotifyId) -> Unit
entry.isFolder || entry.spotifyId.startsWith(LOCAL_PLAYLIST_PREFIX) || entry.spotifyId.startsWith(Api.HomeCache.SHOW_PREFIX) -> Unit
- entry.spotifyId == Api.HomeCache.LOCAL_FILES_ID || entry.spotifyId == Api.HomeCache.DOWNLOADS_ID || entry.spotifyId == Api.HomeCache.EPISODES_ID -> Unit
+ entry.spotifyId == Api.HomeCache.LOCAL_FILES_ID || entry.spotifyId == Api.HomeCache.DOWNLOADS_ID || entry.spotifyId == Api.HomeCache.EPISODES_ID || entry.spotifyId == Api.HomeCache.NEW_EPISODES_ID -> Unit
entry.spotifyId == Api.HomeCache.LIKED_SONGS_ID -> ContextViewModel.prefetch("liked", "")
entry.isPlaylist -> ContextViewModel.prefetch("playlist", entry.spotifyId)
else -> ContextViewModel.prefetch("album", entry.spotifyId)
@@ -635,6 +635,7 @@ private fun openLibraryEntry(entry: LibraryEntry, navController: NavController)
else if (entry.spotifyId == Api.HomeCache.LOCAL_FILES_ID) navController.navigate(Routes.LocalFiles.route)
else if (entry.spotifyId == Api.HomeCache.LIKED_SONGS_ID) navController.navigate(Routes.Liked.route)
else if (entry.spotifyId == Api.HomeCache.EPISODES_ID) navController.navigate(Routes.Episodes.route)
+ else if (entry.spotifyId == Api.HomeCache.NEW_EPISODES_ID) navController.navigate(Routes.NewEpisodes.route)
else if (entry.isPlaylist) navController.navigate(playlistRoute(entry.spotifyId, entry.name))
else navController.navigate(albumRoute(entry.name, entry.artists, entry.spotifyId))
}
diff --git a/base/app/src/main/res/values/myspoti_strings.xml b/base/app/src/main/res/values/myspoti_strings.xml
index 17be21ac..dbccdf18 100644
--- a/base/app/src/main/res/values/myspoti_strings.xml
+++ b/base/app/src/main/res/values/myspoti_strings.xml
@@ -134,6 +134,9 @@
<string name="new_releases_latest">Latest releases</string>
<string name="your_episodes_title">Your Episodes</string>
<string name="your_episodes_empty">Episodes you save show up here.</string>
+ <string name="new_episodes_title">New episodes</string>
+ <string name="new_episodes_subtitle">From the shows you follow</string>
+ <string name="new_episodes_empty">Every recent episode of the shows you follow is played.</string>
<string name="recap_title">Your recap</string>
<string name="recap_month">30 days</string>
<string name="recap_year">12 months</string>