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



commit 87534c7a95a848b551f460fec799fe968bc70800
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, state and validation properties are preserved.

 gnomemusic/grilowrappers/grltrackerplaylists.py | 26 ++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
---
diff --git a/gnomemusic/grilowrappers/grltrackerplaylists.py b/gnomemusic/grilowrappers/grltrackerplaylists.py
index 3aa09f2a..25b16cfb 100644
--- a/gnomemusic/grilowrappers/grltrackerplaylists.py
+++ b/gnomemusic/grilowrappers/grltrackerplaylists.py
@@ -771,22 +771,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(media, self._coreselection, self._grilo)
+            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(media, self._coreselection, self._grilo)
-                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]