Skip to content

Commit 68824ac

Browse files
authored
fix: playlist sorting by date (#634)
1 parent ff4eaac commit 68824ac

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

spotify_player/src/client/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,12 +1307,7 @@ impl Client {
13071307
.all_paging_items(first_page, &market_query())
13081308
.await?
13091309
.into_iter()
1310-
.filter_map(|item| match item.track {
1311-
Some(rspotify::model::PlayableItem::Track(track)) => {
1312-
Track::try_from_full_track(track)
1313-
}
1314-
_ => None,
1315-
})
1310+
.filter_map(Track::try_from_playlist_item)
13161311
.collect::<Vec<_>>();
13171312

13181313
Ok(Context::Playlist {

spotify_player/src/state/model.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,11 @@ impl Track {
338338
}
339339
}
340340

341-
/// tries to convert from a `rspotify::model::FullTrack` into `Track`
342-
pub fn try_from_full_track(track: rspotify::model::FullTrack) -> Option<Self> {
341+
/// tries to convert from a `rspotify::model::FullTrack` into `Track` with a optional added_at date
342+
fn try_from_full_track_with_date(
343+
track: rspotify::model::FullTrack,
344+
added_at: Option<chrono::DateTime<chrono::Utc>>,
345+
) -> Option<Self> {
343346
if track.is_playable.unwrap_or(true) {
344347
let id = match track.linked_from {
345348
Some(d) => d.id?,
@@ -352,12 +355,26 @@ impl Track {
352355
album: Album::try_from_simplified_album(track.album),
353356
duration: track.duration.to_std().expect("valid chrono duration"),
354357
explicit: track.explicit,
355-
added_at: 0,
358+
added_at: added_at.map(|t| t.timestamp() as u64).unwrap_or_default(),
356359
})
357360
} else {
358361
None
359362
}
360363
}
364+
365+
/// tries to convert from a `rspotify::model::FullTrack` into `Track`
366+
pub fn try_from_full_track(track: rspotify::model::FullTrack) -> Option<Self> {
367+
Track::try_from_full_track_with_date(track, None)
368+
}
369+
370+
/// tries to convert from a `rspotify::model::PlaylistItem` into `Track`
371+
pub fn try_from_playlist_item(item: rspotify::model::PlaylistItem) -> Option<Self> {
372+
let rspotify::model::PlayableItem::Track(track) = item.track? else {
373+
return None;
374+
};
375+
376+
Track::try_from_full_track_with_date(track, item.added_at)
377+
}
361378
}
362379

363380
impl std::fmt::Display for Track {

0 commit comments

Comments
 (0)