db: Add favorite track accounting
This patch adds extra handling for changing the value of the Track:favorite property, if the Track is the current track. This lets us have the Now Playing card bind to the current-favorite property to update the UI (and have the UI update the Track). Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
dc8ccff311
commit
8a16b4e05f
|
@ -95,19 +95,31 @@ class Table(table.Table):
|
||||||
|
|
||||||
have_current_track = GObject.Property(type=bool, default=False)
|
have_current_track = GObject.Property(type=bool, default=False)
|
||||||
current_track = GObject.Property(type=Track)
|
current_track = GObject.Property(type=Track)
|
||||||
|
current_favorite = GObject.Property(type=bool, default=False)
|
||||||
|
|
||||||
def __init__(self, sql: GObject.TYPE_PYOBJECT):
|
def __init__(self, sql: GObject.TYPE_PYOBJECT):
|
||||||
"""Initialize a Track Table."""
|
"""Initialize a Track Table."""
|
||||||
super().__init__(sql, filter=Filter())
|
super().__init__(sql, filter=Filter())
|
||||||
self.set_model(None)
|
self.set_model(None)
|
||||||
self.connect("notify::current-track", self.__notify_current_track)
|
self.connect("notify::current-track", self.__notify_current_track)
|
||||||
|
self.connect("notify::current-favorite",
|
||||||
|
self.__notify_current_favorite)
|
||||||
|
|
||||||
def __notify_current_track(self, table: table.Table, param) -> None:
|
def __notify_current_track(self, table: table.Table, param) -> None:
|
||||||
if self.current_track is not None:
|
if self.current_track is not None:
|
||||||
self.have_current_track = True
|
self.have_current_track = True
|
||||||
|
self.current_favorite = self.current_track.favorite
|
||||||
self.sql.playlists.previous.add_track(self.current_track)
|
self.sql.playlists.previous.add_track(self.current_track)
|
||||||
else:
|
else:
|
||||||
self.have_current_track = False
|
self.have_current_track = False
|
||||||
|
self.current_favorite = False
|
||||||
|
|
||||||
|
def __notify_current_favorite(self, table: table.Table, param) -> None:
|
||||||
|
if self.current_track is not None:
|
||||||
|
self.current_track.update_properties(
|
||||||
|
favorite=self.current_favorite)
|
||||||
|
elif self.current_favorite is True:
|
||||||
|
self.current_favorite = False
|
||||||
|
|
||||||
def do_construct(self, **kwargs) -> Track:
|
def do_construct(self, **kwargs) -> Track:
|
||||||
"""Construct a new Track instance."""
|
"""Construct a new Track instance."""
|
||||||
|
@ -166,8 +178,12 @@ class Table(table.Table):
|
||||||
match (column, newval):
|
match (column, newval):
|
||||||
case ("favorite", True):
|
case ("favorite", True):
|
||||||
self.sql.playlists.favorites.add_track(track)
|
self.sql.playlists.favorites.add_track(track)
|
||||||
|
if track == self.current_track:
|
||||||
|
self.current_favorite = True
|
||||||
case ("favorite", False):
|
case ("favorite", False):
|
||||||
self.sql.playlists.favorites.remove_track(track)
|
self.sql.playlists.favorites.remove_track(track)
|
||||||
|
if track == self.current_track:
|
||||||
|
self.current_favorite = False
|
||||||
|
|
||||||
return self.sql(f"UPDATE tracks SET {column}=? WHERE trackid=?",
|
return self.sql(f"UPDATE tracks SET {column}=? WHERE trackid=?",
|
||||||
newval, track.trackid)
|
newval, track.trackid)
|
||||||
|
|
|
@ -511,7 +511,7 @@ class TestTrackTable(tests.util.TestCase):
|
||||||
self.assertTrue(self.tracks.have_current_track)
|
self.assertTrue(self.tracks.have_current_track)
|
||||||
self.playlists.previous.add_track.assert_called_with(track)
|
self.playlists.previous.add_track.assert_called_with(track)
|
||||||
|
|
||||||
track2 = self.tracks.construct(trackid=2, active=True, favorite=True)
|
track2 = self.tracks.construct(trackid=2, active=True)
|
||||||
self.assertEqual(self.tracks.current_track, track2)
|
self.assertEqual(self.tracks.current_track, track2)
|
||||||
self.assertTrue(self.tracks.have_current_track)
|
self.assertTrue(self.tracks.have_current_track)
|
||||||
self.playlists.previous.add_track.assert_called_with(track2)
|
self.playlists.previous.add_track.assert_called_with(track2)
|
||||||
|
@ -520,3 +520,27 @@ class TestTrackTable(tests.util.TestCase):
|
||||||
self.tracks.current_track = None
|
self.tracks.current_track = None
|
||||||
self.assertFalse(self.tracks.have_current_track)
|
self.assertFalse(self.tracks.have_current_track)
|
||||||
self.playlists.previous.add_track.assert_not_called()
|
self.playlists.previous.add_track.assert_not_called()
|
||||||
|
|
||||||
|
def test_current_favorite(self):
|
||||||
|
"""Test the current-favorite property."""
|
||||||
|
self.assertFalse(self.tracks.current_favorite)
|
||||||
|
|
||||||
|
self.tracks.current_favorite = True
|
||||||
|
self.assertFalse(self.tracks.current_favorite)
|
||||||
|
|
||||||
|
track = self.tracks.construct(trackid=2, active=True, favorite=True)
|
||||||
|
self.assertEqual(self.tracks.current_track, track)
|
||||||
|
self.assertTrue(self.tracks.current_favorite)
|
||||||
|
|
||||||
|
track.favorite = False
|
||||||
|
self.assertFalse(self.tracks.current_favorite)
|
||||||
|
track.favorite = True
|
||||||
|
self.assertTrue(self.tracks.current_favorite)
|
||||||
|
|
||||||
|
self.tracks.current_favorite = False
|
||||||
|
self.assertFalse(track.favorite)
|
||||||
|
self.tracks.current_favorite = True
|
||||||
|
self.assertTrue(track.favorite)
|
||||||
|
|
||||||
|
self.tracks.current_track = None
|
||||||
|
self.assertFalse(self.tracks.current_favorite)
|
||||||
|
|
Loading…
Reference in New Issue