Reduce direct dependency on Player even more

This commit is contained in:
Michael Manganiello
2023-06-18 22:45:05 -03:00
parent 4629ede7a7
commit 7161dd40a9
3 changed files with 36 additions and 23 deletions
+17 -1
View File
@@ -40,6 +40,7 @@ type PlaybackManager struct {
onSongChange []func(nowPlaying, justScrobbledIfAny *mediaprovider.Track) onSongChange []func(nowPlaying, justScrobbledIfAny *mediaprovider.Track)
onPlayTimeUpdate []func(float64, float64) onPlayTimeUpdate []func(float64, float64)
onLoopModeChange []func(string)
} }
func NewPlaybackManager( func NewPlaybackManager(
@@ -128,6 +129,11 @@ func (p *PlaybackManager) OnPlayTimeUpdate(cb func(float64, float64)) {
p.onPlayTimeUpdate = append(p.onPlayTimeUpdate, cb) 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. // Loads the specified album into the play queue.
func (p *PlaybackManager) LoadAlbum(albumID string, appendToQueue bool, shuffle bool) error { func (p *PlaybackManager) LoadAlbum(albumID string, appendToQueue bool, shuffle bool) error {
album, err := p.sm.Server.GetAlbum(albumID) 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. // 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. // Useful for toggling UI elements, to change modes without knowing the current player mode.
func (p *PlaybackManager) SetNextLoopMode() error { 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 // call BEFORE updating p.nowPlayingIdx
+18 -21
View File
@@ -116,12 +116,11 @@ type Player struct {
bgCancel context.CancelFunc bgCancel context.CancelFunc
// callbacks // callbacks
onPaused []func() onPaused []func()
onStopped []func() onStopped []func()
onPlaying []func() onPlaying []func()
onSeek []func() onSeek []func()
onLoopModeChanged []func(string) onTrackChange []func(int64)
onTrackChange []func(int64)
} }
// Returns a new player. // 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 { func (p *Player) SetLoopMode(mode LoopMode) error {
if !p.initialized { if !p.initialized {
return ErrUnitialized return ErrUnitialized
@@ -413,22 +417,20 @@ func (p *Player) SetLoopMode(mode LoopMode) error {
switch mode { switch mode {
case LoopNone: case LoopNone:
p.mpv.SetOptionString("loop-playlist", "no") if err := p.mpv.SetOptionString("loop-playlist", "no"); err != nil {
return err
}
case LoopAll: case LoopAll:
p.mpv.SetOptionString("loop-playlist", "inf") if err := p.mpv.SetOptionString("loop-playlist", "inf"); err != nil {
return err
}
} }
p.loopMode = mode p.loopMode = mode
defer func() {
for _, cb := range p.onLoopModeChanged {
cb(p.loopMode.String())
}
}()
return nil 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. // Useful for toggling UI elements, to change modes without knowing the current player mode.
func (p *Player) SetNextLoopMode() error { func (p *Player) SetNextLoopMode() error {
switch p.loopMode { switch p.loopMode {
@@ -542,11 +544,6 @@ func (p *Player) OnSeek(cb func()) {
p.onSeek = append(p.onSeek, cb) 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, // Registers a callback which is invoked when the currently playing track changes,
// or when playback begins at any time from the Stopped state. // or when playback begins at any time from the Stopped state.
// Callback is invoked with the index of the currently playing track (zero-based). // Callback is invoked with the index of the currently playing track (zero-based).
+1 -1
View File
@@ -54,7 +54,7 @@ func NewBottomPanel(p *player.Player, pm *backend.PlaybackManager, contr *contro
p.OnStopped(func() { p.OnStopped(func() {
bp.Controls.SetPlaying(false) bp.Controls.SetPlaying(false)
}) })
p.OnLoopModeChanged(func(mode string) { pm.OnLoopModeChange(func(mode string) {
bp.Controls.SetLoopMode(mode) bp.Controls.SetLoopMode(mode)
}) })