[gnome-music] window: Use a push/pop model for loading notifications



commit b8da571db517478a004ea3404c76a01cc24f13cc
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Thu Dec 15 22:57:33 2016 -0200

    window: Use a push/pop model for loading notifications
    
    The current loading notification is handled by a private function
    that is accessed outside the Window object. This is not only a poor
    design decision, as the notification object is an implementation
    detail of the window, and external objects can't rely on that.
    
    Fix that by hiding the loading notifications management behind a
    push/pop pair of calls.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=776157

 gnomemusic/views/albumsview.py           |    6 +--
 gnomemusic/views/artistsview.py          |    6 +--
 gnomemusic/views/baseview.py             |    3 +-
 gnomemusic/views/playlistview.py         |    5 +--
 gnomemusic/views/searchview.py           |    5 +--
 gnomemusic/views/songsview.py            |    6 +--
 gnomemusic/widgets/artistalbumswidget.py |    5 +--
 gnomemusic/window.py                     |   52 +++++++++++++++++++----------
 8 files changed, 47 insertions(+), 41 deletions(-)
---
diff --git a/gnomemusic/views/albumsview.py b/gnomemusic/views/albumsview.py
index 9f7c16d..de4b896 100644
--- a/gnomemusic/views/albumsview.py
+++ b/gnomemusic/views/albumsview.py
@@ -123,7 +123,7 @@ class AlbumsView(BaseView):
 
     @log
     def populate(self):
-        self.window._init_loading_notification()
+        self.window.push_loading_notification()
         grilo.populate_albums(self._offset, self._add_item)
 
     @log
@@ -141,8 +141,6 @@ class AlbumsView(BaseView):
 
     @log
     def _add_item(self, source, param, item, remaining=0, data=None):
-        self.window.notification.set_timeout(0)
-
         if item:
             # Store all items to optimize 'Select All' action
             self.all_items.append(item)
@@ -151,7 +149,7 @@ class AlbumsView(BaseView):
             child = self._create_album_item(item)
             self.view.add(child)
         elif remaining == 0:
-                self.window.notification.dismiss()
+                self.window.pop_loading_notification()
                 self.view.show()
 
     def _create_album_item(self, item):
diff --git a/gnomemusic/views/artistsview.py b/gnomemusic/views/artistsview.py
index 3267b03..7dd0a43 100644
--- a/gnomemusic/views/artistsview.py
+++ b/gnomemusic/views/artistsview.py
@@ -148,11 +148,9 @@ class ArtistsView(BaseView):
 
     @log
     def _add_item(self, source, param, item, remaining=0, data=None):
-        self.window.notification.set_timeout(0)
-
         if (not item and remaining == 0):
             self.view.set_model(self.model)
-            self.window.notification.dismiss()
+            self.window.pop_loading_notification()
             self.view.show()
             return
         self._offset += 1
@@ -169,7 +167,7 @@ class ArtistsView(BaseView):
     @log
     def populate(self):
         """Populates the view"""
-        self.window._init_loading_notification()
+        self.window.push_loading_notification()
         grilo.populate_artists(self._offset, self._add_item)
 
     @log
diff --git a/gnomemusic/views/baseview.py b/gnomemusic/views/baseview.py
index ad26c1a..4f2e327 100644
--- a/gnomemusic/views/baseview.py
+++ b/gnomemusic/views/baseview.py
@@ -209,11 +209,10 @@ class BaseView(Gtk.Stack):
 
     @log
     def _add_item(self, source, param, item, remaining=0, data=None):
-        self.window.notification.set_timeout(0)
         if not item:
             if remaining == 0:
                 self.view.set_model(self.model)
-                self.window.notification.dismiss()
+                self.window.pop_loading_notification()
                 self.view.show()
             return
 
diff --git a/gnomemusic/views/playlistview.py b/gnomemusic/views/playlistview.py
index 26a536d..862e414 100644
--- a/gnomemusic/views/playlistview.py
+++ b/gnomemusic/views/playlistview.py
@@ -240,7 +240,7 @@ class PlaylistView(BaseView):
     @log
     def _populate(self):
         self._init = True
-        self.window._init_loading_notification()
+        self.window.push_loading_notification()
         self.populate()
 
     @log
@@ -263,11 +263,10 @@ class PlaylistView(BaseView):
 
     @log
     def _add_playlist_item_to_model(self, item, index=None):
-        self.window.notification.set_timeout(0)
         if index is None:
             index = -1
         if not item:
