Improve album loading in artist view (#446)
* Load all albums in artist view * Fetch multiple albums at a time
This commit is contained in:
@@ -43,23 +43,32 @@ impl Artist {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref artist_id) = self.id {
|
if let Some(ref artist_id) = self.id {
|
||||||
if let Some(sas) = spotify.artist_albums(artist_id, 50, 0) {
|
let mut collected_ids: Vec<String> = Vec::new();
|
||||||
let mut albums: Vec<Album> = Vec::new();
|
let mut offset = 0;
|
||||||
|
while let Some(sas) = spotify.artist_albums(artist_id, 50, offset) {
|
||||||
|
let items_len = sas.items.len() as u32;
|
||||||
|
debug!("got {} albums", items_len);
|
||||||
|
|
||||||
for sa in sas.items {
|
for sa in sas.items {
|
||||||
if Some("appears_on".into()) == sa.album_group {
|
if Some("appears_on") == sa.album_group.as_deref() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(album_id) = sa.id {
|
if let Some(album_id) = sa.id {
|
||||||
if let Some(fa) = spotify.full_album(&album_id).as_ref() {
|
collected_ids.push(album_id);
|
||||||
albums.push(fa.into());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.albums = Some(albums);
|
match sas.next {
|
||||||
|
Some(_) => offset += items_len,
|
||||||
|
None => break,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let albums = match spotify.albums(&collected_ids) {
|
||||||
|
Some(fas) => fas.iter().map(Album::from).collect(),
|
||||||
|
None => Vec::new(),
|
||||||
|
};
|
||||||
|
self.albums = Some(albums);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -653,6 +653,16 @@ impl Spotify {
|
|||||||
self.api_with_retry(|api| api.album(album_id))
|
self.api_with_retry(|api| api.album(album_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn albums(&self, album_ids: &[String]) -> Option<Vec<FullAlbum>> {
|
||||||
|
const MAX_SIZE: usize = 20;
|
||||||
|
let mut collected = Vec::new();
|
||||||
|
for ids in album_ids.chunks(MAX_SIZE) {
|
||||||
|
let fas = self.api_with_retry(|api| api.albums(ids.to_vec()))?;
|
||||||
|
collected.extend_from_slice(&fas.albums);
|
||||||
|
}
|
||||||
|
Some(collected)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn artist(&self, artist_id: &str) -> Option<FullArtist> {
|
pub fn artist(&self, artist_id: &str) -> Option<FullArtist> {
|
||||||
self.api_with_retry(|api| api.artist(artist_id))
|
self.api_with_retry(|api| api.artist(artist_id))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user