sidebar: Add the Artist section to the sidebar

I make sure to save the "show-all" property to the settings so it can be
preserved across sessions.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2022-08-22 16:07:37 -04:00
parent 2f239bd94d
commit c396839316
4 changed files with 52 additions and 3 deletions

View File

@ -97,7 +97,10 @@ class Application(Adw.Application):
def build_sidebar(self) -> sidebar.Card: def build_sidebar(self) -> sidebar.Card:
"""Build a new sidebar card.""" """Build a new sidebar card."""
return sidebar.Card(sql=self.db) side_bar = sidebar.Card(sql=self.db)
self.db.settings.bind_setting("sidebar.artists.show-all", side_bar,
"show-all-artists")
return side_bar
def build_window(self) -> window.Window: def build_window(self) -> window.Window:
"""Build a new window instance.""" """Build a new window instance."""

View File

@ -2,6 +2,7 @@
"""A card for displaying the list of playlists.""" """A card for displaying the list of playlists."""
from gi.repository import GObject from gi.repository import GObject
from gi.repository import Gtk from gi.repository import Gtk
from . import artist
from . import playlist from . import playlist
from . import section from . import section
from .. import db from .. import db
@ -12,6 +13,7 @@ class Card(Gtk.Box):
"""Our playlist Sidebar.""" """Our playlist Sidebar."""
sql = GObject.Property(type=db.Connection) sql = GObject.Property(type=db.Connection)
show_all_artists = GObject.Property(type=bool, default=False)
selected_playlist = GObject.Property(type=db.playlist.Playlist) selected_playlist = GObject.Property(type=db.playlist.Playlist)
def __init__(self, sql: db.Connection, **kwargs): def __init__(self, sql: db.Connection, **kwargs):
@ -20,16 +22,19 @@ class Card(Gtk.Box):
sensitive=False, **kwargs) sensitive=False, **kwargs)
self._filter = entry.Filter("playlists") self._filter = entry.Filter("playlists")
self._playlists = playlist.Section(self.sql.playlists) self._playlists = playlist.Section(self.sql.playlists)
self._artists = artist.Section(self.sql.artists, self.sql.albums)
self._group = section.Group(sql) self._group = section.Group(sql)
self.append(self._filter) self.append(self._filter)
for sect in [self._playlists]: for sect in [self._playlists, self._artists]:
self.append(sect) self.append(sect)
self._group.add(sect) self._group.add(sect)
self._group.bind_property("selected-playlist", self._group.bind_property("selected-playlist",
self, "selected-playlist") self, "selected-playlist")
self.bind_property("show-all-artists", self._artists, "show-all",
GObject.BindingFlags.BIDIRECTIONAL)
self._filter.connect("search-changed", self.__search_changed) self._filter.connect("search-changed", self.__search_changed)
self.sql.connect("table-loaded", self.__table_loaded) self.sql.connect("table-loaded", self.__table_loaded)
@ -53,6 +58,8 @@ class Card(Gtk.Box):
match playlist.table: match playlist.table:
case self.sql.playlists: case self.sql.playlists:
section = self._playlists section = self._playlists
case self.sql.artists | self.sql.albums | self.sql.media:
section = self._artists
section.active = True section.active = True
section.select_playlist(playlist) section.select_playlist(playlist)

View File

