misc: Decouple Grid view from Albums
The Grid view and related components were highly coupled to Albums, including the filter button that is currently used only for album filtering. As part of the migration of Artists to Grid view, this is the smallest possible change to convert many of the existing code to generics/interfaces that will allow different media to be displayed and filtered by using Grid views. The main changes in this diff are: * Introduction of generics for Media types (`M`) and Filter options (`F`), which can be later extended to other entities besides Albums. * Complete decoupling of `GridViewPage` and related components from Albums (making these pages also generic). * Refactoring of Subsonic/Jellyfin specific code to understand these new generics when dealing with filtering logic.
This commit is contained in:
@@ -7,17 +7,12 @@ import (
|
||||
"github.com/dweymouth/supersonic/sharedutil"
|
||||
)
|
||||
|
||||
type Filter[T any] interface {
|
||||
IsNil() bool
|
||||
Matches(*T) bool
|
||||
}
|
||||
|
||||
type baseIter[T any] struct {
|
||||
filter Filter[T]
|
||||
prefetchCB func(*T)
|
||||
type baseIter[M, F any] struct {
|
||||
filter mediaprovider.MediaFilter[M, F]
|
||||
prefetchCB func(*M)
|
||||
serverPos int
|
||||
fetcher func(offset, limit int) ([]*T, error)
|
||||
prefetched []*T
|
||||
fetcher func(offset, limit int) ([]*M, error)
|
||||
prefetched []*M
|
||||
prefetchedPos int
|
||||
done bool
|
||||
}
|
||||
@@ -25,7 +20,7 @@ type baseIter[T any] struct {
|
||||
type AlbumFetchFn func(offset, limit int) ([]*mediaprovider.Album, error)
|
||||
|
||||
func NewAlbumIterator(fetchFn AlbumFetchFn, filter mediaprovider.AlbumFilter, cb func(string)) mediaprovider.AlbumIterator {
|
||||
return &baseIter[mediaprovider.Album]{
|
||||
return &baseIter[mediaprovider.Album, mediaprovider.AlbumFilterOptions]{
|
||||
prefetchCB: func(a *mediaprovider.Album) { cb(a.CoverArtID) },
|
||||
filter: filter,
|
||||
fetcher: fetchFn,
|
||||
@@ -35,7 +30,7 @@ func NewAlbumIterator(fetchFn AlbumFetchFn, filter mediaprovider.AlbumFilter, cb
|
||||
type ArtistFetchFn func(offset, limit int) ([]*mediaprovider.Artist, error)
|
||||
|
||||
func NewArtistIterator(fetchFn ArtistFetchFn) mediaprovider.ArtistIterator {
|
||||
return &baseIter[mediaprovider.Artist]{
|
||||
return &baseIter[mediaprovider.Artist, nilFilterOptions]{
|
||||
fetcher: fetchFn,
|
||||
filter: nilFilter[mediaprovider.Artist]{},
|
||||
}
|
||||
@@ -44,14 +39,14 @@ func NewArtistIterator(fetchFn ArtistFetchFn) mediaprovider.ArtistIterator {
|
||||
type TrackFetchFn func(offset, limit int) ([]*mediaprovider.Track, error)
|
||||
|
||||
func NewTrackIterator(fetchFn TrackFetchFn, cb func(string)) mediaprovider.TrackIterator {
|
||||
return &baseIter[mediaprovider.Track]{
|
||||
return &baseIter[mediaprovider.Track, nilFilterOptions]{
|
||||
prefetchCB: func(a *mediaprovider.Track) { cb(a.CoverArtID) },
|
||||
filter: nilFilter[mediaprovider.Track]{},
|
||||
fetcher: fetchFn,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *baseIter[T]) Next() *T {
|
||||
func (r *baseIter[M, F]) Next() *M {
|
||||
if r.done {
|
||||
return nil
|
||||
}
|
||||
@@ -89,7 +84,7 @@ func (r *baseIter[T]) Next() *T {
|
||||
return r.prefetched[0]
|
||||
}
|
||||
|
||||
type randomIter struct {
|
||||
type randomAlbumIter struct {
|
||||
filter mediaprovider.AlbumFilter
|
||||
prefetchCB func(coverArtID string)
|
||||
albumIDSet map[string]bool
|
||||
@@ -108,8 +103,8 @@ type randomIter struct {
|
||||
done bool
|
||||
}
|
||||
|
||||
func NewRandomAlbumIter(deterministicFetcher, randomFetcher AlbumFetchFn, filter mediaprovider.AlbumFilter, prefetchCoverCB func(string)) *randomIter {
|
||||
return &randomIter{
|
||||
func NewRandomAlbumIter(deterministicFetcher, randomFetcher AlbumFetchFn, filter mediaprovider.AlbumFilter, prefetchCoverCB func(string)) *randomAlbumIter {
|
||||
return &randomAlbumIter{
|
||||
filter: filter,
|
||||
prefetchCB: prefetchCoverCB,
|
||||
deterministicFetcher: deterministicFetcher,
|
||||
@@ -118,7 +113,7 @@ func NewRandomAlbumIter(deterministicFetcher, randomFetcher AlbumFetchFn, filter
|
||||
}
|
||||
}
|
||||
|
||||
func (r *randomIter) Next() *mediaprovider.Album {
|
||||
func (r *randomAlbumIter) Next() *mediaprovider.Album {
|
||||
if r.done {
|
||||
return nil
|
||||
}
|
||||
@@ -192,8 +187,14 @@ func (r *randomIter) Next() *mediaprovider.Album {
|
||||
return nil
|
||||
}
|
||||
|
||||
type nilFilter[T any] struct{}
|
||||
type nilFilterOptions struct{}
|
||||
|
||||
func (n nilFilter[T]) IsNil() bool { return true }
|
||||
type nilFilter[M any] struct{}
|
||||
|
||||
func (n nilFilter[T]) Matches(*T) bool { return true }
|
||||
func (n nilFilter[M]) IsNil() bool { return true }
|
||||
|
||||
func (n nilFilter[M]) Matches(*M) bool { return true }
|
||||
|
||||
func (n nilFilter[M]) Options() nilFilterOptions { return nilFilterOptions{} }
|
||||
|
||||
func (n nilFilter[M]) SetOptions(o nilFilterOptions) {}
|
||||
|
||||
@@ -58,7 +58,7 @@ func (j *jellyfinMediaProvider) IterateAlbums(sortOrder string, filter mediaprov
|
||||
jfSort.Field = jellyfin.SortByYear
|
||||
jfSort.Mode = jellyfin.SortDesc
|
||||
}
|
||||
jfFilt := jfFilterFromFilter(&filter)
|
||||
jfFilt := jfFilterFromFilter(filter)
|
||||
|
||||
fetcher := func(offs, limit int) ([]*mediaprovider.Album, error) {
|
||||
al, err := j.client.GetAlbums(jellyfin.QueryOpts{
|
||||
@@ -152,23 +152,29 @@ func (j *jellyfinMediaProvider) IterateArtists(sortOrder string) mediaprovider.A
|
||||
|
||||
// Creates the Jellyfin filter to implement the given mediaprovider filter,
|
||||
// and zeros out the now-unneeded fields in the mediaprovider filter.
|
||||
func jfFilterFromFilter(filter *mediaprovider.AlbumFilter) jellyfin.Filter {
|
||||
func jfFilterFromFilter(filter mediaprovider.AlbumFilter) jellyfin.Filter {
|
||||
var jfFilt jellyfin.Filter
|
||||
if filter.ExcludeUnfavorited {
|
||||
|
||||
filterOptions := filter.Options()
|
||||
|
||||
if filterOptions.ExcludeUnfavorited {
|
||||
jfFilt.Favorite = true
|
||||
filter.ExcludeUnfavorited = false // Jellyfin will handle this filter
|
||||
filterOptions.ExcludeUnfavorited = false // Jellyfin will handle this filter
|
||||
}
|
||||
if filter.MinYear > 0 && filter.MaxYear > 0 {
|
||||
jfFilt.YearRange = [2]int{filter.MinYear, filter.MaxYear}
|
||||
filter.MinYear, filter.MaxYear = 0, 0
|
||||
} else if filter.MinYear > 0 {
|
||||
jfFilt.YearRange = [2]int{filter.MinYear, time.Now().Year()}
|
||||
filter.MinYear, filter.MaxYear = 0, 0
|
||||
} else if filter.MaxYear > 0 {
|
||||
jfFilt.YearRange = [2]int{1900, filter.MaxYear}
|
||||
filter.MinYear, filter.MaxYear = 0, 0
|
||||
if filterOptions.MinYear > 0 && filterOptions.MaxYear > 0 {
|
||||
jfFilt.YearRange = [2]int{filterOptions.MinYear, filterOptions.MaxYear}
|
||||
filterOptions.MinYear, filterOptions.MaxYear = 0, 0
|
||||
} else if filterOptions.MinYear > 0 {
|
||||
jfFilt.YearRange = [2]int{filterOptions.MinYear, time.Now().Year()}
|
||||
filterOptions.MinYear, filterOptions.MaxYear = 0, 0
|
||||
} else if filterOptions.MaxYear > 0 {
|
||||
jfFilt.YearRange = [2]int{1900, filterOptions.MaxYear}
|
||||
filterOptions.MinYear, filterOptions.MaxYear = 0, 0
|
||||
}
|
||||
jfFilt.Genres = filter.Genres
|
||||
filter.Genres = nil
|
||||
jfFilt.Genres = filterOptions.Genres
|
||||
filterOptions.Genres = nil
|
||||
|
||||
filter.SetOptions(filterOptions)
|
||||
|
||||
return jfFilt
|
||||
}
|
||||
|
||||
@@ -7,7 +7,24 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type AlbumFilter struct {
|
||||
type MediaIterator[M any] interface {
|
||||
Next() *M
|
||||
}
|
||||
|
||||
type ArtistIterator = MediaIterator[Artist]
|
||||
type AlbumIterator = MediaIterator[Album]
|
||||
type TrackIterator = MediaIterator[Track]
|
||||
|
||||
type MediaFilter[M, F any] interface {
|
||||
Options() F
|
||||
SetOptions(F)
|
||||
IsNil() bool
|
||||
Matches(*M) bool
|
||||
}
|
||||
|
||||
type AlbumFilter = MediaFilter[Album, AlbumFilterOptions]
|
||||
|
||||
type AlbumFilterOptions struct {
|
||||
MinYear int
|
||||
MaxYear int // 0 == unset/match any
|
||||
Genres []string // len(0) == unset/match any
|
||||
@@ -16,42 +33,46 @@ type AlbumFilter struct {
|
||||
ExcludeUnfavorited bool // mut. exc. with ExcludeFavorited
|
||||
}
|
||||
|
||||
// Returns true if the filter is the nil filter - i.e. matches everything
|
||||
func (a AlbumFilter) IsNil() bool {
|
||||
return a.MinYear == 0 && a.MaxYear == 0 &&
|
||||
len(a.Genres) == 0 &&
|
||||
!a.ExcludeFavorited && !a.ExcludeUnfavorited
|
||||
type albumFilter struct {
|
||||
options AlbumFilterOptions
|
||||
}
|
||||
|
||||
func (f AlbumFilter) Matches(album *Album) bool {
|
||||
func NewAlbumFilter(options AlbumFilterOptions) *albumFilter {
|
||||
return &albumFilter{options}
|
||||
}
|
||||
|
||||
func (a albumFilter) Options() AlbumFilterOptions {
|
||||
return a.options
|
||||
}
|
||||
|
||||
func (a *albumFilter) SetOptions(o AlbumFilterOptions) {
|
||||
a.options = o
|
||||
}
|
||||
|
||||
// Returns true if the filter is the nil filter - i.e. matches everything
|
||||
func (a albumFilter) IsNil() bool {
|
||||
return a.options.MinYear == 0 && a.options.MaxYear == 0 &&
|
||||
len(a.options.Genres) == 0 &&
|
||||
!a.options.ExcludeFavorited && !a.options.ExcludeUnfavorited
|
||||
}
|
||||
|
||||
func (f albumFilter) Matches(album *Album) bool {
|
||||
if album == nil {
|
||||
return false
|
||||
}
|
||||
if f.ExcludeFavorited && album.Favorite {
|
||||
if f.options.ExcludeFavorited && album.Favorite {
|
||||
return false
|
||||
}
|
||||
if f.ExcludeUnfavorited && !album.Favorite {
|
||||
if f.options.ExcludeUnfavorited && !album.Favorite {
|
||||
return false
|
||||
}
|
||||
if y := album.Year; y < f.MinYear || (f.MaxYear > 0 && y > f.MaxYear) {
|
||||
if y := album.Year; y < f.options.MinYear || (f.options.MaxYear > 0 && y > f.options.MaxYear) {
|
||||
return false
|
||||
}
|
||||
if len(f.Genres) == 0 {
|
||||
if len(f.options.Genres) == 0 {
|
||||
return true
|
||||
}
|
||||
return genresMatch(f.Genres, album.Genres)
|
||||
}
|
||||
|
||||
type ArtistIterator interface {
|
||||
Next() *Artist
|
||||
}
|
||||
|
||||
type AlbumIterator interface {
|
||||
Next() *Album
|
||||
}
|
||||
|
||||
type TrackIterator interface {
|
||||
Next() *Track
|
||||
return genresMatch(f.options.Genres, album.Genres)
|
||||
}
|
||||
|
||||
type RatingFavoriteParameters struct {
|
||||
|
||||
@@ -35,23 +35,24 @@ func (s *subsonicMediaProvider) AlbumSortOrders() []string {
|
||||
}
|
||||
}
|
||||
|
||||
func filterMatches(f mediaprovider.AlbumFilter, album *subsonic.AlbumID3, ignoreGenre bool) bool {
|
||||
func filterAlbumMatches(f mediaprovider.AlbumFilter, album *subsonic.AlbumID3, ignoreGenre bool) bool {
|
||||
filterOptions := f.Options()
|
||||
if album == nil {
|
||||
return false
|
||||
}
|
||||
if f.ExcludeFavorited && !album.Starred.IsZero() {
|
||||
if filterOptions.ExcludeFavorited && !album.Starred.IsZero() {
|
||||
return false
|
||||
}
|
||||
if f.ExcludeUnfavorited && album.Starred.IsZero() {
|
||||
if filterOptions.ExcludeUnfavorited && album.Starred.IsZero() {
|
||||
return false
|
||||
}
|
||||
if y := album.Year; y < f.MinYear || (f.MaxYear > 0 && y > f.MaxYear) {
|
||||
if y := album.Year; y < filterOptions.MinYear || (filterOptions.MaxYear > 0 && y > filterOptions.MaxYear) {
|
||||
return false
|
||||
}
|
||||
if ignoreGenre || len(f.Genres) == 0 {
|
||||
if ignoreGenre || len(filterOptions.Genres) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, g := range f.Genres {
|
||||
for _, g := range filterOptions.Genres {
|
||||
if strings.EqualFold(g, album.Genre) {
|
||||
return true
|
||||
}
|
||||
@@ -60,21 +61,24 @@ func filterMatches(f mediaprovider.AlbumFilter, album *subsonic.AlbumID3, ignore
|
||||
}
|
||||
|
||||
func (s *subsonicMediaProvider) IterateAlbums(sortOrder string, filter mediaprovider.AlbumFilter) mediaprovider.AlbumIterator {
|
||||
if sortOrder == "" && len(filter.Genres) == 1 {
|
||||
genre := filter.Genres[0]
|
||||
filterOptions := filter.Options()
|
||||
if sortOrder == "" && len(filterOptions.Genres) == 1 {
|
||||
genre := filterOptions.Genres[0]
|
||||
// The Subsonic API (non-OpenSubsonic) returns only the first genre for multi-genre albums,
|
||||
// but servers do internally match against all the genres the album is categorized with.
|
||||
// So we must not additionally filter by genre to avoid excluding results where
|
||||
// the single genre returned by Subsonic isn't the one we're iterating on.
|
||||
filter.Genres = nil
|
||||
filterOptions.Genres = nil
|
||||
fetchFn := func(offset, limit int) ([]*subsonic.AlbumID3, error) {
|
||||
return s.client.GetAlbumList2("byGenre",
|
||||
map[string]string{"genre": genre, "offset": strconv.Itoa(offset), "limit": strconv.Itoa(limit)})
|
||||
}
|
||||
filter.SetOptions(filterOptions)
|
||||
return helpers.NewAlbumIterator(makeFetchFn(fetchFn), filter, s.prefetchCoverCB)
|
||||
}
|
||||
if sortOrder == "" && filter.ExcludeUnfavorited {
|
||||
filter.ExcludeUnfavorited = false // we're already filtering by this
|
||||
if sortOrder == "" && filterOptions.ExcludeUnfavorited {
|
||||
filterOptions.ExcludeUnfavorited = false // we're already filtering by this
|
||||
filter.SetOptions(filterOptions)
|
||||
return s.baseIterFromSimpleSortOrder("starred", filter)
|
||||
}
|
||||
if sortOrder == "" {
|
||||
@@ -112,10 +116,10 @@ func (s *subsonicMediaProvider) IterateAlbums(sortOrder string, filter mediaprov
|
||||
}
|
||||
|
||||
func (s *subsonicMediaProvider) SearchAlbums(searchQuery string, filter mediaprovider.AlbumFilter) mediaprovider.AlbumIterator {
|
||||
return s.newSearchIter(searchQuery, filter, s.prefetchCoverCB)
|
||||
return s.newSearchAlbumIter(searchQuery, filter, s.prefetchCoverCB)
|
||||
}
|
||||
|
||||
type searchIter struct {
|
||||
type searchAlbumIter struct {
|
||||
searchIterBase
|
||||
|
||||
prefetchCB func(string)
|
||||
@@ -126,8 +130,8 @@ type searchIter struct {
|
||||
done bool
|
||||
}
|
||||
|
||||
func (s *subsonicMediaProvider) newSearchIter(query string, filter mediaprovider.AlbumFilter, cb func(string)) *searchIter {
|
||||
return &searchIter{
|
||||
func (s *subsonicMediaProvider) newSearchAlbumIter(query string, filter mediaprovider.AlbumFilter, cb func(string)) *searchAlbumIter {
|
||||
return &searchAlbumIter{
|
||||
searchIterBase: searchIterBase{
|
||||
query: query,
|
||||
s: s.client,
|
||||
@@ -138,7 +142,7 @@ func (s *subsonicMediaProvider) newSearchIter(query string, filter mediaprovider
|
||||
}
|
||||
}
|
||||
|
||||
func (s *searchIter) Next() *mediaprovider.Album {
|
||||
func (s *searchAlbumIter) Next() *mediaprovider.Album {
|
||||
if s.done {
|
||||
return nil
|
||||
}
|
||||
@@ -197,12 +201,12 @@ func (s *searchIter) Next() *mediaprovider.Album {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *searchIter) addNewAlbums(al []*subsonic.AlbumID3) {
|
||||
func (s *searchAlbumIter) addNewAlbums(al []*subsonic.AlbumID3) {
|
||||
for _, album := range al {
|
||||
if _, have := s.albumIDset[album.ID]; have {
|
||||
continue
|
||||
}
|
||||
if !filterMatches(s.filter, album, false) {
|
||||
if !filterAlbumMatches(s.filter, album, false) {
|
||||
continue
|
||||
}
|
||||
s.prefetched = append(s.prefetched, album)
|
||||
|
||||
@@ -10,8 +10,11 @@ import (
|
||||
func (s *subsonicMediaProvider) IterateTracks(searchQuery string) mediaprovider.TrackIterator {
|
||||
if searchQuery == "" {
|
||||
return &allTracksIterator{
|
||||
s: s,
|
||||
albumIter: s.IterateAlbums(AlbumSortArtistAZ, mediaprovider.AlbumFilter{}),
|
||||
s: s,
|
||||
albumIter: s.IterateAlbums(
|
||||
AlbumSortArtistAZ,
|
||||
mediaprovider.NewAlbumFilter(mediaprovider.AlbumFilterOptions{}),
|
||||
),
|
||||
}
|
||||
}
|
||||
return &searchTracksIterator{
|
||||
|
||||
Reference in New Issue
Block a user