Improve album loading in artist view (#446)

* Load all albums in artist view

* Fetch multiple albums at a time
This commit is contained in:
algon
2021-02-26 05:24:10 +09:00
committed by GitHub
parent db894d7ce8
commit dfb60ee4be
2 changed files with 27 additions and 8 deletions

View File

@@ -43,23 +43,32 @@ impl Artist {
}
if let Some(ref artist_id) = self.id {
if let Some(sas) = spotify.artist_albums(artist_id, 50, 0) {
let mut albums: Vec<Album> = Vec::new();
let mut collected_ids: Vec<String> = 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 {
if Some("appears_on".into()) == sa.album_group {
if Some("appears_on") == sa.album_group.as_deref() {
continue;
}
if let Some(album_id) = sa.id {
if let Some(fa) = spotify.full_album(&album_id).as_ref() {
albums.push(fa.into());
}
collected_ids.push(album_id);
}
}
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);
}
}

View File

@@ -653,6 +653,16 @@ impl Spotify {
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> {
self.api_with_retry(|api| api.artist(artist_id))
}