[gnome-music/wip/mschraal/playlists-fixes: 3/3] grltrackerplaylists: Sort most played



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

    grltrackerplaylists: Sort most played

 gnomemusic/coregrilo.py                         |  2 ++
 gnomemusic/grilowrappers/grltrackerplaylists.py | 32 ++++++++++++++++++++++---
 2 files changed, 31 insertions(+), 3 deletions(-)
---
diff --git a/gnomemusic/coregrilo.py b/gnomemusic/coregrilo.py
index 3fcdb3ec..83887abb 100644
--- a/gnomemusic/coregrilo.py
+++ b/gnomemusic/coregrilo.py
@@ -182,6 +182,8 @@ class CoreGrilo(GObject.GObject):
             if failed_keys:
                 self._log.warning("Unable to update {}".format(failed_keys))
 
+            self._coremodel.emit("smart-playlist-change")
+
         for wrapper in self._wrappers.values():
             if media.get_source() == wrapper.source.props.source_id:
                 wrapper.props.source.store_metadata(
diff --git a/gnomemusic/grilowrappers/grltrackerplaylists.py b/gnomemusic/grilowrappers/grltrackerplaylists.py
index c5f2b2bf..e44c07b0 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
@@ -730,11 +730,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()
 
@@ -760,7 +767,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."""
@@ -808,6 +815,8 @@ class SmartPlaylist(Playlist):
                 self._model.append(coresong)
                 self.props.count += 1
 
+        self._filter_model.resort()
+
 
 class MostPlayed(SmartPlaylist):
     """Most Played smart playlist"""
@@ -845,6 +854,23 @@ class MostPlayed(SmartPlaylist):
             "location_filter": self._tracker_wrapper.location_filter()
         }
 
+    def play_count_sort(coresong_a, coresong_b):
+        playcount_a = coresong_a.props.play_count
+        playcount_b = coresong_b.props.play_count
+
+        if playcount_a == playcount_b:
+            return utils.natural_sort_names(
+                coresong_a.props.title, coresong_b.props.title)
+
+        return coresong_a.props.play_count < coresong_b.props.play_count
+
+    def _finish_update(self, new_model_medias):
+        super()._finish_update(new_model_medias)
+
+        # NOTE: Debug print.
+        for idx, coresong in enumerate(self._filter_model):
+            print(idx, coresong.props.play_count, coresong.props.title)
+
 
 class NeverPlayed(SmartPlaylist):
     """Never Played smart playlist"""


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