-            self.window.notification.dismiss()
+            self.window.pop_loading_notification()
             self.emit('playlists-loaded')
             return
         _iter = self.playlists_model.insert_with_valuesv(
diff --git a/gnomemusic/views/searchview.py b/gnomemusic/views/searchview.py
index 1202f19..30063d5 100644
--- a/gnomemusic/views/searchview.py
+++ b/gnomemusic/views/searchview.py
@@ -188,7 +188,6 @@ class SearchView(BaseView):
 
     @log
     def _add_item(self, source, param, item, remaining=0, data=None):
-        self.window.notification.set_timeout(0)
         if data is None:
             return
 
@@ -210,7 +209,7 @@ class SearchView(BaseView):
             self.previous_view = self.window.prev_view
 
         if remaining == 0:
-            self.window.notification.dismiss()
+            self.window.pop_loading_notification()
             self.view.show()
 
         if not item or model != self.model:
@@ -304,7 +303,7 @@ class SearchView(BaseView):
     @log
     def populate(self):
         self._init = True
-        self.window._init_loading_notification()
+        self.window.push_loading_notification()
         self.header_bar.set_state(ToolbarState.MAIN)
 
     @log
diff --git a/gnomemusic/views/songsview.py b/gnomemusic/views/songsview.py
index 8e95bef..15343e6 100644
--- a/gnomemusic/views/songsview.py
+++ b/gnomemusic/views/songsview.py
@@ -117,11 +117,9 @@ class SongsView(BaseView):
 
     def _add_item(self, source, param, item, remaining=0, data=None):
         """Adds track item to the model"""
-        self.window.notification.set_timeout(0)
-
         if not item and not remaining:
             self.view.set_model(self.model)
-            self.window.notification.dismiss()
+            self.window.pop_loading_notification()
             self.view.show()
             return
 
@@ -229,7 +227,7 @@ class SongsView(BaseView):
         """Populates the view"""
         self._init = True
         if grilo.tracker:
-            self.window._init_loading_notification()
+            self.window.push_loading_notification()
             GLib.idle_add(grilo.populate_songs, self._offset, self._add_item)
 
     @log
diff --git a/gnomemusic/widgets/artistalbumswidget.py b/gnomemusic/widgets/artistalbumswidget.py
index 00763d5..e9975ea 100644
--- a/gnomemusic/widgets/artistalbumswidget.py
+++ b/gnomemusic/widgets/artistalbumswidget.py
@@ -77,7 +77,7 @@ class ArtistAlbumsWidget(Gtk.Box):
         self.pack_start(self._scrolledWindow, True, True, 0)
 
         self.hide()
-        self.window._init_loading_notification()
+        self.window.push_loading_notification()
 
         for album in albums:
             is_last_album = False
@@ -106,12 +106,11 @@ class ArtistAlbumsWidget(Gtk.Box):
         )
 
     def _on_last_album_displayed(self, data=None):
-        self.window.notification.dismiss()
+        self.window.pop_loading_notification()
         self.show_all()
 
     @log
     def add_album(self, album, is_last_album=False):
-        self.window.notification.set_timeout(0)
         widget = ArtistAlbumWidget(
             album, self.player, self._model,
             self.header_bar, self.selectionModeAllowed,
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index f155f4f..0f71555 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -81,6 +81,7 @@ class Window(Gtk.ApplicationWindow):
         self.set_size_request(200, 100)
         self.set_icon_name('gnome-music')
         self.notification_handler = None
+        self._loading_counter = 0
 
         self.prev_view = None
         self.curr_view = None
@@ -308,24 +309,6 @@ class Window(Gtk.ApplicationWindow):
         return False
 
     @log
-    def _init_loading_notification(self):
-        self.notification = Gd.Notification()
-        self.notification.set_timeout(5)
-        grid = Gtk.Grid(valign=Gtk.Align.CENTER, margin_end=8)
-        grid.set_column_spacing(8)
-        spinner = Gtk.Spinner()
-        spinner.start()
-        grid.add(spinner)
-        label = Gtk.Label.new(_("Loading"))
-        grid.add(label)
-        self.notification.add(grid)
-        self._overlay.add_overlay(self.notification)
-        if self.notification_handler:
-            GLib.Source.remove(self.notification_handler)
-            self.notification_handler = None
-        self.notification_handler = GLib.timeout_add(1000, self._show_notification)
-
-    @log
     def _init_playlist_removal_notification(self):
         if self.pl_todelete_notification:
             self.views[3].really_delete = False
@@ -536,3 +519,36 @@ class Window(Gtk.ApplicationWindow):
             self.toolbar.set_selection_mode(False)
 
         self._stack.get_visible_child().get_selected_tracks(callback)
+
+    @log
+    def push_loading_notification(self):
+        """ Increases the counter of loading notification triggers
+        running. If there is no notification is visible, the loading
+        notification is started.
+        """
+        if self._loading_counter == 0:
+            self.notification = Gd.Notification()
+            self.notification.set_timeout(5)
+            grid = Gtk.Grid(valign=Gtk.Align.CENTER, margin_end=8)
+            grid.set_column_spacing(8)
+            spinner = Gtk.Spinner()
+            spinner.start()
+            grid.add(spinner)
+            label = Gtk.Label.new(_("Loading"))
+            grid.add(label)
+            self.notification.add(grid)
+            self._overlay.add_overlay(self.notification)
+            self.notification.show_all()
+
+        self._loading_counter = self._loading_counter + 1
+
+    @log
+    def pop_loading_notification(self):
+        """ Decreases the counter of loading notification triggers
+        running. If it reaches zero, the notification is withdrawn.
+        """
+        self._loading_counter = self._loading_counter - 1
+
+        if self._loading_counter == 0:
+            self.notification.dismiss()
+            self.notification = None


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