[gnome-music/wip/jfelder/coremodel-playlists-available] coremodel: Correctly register playlists availability



commit 08d57439cbc75d87cf913eb1770244f0df0184d8
Author: Jean Felder <jfelder src gnome org>
Date:   Tue Jan 7 19:20:43 2020 +0100

    coremodel: Correctly register playlists availability
    
    In some cases (especially on the flatpak version), the playlists model
    is populated before the PlaylistsView is created. The
    "playlists-loaded" signal is never received by the view because it has
    already been emitted. This results in a broken view.
    
    This issue can be fixed by replacing the `playlists-loaded` signal with
    a `playlists_available` property. Indeed, when MPRIS or PlaylistsView
    need to know if the playlists are loaded, they can check the
    `playlists_available` value. If False, they need to register to
    `notify::playlists-available` signal to wait for the availability.
    
    Closes: #348

 gnomemusic/coremodel.py                         |  4 ++--
 gnomemusic/grilowrappers/grltrackerplaylists.py |  4 +++-
 gnomemusic/mpris.py                             | 19 +++++++++++++++----
 gnomemusic/views/playlistsview.py               | 19 +++++++++++++++----
 4 files changed, 35 insertions(+), 11 deletions(-)
---
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index a8579159..bf7cc36d 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -67,12 +67,12 @@ class CoreModel(GObject.GObject):
 
     __gsignals__ = {
         "artists-loaded": (GObject.SignalFlags.RUN_FIRST, None, ()),
-        "playlist-loaded": (GObject.SignalFlags.RUN_FIRST, None, (int,)),
-        "playlists-loaded": (GObject.SignalFlags.RUN_FIRST, None, ()),
+        "playlist-loaded": (GObject.SignalFlags.RUN_FIRST, None, (int,))
     }
 
     active_playlist = GObject.Property(type=Playlist, default=None)
     grilo = GObject.Property(type=CoreGrilo, default=None)
+    playlists_available = GObject.Property(type=bool, default=False)
     songs_available = GObject.Property(type=bool, default=False)
 
     def __init__(self, application):
diff --git a/gnomemusic/grilowrappers/grltrackerplaylists.py b/gnomemusic/grilowrappers/grltrackerplaylists.py
index acd626a9..44e5a0ee 100644
--- a/gnomemusic/grilowrappers/grltrackerplaylists.py
+++ b/gnomemusic/grilowrappers/grltrackerplaylists.py
@@ -93,6 +93,8 @@ class GrlTrackerPlaylists(GObject.GObject):
         self._initial_playlists_fill()
 
     def _initial_playlists_fill(self):
+        self._coremodel.props.playlists_available = False
+
         args = {
             "source": self._source,
             "application": self._application,
@@ -145,7 +147,7 @@ class GrlTrackerPlaylists(GObject.GObject):
             self._window.notifications_popup.pop_loading()
             return
         if not media:
-            self._coremodel.emit("playlists-loaded")
+            self._coremodel.props.playlists_available = True
             self._window.notifications_popup.pop_loading()
             return
 
diff --git a/gnomemusic/mpris.py b/gnomemusic/mpris.py
index dc9822a4..00730238 100644
--- a/gnomemusic/mpris.py
+++ b/gnomemusic/mpris.py
@@ -299,8 +299,13 @@ class MPRIS(DBusInterface):
             "playlist-loaded", self._on_player_playlist_changed)
 
         self._playlists_model = self._coremodel.props.playlists_sort
-        self._playlists_loaded_id = self._coremodel.connect(
-            "playlists-loaded", self._on_playlists_loaded)
+
+        if self._coremodel.props.playlists_available is True:
+            self._playlists_loaded_id = None
+            self._on_playlists_available()
+        else:
+            self._playlists_loaded_id = self._coremodel.connect(
+                "notify::playlists-available", self._on_playlists_available)
 
         self._player_playlist_type = None
         self._path_list = []
@@ -603,8 +608,14 @@ class MPRIS(DBusInterface):
         self._properties_changed(
             MPRIS.MEDIA_PLAYER2_PLAYLISTS_IFACE, properties, [])
 
-    def _on_playlists_loaded(self, klass):
-        self._coremodel.disconnect(self._playlists_loaded_id)
+    def _on_playlists_available(self, klass=None, value=None):
+        if self._coremodel.props.playlists_available is False:
+            return
+
+        if self._playlists_loaded_id is not None:
+            self._coremodel.disconnect(self._playlists_loaded_id)
+            self._playlists_loaded_id = None
+
         for playlist in self._playlists_model:
             playlist.connect("notify::title", self._on_playlist_renamed)
             playlist.connect(
diff --git a/gnomemusic/views/playlistsview.py b/gnomemusic/views/playlistsview.py
index e7c60755..6fbd4811 100644
--- a/gnomemusic/views/playlistsview.py
+++ b/gnomemusic/views/playlistsview.py
@@ -108,8 +108,13 @@ class PlaylistsView(BaseView):
 
         self._sidebar.bind_model(self._model, self._add_playlist_to_sidebar)
 
-        self._loaded_id = self._coremodel.connect(
-            "playlists-loaded", self._on_playlists_loaded)
+        if self._coremodel.props.playlists_available is True:
+            self._loaded_id = None
+            self._on_playlists_available()
+        else:
+            self._loaded_id = self._coremodel.connect(
+                "notify::playlists-available", self._on_playlists_available)
+
         self._active_playlist_id = self._coremodel.connect(
             "notify::active-playlist", self._on_active_playlist_changed)
 
@@ -147,8 +152,14 @@ class PlaylistsView(BaseView):
         row = PlaylistTile(playlist)
         return row
 
-    def _on_playlists_loaded(self, klass):
-        self._coremodel.disconnect(self._loaded_id)
+    def _on_playlists_available(self, klass=None, value=None):
+        if self._coremodel.props.playlists_available is False:
+            return
+
+        if self._loaded_id is not None:
+            self._coremodel.disconnect(self._loaded_id)
+            self._loaded_id = None
+
         self._model.connect("items-changed", self._on_playlists_model_changed)
 
         first_row = self._sidebar.get_row_at_index(0)


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