diff --git a/CHANGELOG.md b/CHANGELOG.md index 162636b..a731971 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - [#119](https://github.com/dweymouth/supersonic/issues/119) Add file path column to tracklist view - [#117](https://github.com/dweymouth/supersonic/issues/117) Add (optional) system tray menu and close to tray support - [#55](https://github.com/dweymouth/supersonic/issues/55) Show disc number and disc count for multi-disc albums +- [#115](https://github.com/dweymouth/supersonic/issues/115) Add search bar to artist, genres, and playlists pages ### Fixes - **todo-commithash** Don't show update available prompt if the found version is the same as the running app version diff --git a/sharedutil/sharedutil.go b/sharedutil/sharedutil.go index 3077bdb..1dbbee4 100644 --- a/sharedutil/sharedutil.go +++ b/sharedutil/sharedutil.go @@ -25,6 +25,16 @@ func IntSliceContains(slice []int, i int) bool { return false } +func FilterSlice[T any](ss []T, test func(T) bool) []T { + result := make([]T, 0) + for _, s := range ss { + if test(s) { + result = append(result, s) + } + } + return result +} + func FindTrackByID(id string, tracks []*subsonic.Child) *subsonic.Child { for _, tr := range tracks { if id == tr.ID { diff --git a/ui/browsing/artistsgenrespage.go b/ui/browsing/artistsgenrespage.go index f325f04..40add25 100644 --- a/ui/browsing/artistsgenrespage.go +++ b/ui/browsing/artistsgenrespage.go @@ -2,7 +2,9 @@ package browsing import ( "log" + "strings" "supersonic/backend" + "supersonic/sharedutil" "supersonic/ui/controller" "supersonic/ui/layouts" "supersonic/ui/widgets" @@ -10,6 +12,7 @@ import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/container" + "fyne.io/fyne/v2/layout" "fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/widget" "github.com/dweymouth/go-subsonic/subsonic" @@ -23,12 +26,19 @@ type ArtistsGenresPage struct { isGenresPage bool contr *controller.Controller sm *backend.ServerManager - titleDisp *widget.RichText - container *fyne.Container + model []widgets.ArtistGenreListItemModel list *widgets.ArtistGenreList + + titleDisp *widget.RichText + container *fyne.Container + searcher *widgets.Searcher } func NewArtistsGenresPage(isGenresPage bool, contr *controller.Controller, sm *backend.ServerManager) *ArtistsGenresPage { + return newArtistsGenresPage(isGenresPage, contr, sm, "") +} + +func newArtistsGenresPage(isGenresPage bool, contr *controller.Controller, sm *backend.ServerManager, searchText string) *ArtistsGenresPage { title := "Artists" if isGenresPage { title = "Genres" @@ -51,27 +61,49 @@ func NewArtistsGenresPage(isGenresPage bool, contr *controller.Controller, sm *b a.contr.NavigateTo(controller.ArtistRoute(id)) } } + a.searcher = widgets.NewSearcher() + a.searcher.OnSearched = a.onSearched + a.searcher.Entry.Text = searchText a.buildContainer() - go a.load() + go a.load(searchText != "") return a } // should be called asynchronously -func (a *ArtistsGenresPage) load() { +func (a *ArtistsGenresPage) load(searchOnLoad bool) { if a.isGenresPage { genres, err := a.sm.Server.GetGenres() if err != nil { log.Printf("error loading genres: %v", err.Error()) } - a.list.Items = a.buildGenresListModel(genres) + a.model = a.buildGenresListModel(genres) } else { artists, err := a.sm.Server.GetArtists(nil) if err != nil { log.Printf("error loading artists: %v", err.Error()) } - a.list.Items = a.buildArtistListModel(artists) + a.model = a.buildArtistListModel(artists) } - a.Refresh() + if searchOnLoad { + a.onSearched(a.searcher.Entry.Text) + } else { + a.list.Items = a.model + a.list.Refresh() + } +} + +func (a *ArtistsGenresPage) onSearched(query string) { + // since the artists and genres lists are returned in full non-paginated, we will do our own + // simple search based on the artist/genre name, rather than calling a server API + if query == "" { + a.list.Items = a.model + } else { + result := sharedutil.FilterSlice(a.model, func(x widgets.ArtistGenreListItemModel) bool { + return strings.Contains(strings.ToLower(x.Name), strings.ToLower(query)) + }) + a.list.Items = result + } + a.list.Refresh() } func (a *ArtistsGenresPage) Route() controller.Route { @@ -82,7 +114,7 @@ func (a *ArtistsGenresPage) Route() controller.Route { } func (a *ArtistsGenresPage) Reload() { - go a.load() + go a.load(false) } func (a *ArtistsGenresPage) Save() SavedPage { @@ -90,6 +122,7 @@ func (a *ArtistsGenresPage) Save() SavedPage { isGenresPage: a.isGenresPage, contr: a.contr, sm: a.sm, + searchText: a.searcher.Entry.Text, } } @@ -97,10 +130,11 @@ type savedArtistsGenresPage struct { isGenresPage bool contr *controller.Controller sm *backend.ServerManager + searchText string } func (s *savedArtistsGenresPage) Restore() Page { - return NewArtistsGenresPage(s.isGenresPage, s.contr, s.sm) + return newArtistsGenresPage(s.isGenresPage, s.contr, s.sm, s.searchText) } func (a *ArtistsGenresPage) buildArtistListModel(artists *subsonic.ArtistsID3) []widgets.ArtistGenreListItemModel { @@ -133,9 +167,11 @@ func (a *ArtistsGenresPage) buildGenresListModel(genres []*subsonic.Genre) []wid } func (a *ArtistsGenresPage) buildContainer() { + searchVbox := container.NewVBox(layout.NewSpacer(), a.searcher.Entry, layout.NewSpacer()) a.container = container.New(&layouts.MaxPadLayout{PadLeft: 15, PadRight: 15, PadTop: 5, PadBottom: 15}, container.NewBorder( - container.New(&layouts.MaxPadLayout{PadLeft: -5}, a.titleDisp), + container.New(&layouts.MaxPadLayout{PadLeft: -5}, + container.NewHBox(a.titleDisp, layout.NewSpacer(), searchVbox)), nil, nil, nil, a.list)) } diff --git a/ui/browsing/playlistspage.go b/ui/browsing/playlistspage.go index 7c30f10..9128ca1 100644 --- a/ui/browsing/playlistspage.go +++ b/ui/browsing/playlistspage.go @@ -3,13 +3,16 @@ package browsing import ( "log" "strconv" + "strings" "supersonic/backend" + "supersonic/sharedutil" "supersonic/ui/controller" "supersonic/ui/layouts" "supersonic/ui/widgets" "fyne.io/fyne/v2" "fyne.io/fyne/v2/container" + "fyne.io/fyne/v2/layout" "fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/widget" "github.com/dweymouth/go-subsonic/subsonic" @@ -20,12 +23,19 @@ type PlaylistsPage struct { contr *controller.Controller sm *backend.ServerManager + playlists []*subsonic.Playlist + + searcher *widgets.Searcher titleDisp *widget.RichText container *fyne.Container list *PlaylistList } func NewPlaylistsPage(contr *controller.Controller, sm *backend.ServerManager) *PlaylistsPage { + return newPlaylistsPage(contr, sm, "") +} + +func newPlaylistsPage(contr *controller.Controller, sm *backend.ServerManager, searchText string) *PlaylistsPage { a := &PlaylistsPage{ sm: sm, contr: contr, @@ -37,17 +47,42 @@ func NewPlaylistsPage(contr *controller.Controller, sm *backend.ServerManager) * a.list.OnNavTo = func(id string) { a.contr.NavigateTo(controller.PlaylistRoute(id)) } + a.searcher = widgets.NewSearcher() + a.searcher.OnSearched = a.onSearched + a.searcher.Entry.Text = searchText a.buildContainer() - go a.loadAsync() + go a.load(searchText != "") return a } -func (a *PlaylistsPage) loadAsync() { +func (a *PlaylistsPage) load(searchOnLoad bool) { playlists, err := a.sm.Server.GetPlaylists(nil) if err != nil { log.Printf("error loading playlists: %v", err.Error()) } - a.list.Playlists = playlists + a.playlists = playlists + if searchOnLoad { + a.onSearched(a.searcher.Entry.Text) + } else { + a.list.Playlists = playlists + a.list.Refresh() + } +} + +func (a *PlaylistsPage) onSearched(query string) { + // since the playlist list is returned in full non-paginated, we will do our own + // simple search based on the name, description, and owner, rather than calling a server API + if query == "" { + a.list.Playlists = a.playlists + } else { + result := sharedutil.FilterSlice(a.playlists, func(p *subsonic.Playlist) bool { + qLower := strings.ToLower(query) + return strings.Contains(strings.ToLower(p.Name), qLower) || + strings.Contains(strings.ToLower(p.Comment), qLower) || + strings.Contains(strings.ToLower(p.Owner), qLower) + }) + a.list.Playlists = result + } a.list.Refresh() } @@ -56,28 +91,33 @@ func (a *PlaylistsPage) Route() controller.Route { } func (a *PlaylistsPage) Reload() { - go a.loadAsync() + go a.load(false) } func (a *PlaylistsPage) Save() SavedPage { return &savedPlaylistsPage{ - contr: a.contr, - sm: a.sm, + contr: a.contr, + sm: a.sm, + searchText: a.searcher.Entry.Text, } } type savedPlaylistsPage struct { - contr *controller.Controller - sm *backend.ServerManager + contr *controller.Controller + sm *backend.ServerManager + searchText string } func (s *savedPlaylistsPage) Restore() Page { - return NewPlaylistsPage(s.contr, s.sm) + return newPlaylistsPage(s.contr, s.sm, s.searchText) } func (a *PlaylistsPage) buildContainer() { + searchVbox := container.NewVBox(layout.NewSpacer(), a.searcher.Entry, layout.NewSpacer()) a.container = container.New(&layouts.MaxPadLayout{PadLeft: 15, PadRight: 15, PadTop: 5, PadBottom: 15}, - container.NewBorder(a.titleDisp, nil, nil, nil, a.list)) + container.NewBorder( + container.NewHBox(a.titleDisp, layout.NewSpacer(), searchVbox), + nil, nil, nil, a.list)) } func (a *PlaylistsPage) CreateRenderer() fyne.WidgetRenderer {