tracklist: Commit the database after adding, moving, or removing tracks

Otherwise we could lose the changes if the app crashes.

Fixes: #63 ("The database isn't being committed enough")
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2023-06-07 13:49:58 -04:00
parent cae93cae11
commit c5f9608c49
2 changed files with 38 additions and 24 deletions

View File

@ -152,6 +152,7 @@ class MoveButtons(Gtk.Box):
class OSD(Gtk.Overlay):
"""An Overlay with extra controls for the Tracklist."""
sql = GObject.Property(type=db.Connection)
playlist = GObject.Property(type=playlist.playlist.Playlist)
selection = GObject.Property(type=Gtk.SelectionModel)
@ -161,7 +162,7 @@ class OSD(Gtk.Overlay):
def __init__(self, sql: db.Connection,
selection: Gtk.SelectionModel, **kwargs):
"""Initialize an OSD."""
super().__init__(selection=selection, **kwargs)
super().__init__(sql=sql, selection=selection, **kwargs)
self._add = PopoverButton(child=Adw.ButtonContent(label="Add",
icon_name="list-add-symbolic"),
halign=Gtk.Align.START, valign=Gtk.Align.END,
@ -206,12 +207,14 @@ class OSD(Gtk.Overlay):
playlist: db.playlists.Playlist) -> None:
for track in self.__get_selected_tracks():
playlist.add_track(track)
self.sql.commit()
self.clear_selection()
def __remove_clicked(self, button: Gtk.Button) -> None:
if self.playlist is not None:
for track in self.__get_selected_tracks():
self.playlist.remove_track(track)
self.sql.commit()
self.clear_selection()
def __move_track_down(self, move: MoveButtons) -> None:
@ -219,6 +222,7 @@ class OSD(Gtk.Overlay):
index = self.selection.get_selection().get_nth(0)
self.selection.get_model().set_incremental(False)
self.playlist.move_track_down(self.selection[index])
self.sql.commit()
self.selection.get_model().set_incremental(True)
self.__update_visibility()
@ -227,6 +231,7 @@ class OSD(Gtk.Overlay):
index = self.selection.get_selection().get_nth(0)
self.selection.get_model().set_incremental(False)
self.playlist.move_track_up(self.selection[index])
self.sql.commit()
self.selection.get_model().set_incremental(True)
self.__update_visibility()

View File

@ -249,6 +249,7 @@ class TestOsd(tests.util.TestCase):
self.assertIsInstance(self.osd, Gtk.Overlay)
self.assertIsInstance(self.osd._sizegroup, Gtk.SizeGroup)
self.assertEqual(self.osd.selection, self.selection)
self.assertEqual(self.osd.sql, self.sql)
self.assertIsNone(self.osd.playlist)
self.assertEqual(self.osd._sizegroup.get_mode(),
@ -296,12 +297,14 @@ class TestOsd(tests.util.TestCase):
with unittest.mock.patch.object(self.db_plist,
"add_track") as mock_add:
self.selection.select_all()
self.osd._add.popover_child.emit("playlist-selected",
self.db_plist)
mock_add.assert_has_calls([unittest.mock.call(self.model[0]),
unittest.mock.call(self.model[1]),
unittest.mock.call(self.model[2])])
self.assertEqual(self.osd.n_selected, 0)
with unittest.mock.patch.object(self.sql, "commit") as mock_commit:
self.osd._add.popover_child.emit("playlist-selected",
self.db_plist)
mock_add.assert_has_calls([unittest.mock.call(self.model[0]),
unittest.mock.call(self.model[1]),
unittest.mock.call(self.model[2])])
mock_commit.assert_called_once()
self.assertEqual(self.osd.n_selected, 0)
def test_remove_button(self):
"""Test the remove tracks button."""
@ -341,16 +344,18 @@ class TestOsd(tests.util.TestCase):
"""Test clicking the remove button."""
with unittest.mock.patch.object(self.db_plist,
"remove_track") as mock_remove:
self.selection.select_all()
self.osd._remove.emit("clicked")
mock_remove.assert_not_called()
with unittest.mock.patch.object(self.sql, "commit") as mock_commit:
self.selection.select_all()
self.osd._remove.emit("clicked")
mock_remove.assert_not_called()
mock_commit.assert_not_called()
self.osd.playlist = self.playlist
self.selection.select_all()
self.osd._remove.emit("clicked")
mock_remove.assert_has_calls([unittest.mock.call(self.model[0]),
unittest.mock.call(self.model[1]),
unittest.mock.call(self.model[2])])
self.osd.playlist = self.playlist
self.selection.select_all()
self.osd._remove.emit("clicked")
mock_remove.assert_has_calls([unittest.mock.call(self.model[i])
for i in range(3)])
mock_commit.assert_called_once()
def test_move_buttons(self):
"""Test the move buttons."""
@ -372,18 +377,22 @@ class TestOsd(tests.util.TestCase):
with unittest.mock.patch.object(self.playlist,
"move_track_down") as mock_move_down:
self.osd._move.emit("move-down")
mock_move_down.assert_called_with(self.model[1])
set_incremental.assert_has_calls([unittest.mock.call(False),
unittest.mock.call(True)])
with unittest.mock.patch.object(self.sql, "commit") as mock_commit:
self.osd._move.emit("move-down")
mock_move_down.assert_called_with(self.model[1])
set_incremental.assert_has_calls([unittest.mock.call(False),
unittest.mock.call(True)])
mock_commit.assert_called_once()
set_incremental.reset_mock()
with unittest.mock.patch.object(self.playlist,
"move_track_up") as mock_move_up:
self.osd._move.emit("move-up")
mock_move_up.assert_called_with(self.model[1])
set_incremental.assert_has_calls([unittest.mock.call(False),
unittest.mock.call(True)])
with unittest.mock.patch.object(self.sql, "commit") as mock_commit:
self.osd._move.emit("move-up")
mock_move_up.assert_called_with(self.model[1])
set_incremental.assert_has_calls([unittest.mock.call(False),
unittest.mock.call(True)])
mock_commit.assert_called_once()
def test_move_buttons_sensitive(self):
"""Test the move button sensitivity."""