Skip to content

Commit

Permalink
translate sort order strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jul 21, 2024
1 parent 53f29e0 commit 4afedda
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 111 deletions.
20 changes: 8 additions & 12 deletions backend/mediaprovider/jellyfin/artistiterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@ import (
"github.com/dweymouth/supersonic/sharedutil"
)

const (
ArtistSortAlbumCount string = "Album Count"
ArtistSortNameAZ string = "Name (A-Z)"
ArtistSortRandom string = "Random"
)
const ()

func (j *jellyfinMediaProvider) ArtistSortOrders() []string {
return []string{
ArtistSortAlbumCount,
ArtistSortNameAZ,
ArtistSortRandom,
mediaprovider.ArtistSortAlbumCount,
mediaprovider.ArtistSortNameAZ,
mediaprovider.ArtistSortRandom,
}
}

Expand All @@ -29,10 +25,10 @@ func (j *jellyfinMediaProvider) IterateArtists(sortOrder string, filter mediapro
var sortFn func([]*jellyfin.Artist) []*jellyfin.Artist

if sortOrder == "" {
sortOrder = ArtistSortNameAZ // default
sortOrder = mediaprovider.ArtistSortNameAZ // default
}
switch sortOrder {
case ArtistSortAlbumCount:
case mediaprovider.ArtistSortAlbumCount:
// Pagination needs to be disabled, to retrieve all results in a single request, and correctly sort them.
disablePagination = true
sortFn = func(artists []*jellyfin.Artist) []*jellyfin.Artist {
Expand All @@ -41,10 +37,10 @@ func (j *jellyfinMediaProvider) IterateArtists(sortOrder string, filter mediapro
})
return artists
}
case ArtistSortNameAZ:
case mediaprovider.ArtistSortNameAZ:
jfSort.Field = jellyfin.SortByName
jfSort.Mode = jellyfin.SortAsc
case ArtistSortRandom:
case mediaprovider.ArtistSortRandom:
jfSort.Field = jellyfin.SortByRandom
}

Expand Down
35 changes: 13 additions & 22 deletions backend/mediaprovider/jellyfin/iterators.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,35 @@ import (
"github.com/dweymouth/supersonic/sharedutil"
)

const (
AlbumSortRecentlyAdded string = "Recently Added"
AlbumSortRandom string = "Random"
AlbumSortTitleAZ string = "Title (A-Z)"
AlbumSortArtistAZ string = "Artist (A-Z)"
AlbumSortYearAscending string = "Year (ascending)"
AlbumSortYearDescending string = "Year (descending)"
)

func (j *jellyfinMediaProvider) AlbumSortOrders() []string {
return []string{
AlbumSortRecentlyAdded,
AlbumSortRandom,
AlbumSortTitleAZ,
AlbumSortArtistAZ,
AlbumSortYearAscending,
AlbumSortYearDescending,
mediaprovider.AlbumSortRecentlyAdded,
mediaprovider.AlbumSortRandom,
mediaprovider.AlbumSortTitleAZ,
mediaprovider.AlbumSortArtistAZ,
mediaprovider.AlbumSortYearAscending,
mediaprovider.AlbumSortYearDescending,
}
}

