db: Have playlists clamp the current track value
And add a get_current_track() function to make it easier to get the currently selected track. Clamping behaves differently if random or loop are enabled to ensure a track is still picked. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
c658e873a6
commit
23699a601d
|
@ -44,6 +44,9 @@ class Playlist(GObject.GObject):
|
|||
f"ORDER BY {order}", [ self.rowid ]).fetchall()
|
||||
return [ track.Table.factory(row) for row in rows ]
|
||||
|
||||
def get_current_track(self):
|
||||
return self.get_track(self.current) if self.current > -1 else None
|
||||
|
||||
def get_track_index(self, track):
|
||||
order = ', '.join(self.plist_state.sort)
|
||||
cur = sql.execute(f"SELECT * FROM (SELECT trackid,ROW_NUMBER() "
|
||||
|
@ -78,7 +81,13 @@ class Playlist(GObject.GObject):
|
|||
def current(self): return self._plstate.get_property("current")
|
||||
|
||||
@current.setter
|
||||
def current(self, newval): self._plstate.set_property("current", newval)
|
||||
def current(self, newval):
|
||||
if newval >= self.get_n_tracks():
|
||||
if self.get_n_tracks() == 0: newval = -1
|
||||
elif self.random: newval = newval % self.get_n_tracks()
|
||||
elif self.loop: newval = 0
|
||||
else: newval = -1
|
||||
self._plstate.set_property("current", newval)
|
||||
|
||||
@GObject.Property(type=bool,default=False)
|
||||
def loop(self): return self._plstate.get_property("loop")
|
||||
|
|
|
@ -12,6 +12,8 @@ class TestRow:
|
|||
return 10
|
||||
|
||||
class TestPlaylist(unittest.TestCase):
|
||||
def setUp(self): db.reset()
|
||||
|
||||
def on_refresh(self, plist): self.refreshed = True
|
||||
|
||||
def test_init(self):
|
||||
|
@ -37,27 +39,68 @@ class TestPlaylist(unittest.TestCase):
|
|||
with self.assertRaises(NotImplementedError):
|
||||
plist.delete()
|
||||
|
||||
def test_passthrough_current(self):
|
||||
def test_current(self):
|
||||
plist = db.user.Table.find("Test Playlist")
|
||||
self.assertEqual(plist.get_property("current"), -1)
|
||||
plist.set_property("current", 5)
|
||||
self.assertEqual(plist.get_property("current"), 5)
|
||||
self.assertEqual(plist.plist_state.get_property("current"), 5)
|
||||
self.assertIsNone(plist.get_current_track())
|
||||
plist.set_property("current", 1)
|
||||
self.assertEqual(plist.get_property("current"), -1)
|
||||
|
||||
def test_passthrough_loop(self):
|
||||
plist.add_track(db.make_fake_track(1, 1, "Track 1", "/a/b/c/1.ogg"))
|
||||
plist.add_track(db.make_fake_track(2, 2, "Track 2", "/a/b/c/2.ogg"))
|
||||
|
||||
plist.set_property("current", 0)
|
||||
self.assertEqual(plist.get_property("current"), 0)
|
||||
self.assertEqual(plist.get_current_track(), plist.get_track(0))
|
||||
|
||||
plist.set_property("current", 1)
|
||||
self.assertEqual(plist.get_property("current"), 1)
|
||||
self.assertEqual(plist.get_current_track(), plist.get_track(1))
|
||||
|
||||
plist.set_property("current", 2)
|
||||
self.assertEqual(plist.get_property("current"), -1)
|
||||
self.assertIsNone(plist.get_current_track())
|
||||
|
||||
def test_loop(self):
|
||||
plist = db.user.Table.find("Test Playlist")
|
||||
self.assertFalse(plist.get_property("loop"))
|
||||
plist.set_property("loop", True)
|
||||
self.assertTrue(plist.get_property("loop"))
|
||||
self.assertTrue(plist.plist_state.get_property("loop"))
|
||||
|
||||
def test_passthrough_random(self):
|
||||
plist.set_property("current", 0)
|
||||
self.assertEqual(plist.get_property("current"), -1)
|
||||
|
||||
plist.add_track(db.make_fake_track(1, 1, "Track 1", "/a/b/c/1.ogg"))
|
||||
plist.add_track(db.make_fake_track(2, 2, "Track 2", "/a/b/c/2.ogg"))
|
||||
|
||||
plist.set_property("current", 0)
|
||||
self.assertEqual(plist.get_current_track(), plist.get_track(0))
|
||||
plist.set_property("current", 1)
|
||||
self.assertEqual(plist.get_current_track(), plist.get_track(1))
|
||||
plist.set_property("current", 2)
|
||||
self.assertEqual(plist.get_property("current"), 0)
|
||||
self.assertEqual(plist.get_current_track(), plist.get_track(0))
|
||||
|
||||
def test_random(self):
|
||||
plist = db.user.Table.find("Test Playlist")
|
||||
self.assertFalse(plist.get_property("random"))
|
||||
plist.set_property("random", True)
|
||||
self.assertTrue(plist.get_property("random"))
|
||||
self.assertTrue(plist.plist_state.get_property("random"))
|
||||
|
||||
plist.set_property("current", 0)
|
||||
self.assertEqual(plist.get_property("current"), -1)
|
||||
|
||||
plist.add_track(db.make_fake_track(1, 1, "Track 1", "/a/b/c/1.ogg"))
|
||||
plist.add_track(db.make_fake_track(2, 2, "Track 2", "/a/b/c/2.ogg"))
|
||||
|
||||
plist.set_property("current", 0)
|
||||
self.assertEqual(plist.get_current_track(), plist.get_track(0))
|
||||
plist.set_property("current", 5)
|
||||
self.assertEqual(plist.get_property("current"), 1)
|
||||
self.assertEqual(plist.get_current_track(), plist.get_track(1))
|
||||
|
||||
def test_passthrough_sort(self):
|
||||
plist = db.user.Table.find("Test Playlist")
|
||||
plist.connect("refreshed", self.on_refresh)
|
||||
|
|
Loading…
Reference in New Issue