@ -21,6 +21,7 @@ class TestSidebar(tests.util.TestCase):
self.assertEqual(self.sidebar.sql, self.sql) self.assertEqual(self.sidebar.sql, self.sql)
self.assertEqual(self.sidebar.get_orientation(), self.assertEqual(self.sidebar.get_orientation(),
Gtk.Orientation.VERTICAL) Gtk.Orientation.VERTICAL)
self.assertFalse(self.sidebar.get_sensitive())
self.assertTrue(self.sidebar.has_css_class("background")) self.assertTrue(self.sidebar.has_css_class("background"))
self.assertTrue(self.sidebar.has_css_class("linked")) self.assertTrue(self.sidebar.has_css_class("linked"))
@ -56,6 +57,14 @@ class TestSidebar(tests.util.TestCase):
self.sql.emit("table-loaded", tables[0]) self.sql.emit("table-loaded", tables[0])
self.sidebar.select_playlist.assert_not_called() self.sidebar.select_playlist.assert_not_called()
def test_show_all_artists(self):
"""Test the show-all-artists property."""
self.assertFalse(self.sidebar.show_all_artists)
self.sidebar.show_all_artists = True
self.assertTrue(self.sidebar._artists.show_all)
self.sidebar._artists.show_all = False
self.assertFalse(self.sidebar.show_all_artists)
def test_selected_playlist(self): def test_selected_playlist(self):
"""Test the selected-playlist property.""" """Test the selected-playlist property."""
self.assertIsNone(self.sidebar.selected_playlist) self.assertIsNone(self.sidebar.selected_playlist)
@ -70,17 +79,24 @@ class TestSidebar(tests.util.TestCase):
emmental.sidebar.section.Group) emmental.sidebar.section.Group)
self.assertListEqual(self.sidebar._group._sections, self.assertListEqual(self.sidebar._group._sections,
[self.sidebar._playlists]) [self.sidebar._playlists,
self.sidebar._artists])
def test_sections(self): def test_sections(self):
"""Tests sidebar section models.""" """Tests sidebar section models."""
self.assertIsInstance(self.sidebar._playlists, self.assertIsInstance(self.sidebar._playlists,
emmental.sidebar.playlist.Section) emmental.sidebar.playlist.Section)
self.assertIsInstance(self.sidebar._artists,
emmental.sidebar.artist.Section)
self.assertEqual(self.sidebar._filter.get_next_sibling(), self.assertEqual(self.sidebar._filter.get_next_sibling(),
self.sidebar._playlists) self.sidebar._playlists)
self.assertEqual(self.sidebar._playlists.get_next_sibling(),
self.sidebar._artists)
self.assertEqual(self.sidebar._playlists.table, self.sql.playlists) self.assertEqual(self.sidebar._playlists.table, self.sql.playlists)
self.assertEqual(self.sidebar._artists.table, self.sql.artists)
self.assertEqual(self.sidebar._artists.album_table, self.sql.albums)
def test_select_playlist(self): def test_select_playlist(self):
"""Test setting the active playlist.""" """Test setting the active playlist."""
@ -88,3 +104,16 @@ class TestSidebar(tests.util.TestCase):
self.sidebar.select_playlist(playlist) self.sidebar.select_playlist(playlist)
self.assertTrue(self.sidebar._playlists.active) self.assertTrue(self.sidebar._playlists.active)
self.assertEqual(self.sidebar.selected_playlist, playlist) self.assertEqual(self.sidebar.selected_playlist, playlist)
artist = self.sql.artists.create("Test Artist")
album = self.sql.albums.create("Test Album", "Test Artist", "2023")
medium = self.sql.media.create(album, "Test Medium", number=1)
self.sidebar._artists.select_playlist = unittest.mock.Mock()
for plist in [artist, album, medium]:
self.sidebar._artists.select_playlist.reset_mock()
self.sidebar._artists.active = False
self.sidebar.select_playlist(plist)
self.assertTrue(self.sidebar._artists.active)
self.sidebar._artists.select_playlist.assert_called_with(plist)

View File

@ -108,3 +108,13 @@ class TestSettings(unittest.TestCase):
self.assertEqual(self.settings["sidebar.size"], 400) self.assertEqual(self.settings["sidebar.size"], 400)
self.assertEqual(self.app.build_window().sidebar_size, 400) self.assertEqual(self.app.build_window().sidebar_size, 400)
def test_save_sidebar_show_all_artists(self, mock_stdout: io.StringIO):
"""Check saving and loading the show-all artists setting."""
self.assertFalse(self.win.sidebar.show_all_artists)
self.assertFalse(self.settings["sidebar.artists.show-all"])
self.win.sidebar.show_all_artists = True
self.assertTrue(self.settings["sidebar.artists.show-all"])
self.assertTrue(self.app.build_window().sidebar.show_all_artists)