Skip to content

Commit

Permalink
feat(daemon): Emit websocket events for repeat/shuffle changes
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Oct 19, 2023
1 parent fed950c commit c8c5a2d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
35 changes: 25 additions & 10 deletions cmd/daemon/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,19 @@ const (
type ApiEventType string

const (
ApiEventTypePlaying ApiEventType = "playing"
ApiEventTypeNotPlaying ApiEventType = "not_playing"
ApiEventTypeWillPlay ApiEventType = "will_play"
ApiEventTypePaused ApiEventType = "paused"
ApiEventTypeActive ApiEventType = "active"
ApiEventTypeInactive ApiEventType = "inactive"
ApiEventTypeMetadata ApiEventType = "metadata"
ApiEventTypeVolume ApiEventType = "volume"
ApiEventTypeSeek ApiEventType = "seek"
ApiEventTypeStopped ApiEventType = "stopped"
ApiEventTypePlaying ApiEventType = "playing"
ApiEventTypeNotPlaying ApiEventType = "not_playing"
ApiEventTypeWillPlay ApiEventType = "will_play"
ApiEventTypePaused ApiEventType = "paused"
ApiEventTypeActive ApiEventType = "active"
ApiEventTypeInactive ApiEventType = "inactive"
ApiEventTypeMetadata ApiEventType = "metadata"
ApiEventTypeVolume ApiEventType = "volume"
ApiEventTypeSeek ApiEventType = "seek"
ApiEventTypeStopped ApiEventType = "stopped"
ApiEventTypeRepeatTrack ApiEventType = "repeat_track"
ApiEventTypeRepeatContext ApiEventType = "repeat_context"
ApiEventTypeShuffleContext ApiEventType = "shuffle_context"
)

type ApiRequest struct {
Expand Down Expand Up @@ -181,6 +184,18 @@ type ApiEventDataSeek struct {
PlayOrigin string `json:"play_origin"`
}

type ApiEventDataRepeatTrack struct {
Value bool `json:"value"`
}

type ApiEventDataRepeatContext struct {
Value bool `json:"value"`
}

type ApiEventDataShuffleContext struct {
Value bool `json:"value"`
}

func NewApiServer(port int) (_ *ApiServer, err error) {
s := &ApiServer{}
s.requests = make(chan ApiRequest)
Expand Down
21 changes: 21 additions & 0 deletions cmd/daemon/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ func (s *Session) setRepeatingContext(val bool) {

s.state.player.Options.RepeatingContext = val
s.updateState()

s.app.server.Emit(&ApiEvent{
Type: ApiEventTypeRepeatContext,
Data: ApiEventDataRepeatContext{
Value: val,
},
})
}

func (s *Session) setRepeatingTrack(val bool) {
Expand All @@ -169,6 +176,13 @@ func (s *Session) setRepeatingTrack(val bool) {

s.state.player.Options.RepeatingTrack = val
s.updateState()

s.app.server.Emit(&ApiEvent{
Type: ApiEventTypeRepeatTrack,
Data: ApiEventDataRepeatTrack{
Value: val,
},
})
}

func (s *Session) setShufflingContext(val bool) {
Expand All @@ -180,6 +194,13 @@ func (s *Session) setShufflingContext(val bool) {
log.Warnf("shuffle context is not supported yet")
s.state.player.Options.ShufflingContext = val
s.updateState()

s.app.server.Emit(&ApiEvent{
Type: ApiEventTypeShuffleContext,
Data: ApiEventDataShuffleContext{
Value: val,
},
})
}

func (s *Session) play() error {
Expand Down

0 comments on commit c8c5a2d

Please sign in to comment.