[gnome-music/wip/jfelder/smartplaylist-update-sorted] grltrackerplaylists: Improve smart playlists updates




commit a6ec6d80adc67c0144adb541ff4e150e7dbac8aa
Author: Jean Felder <jfelder src gnome org>
Date:   Sun Mar 1 14:22:35 2020 +0100

    grltrackerplaylists: Improve smart playlists updates
    
    The existing update method removes the songs which are not part of the
    playlist anymore and adds the new songs at the end of the model.
    
    This behavior assumes that the position of an existing song does not
    change on update. This is wrong for most of the smart playlists (for
    example, the MostPlayed playlist).
    
    This issue is fixed by removing the existing songs and adding the new
    ones in one go by a splice call. If a song was already part of the
    playlist, its state is preserved.

 gnomemusic/grilowrappers/grltrackerplaylists.py | 27 ++++++++++++-------------
 1 file changed, 13 insertions(+), 14 deletions(-)
---
diff --git a/gnomemusic/grilowrappers/grltrackerplaylists.py b/gnomemusic/grilowrappers/grltrackerplaylists.py
index 720c541ca..57eebb6b5 100644
--- a/gnomemusic/grilowrappers/grltrackerplaylists.py
+++ b/gnomemusic/grilowrappers/grltrackerplaylists.py
@@ -797,23 +797,22 @@ class SmartPlaylist(Playlist):
 
         current_models_ids = [coresong.props.media.get_id()
                               for coresong in self._model]
-        new_model_ids = [media.get_id() for media in new_model_medias]
 
-        idx_to_delete = []
-        for idx, media_id in enumerate(current_models_ids):
-            if media_id not in new_model_ids:
-                idx_to_delete.insert(0, idx)
+        new_songs = []
+        for media in new_model_medias:
+            coresong = CoreSong(self._application, media)
+            new_songs.append(coresong)
+            try:
+                idx = current_models_ids.index(media.get_id())
+            except ValueError:
+                continue
 
-        for idx in idx_to_delete:
-            self._model.remove(idx)
-            self.props.count -= 1
+            current_song = self._model[idx]
+            coresong.props.state = current_song.props.state
+            coresong.props.validation = current_song.props.validation
 
-        for idx, media in enumerate(new_model_medias):
-            if media.get_id() not in current_models_ids:
-                coresong = CoreSong(self._application, media)
-                self._bind_to_main_song(coresong)
-                self._model.append(coresong)
-                self.props.count += 1
+        self._model.splice(0, self._model.get_n_items(), new_songs)
+        self.props.count = len(new_songs)
 
 
 class MostPlayed(SmartPlaylist):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]