Repository: aome510/spotify-player (language: rust)

Issue:
Sort tracks by added date does not work
**Describe the bug**
SortTrackByAddedDate does not work. 

**To Reproduce**
1. Navigate to a playlist (happens on any playlist for me)
2. Sort playlist by duration(or anything that isn't date related)
3. Sort playlist by added date
4. Nothing changes

**Expected behaviour**
Sort by added date

**Log and backtrace**
[spotifyplayerlog.txt](https://github.com/user-attachments/files/18054486/spotifyplayerlog.txt)
nothing in backtrace

**Screenshots**
[2024-12-08 18-01-34-[00.00.000-00.07.867]-audio.webm](https://github.com/user-attachments/assets/e636185d-d4f2-4d22-b5e3-151dcd39170b)

**Environment**
- OS: Arch
- Application version: v0.20.3
- Application features: Built with notify and image



Discussion / hints:

Fix patch:
diff --git a/spotify_player/src/client/mod.rs b/spotify_player/src/client/mod.rs
index bc57f1f4..86f7150d 100644
--- a/spotify_player/src/client/mod.rs
+++ b/spotify_player/src/client/mod.rs
@@ -1307,12 +1307,7 @@ impl Client {
             .all_paging_items(first_page, &market_query())
             .await?
             .into_iter()
-            .filter_map(|item| match item.track {
-                Some(rspotify::model::PlayableItem::Track(track)) => {
-                    Track::try_from_full_track(track)
-                }
-                _ => None,
-            })
+            .filter_map(Track::try_from_playlist_item)
             .collect::<Vec<_>>();
 
         Ok(Context::Playlist {
diff --git a/spotify_player/src/state/model.rs b/spotify_player/src/state/model.rs
index 78245eaa..c0f2cb22 100644
--- a/spotify_player/src/state/model.rs
+++ b/spotify_player/src/state/model.rs
@@ -338,8 +338,11 @@ impl Track {
         }
     }
 
-    /// tries to convert from a `rspotify::model::FullTrack` into `Track`
-    pub fn try_from_full_track(track: rspotify::model::FullTrack) -> Option<Self> {
+    /// tries to convert from a `rspotify::model::FullTrack` into `Track` with a optional added_at date
+    fn try_from_full_track_with_date(
+        track: rspotify::model::FullTrack,
+        added_at: Option<chrono::DateTime<chrono::Utc>>,
+    ) -> Option<Self> {
         if track.is_playable.unwrap_or(true) {
             let id = match track.linked_from {
                 Some(d) => d.id?,
@@ -352,12 +355,26 @@ impl Track {
                 album: Album::try_from_simplified_album(track.album),
                 duration: track.duration.to_std().expect("valid chrono duration"),
                 explicit: track.explicit,
-                added_at: 0,
+                added_at: added_at.map(|t| t.timestamp() as u64).unwrap_or_default(),
             })
         } else {
             None
         }
     }
+
+    /// tries to convert from a `rspotify::model::FullTrack` into `Track`
+    pub fn try_from_full_track(track: rspotify::model::FullTrack) -> Option<Self> {
+        Track::try_from_full_track_with_date(track, None)
+    }
+
+    /// tries to convert from a `rspotify::model::PlaylistItem` into `Track`
+    pub fn try_from_playlist_item(item: rspotify::model::PlaylistItem) -> Option<Self> {
+        let rspotify::model::PlayableItem::Track(track) = item.track? else {
+            return None;
+        };
+
+        Track::try_from_full_track_with_date(track, item.added_at)
+    }
 }
 
 impl std::fmt::Display for Track {


Test patch: