[gnome-music/wip/mschraal/coredisc-tracker-cleanup: 66/66] coredisc: Fill disc model in source




commit 424445aff0054a85ad75496a257b48aef9c40b39
Author: Marinus Schraal <mschraal gnome org>
Date:   Fri Aug 20 17:50:04 2021 +0200

    coredisc: Fill disc model in source
    
    In line with the other source functions, pass the model to a source to
    get filled instead of handling the callback in CoreDisc.

 gnomemusic/coredisc.py                        | 26 +--------------------
 gnomemusic/coregrilo.py                       | 15 ++++++------
 gnomemusic/grilowrappers/grltrackerwrapper.py | 33 +++++++++++++++++++--------
 3 files changed, 32 insertions(+), 42 deletions(-)
---
diff --git a/gnomemusic/coredisc.py b/gnomemusic/coredisc.py
index 697089a65..31eb222fe 100644
--- a/gnomemusic/coredisc.py
+++ b/gnomemusic/coredisc.py
@@ -47,7 +47,6 @@ class CoreDisc(GObject.GObject):
         self._filter_model = None
         self._log = application.props.log
         self._model = None
-        self._old_album_ids = []
         self._selected = False
 
         self.update(media)
@@ -71,7 +70,7 @@ class CoreDisc(GObject.GObject):
 
             self._model.connect("items-changed", self._on_disc_changed)
 
-            self._get_album_disc(
+            self._coregrilo.get_album_disc(
                 self.props.media, self.props.disc_nr, self._filter_model)
 
         return self._model
@@ -85,29 +84,6 @@ class CoreDisc(GObject.GObject):
 
             self.props.duration = duration
 
-    def _get_album_disc(self, media, discnr, model):
-        album_ids = []
-        model_filter = model
-
-        def _filter_func(core_song):
-            return core_song.props.grlid in album_ids
-
-        def _callback(source, op_id, media, remaining, error):
-            if error:
-                self._log.warning("Error: {}".format(error))
-                return
-
-            if media is None:
-                if sorted(album_ids) == sorted(self._old_album_ids):
-                    return
-                model_filter.set_filter_func(_filter_func)
-                self._old_album_ids = album_ids
-                return
-
-            album_ids.append(media.get_source() + media.get_id())
-
-        self._coregrilo.populate_album_disc_songs(media, discnr, _callback)
-
     @GObject.Property(
         type=bool, default=False, flags=GObject.BindingFlags.SYNC_CREATE)
     def selected(self):
diff --git a/gnomemusic/coregrilo.py b/gnomemusic/coregrilo.py
index 99368882f..002d5b921 100644
--- a/gnomemusic/coregrilo.py
+++ b/gnomemusic/coregrilo.py
@@ -25,8 +25,8 @@
 import weakref
 
 import gi
-gi.require_version('Grl', '0.3')
-from gi.repository import Grl, GLib, GObject
+gi.require_versions({"Grl": "0.3", "Gfm": "0.1"})
+from gi.repository import Grl, GLib, GObject, Gfm
 
 from gnomemusic.grilowrappers.grlsearchwrapper import GrlSearchWrapper
 from gnomemusic.grilowrappers.grltrackerwrapper import GrlTrackerWrapper
@@ -192,16 +192,17 @@ class CoreGrilo(GObject.GObject):
         source = media.get_source()
         self._wrappers[source].get_album_discs(media, disc_model)
 
-    def populate_album_disc_songs(self, media, discnr, callback):
+    def get_album_disc(
+            self, media: Grl.Media, discnr: int,
+            model: Gfm.FilterListModel) -> None:
         """Get all songs from an album disc
 
-        :param Grl.Media media: A Grilo Media item that represents Album
+        :param Grl.Media media: An album
         :param int discnr: The disc number
-        :param callback: The callback to call for every song added
+        :param Gfm.FilterListModel model: The model to fill
         """
         source = media.get_source()
-        self._wrappers[source].populate_album_disc_songs(
-            media, discnr, callback)
+        self._wrappers[source].get_album_disc(media, discnr, model)
 
     def writeback(self, media, key):
         """Store the values associated with the key.
diff --git a/gnomemusic/grilowrappers/grltrackerwrapper.py b/gnomemusic/grilowrappers/grltrackerwrapper.py
index 7567275a4..a0a385fe7 100644
--- a/gnomemusic/grilowrappers/grltrackerwrapper.py
+++ b/gnomemusic/grilowrappers/grltrackerwrapper.py
@@ -830,21 +830,16 @@ class GrlTrackerWrapper(GObject.GObject):
             query, [Grl.METADATA_KEY_ALBUM_DISC_NUMBER], self._fast_options,
             _disc_nr_cb)
 
-    def populate_album_disc_songs(
+    def get_album_disc(
             self, media: Grl.Media, disc_nr: int,
-            callback: Callable[
-                [Grl.Source, int, Optional[Grl.Media], int,
-                 Optional[GLib.Error]], None]) -> None:
-        # FIXME: Pass a model and fill it.
-        # FIXME: The query is similar to the other song queries, reuse
-        # if possible.
+            model: Gfm.FilterListModel) -> None:
         """Get all songs of an album disc
 
         :param Grl.Media media: The media with the album id
         :param int disc_nr: The disc number
-        :param callback: The callback to call for every song added
+        :param Gfm.FilterListModel model: The model to fill
         """
-        album_id: str = media.get_id()
+        album_id = media.get_id()
 
         query = """
         SELECT
@@ -908,8 +903,26 @@ class GrlTrackerWrapper(GObject.GObject):
             Grl.METADATA_KEY_URL
         ]
 
+        disc_song_ids: List[int] = []
+
+        def _filter_func(coresong: CoreSong) -> bool:
+            return coresong.props.grlid in disc_song_ids
+
+        def _callback(
+                source: Grl.Source, op_id: int, media: Grl.Media,
+                remaining: int, error: GLib.Error) -> None:
+            if error:
+                self._log.warning(f"Error: {error.domain}, {error.message}")
+                return
+
+            if media is None:
+                model.set_filter_func(_filter_func)
+                return
+
+            disc_song_ids.append(media.get_source() + media.get_id())
+
         self.props.source.query(
-            query, metadata_keys, self._fast_options, callback)
+            query, metadata_keys, self._fast_options, _callback)
 
     def search(self, text: str) -> None:
         # FIXME: Searches are limited to not bog down the UI with


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