[gnome-music/wip/mschraal/playlists-fixes: 5/6] grltrackerplaylists: Resort Most Played



commit aef8dd9b18ef1bb0a4eab61961255bd88e21238b
Author: Marinus Schraal <mschraal gnome org>
Date:   Fri Feb 21 12:19:38 2020 +0100

    grltrackerplaylists: Resort Most Played
    
    The smart playlists do not get resorted after changes, which is to be
    expected for playlists like 'Most Played' and 'Recently Played'. The
    initial sorting is done by the Tracker query, but this does not resort
    on later updates.
    
    This commit adds a SortListModel to SmartPlaylist, which resorts on
    changes as expected. The default sorting method is to do nothing and
    depend on the initial Tracker sorting.
    
    For 'Most Played' add a playcount sorter.
    
    Note that this is an incomplete fix: it is now possible to enter a
    cyclical play loop when songs get sorted and switch places, as this is
    propagated to the PlayerPlaylist model.

 gnomemusic/grilowrappers/grltrackerplaylists.py | 39 +++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 3 deletions(-)
---
diff --git a/gnomemusic/grilowrappers/grltrackerplaylists.py b/gnomemusic/grilowrappers/grltrackerplaylists.py
index 0573a6cd..3a941af3 100644
--- a/gnomemusic/grilowrappers/grltrackerplaylists.py
+++ b/gnomemusic/grilowrappers/grltrackerplaylists.py
@@ -27,8 +27,8 @@ import time
 from gettext import gettext as _
 
 import gi
-gi.require_versions({"Grl": "0.3"})
-from gi.repository import Gio, Grl, GLib, GObject
+gi.require_versions({"Gfm": "0.1", "Grl": "0.3"})
+from gi.repository import Gfm, Gio, Grl, GLib, GObject
 
 from gnomemusic.coresong import CoreSong
 import gnomemusic.utils as utils
@@ -732,11 +732,18 @@ class SmartPlaylist(Playlist):
         super().__init__(**args)
 
         self.props.is_smart = True
+        self._filter_model = None
+
+    def _playlist_sort(self, coresong_a, coresong_b):
+        return 0
 
     @GObject.Property(type=Gio.ListStore, default=None)
     def model(self):
         if self._model is None:
             self._model = Gio.ListStore.new(CoreSong)
+            self._filter_model = Gfm.SortListModel.new(self._model)
+            self._filter_model.set_sort_func(
+                utils.wrap_list_store_sort_func(self._playlist_sort))
 
             self._window.notifications_popup.push_loading()
 
@@ -762,7 +769,7 @@ class SmartPlaylist(Playlist):
             self._source.query(
                 self.props.query, self.METADATA_KEYS, options, _add_to_model)
 
-        return self._model
+        return self._filter_model
 
     def update(self):
         """Updates playlist model."""
@@ -810,6 +817,8 @@ class SmartPlaylist(Playlist):
                 self._model.append(coresong)
                 self.props.count += 1
 
+        self._filter_model.resort()
+
 
 class MostPlayed(SmartPlaylist):
     """Most Played smart playlist"""
@@ -847,6 +856,30 @@ class MostPlayed(SmartPlaylist):
             "location_filter": self._tracker_wrapper.location_filter()
         }
 
+    def _playlist_sort(self, coresong_a, coresong_b):
+        playcount_a = coresong_a.props.play_count
+        playcount_b = coresong_b.props.play_count
+
+        if playcount_a != playcount_b:
+            return coresong_a.props.play_count < coresong_b.props.play_count
+
+        title_a = coresong_a.props.title
+        title_b = coresong_b.props.title
+        title_cmp = utils.natural_sort_names(title_a, title_b)
+        if title_cmp != 0:
+            return title_cmp
+
+        album_a = coresong_a.props.album
+        album_b = coresong_b.props.album
+        album_cmp = utils.natural_sort_names(album_a, album_b)
+        if album_cmp != 0:
+            return album_cmp
+
+        artist_a = coresong_a.props.artist
+        artist_b = coresong_b.props.artist
+
+        return utils.natural_sort_names(artist_a, artist_b)
+
 
 class NeverPlayed(SmartPlaylist):
     """Never Played smart playlist"""


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