playlist: Give Playlists a can-go-next property
This property is updated whenever the items in the Playlist change or if the current-track property is changed. It can be used to know in advance if calling next_track() can be expected to return a valid Track instance. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
6592b97cbd
commit
14bcef6e52
|
@ -10,6 +10,8 @@ FLAGS = GObject.ParamFlags.READWRITE | GObject.ParamFlags.EXPLICIT_NOTIFY
|
||||||
class Playlist(model.TrackidModel):
|
class Playlist(model.TrackidModel):
|
||||||
"""A TrackidModel with extra Playlist features."""
|
"""A TrackidModel with extra Playlist features."""
|
||||||
|
|
||||||
|
can_go_next = GObject.Property(type=bool, default=False)
|
||||||
|
|
||||||
def __init__(self, sql: db.Connection,
|
def __init__(self, sql: db.Connection,
|
||||||
playlist: db.playlist.Playlist = None):
|
playlist: db.playlist.Playlist = None):
|
||||||
"""Initialize the Playlist instance."""
|
"""Initialize the Playlist instance."""
|
||||||
|
@ -23,6 +25,9 @@ class Playlist(model.TrackidModel):
|
||||||
if playlist is not None:
|
if playlist is not None:
|
||||||
self.playlist = playlist
|
self.playlist = playlist
|
||||||
|
|
||||||
|
self.connect("items-changed", self.__update_can_go_next)
|
||||||
|
self.connect("notify::current-track", self.__update_can_go_next)
|
||||||
|
|
||||||
def __get_nth_track(self, n: int | None) -> db.tracks.Track | None:
|
def __get_nth_track(self, n: int | None) -> db.tracks.Track | None:
|
||||||
return self[n] if n is not None and n < len(self.trackids) else None
|
return self[n] if n is not None and n < len(self.trackids) else None
|
||||||
|
|
||||||
|
@ -42,6 +47,13 @@ class Playlist(model.TrackidModel):
|
||||||
self.on_trackids_reset(plist.tracks)
|
self.on_trackids_reset(plist.tracks)
|
||||||
self.notify("sort-order")
|
self.notify("sort-order")
|
||||||
|
|
||||||
|
def __update_can_go_next(self, *args) -> None:
|
||||||
|
if len(self.trackids) == 0:
|
||||||
|
self.can_go_next = False
|
||||||
|
else:
|
||||||
|
current = self.__playlist.current_trackid
|
||||||
|
self.can_go_next = current != self.trackids[-1]
|
||||||
|
|
||||||
def __track_moved(self, track: db.tracks.Track, *, offset: int) -> None:
|
def __track_moved(self, track: db.tracks.Track, *, offset: int) -> None:
|
||||||
index = self.index(track)
|
index = self.index(track)
|
||||||
new_pos = index + offset
|
new_pos = index + offset
|
||||||
|
|
|
@ -56,6 +56,25 @@ class TestPlaylist(tests.util.TestCase):
|
||||||
plist2 = emmental.playlist.playlist.Playlist(self.sql, self.db_plist)
|
plist2 = emmental.playlist.playlist.Playlist(self.sql, self.db_plist)
|
||||||
self.assertEqual(plist2.playlist, self.db_plist)
|
self.assertEqual(plist2.playlist, self.db_plist)
|
||||||
|
|
||||||
|
def test_can_go_next(self):
|
||||||
|
"""Test the playlist can_go_next() property."""
|
||||||
|
self.assertFalse(self.playlist.can_go_next)
|
||||||
|
|
||||||
|
self.playlist.playlist = self.db_plist
|
||||||
|
self.playlist.add_track(self.track1)
|
||||||
|
self.assertTrue(self.playlist.can_go_next)
|
||||||
|
|
||||||
|
self.playlist.add_track(self.track2)
|
||||||
|
self.playlist.next_track()
|
||||||
|
self.assertTrue(self.playlist.can_go_next)
|
||||||
|
self.playlist.next_track()
|
||||||
|
self.assertFalse(self.playlist.can_go_next)
|
||||||
|
|
||||||
|
self.playlist.move_track_up(self.track2)
|
||||||
|
self.assertTrue(self.playlist.can_go_next)
|
||||||
|
self.playlist.move_track_down(self.track2)
|
||||||
|
self.assertFalse(self.playlist.can_go_next)
|
||||||
|
|
||||||
def test_add_track(self):
|
def test_add_track(self):
|
||||||
"""Test the playlist add_track() function."""
|
"""Test the playlist add_track() function."""
|
||||||
self.playlist.add_track(self.track1)
|
self.playlist.add_track(self.track1)
|
||||||
|
|
Loading…
Reference in New Issue