From 7161dd40a9a45e157550c91d0ae3551f49050529 Mon Sep 17 00:00:00 2001 From: Michael Manganiello Date: Sun, 18 Jun 2023 22:38:48 -0300 Subject: [PATCH] Reduce direct dependency on Player even more --- backend/playbackmanager.go | 18 +++++++++++++++++- player/player.go | 39 ++++++++++++++++++-------------------- ui/bottompanel.go | 2 +- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/backend/playbackmanager.go b/backend/playbackmanager.go index 3ec111a..7031973 100644 --- a/backend/playbackmanager.go +++ b/backend/playbackmanager.go @@ -40,6 +40,7 @@ type PlaybackManager struct { onSongChange []func(nowPlaying, justScrobbledIfAny *mediaprovider.Track) onPlayTimeUpdate []func(float64, float64) + onLoopModeChange []func(string) } func NewPlaybackManager( @@ -128,6 +129,11 @@ func (p *PlaybackManager) OnPlayTimeUpdate(cb func(float64, float64)) { p.onPlayTimeUpdate = append(p.onPlayTimeUpdate, cb) } +// Registers a callback that is notified whenever the loop mode changes. +func (p *PlaybackManager) OnLoopModeChange(cb func(string)) { + p.onLoopModeChange = append(p.onLoopModeChange, cb) +} + // Loads the specified album into the play queue. func (p *PlaybackManager) LoadAlbum(albumID string, appendToQueue bool, shuffle bool) error { album, err := p.sm.Server.GetAlbum(albumID) @@ -294,7 +300,17 @@ func (p *PlaybackManager) SetReplayGainOptions(config ReplayGainConfig) { // Changes the loop mode of the player to the next one. // Useful for toggling UI elements, to change modes without knowing the current player mode. func (p *PlaybackManager) SetNextLoopMode() error { - return p.player.SetNextLoopMode() + if err := p.player.SetNextLoopMode(); err != nil { + return err + } + + defer func() { + for _, cb := range p.onLoopModeChange { + cb(p.player.GetLoopMode().String()) + } + }() + + return nil } // call BEFORE updating p.nowPlayingIdx diff --git a/player/player.go b/player/player.go index 6d034ec..e9ad599 100644 --- a/player/player.go +++ b/player/player.go @@ -116,12 +116,11 @@ type Player struct { bgCancel context.CancelFunc // callbacks - onPaused []func() - onStopped []func() - onPlaying []func() - onSeek []func() - onLoopModeChanged []func(string) - onTrackChange []func(int64) + onPaused []func() + onStopped []func() + onPlaying []func() + onSeek []func() + onTrackChange []func(int64) } // Returns a new player. @@ -400,7 +399,12 @@ func (p *Player) PlayPause() error { } } -// Sets the loop mode of the player. +// Get the loop mode of the player. +func (p *Player) GetLoopMode() LoopMode { + return p.loopMode +} + +// Set the loop mode of the player. func (p *Player) SetLoopMode(mode LoopMode) error { if !p.initialized { return ErrUnitialized @@ -413,22 +417,20 @@ func (p *Player) SetLoopMode(mode LoopMode) error { switch mode { case LoopNone: - p.mpv.SetOptionString("loop-playlist", "no") + if err := p.mpv.SetOptionString("loop-playlist", "no"); err != nil { + return err + } case LoopAll: - p.mpv.SetOptionString("loop-playlist", "inf") + if err := p.mpv.SetOptionString("loop-playlist", "inf"); err != nil { + return err + } } p.loopMode = mode - defer func() { - for _, cb := range p.onLoopModeChanged { - cb(p.loopMode.String()) - } - }() - return nil } -// Changes the loop mode of the player to the next one. +// Change the loop mode of the player to the next one. // Useful for toggling UI elements, to change modes without knowing the current player mode. func (p *Player) SetNextLoopMode() error { switch p.loopMode { @@ -542,11 +544,6 @@ func (p *Player) OnSeek(cb func()) { p.onSeek = append(p.onSeek, cb) } -// Registers a callback which is invoked when the player enables queue repeat. -func (p *Player) OnLoopModeChanged(cb func(string)) { - p.onLoopModeChanged = append(p.onLoopModeChanged, cb) -} - // Registers a callback which is invoked when the currently playing track changes, // or when playback begins at any time from the Stopped state. // Callback is invoked with the index of the currently playing track (zero-based). diff --git a/ui/bottompanel.go b/ui/bottompanel.go index e16c8ab..5859211 100644 --- a/ui/bottompanel.go +++ b/ui/bottompanel.go @@ -54,7 +54,7 @@ func NewBottomPanel(p *player.Player, pm *backend.PlaybackManager, contr *contro p.OnStopped(func() { bp.Controls.SetPlaying(false) }) - p.OnLoopModeChanged(func(mode string) { + pm.OnLoopModeChange(func(mode string) { bp.Controls.SetLoopMode(mode) })