Skip to content

Commit

Permalink
fix: playlists not loading
Browse files Browse the repository at this point in the history
This fix is extremely scuffed, as Spotify's API will not return filtered fields when multiple complex objects are in the same query. This seems to be a regression from previous behavior...
fix: playlist not loading if the image field is null
  • Loading branch information
QPixel authored and Diegovsky committed Jan 17, 2024
1 parent 5bab879 commit 9f44cfb
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
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
2 changes: 1 addition & 1 deletion src/api/api_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub struct PlaylistOwner {

impl WithImages for Playlist {
fn images(&self) -> &[Image] {
&self.images[..]
&self.images
}
}

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

0 comments on commit 9f44cfb

Please sign in to comment.