func (j *jellyfinMediaProvider) IterateAlbums(sortOrder string, filter mediaprovider.AlbumFilter) mediaprovider.AlbumIterator {
var jfSort jellyfin.Sort
switch sortOrder {
case AlbumSortRecentlyAdded:
case mediaprovider.AlbumSortRecentlyAdded:
jfSort.Field = jellyfin.SortByDateCreated
jfSort.Mode = jellyfin.SortDesc
case AlbumSortRandom:
case mediaprovider.AlbumSortRandom:
jfSort.Field = jellyfin.SortByRandom
case AlbumSortArtistAZ:
case mediaprovider.AlbumSortArtistAZ:
jfSort.Field = jellyfin.SortByArtist
jfSort.Mode = jellyfin.SortAsc
case AlbumSortTitleAZ:
case mediaprovider.AlbumSortTitleAZ:
jfSort.Field = jellyfin.SortByName
jfSort.Mode = jellyfin.SortAsc
case AlbumSortYearAscending:
case mediaprovider.AlbumSortYearAscending:
jfSort.Field = jellyfin.SortByYear
jfSort.Mode = jellyfin.SortAsc
case AlbumSortYearDescending:
case mediaprovider.AlbumSortYearDescending:
jfSort.Field = jellyfin.SortByYear
jfSort.Mode = jellyfin.SortDesc
}
Expand All @@ -64,7 +55,7 @@ func (j *jellyfinMediaProvider) IterateAlbums(sortOrder string, filter mediaprov
return sharedutil.MapSlice(al, toAlbum), nil
}

if sortOrder == AlbumSortRandom {
if sortOrder == mediaprovider.AlbumSortRandom {
determFetcher := func(offs, limit int) ([]*mediaprovider.Album, error) {
al, err := j.client.GetAlbums(jellyfin.QueryOpts{
Sort: jellyfin.Sort{Field: "SortName", Mode: jellyfin.SortAsc},
Expand Down
19 changes: 19 additions & 0 deletions backend/mediaprovider/mediaprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ import (
"github.com/deluan/sanitize"
)

const (
// set of all supported album sorts across all media providers
// these strings may be translated
AlbumSortRecentlyAdded string = "Recently Added"
AlbumSortRecentlyPlayed string = "Recently Played"
AlbumSortFrequentlyPlayed string = "Frequently Played"
AlbumSortRandom string = "Random"
AlbumSortTitleAZ string = "Title (A-Z)"
AlbumSortArtistAZ string = "Artist (A-Z)"
AlbumSortYearAscending string = "Year (ascending)"
AlbumSortYearDescending string = "Year (descending)"

// set of all supported artist sorts across all media providers
// these strings may be translated
ArtistSortAlbumCount string = "Album Count"
ArtistSortNameAZ string = "Name (A-Z)"
ArtistSortRandom string = "Random"
)

type MediaIterator[M any] interface {
Next() *M
}
Expand Down
45 changes: 17 additions & 28 deletions backend/mediaprovider/subsonic/albumiterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,16 @@ import (
"github.com/dweymouth/supersonic/sharedutil"
)

const (
AlbumSortRecentlyAdded string = "Recently Added"
AlbumSortRecentlyPlayed string = "Recently Played"
AlbumSortFrequentlyPlayed string = "Frequently Played"
AlbumSortRandom string = "Random"
AlbumSortTitleAZ string = "Title (A-Z)"
AlbumSortArtistAZ string = "Artist (A-Z)"
AlbumSortYearAscending string = "Year (ascending)"
AlbumSortYearDescending string = "Year (descending)"
)

func (s *subsonicMediaProvider) AlbumSortOrders() []string {
return []string{
AlbumSortRecentlyAdded,
AlbumSortRecentlyPlayed,
AlbumSortFrequentlyPlayed,
AlbumSortRandom,
AlbumSortTitleAZ,
AlbumSortArtistAZ,
AlbumSortYearAscending,
AlbumSortYearDescending,
mediaprovider.AlbumSortRecentlyAdded,
mediaprovider.AlbumSortRecentlyPlayed,
mediaprovider.AlbumSortFrequentlyPlayed,
mediaprovider.AlbumSortRandom,
mediaprovider.AlbumSortTitleAZ,
mediaprovider.AlbumSortArtistAZ,
mediaprovider.AlbumSortYearAscending,
mediaprovider.AlbumSortYearDescending,
}
}

Expand Down Expand Up @@ -86,28 +75,28 @@ func (s *subsonicMediaProvider) IterateAlbums(sortOrder string, filter mediaprov
return s.baseIterFromSimpleSortOrder("starred", modifiedFilter)
}
if sortOrder == "" {
sortOrder = AlbumSortRecentlyAdded // default
sortOrder = mediaprovider.AlbumSortRecentlyAdded // default
}
switch sortOrder {
case AlbumSortRecentlyAdded:
case mediaprovider.AlbumSortRecentlyAdded:
return s.baseIterFromSimpleSortOrder("newest", filter)
case AlbumSortRecentlyPlayed:
case mediaprovider.AlbumSortRecentlyPlayed:
return s.baseIterFromSimpleSortOrder("recent", filter)
case AlbumSortFrequentlyPlayed:
case mediaprovider.AlbumSortFrequentlyPlayed:
return s.baseIterFromSimpleSortOrder("frequent", filter)
case AlbumSortRandom:
case mediaprovider.AlbumSortRandom:
return s.newRandomIter(filter, s.prefetchCoverCB)
case AlbumSortTitleAZ:
case mediaprovider.AlbumSortTitleAZ:
return s.baseIterFromSimpleSortOrder("alphabeticalByName", filter)
case AlbumSortArtistAZ:
case mediaprovider.AlbumSortArtistAZ:
return s.baseIterFromSimpleSortOrder("alphabeticalByArtist", filter)
case AlbumSortYearAscending:
case mediaprovider.AlbumSortYearAscending:
fetchFn := func(offset, limit int) ([]*subsonic.AlbumID3, error) {
return s.client.GetAlbumList2("byYear",
map[string]string{"fromYear": "0", "toYear": "3000", "offset": strconv.Itoa(offset), "limit": strconv.Itoa(limit)})
}
return helpers.NewAlbumIterator(makeFetchFn(fetchFn), filter, s.prefetchCoverCB)
case AlbumSortYearDescending:
case mediaprovider.AlbumSortYearDescending:
fetchFn := func(offset, limit int) ([]*subsonic.AlbumID3, error) {
return s.client.GetAlbumList2("byYear",
map[string]string{"fromYear": "3000", "toYear": "0", "offset": strconv.Itoa(offset), "limit": strconv.Itoa(limit)})
Expand Down
20 changes: 7 additions & 13 deletions backend/mediaprovider/subsonic/artistiterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@ import (
"github.com/dweymouth/supersonic/sharedutil"
)

const (
ArtistSortAlbumCount string = "Album Count"
ArtistSortNameAZ string = "Name (A-Z)"
ArtistSortRandom string = "Random"
)

func (s *subsonicMediaProvider) ArtistSortOrders() []string {
return []string{
ArtistSortAlbumCount,
ArtistSortNameAZ,
ArtistSortRandom,
mediaprovider.ArtistSortAlbumCount,
mediaprovider.ArtistSortNameAZ,
mediaprovider.ArtistSortRandom,
}
}

Expand All @@ -37,10 +31,10 @@ func filterArtistMatches(f mediaprovider.ArtistFilter, artist *subsonic.ArtistID

func (s *subsonicMediaProvider) IterateArtists(sortOrder string, filter mediaprovider.ArtistFilter) mediaprovider.ArtistIterator {
if sortOrder == "" {
sortOrder = ArtistSortNameAZ // default
sortOrder = mediaprovider.ArtistSortNameAZ // default
}
switch sortOrder {
case ArtistSortAlbumCount:
case mediaprovider.ArtistSortAlbumCount:
return s.baseArtistIterFromSimpleSortOrder(
func(artists []*subsonic.ArtistID3) []*subsonic.ArtistID3 {
slices.SortStableFunc(artists, func(a, b *subsonic.ArtistID3) int {
Expand All @@ -50,7 +44,7 @@ func (s *subsonicMediaProvider) IterateArtists(sortOrder string, filter mediapro
},
filter,
)
case ArtistSortNameAZ:
case mediaprovider.ArtistSortNameAZ:
return s.baseArtistIterFromSimpleSortOrder(
func(artists []*subsonic.ArtistID3) []*subsonic.ArtistID3 {
c := collate.New(language.English, collate.Loose)
Expand All @@ -61,7 +55,7 @@ func (s *subsonicMediaProvider) IterateArtists(sortOrder string, filter mediapro
},
filter,
)
case ArtistSortRandom:
case mediaprovider.ArtistSortRandom:
return s.baseArtistIterFromSimpleSortOrder(
func(artists []*subsonic.ArtistID3) []*subsonic.ArtistID3 {
newArtists := make([]*subsonic.ArtistID3, len(artists))
Expand Down
2 changes: 1 addition & 1 deletion backend/mediaprovider/subsonic/trackiterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (s *subsonicMediaProvider) IterateTracks(searchQuery string) mediaprovider.
return &allTracksIterator{
s: s,
albumIter: s.IterateAlbums(
AlbumSortArtistAZ,
mediaprovider.AlbumSortArtistAZ,
mediaprovider.NewAlbumFilter(mediaprovider.AlbumFilterOptions{}),
),
}
Expand Down
12 changes: 11 additions & 1 deletion res/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Album": "Album",
"album": "album",
"Album count": "Album count",
"Album Count": "Album Count",
"Album filters": "Album filters",
"Album gain": "Album gain",
"Album info not available": "Album info not available",
Expand All @@ -16,6 +17,7 @@
"Alt. URL": "Alt. URL",
"Are you sure you want to delete the server": "Are you sure you want to delete the server",
"Artist": "Artist",
"Artist (A-Z)": "Artist (A-Z)",
"Artists": "Artists",
"Artist biography not available.": "Artist biography not available.",
"Audio device": "Audio device",
Expand Down Expand Up @@ -69,6 +71,7 @@
"File path": "File path",
"File size": "File size",
"Filter genres": "Filter genres",
"Frequently Played": "Frequently Played",
"General": "General",
"Genre": "Genre",
"Genres": "Genres",
Expand All @@ -91,6 +94,7 @@
"Mixtape": "Mixtape",
"My Server": "My Server",
"Name": "Name",
"Name (A-Z)": "Name (A-Z)",
"Nickname": "Nickname",
"No radio stations available": "No radio stations available",
"None": "None",
Expand Down Expand Up @@ -119,8 +123,11 @@
"Prevent clipping": "Prevent clipping",
"Private": "Private",
"Public": "Public",
"Random": "Random",
"Rating": "Rating",
"reissued": "reissued",
"Recently Added": "Recently Added",
"Recently Played": "Recently Played",
"Related": "Related",
"Remix": "Remix",
"Remove from playlist": "Remove from playlist",
Expand Down Expand Up @@ -159,6 +166,7 @@
"Testing connection": "Testing connection",
"Theme": "Theme",
"Title": "Title",
"Title (A-Z)": "Title (A-Z)",
"Top Tracks": "Top Tracks",
"Total time": "Total time",
"Track": "Track",
Expand All @@ -177,5 +185,7 @@
"wrong URL": "wrong URL",
"wrong username/password": "wrong username/password",
"Year": "Year",
"Year (ascending)": "Year (ascending)",
"Year (descending)": "Year (descending)",
"Year from": "Year from"
}
}
20 changes: 12 additions & 8 deletions ui/browsing/albumspage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fyne.io/fyne/v2/widget"
"github.com/dweymouth/supersonic/backend"
"github.com/dweymouth/supersonic/backend/mediaprovider"
"github.com/dweymouth/supersonic/sharedutil"
"github.com/dweymouth/supersonic/ui/controller"
myTheme "github.com/dweymouth/supersonic/ui/theme"
"github.com/dweymouth/supersonic/ui/util"
Expand Down Expand Up @@ -50,22 +51,25 @@ func (a *albumsPageAdapter) PlaceholderResource() fyne.Resource { return myTheme

func (a *albumsPageAdapter) Route() controller.Route { return controller.AlbumsRoute() }

func (a *albumsPageAdapter) SortOrders() ([]string, string) {
func (a *albumsPageAdapter) SortOrders() ([]string, int) {
orders := a.mp.AlbumSortOrders()
sortOrder := a.cfg.SortOrder
if !slices.Contains(orders, sortOrder) {
sortOrder = string(orders[0])
sortOrder := slices.Index(orders, a.cfg.SortOrder)
if sortOrder < 0 {
sortOrder = 0
}
return orders, sortOrder

translatedOrders := sharedutil.MapSlice(orders, func(s string) string { return lang.L(s) })
return translatedOrders, sortOrder
}

func (a *albumsPageAdapter) SaveSortOrder(order string) {
a.cfg.SortOrder = order
func (a *albumsPageAdapter) SaveSortOrder(orderIdx int) {
a.cfg.SortOrder = a.mp.AlbumSortOrders()[orderIdx]
}

func (a *albumsPageAdapter) ActionButton() *widget.Button { return nil }

func (a *albumsPageAdapter) Iter(sortOrder string, filter mediaprovider.AlbumFilter) widgets.GridViewIterator {
func (a *albumsPageAdapter) Iter(sortOrderIdx int, filter mediaprovider.AlbumFilter) widgets.GridViewIterator {
sortOrder := a.mp.AlbumSortOrders()[sortOrderIdx]
return widgets.NewGridViewAlbumIterator(a.mp.IterateAlbums(sortOrder, filter))
}

Expand Down
Loading

0 comments on commit 4afedda

Please sign in to comment.