[gnome-music/wip/mschraal/core: 31/118] albumsview: Minimal working albumsview ported



commit 4c6965891fe0cb57301efd81bc4d8b483df10eeb
Author: Marinus Schraal <mschraal gnome org>
Date:   Fri May 10 01:17:27 2019 +0200

    albumsview: Minimal working albumsview ported

 gnomemusic/corealbum.py        | 29 +++++++++++++++++++++++++++++
 gnomemusic/coregrilo.py        | 38 +++++++++++++++++++++++++++++++++++++-
 gnomemusic/coremodel.py        |  8 +++++++-
 gnomemusic/views/albumsview.py | 19 ++++++++++++++++---
 4 files changed, 89 insertions(+), 5 deletions(-)
---
diff --git a/gnomemusic/corealbum.py b/gnomemusic/corealbum.py
new file mode 100644
index 00000000..010997c9
--- /dev/null
+++ b/gnomemusic/corealbum.py
@@ -0,0 +1,29 @@
+import gi
+gi.require_version('Grl', '0.3')
+from gi.repository import Grl, GObject
+
+from gnomemusic import log
+from gnomemusic.grilo import grilo
+import gnomemusic.utils as utils
+
+
+class CoreAlbum(GObject.GObject):
+    """Exposes a Grl.Media with relevant data as properties
+    """
+
+    artist = GObject.Property(type=str)
+    media = GObject.Property(type=Grl.Media)
+    selected = GObject.Property(type=bool, default=False)
+    title = GObject.Property(type=str)
+
+    @log
+    def __init__(self, media):
+        super().__init__()
+
+        self.update(media)
+
+    @log
+    def update(self, media):
+        self.props.media = media
+        self.props.artist = utils.get_artist_name(media)
+        self.props.title = utils.get_media_title(media)
diff --git a/gnomemusic/coregrilo.py b/gnomemusic/coregrilo.py
index 8f33aa77..cd26c374 100644
--- a/gnomemusic/coregrilo.py
+++ b/gnomemusic/coregrilo.py
@@ -2,6 +2,8 @@ import gi
 gi.require_version('Grl', '0.3')
 from gi.repository import Grl, GObject
 
+from gnomemusic import log
+from gnomemusic.corealbum import CoreAlbum
 from gnomemusic.coresong import CoreSong
 
 
@@ -32,10 +34,11 @@ class CoreGrilo(GObject.GObject):
     def __repr__(self):
         return "<CoreGrilo>"
 
-    def __init__(self, model, table, url_hash):
+    def __init__(self, model, table, url_hash, albums_model):
         super().__init__()
 
         self._model = model
+        self._albums_model = albums_model
         self._table = table
         # Only way to figure out removed items
         self._url_table = url_hash
@@ -59,6 +62,7 @@ class CoreGrilo(GObject.GObject):
         if source.props.source_id == "grl-tracker-source":
             self._tracker_source = source
             self._tracker_initial_fill(source)
+            self._initial_albums_fill(source)
             print(self._tracker_source, "added")
             self._tracker_source.connect(
                 "content-changed", self._on_content_changed)
@@ -175,3 +179,35 @@ class CoreGrilo(GObject.GObject):
         self._url_table[media.get_url()] = song
 
         # print(song.props.title, song.props.url)
+
+    @log
+    def _initial_albums_fill(self, source):
+        query = """
+        SELECT
+            rdf:type(?album)
+            tracker:id(?album) AS ?id
+            nmm:artistName(?album_artist) AS ?album_artist
+            nie:title(?album) as ?title
+        {
+            ?album a nmm:MusicAlbum .
+            OPTIONAL { ?album nmm:albumArtist ?albumArtist . }
+        }
+        """.replace('\n', ' ').strip()
+
+        options = self._fast_options.copy()
+
+        source.query(
+            query, self.METADATA_KEYS, options, self._add_to_albums_model)
+
+    @log
+    def _add_to_albums_model(self, source, op_id, media, user_data, error):
+        if error:
+            print("ERROR", error)
+            return
+
+        if not media:
+            print("NO MEDIA", source, op_id, media, error)
+            return
+
+        album = CoreAlbum(media)
+        self._albums_model.append(album)
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index 12c7ac01..7b7255ba 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -14,11 +14,13 @@ class CoreModel(GObject.GObject):
         super().__init__()
 
         self._model = Gio.ListStore()
+        self._album_model = Gio.ListStore()
         self._album_store = None
         self._hash = {}
         self._url_hash = {}
 
-        self._grilo = CoreGrilo(self._model, self._hash, self._url_hash)
+        self._grilo = CoreGrilo(
+            self._model, self._hash, self._url_hash, self._album_model)
         self._grilo.connect("media-removed", self._on_media_removed)
 
     @log
@@ -50,6 +52,10 @@ class CoreModel(GObject.GObject):
 
         return model_filter
 
+    @log
+    def get_albums_model(self):
+        return self._album_model
+
     @log
     def _on_media_removed(self, klass, media):
         try:
diff --git a/gnomemusic/views/albumsview.py b/gnomemusic/views/albumsview.py
index 4d9fe3f9..2945cbf5 100644
--- a/gnomemusic/views/albumsview.py
+++ b/gnomemusic/views/albumsview.py
@@ -43,6 +43,7 @@ class AlbumsView(BaseView):
 
     @log
     def __init__(self, window, player):
+        self._window = window
         super().__init__('albums', _("Albums"), window)
 
         self.player = player
@@ -90,7 +91,7 @@ class AlbumsView(BaseView):
             homogeneous=True, hexpand=True, halign=Gtk.Align.FILL,
             valign=Gtk.Align.START, selection_mode=Gtk.SelectionMode.NONE,
             margin=18, row_spacing=12, column_spacing=6,
-            min_children_per_line=1, max_children_per_line=20)
+            min_children_per_line=1, max_children_per_line=20, visible=True)
 
         self._view.get_style_context().add_class('content-view')
         self._view.connect('child-activated', self._on_child_activated)
@@ -101,6 +102,17 @@ class AlbumsView(BaseView):
 
         self._box.add(scrolledwin)
 
+        self._model = self._window._app._coremodel.get_albums_model()
+        self._view.bind_model(self._model, self._create_widget)
+
+        self._view.show()
+
+    @log
+    def _create_widget(self, album):
+        album_widget = AlbumCover(album.props.media)
+        print(album.props.artist)
+        return album_widget
+
     @log
     def _back_button_clicked(self, widget, data=None):
         self._headerbar.state = HeaderBar.State.MAIN
@@ -126,9 +138,10 @@ class AlbumsView(BaseView):
 
     @log
     def _populate(self, data=None):
-        self._window.notifications_popup.push_loading()
-        grilo.populate_albums(self._offset, self._add_item)
+        # self._window.notifications_popup.push_loading()
+        # grilo.populate_albums(self._offset, self._add_item)
         self._init = True
+        self._view.show()
 
     @log
     def get_selected_songs(self, callback):


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