[gnome-music/wip/mschraal/core] window: Add bare-bones empty state check



commit fdb283a0958d1fc65bb872b956a8ada27cd1766b
Author: Marinus Schraal <mschraal gnome org>
Date:   Sun Jul 14 18:23:04 2019 +0200

    window: Add bare-bones empty state check

 gnomemusic/coremodel.py | 16 ++++++++++++++
 gnomemusic/window.py    | 56 ++++++++++++++-----------------------------------
 2 files changed, 32 insertions(+), 40 deletions(-)
---
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index 077176df..908f8247 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -37,6 +37,8 @@ class CoreModel(GObject.GObject):
         "playlists-loaded": (GObject.SignalFlags.RUN_FIRST, None, ()),
     }
 
+    songs_available = GObject.Property(type=bool, default=False)
+
     @log
     def __init__(self, coreselection):
         super().__init__()
@@ -84,6 +86,20 @@ class CoreModel(GObject.GObject):
 
         self._grilo = CoreGrilo(self, self._coreselection)
 
+        self._model.connect("items-changed", self._on_songs_items_changed)
+
+    def _on_songs_items_changed(self, model, position, removed, added):
+        available = self.props.songs_available
+        now_available = model.get_n_items() > 0
+
+        if available == now_available:
+            return
+
+        if model.get_n_items() > 0:
+            self.props.songs_available = True
+        else:
+            self.props.songs_available = False
+
     def _filter_selected(self, coresong):
         return coresong.props.selected
 
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index b95dbb2d..3aa4a39d 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -29,7 +29,6 @@ from gnomemusic import log
 from gnomemusic.gstplayer import Playback
 from gnomemusic.mediakeys import MediaKeys
 from gnomemusic.player import RepeatMode
-from gnomemusic.query import Query
 from gnomemusic.search import Search
 from gnomemusic.utils import View
 from gnomemusic.views.albumsview import AlbumsView
@@ -102,29 +101,6 @@ class Window(Gtk.ApplicationWindow):
 
         MediaKeys(self._player, self)
 
-    @log
-    def _on_changes_pending(self, data=None):
-        # FIXME: This is not working right.
-        def songs_available_cb(available):
-            view_count = len(self.views)
-            if (available
-                    and view_count == 1):
-                self._switch_to_player_view()
-            elif (not available
-                    and not self.props.selection_mode
-                    and view_count != 1):
-                self._stack.disconnect(self._on_notify_model_id)
-                self.disconnect(self._key_press_event_id)
-
-                for i in range(View.ALBUM, view_count):
-                    view = self.views.pop()
-                    view.destroy()
-
-                self._switch_to_empty_view()
-
-        # FIXME: Don't assume available.
-        songs_available_cb(True)
-
     @log
     def _setup_view(self):
         self._search = Search()
@@ -193,32 +169,34 @@ class Window(Gtk.ApplicationWindow):
         self._headerbar.props.state = HeaderBar.State.MAIN
         self._headerbar.show()
 
-        def songs_available_cb(available):
-            if available:
-                self._switch_to_player_view()
-            else:
-                self._switch_to_empty_view()
+        self._app.props.coremodel.connect(
+            "notify::songs-available", self._on_songs_available)
 
-        if Query().music_folder:
-            # FIXME: Do not assume True.
-            songs_available_cb(True)
+        if self._app.props.coremodel.props.songs_available:
+            self._switch_to_player_view()
         else:
             self._switch_to_empty_view()
 
     @log
     def _switch_to_empty_view(self):
-        # did_initial_state = self._settings.get_boolean('did-initial-state')
+        did_initial_state = self._settings.get_boolean('did-initial-state')
 
-        # FIXME: Fix the startup views.
         # if not grilo.props.tracker_available:
         #     self.views[View.EMPTY].props.state = EmptyView.State.NO_TRACKER
-        # elif did_initial_state:
-        #     self.views[View.EMPTY].props.state = EmptyView.State.EMPTY
-        # else:
-        #     self.views[View.EMPTY].props.state = EmptyView.State.INITIAL
+        if did_initial_state:
+            self.views[View.EMPTY].props.state = EmptyView.State.EMPTY
+        else:
+            # FIXME: On switch back this view does not show properly.
+            self.views[View.EMPTY].props.state = EmptyView.State.INITIAL
 
         self._headerbar.props.state = HeaderBar.State.EMPTY
 
+    def _on_songs_available(self, klass, value):
+        if self._app.props.coremodel.props.songs_available:
+            self._switch_to_player_view()
+        else:
+            self._switch_to_empty_view()
+
     @log
     def _switch_to_player_view(self):
         self._settings.set_boolean('did-initial-state', True)
@@ -481,8 +459,6 @@ class Window(Gtk.ApplicationWindow):
         if (not self.props.selection_mode
                 and self._player.state == Playback.STOPPED):
             self._player_toolbar.hide()
-        if not self.props.selection_mode:
-            self._on_changes_pending()
 
     @log
     def _on_add_to_playlist(self, widget):


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