Skip to content

Commit

Permalink
use error toasts for errors playing random/similar tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jan 7, 2025
1 parent 177aea8 commit ecc691f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
13 changes: 7 additions & 6 deletions backend/playbackmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ func (p *PlaybackManager) PlayTrackAt(idx int) {
p.cmdQueue.PlayTrackAt(idx)
}

func (p *PlaybackManager) PlayRandomSongs(genreName string) {
p.fetchAndPlayTracks(func() ([]*mediaprovider.Track, error) {
func (p *PlaybackManager) PlayRandomSongs(genreName string) error {
return p.fetchAndPlayTracks(func() ([]*mediaprovider.Track, error) {
return p.engine.sm.Server.GetRandomTracks(genreName, p.cfg.EnqueueBatchSize)
})
}

func (p *PlaybackManager) PlaySimilarSongs(id string) {
p.fetchAndPlayTracks(func() ([]*mediaprovider.Track, error) {
func (p *PlaybackManager) PlaySimilarSongs(id string) error {
return p.fetchAndPlayTracks(func() ([]*mediaprovider.Track, error) {
return p.engine.sm.Server.GetSimilarTracks(id, p.cfg.EnqueueBatchSize)
})
}
Expand All @@ -298,15 +298,16 @@ func (p *PlaybackManager) PlayRadioStation(station *mediaprovider.RadioStation)
p.PlayFromBeginning()
}

func (p *PlaybackManager) fetchAndPlayTracks(fetchFn func() ([]*mediaprovider.Track, error)) {
func (p *PlaybackManager) fetchAndPlayTracks(fetchFn func() ([]*mediaprovider.Track, error)) error {
if songs, err := fetchFn(); err != nil {
log.Printf("error fetching tracks: %s", err.Error())
return err
} else {
p.LoadTracks(songs, Replace, false)
if p.engine.replayGainCfg.Mode == ReplayGainAuto {
p.SetReplayGainMode(player.ReplayGainTrack)
}
p.PlayFromBeginning()
return nil
}
}

Expand Down
2 changes: 2 additions & 0 deletions res/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@
"Track peak": "Track peak",
"tracks": "tracks",
"UI Scaling": "UI Scaling",
"Unable to play artist radio": "Unable to play artist radio",
"Unable to play random tracks": "Unable to play random tracks",
"URL": "URL",
"Use legacy authentication": "Use legacy authentication",
"Username": "Username",
Expand Down
8 changes: 7 additions & 1 deletion ui/browsing/artistpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,13 @@ func (g *ArtistPage) Scroll(scrollAmt float32) {
}

func (a *ArtistPage) playArtistRadio() {
go a.pm.PlaySimilarSongs(a.artistID)
go func() {
err := a.pm.PlaySimilarSongs(a.artistID)
if err != nil {
log.Println("error playing similar songs: %v", err)
a.contr.ToastProvider.ShowErrorToast(lang.L("Unable to play artist radio"))
}
}()
}

func (a *ArtistPage) showAlbumSortMenu() {
Expand Down
12 changes: 11 additions & 1 deletion ui/browsing/genrepage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package browsing

import (
"log"

"github.com/dweymouth/supersonic/backend"
"github.com/dweymouth/supersonic/backend/mediaprovider"
"github.com/dweymouth/supersonic/ui/controller"
Expand Down Expand Up @@ -58,7 +60,15 @@ func (g *genrePageAdapter) Route() controller.Route {
}

func (g *genrePageAdapter) ActionButton() *widget.Button {
fn := func() { go g.pm.PlayRandomSongs(g.genre) }
fn := func() {
go func() {
err := g.pm.PlayRandomSongs(g.genre)
if err != nil {
log.Println("error playing random tracks: %v", err)
g.contr.ToastProvider.ShowErrorToast(lang.L("Unable to play random tracks"))
}
}()
}
return widget.NewButtonWithIcon(lang.L("Play random"), myTheme.ShuffleIcon, fn)
}

Expand Down
10 changes: 9 additions & 1 deletion ui/browsing/trackspage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package browsing

import (
"log"

"github.com/dweymouth/supersonic/backend"
"github.com/dweymouth/supersonic/backend/mediaprovider"
"github.com/dweymouth/supersonic/sharedutil"
Expand Down Expand Up @@ -205,7 +207,13 @@ func (s *tracksPageState) Restore() Page {
}

func (t *TracksPage) playRandomSongs() {
t.contr.App.PlaybackManager.PlayRandomSongs("")
go func() {
err := t.contr.App.PlaybackManager.PlayRandomSongs("")
if err != nil {
log.Println("error playing random tracks: %v", err)
t.contr.ToastProvider.ShowErrorToast(lang.L("Unable to play random tracks"))
}
}()
}

func (t *TracksPage) obtainTracklist() *widgets.Tracklist {
Expand Down

0 comments on commit ecc691f

Please sign in to comment.