feat: cover art cache, Collection tabs, and album, artist and playlist pages
- ym-api: album/artist/playlist summaries and pages, liked albums and artists, own and liked playlists (batch-resolved from like ids), playlist tracks with missing ones looked up, image download - Cover cache: fetched when a cover nears the viewport (sensor), at most eight at once, kept in memory and on disk; arrivals are batched every 150 ms so cached lists rebuild once per batch - Collection tabs for liked tracks, albums, artists and playlists, each loading on first visit with a retry on failure - Album, Artist and Playlist routes with cover headers, Play/Shuffle and track lists; pages cached per session; sidebar lists playlists - Shared lazy track list keyed on contents, playing track, likes and cover version; card grid that wraps
This commit is contained in:
1 parent
b73af1f870
commit
3b934df8cc
22 files changed
+1773
-247
No files matched your search
@@ -1,6 +1,10 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::auth::Credentials;
|
||||
use crate::library::{
|
||||
AlbumPage, AlbumSummary, ArtistPage, ArtistSummary, LibraryPlaylists, PlaylistId, PlaylistPage,
|
||||
PlaylistSummary,
|
||||
};
|
||||
use crate::models::{Stream, Track, best_variant};
|
||||
use crate::{Error, Result};
|
||||
|
||||
@@ -118,6 +122,102 @@ impl YmClient {
|
||||
Ok(bytes.into())
|
||||
}
|
||||
|
||||
/// Albums the user liked, newest first, with full details.
|
||||
pub async fn liked_albums(&self) -> Result<Vec<AlbumSummary>> {
|
||||
let likes = self.inner.liked_albums(None).await?;
|
||||
let ids: Vec<i64> = likes
|
||||
.into_iter()
|
||||
.filter_map(|like| like.album.and_then(|a| a.id).or_else(|| like.id.and_then(|i| i.as_number())))
|
||||
.collect();
|
||||
if ids.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let albums = self.inner.albums(&ids).await?;
|
||||
Ok(albums.into_iter().filter_map(AlbumSummary::from_yamuse).collect())
|
||||
}
|
||||
|
||||
pub async fn liked_artists(&self) -> Result<Vec<ArtistSummary>> {
|
||||
let artists = self.inner.liked_artists(None).await?;
|
||||
Ok(artists.into_iter().filter_map(ArtistSummary::from_yamuse).collect())
|
||||
}
|
||||
|
||||
/// Playlists the user owns, and ones they liked.
|
||||
pub async fn library_playlists(&self) -> Result<LibraryPlaylists> {
|
||||
let own = self.inner.user_playlists_list(None).await?;
|
||||
let likes = self.inner.liked_playlists(None).await?;
|
||||
let liked_ids: Vec<String> = likes
|
||||
.iter()
|
||||
.filter_map(|like| {
|
||||
let p = like.playlist.as_ref()?;
|
||||
let owner = p.uid.or_else(|| p.owner.as_ref().and_then(|o| o.uid))?;
|
||||
Some(format!("{owner}:{}", p.kind?))
|
||||
})
|
||||
.collect();
|
||||
let liked = if liked_ids.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
self.inner.playlists_list(&liked_ids).await?
|
||||
};
|
||||
Ok(LibraryPlaylists {
|
||||
own: own.iter().filter_map(PlaylistSummary::from_yamuse).collect(),
|
||||
liked: liked.iter().filter_map(PlaylistSummary::from_yamuse).collect(),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn album(&self, id: i64) -> Result<AlbumPage> {
|
||||
let album = self.inner.album_with_tracks(id).await?;
|
||||
AlbumPage::from_yamuse(album).ok_or_else(|| Error::Api(format!("album {id} has no id")))
|
||||
}
|
||||
|
||||
pub async fn artist(&self, id: i64) -> Result<ArtistPage> {
|
||||
let brief = self.inner.artist_brief_info(id).await?;
|
||||
ArtistPage::from_yamuse(brief).ok_or_else(|| Error::Api(format!("artist {id} not found")))
|
||||
}
|
||||
|
||||
/// A playlist with all its tracks. Tracks the response didn't embed are looked up.
|
||||
pub async fn playlist(&self, id: PlaylistId) -> Result<PlaylistPage> {
|
||||
let raw = self.inner.user_playlist(id.kind, Some(id.owner)).await?;
|
||||
let playlist = PlaylistSummary::from_yamuse(&raw)
|
||||
.ok_or_else(|| Error::Api(format!("playlist {}:{} incomplete", id.owner, id.kind)))?;
|
||||
|
||||
let mut tracks: Vec<Option<Track>> = Vec::with_capacity(raw.tracks.len());
|
||||
let mut missing = Vec::new();
|
||||
for (i, short) in raw.tracks.into_iter().enumerate() {
|
||||
match short.track.and_then(Track::from_yamuse) {
|
||||
Some(track) => tracks.push(Some(track)),
|
||||
None => {
|
||||
if let Some(id) = short.id {
|
||||
let id = match id {
|
||||
yamuse::models::Id::Number(n) => n.to_string(),
|
||||
yamuse::models::Id::Text(s) => s,
|
||||
};
|
||||
missing.push((i, id));
|
||||
}
|
||||
tracks.push(None);
|
||||
}
|
||||
}
|
||||
}
|
||||
if !missing.is_empty() {
|
||||
let fetched = self.tracks(missing.iter().map(|(_, id)| id.clone()).collect()).await?;
|
||||
for ((slot, _), track) in missing.iter().zip(fetched) {
|
||||
tracks[*slot] = Some(track);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(PlaylistPage {
|
||||
playlist,
|
||||
description: raw.description.filter(|d| !d.trim().is_empty()),
|
||||
tracks: tracks.into_iter().flatten().collect(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Downloads an image (covers are a few kilobytes).
|
||||
pub async fn image(&self, url: &str) -> Result<Vec<u8>> {
|
||||
Ok(yamuse::download::Downloader::new(self.inner.transport(), [url.to_owned()])
|
||||
.to_bytes()
|
||||
.await?)
|
||||
}
|
||||
|
||||
/// Re-reads the account, e.g. after a subscription may have changed.
|
||||
pub async fn account(&self) -> Result<Account> {
|
||||
let status = self.inner.refresh_account().await?;
|
||||
|
||||
Reference in new issue
Block a user