[gnome-music/wip/mschraal/core: 78/92] player: make prev/next work



commit c7fda16748e24c0838e4f2a202a4f509d9d789d5
Author: Marinus Schraal <mschraal gnome org>
Date:   Sun Jun 2 21:17:47 2019 +0200

    player: make prev/next work

 gnomemusic/player.py | 73 ++++++++++++++++++++++++----------------------------
 1 file changed, 33 insertions(+), 40 deletions(-)
---
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index 789d78d8..58261ee8 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -373,13 +373,12 @@ class PlayerPlaylist(GObject.GObject):
         :return: True if there is a song. False otherwise.
         :rtype: bool
         """
-        if (self.props.repeat_mode == RepeatMode.SHUFFLE
-                and self._shuffle_indexes):
-            index = self._shuffle_indexes.index(self._current_index)
-            return index < (len(self._shuffle_indexes) - 1)
-        if self.props.repeat_mode != RepeatMode.NONE:
-            return True
-        return self._current_index < (len(self._songs) - 1)
+        for idx, coresong in enumerate(self._model):
+            if coresong.props.media == self.props.current_song:
+                if idx < len(self._model) - 1:
+                    return True
+
+        return False
 
     @log
     def has_previous(self):
@@ -388,13 +387,12 @@ class PlayerPlaylist(GObject.GObject):
         :return: True if there is a song. False otherwise.
         :rtype: bool
         """
-        if (self.props.repeat_mode == RepeatMode.SHUFFLE
-                and self._shuffle_indexes):
-            index = self._shuffle_indexes.index(self._current_index)
-            return index > 0
-        if self.props.repeat_mode != RepeatMode.NONE:
-            return True
-        return self._current_index > 0
+        for idx, coresong in enumerate(self._model):
+            if coresong.props.media == self.props.current_song:
+                if idx > 0:
+                    return True
+
+        return False
 
     @log
     def next(self):
@@ -403,14 +401,12 @@ class PlayerPlaylist(GObject.GObject):
         :return: True if the operation succeeded. False otherwise.
         :rtype: bool
         """
-        next_index = self._get_next_index()
-        if next_index >= 0:
-            self._current_index = next_index
-            if self._current_song_is_valid():
-                self._validate_next_song()
+        for idx, coresong in enumerate(self._model):
+            if coresong.props.media == self.props.current_song:
+                coresong.props.state = SongWidget.State.PLAYED
+                self._model[idx+1].props.state = SongWidget.State.PLAYING
                 return True
-            else:
-                return self.next()
+
         return False
 
     @log
@@ -420,14 +416,12 @@ class PlayerPlaylist(GObject.GObject):
         :return: True if the operation succeeded. False otherwise.
         :rtype: bool
         """
-        previous_index = self._get_previous_index()
-        if previous_index >= 0:
-            self._current_index = previous_index
-            if self._current_song_is_valid():
-                self._validate_previous_song()
+        for idx, coresong in enumerate(self._model):
+            if coresong.props.media == self.props.current_song:
+                coresong.props.state = SongWidget.State.PLAYED
+                self._model[idx-1].props.state = SongWidget.State.PLAYING
                 return True
-            else:
-                return self.previous()
+
         return False
 
     @GObject.Property(type=int, default=0, flags=GObject.ParamFlags.READABLE)
@@ -448,17 +442,11 @@ class PlayerPlaylist(GObject.GObject):
         :rtype: Grl.Media
         """
         for coresong in self._model:
-            print("CORESONG", coresong.props.state, SongWidget.State.PLAYING)
             if coresong.props.state == SongWidget.State.PLAYING:
-                print("RETURN CURRENT SONG")
                 return coresong.props.media
 
         return None
 
-        # if self._songs:
-        #     return self._songs[self._current_index][PlayerField.SONG]
-        # return None
-
     def _current_song_is_valid(self):
         """Check if current song can be played.
 
@@ -652,7 +640,7 @@ class Player(GObject.GObject):
         self._gst_player.props.url = song.get_url()
         print(song.get_url())
 
-        # self.emit('song-changed')
+        self.emit('song-changed')
 
     @log
     def play(self, song_changed=True, song_offset=None, media=None):
@@ -664,12 +652,17 @@ class Player(GObject.GObject):
         :param bool song_changed: indicate if a new song must be loaded
         :param int song_offset: position relative to current song
         """
+        if self.props.current_song is None:
+            return
+
+        if media is None:
+            media = self._playlist.props.current_song
+
         self._load2(media)
 
         self._gst_player.props.state = Playback.PLAYING
         return
-        if self.props.current_song is None:
-            return
+
 
         if (song_offset is not None
                 and not self._playlist.set_song(song_offset)):
@@ -697,7 +690,7 @@ class Player(GObject.GObject):
         Play the next song of the playlist, if any.
         """
         if self._playlist.next():
-            self.play()
+            self.play(None, None, self._playlist.props.current_song)
 
     @log
     def previous(self):
@@ -711,7 +704,7 @@ class Player(GObject.GObject):
             return
 
         if self._playlist.previous():
-            self.play()
+            self.play(None, None, self._playlist.props.current_song)
 
     @log
     def play_pause(self):
@@ -800,7 +793,7 @@ class Player(GObject.GObject):
             tick, self._gst_player.props.position))
 
         current_song = self._playlist.props.current_song
-        print("TICK", current_song)
+
         if tick == 0:
             self._new_clock = True
             self._lastfm.now_playing(current_song)


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