[gnome-music/wip/jfelder/playback-status-v4: 1/12] coremodel: Add a recent playlist model



commit c38995c3275f5c22c1325674c8d14ccf8ab392fc
Author: Jean Felder <jfelder src gnome org>
Date:   Fri Jan 10 01:08:19 2020 +0100

    coremodel: Add a recent playlist model
    
    This model is a slice of the player playlist. It will be used by the
    PlaybackPopover widget introduced in the following commits.

 gnomemusic/coremodel.py | 16 ++++++++++++++++
 gnomemusic/player.py    | 14 +++++++++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)
---
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index 75a1e21c..f3d46b16 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -71,6 +71,8 @@ class CoreModel(GObject.GObject):
     active_playlist = GObject.Property(type=Playlist, default=None)
     songs_available = GObject.Property(type=bool, default=False)
 
+    _recent_size = 21
+
     def __init__(self, application):
         """Initiate the CoreModel object
 
@@ -100,6 +102,8 @@ class CoreModel(GObject.GObject):
 
         self._playlist_model = Gio.ListStore.new(CoreSong)
         self._playlist_model_sort = Gfm.SortListModel.new(self._playlist_model)
+        self._playlist_model_recent = Gfm.SliceListModel.new(
+            self._playlist_model_sort, 0, self._recent_size)
 
         self._songs_search_proxy = Gio.ListStore.new(Gfm.FilterListModel)
         self._songs_search_flatten = Gfm.FlattenListModel.new(CoreSong)
@@ -330,6 +334,18 @@ class CoreModel(GObject.GObject):
     def playlist_sort(self):
         return self._playlist_model_sort
 
+    @GObject.Property(
+        type=Gfm.SliceListModel, default=None,
+        flags=GObject.ParamFlags.READABLE)
+    def playlist_recent(self):
+        return self._playlist_model_recent
+
+    @GObject.Property(
+        type=int, default=None,
+        flags=GObject.ParamFlags.READABLE)
+    def playlist_recent_size(self):
+        return self._recent_size // 2
+
     @GObject.Property(
         type=Gfm.FilterListModel, default=None,
         flags=GObject.ParamFlags.READABLE)
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index 7104fcac..0148ef1d 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -75,7 +75,9 @@ class PlayerPlaylist(GObject.GObject):
         self._discoverer.connect("discovered", self._on_discovered)
         self._discoverer.start()
 
-        self._model = self._app.props.coremodel.props.playlist_sort
+        self._coremodel = self._app.props.coremodel
+        self._model = self._coremodel.props.playlist_sort
+        self._model_recent = self._coremodel.props.playlist_recent
 
         self.connect("notify::repeat-mode", self._on_repeat_mode_changed)
 
@@ -149,6 +151,7 @@ class PlayerPlaylist(GObject.GObject):
         if next_song.props.validation == CoreSong.Validation.FAILED:
             return self.next()
 
+        self._update_model_recent()
         next_song.props.state = SongWidget.State.PLAYING
         self._validate_next_song()
         return True
@@ -177,6 +180,7 @@ class PlayerPlaylist(GObject.GObject):
         if previous_song.props.validation == CoreSong.Validation.FAILED:
             return self.previous()
 
+        self._update_model_recent()
         self._model[previous_position].props.state = SongWidget.State.PLAYING
         self._validate_previous_song()
         return True
@@ -208,6 +212,7 @@ class PlayerPlaylist(GObject.GObject):
         for idx, coresong in enumerate(self._model):
             if coresong.props.state == SongWidget.State.PLAYING:
                 self._position = idx
+                self._update_model_recent()
                 return coresong
 
         return None
@@ -234,6 +239,7 @@ class PlayerPlaylist(GObject.GObject):
             self._position = position
             self._validate_song(song)
             self._validate_next_song()
+            self._update_model_recent()
             return song
 
         for idx, coresong in enumerate(self._model):
@@ -242,10 +248,16 @@ class PlayerPlaylist(GObject.GObject):
                 self._position = idx
                 self._validate_song(song)
                 self._validate_next_song()
+                self._update_model_recent()
                 return song
 
         return None
 
+    def _update_model_recent(self):
+        recent_size = self._coremodel.props.playlist_recent_size
+        offset = max(0, self._position - recent_size)
+        self._model_recent.set_offset(offset)
+
     def _on_repeat_mode_changed(self, klass, param):
         # FIXME: This shuffle is too simple.
         def _shuffle_sort(song_a, song_b):


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