Skip to content

Commit

Permalink
increase station counter request
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanCeausu committed Jun 29, 2024
1 parent f48f01f commit 1783cfa
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
13 changes: 13 additions & 0 deletions browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ func (a *Api) GetStations(uuids []string) ([]Station, error) {
return nil, serverErrMsg
}

func (a *Api) StationCounter(uuid string) error {
log := slog.With("method", "Api.StationCounter")
url := urlClickCount + uuid
res, err := a.doServerRequest(http.MethodPost, url, nil)
if err != nil {
log.Error("", "request error", err)
time.Sleep(serverRetryMillis * time.Millisecond)
return err
}
log.Info(string(res))
return nil
}

func (a *Api) doServerRequest(method string, path string, body []byte) ([]byte, error) {
ix := rand.IntN(len(a.servers))
ip := a.servers[ix]
Expand Down
8 changes: 8 additions & 0 deletions browser/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ func Test_getCountries(t *testing.T) {
}
t.Log(res)
}

func TestApi_StationCounter(t *testing.T) {
a := NewApi(config.Value{Version: "", Debug: true})
err := a.StationCounter("748d830c-d934-41e8-bd14-870add931e1d")
if err != nil {
t.Error(err)
}
}
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
var debug = flag.Bool("debug", false, "use -debug arg to log to a file")

const (
defVersion = "0.2.3"
defVersion = "0.2.4"
cfgSubDir = "sonicRadio"
cfgFilename = "config.json"
)
Expand Down
11 changes: 10 additions & 1 deletion ui/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import (

const startWaitMillis = 500 * 3

func newStationDelegate(cfg *config.Value, p player.Player) *stationDelegate {
func newStationDelegate(cfg *config.Value, p player.Player, b *browser.Api) *stationDelegate {
keymap := newDelegateKeyMap()

d := list.NewDefaultDelegate()

return &stationDelegate{
player: p,
b: b,
cfg: cfg,
keymap: keymap,
defaultDelegate: d,
Expand All @@ -33,6 +34,7 @@ func newStationDelegate(cfg *config.Value, p player.Player) *stationDelegate {

type stationDelegate struct {
player player.Player
b *browser.Api
cfg *config.Value
prevPlaying *browser.Station
currPlaying *browser.Station
Expand Down Expand Up @@ -75,8 +77,15 @@ func (d *stationDelegate) playCmd(s *browser.Station) tea.Cmd {
}
}

func (d *stationDelegate) increaseCounter(station browser.Station) {
d.b.StationCounter(station.Stationuuid)
}

func (d *stationDelegate) playStation(station browser.Station) error {
log := slog.With("method", "stationDelegate.playStation")

go d.increaseCounter(station)

log.Debug("playing", "id", station.Stationuuid)
err := d.player.Play(station.URL)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewProgram(cfg *config.Value, b *browser.Api, p player.Player) *tea.Program
func initialModel(cfg *config.Value, b *browser.Api, p player.Player) *model {
lipgloss.DefaultRenderer().SetHasDarkBackground(true)

delegate := newStationDelegate(cfg, p)
delegate := newStationDelegate(cfg, p, b)
activeIx := browseTabIx
if len(cfg.Favorites) > 0 {
activeIx = favoriteTabIx
Expand Down

0 comments on commit 1783cfa

Please sign in to comment.