Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix playlist loading #690

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ bbb651
Julius Rüberg
janbrummer
Alisson Lauffer
Riley Smith
8 changes: 6 additions & 2 deletions src/api/api_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ trait WithImages {
pub struct Playlist {
pub id: String,
pub name: String,
pub images: Vec<Image>,
pub images: Option<Vec<Image>>,
pub tracks: Page<PlaylistTrack>,
pub owner: PlaylistOwner,
}
Expand All @@ -199,7 +199,11 @@ pub struct PlaylistOwner {

impl WithImages for Playlist {
fn images(&self) -> &[Image] {
&self.images[..]
if let Some(ref images) = self.images {
images
} else {
&[]
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,10 @@ impl SpotifyClient {

pub(crate) fn get_playlist(&self, id: &str) -> SpotifyRequest<'_, (), Playlist> {
let query = make_query_params()
.append_pair(
"fields",
"id,name,images,owner,tracks(total,items(is_local,track(name,id,uri,duration_ms,artists(name,id),album(name,id,images,artists))))",
)
.append_pair("market", "from_token")
// why still grab the tracks field?
// the model still expects the appearance of a tracks field
.append_pair("fields", "id,name,images,owner,tracks(total)")
.finish();
self.request()
.method(Method::GET)
Expand All @@ -404,6 +404,7 @@ impl SpotifyClient {
limit: usize,
) -> SpotifyRequest<'_, (), Page<PlaylistTrack>> {
let query = make_query_params()
.append_pair("market", "from_token")
.append_pair("offset", &offset.to_string()[..])
.append_pair("limit", &limit.to_string()[..])
.finish();
Expand Down
21 changes: 18 additions & 3 deletions src/app/components/playlist_details/playlist_details_model.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use gettextrs::gettext;
use gio::prelude::*;
use gio::SimpleActionGroup;
use std::cell::Ref;
Expand Down Expand Up @@ -62,6 +63,17 @@ impl PlaylistDetailsModel {
self.dispatcher
.dispatch(AppAction::PlaybackAction(PlaybackAction::ToggleShuffle));
}
// The playlist has no songs and the user has still decided to click the play button,
// lets just do an early return and show an error...
if playlist.songs.songs.is_empty() {
error!("Unable to start playback because songs is empty");
self.dispatcher
.dispatch(AppAction::ShowNotification(gettext(
"An error occured. Check logs for details!",
)));
return;
}

let id_of_first_song = playlist.songs.songs[0].id.as_str();
self.play_song_at(0, id_of_first_song);
return;
Expand All @@ -82,10 +94,13 @@ impl PlaylistDetailsModel {
self.dispatcher
.call_spotify_and_dispatch(move || async move {
let playlist = api.get_playlist(&id).await;
let playlist_tracks = api.get_playlist_tracks(&id, 0, 100).await?;
match playlist {
Ok(playlist) => {
Ok(BrowserAction::SetPlaylistDetails(Box::new(playlist)).into())
}
Ok(playlist) => Ok(BrowserAction::SetPlaylistDetails(
Box::new(playlist),
Box::new(playlist_tracks),
)
.into()),
Err(SpotifyApiError::BadStatus(400, _))
| Err(SpotifyApiError::BadStatus(404, _)) => {
Ok(BrowserAction::NavigationPop.into())
Expand Down
2 changes: 1 addition & 1 deletion src/app/state/browser_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum BrowserAction {
RemoveTracksFromPlaylist(String, Vec<String>),
SetAlbumDetails(Box<AlbumFullDescription>),
AppendAlbumTracks(String, Box<SongBatch>),
SetPlaylistDetails(Box<PlaylistDescription>),
SetPlaylistDetails(Box<PlaylistDescription>, Box<SongBatch>),
UpdatePlaylistName(PlaylistSummary),
AppendPlaylistTracks(String, Box<SongBatch>),
Search(String),
Expand Down
6 changes: 3 additions & 3 deletions src/app/state/screen_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ impl UpdatableState for PlaylistDetailsState {

fn update_with(&mut self, action: Cow<Self::Action>) -> Vec<Self::Event> {
match action.as_ref() {
BrowserAction::SetPlaylistDetails(playlist) if playlist.id == self.id => {
let PlaylistDescription { id, songs, .. } = *playlist.clone();
self.songs.add(songs).commit();
BrowserAction::SetPlaylistDetails(playlist, song_batch) if playlist.id == self.id => {
let PlaylistDescription { id, .. } = *playlist.clone();
self.songs.add(*song_batch.clone()).commit();
self.playlist = Some(*playlist.clone());
vec![BrowserEvent::PlaylistDetailsLoaded(id)]
}
Expand Down
Loading