[gnome-music/wip/mschraal/core: 176/190] coremodel: Make playlist react to all types for changes



commit c561b04d0c3bcea8d673a93d0320468325ee6c2c
Author: Marinus Schraal <mschraal gnome org>
Date:   Wed Jul 3 16:11:15 2019 +0200

    coremodel: Make playlist react to all types for changes

 gnomemusic/coremodel.py | 83 +++++++++++++++++++++++++++++++------------------
 1 file changed, 52 insertions(+), 31 deletions(-)
---
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index eead4383..a895e689 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -37,6 +37,8 @@ class CoreModel(GObject.GObject):
         super().__init__()
 
         self._flatten_model = None
+        self._search_signal_id = None
+        self._song_signal_id = None
 
         self._model = Gio.ListStore.new(CoreSong)
         self._songliststore = SongListStore(self._model)
@@ -120,30 +122,31 @@ class CoreModel(GObject.GObject):
         return albums_model_sort
 
     def set_playlist_model(self, playlist_type, coresong, model):
-        with model.freeze_notify():
 
-            if playlist_type == PlayerPlaylist.Type.ALBUM:
+        def _on_items_changed(model, position, removed, added):
+            if removed > 0:
+                for i in list(range(removed)):
+                    self._playlist_model.remove(position)
 
-                self._playlist_model.remove_all()
-                proxy_model = Gio.ListStore.new(Gio.ListModel)
+            if added > 0:
+                for i in list(range(added)):
+                    coresong = model[position + i]
+                    song = CoreSong(
+                        coresong.props.media, self._coreselection,
+                        self._grilo)
+
+                    self._playlist_model.insert(position + i, song)
 
-                def _on_items_changed(model, position, removed, added):
-                    if removed > 0:
-                        for i in list(range(removed)):
-                            self._playlist_model.remove(position)
+                    song.bind_property(
+                        "state", coresong, "state",
+                        GObject.BindingFlags.SYNC_CREATE)
 
-                    if added > 0:
-                        for i in list(range(added)):
-                            coresong = model[position + i]
-                            song = CoreSong(
-                                coresong.props.media, self._coreselection,
-                                self._grilo)
+        with model.freeze_notify():
 
-                            self._playlist_model.insert(position + i, song)
+            if playlist_type == PlayerPlaylist.Type.ALBUM:
 
-                            song.bind_property(
-                                "state", coresong, "state",
-                                GObject.BindingFlags.SYNC_CREATE)
+                self._playlist_model.remove_all()
+                proxy_model = Gio.ListStore.new(Gio.ListModel)
 
                 for disc in model:
                     proxy_model.append(disc.props.model)
@@ -169,25 +172,36 @@ class CoreModel(GObject.GObject):
                 self.emit("playlist-loaded")
             elif playlist_type == PlayerPlaylist.Type.ARTIST:
                 self._playlist_model.remove_all()
+                proxy_model = Gio.ListStore.new(Gio.ListModel)
 
                 for artist_album in model:
                     for disc in artist_album.model:
-                        for model_song in disc.model:
-                            song = CoreSong(
-                                model_song.props.media, self._coreselection,
-                                self._grilo)
+                        proxy_model.append(disc.props.model)
+
+                self._flatten_model = Gfm.FlattenListModel.new(
+                    CoreSong, proxy_model)
+                self._flatten_model.connect("items-changed", _on_items_changed)
 
-                            self._playlist_model.append(song)
-                            song.bind_property(
-                                "state", model_song, "state",
-                                GObject.BindingFlags.SYNC_CREATE)
+                for model_song in self._flatten_model:
+                    song = CoreSong(
+                        model_song.props.media, self._coreselection,
+                        self._grilo)
+
+                    self._playlist_model.append(song)
+                    song.bind_property(
+                        "state", model_song, "state",
+                        GObject.BindingFlags.SYNC_CREATE)
 
-                            song_id = coresong.props.media.get_id()
-                            if song.props.media.get_id() == song_id:
-                                song.props.state = SongWidget.State.PLAYING
+                    song_id = coresong.props.media.get_id()
+                    if song.props.media.get_id() == song_id:
+                        song.props.state = SongWidget.State.PLAYING
 
                 self.emit("playlist-loaded")
             elif playlist_type == PlayerPlaylist.Type.SONGS:
+                if self._song_signal_id:
+                    self._songliststore.props.model.disconnect(
+                        self._song_signal_id)
+
                 self._playlist_model.remove_all()
 
                 for song in self._songliststore.props.model:
@@ -198,18 +212,25 @@ class CoreModel(GObject.GObject):
                     if song is coresong:
                         song.props.state = SongWidget.State.PLAYING
 
+                self._song_signal_id = self._songliststore.props.model.connect(
+                    "items-changed", _on_items_changed)
+
                 self.emit("playlist-loaded")
             elif playlist_type == PlayerPlaylist.Type.SEARCH_RESULT:
+                if self._search_signal_id:
+                    self._song_search_model.disconnect(self._search_signal_id)
+
                 self._playlist_model.remove_all()
 
                 for song in self._song_search_model:
                     self._playlist_model.append(song)
 
-                    if song.props.state == SongWidget.State.PLAYING:
-                        song.props.state = SongWidget.State.PLAYED
                     if song is coresong:
                         song.props.state = SongWidget.State.PLAYING
 
+                self._search_signal_id = self._song_search_model.connect(
+                    "items-changed", _on_items_changed)
+
                 self.emit("playlist-loaded")
 
     def search(self, text):


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