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:
Anna Schumaker 2022-09-23 09:16:54 -04:00
parent dc8ccff311
commit 8a16b4e05f
2 changed files with 41 additions and 1 deletions

View File

@ -95,19 +95,31 @@ class Table(table.Table):
have_current_track = GObject.Property(type=bool, default=False)
current_track = GObject.Property(type=Track)
current_favorite = GObject.Property(type=bool, default=False)
def __init__(self, sql: GObject.TYPE_PYOBJECT):
"""Initialize a Track Table."""
super().__init__(sql, filter=Filter())
self.set_model(None)
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:
if self.current_track is not None:
self.have_current_track = True
self.current_favorite = self.current_track.favorite
self.sql.playlists.previous.add_track(self.current_track)
else:
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:
"""Construct a new Track instance."""
@ -166,8 +178,12 @@ class Table(table.Table):
match (column, newval):
case ("favorite", True):
self.sql.playlists.favorites.add_track(track)
if track == self.current_track:
self.current_favorite = True
case ("favorite", False):
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=?",
newval, track.trackid)

View File

@ -511,7 +511,7 @@ class TestTrackTable(tests.util.TestCase):
self.assertTrue(self.tracks.have_current_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.assertTrue(self.tracks.have_current_track)
self.playlists.previous.add_track.assert_called_with(track2)
@ -520,3 +520,27 @@ class TestTrackTable(tests.util.TestCase):
self.tracks.current_track = None
self.assertFalse(self.tracks.have_current_track)
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)