[gnome-music/wip/mschraal/core] coremodel: Make get_artist_albums async



commit 295692c1220ac73d389d5159466619c18e75a72f
Author: Marinus Schraal <mschraal gnome org>
Date:   Sat Jun 29 01:11:23 2019 +0200

    coremodel: Make get_artist_albums async

 gnomemusic/coregrilo.py                      |  6 +++---
 gnomemusic/coremodel.py                      | 19 ++++---------------
 gnomemusic/grilowrappers/grldleynasource.py  |  2 +-
 gnomemusic/grilowrappers/grltrackersource.py | 25 ++++++++++++++++++++-----
 gnomemusic/widgets/artistalbumswidget.py     |  6 ++++++
 5 files changed, 34 insertions(+), 24 deletions(-)
---
diff --git a/gnomemusic/coregrilo.py b/gnomemusic/coregrilo.py
index 368d98da..fad36f0e 100644
--- a/gnomemusic/coregrilo.py
+++ b/gnomemusic/coregrilo.py
@@ -51,9 +51,9 @@ class CoreGrilo(GObject.GObject):
         # FIXME: Handle removing sources.
         print("removed,", source.props.source_id)
 
-    def get_artist_albums(self, artist):
-        # FIXME: Iterate the wrappers
-        return self._tracker_source.get_artist_albums(artist)
+    def get_artist_albums(self, artist, filter_model):
+        for wrapper in self._wrappers:
+            wrapper.get_artist_albums(artist, filter_model)
 
     def get_album_disc_numbers(self, media):
         # FIXME: Iterate the wrappers
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index f3b383e5..c9516859 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -119,23 +119,12 @@ class CoreModel(GObject.GObject):
         return disc_model_sort
 
     def get_artists_model_full(self, media):
-        albums = self._grilo.get_artist_albums(media)
+        albums_model_filter = Dazzle.ListModelFilter.new(self._album_model)
+        albums_model_filter.set_filter_func(lambda a: False)
 
-        albums_model = Gio.ListStore()
-        albums_model_sort = Gfm.SortListModel.new(albums_model)
+        albums_model_sort = Gfm.SortListModel.new(albums_model_filter)
 
-        for album in albums:
-            artist_album = None
-            for corealbum in self._album_model:
-                if album.get_id() == corealbum.props.media.get_id():
-                    artist_album = corealbum
-                    break
-
-            if artist_album is None:
-                artist_album = CoreAlbum(album, self)
-                self._album_model.append(artist_album)
-
-            albums_model.append(artist_album)
+        self._grilo.get_artist_albums(media, albums_model_filter)
 
         def _album_sort(album_a, album_b):
             return album_a.props.year > album_b.props.year
diff --git a/gnomemusic/grilowrappers/grldleynasource.py b/gnomemusic/grilowrappers/grldleynasource.py
index 76021a14..6a20d57a 100644
--- a/gnomemusic/grilowrappers/grldleynasource.py
+++ b/gnomemusic/grilowrappers/grldleynasource.py
@@ -89,7 +89,7 @@ class GrlDLeynaSource(GObject.GObject):
             "ADDING DLNA ARTIST", media.get_title(), media.get_artist(),
             media.get_id())
 
-    def get_artist_albums(self, artist):
+    def get_artist_albums(self, artist, filter_model):
         pass
 
     def populate_album_disc_songs(self, media, discnr, callback):
diff --git a/gnomemusic/grilowrappers/grltrackersource.py b/gnomemusic/grilowrappers/grltrackersource.py
index 09ab741d..09b60a26 100644
--- a/gnomemusic/grilowrappers/grltrackersource.py
+++ b/gnomemusic/grilowrappers/grltrackersource.py
@@ -344,9 +344,8 @@ class GrlTrackerSource(GObject.GObject):
         artist = CoreArtist(media, self._coremodel)
         self._artists_model.append(artist)
 
-    def get_artist_albums(self, media):
+    def get_artist_albums(self, media, model):
         artist_id = media.get_id()
-        print("ID", artist_id)
 
         query = """
         SELECT DISTINCT
@@ -369,11 +368,27 @@ class GrlTrackerSource(GObject.GObject):
 
         options = self._fast_options.copy()
 
-        albums = self._source.query_sync(query, self.METADATA_KEYS, options)
+        albums = []
 
-        print("ALBUMS", albums)
+        def query_cb(source, op_id, media, user_data, error):
+            if error:
+                print("ERROR", error)
+                return
+
+            if not media:
+                model.set_filter_func(albums_filter, albums)
+                return
+
+            albums.append(media)
+
+        def albums_filter(corealbum, albums):
+            for media in albums:
+                if media.get_id() == corealbum.props.media.get_id():
+                    return True
+
+            return False
 
-        return albums
+        self._source.query(query, self.METADATA_KEYS, options, query_cb)
 
     def get_album_disc_numbers(self, media):
         album_id = media.get_id()
diff --git a/gnomemusic/widgets/artistalbumswidget.py b/gnomemusic/widgets/artistalbumswidget.py
index 5f77bbcc..566d7fb7 100644
--- a/gnomemusic/widgets/artistalbumswidget.py
+++ b/gnomemusic/widgets/artistalbumswidget.py
@@ -82,12 +82,18 @@ class ArtistAlbumsWidget(Gtk.Box):
         self._songs_grid_size_group = Gtk.SizeGroup.new(
             Gtk.SizeGroupMode.HORIZONTAL)
 
+        self._model.connect("items-changed", self._on_album_items_changed)
+
         # FIXME: Make this a ListBox as well.
         for album in self._model:
             self._add_album(album)
 
         self.show_all()
 
+    def _on_album_items_changed(self, model, position, removed, added):
+        self._add_album(model[position])
+        self.show_all()
+
     def _song_activated(self, widget, song_widget):
         self._album = None
 


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