/
menu_album_detail.go
73 lines (60 loc) · 1.64 KB
/
menu_album_detail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package ui
import (
"context"
"github.com/anhoder/foxful-cli/model"
"github.com/go-musicfox/spotifox/utils"
"github.com/pkg/errors"
"github.com/zmb3/spotify/v2"
)
type AlbumDetailMenu struct {
baseMenu
menus []model.MenuItem
songs []spotify.FullTrack
album spotify.SimpleAlbum
}
func NewAlbumDetailMenu(base baseMenu, album spotify.SimpleAlbum) *AlbumDetailMenu {
return &AlbumDetailMenu{
baseMenu: base,
album: album,
}
}
func (m *AlbumDetailMenu) IsSearchable() bool {
return true
}
func (m *AlbumDetailMenu) IsPlayable() bool {
return true
}
func (m *AlbumDetailMenu) GetMenuKey() string {
return "album_detail_" + string(m.album.ID)
}
func (m *AlbumDetailMenu) MenuViews() []model.MenuItem {
return m.menus
}
func (m *AlbumDetailMenu) BeforeEnterMenuHook() model.Hook {
return func(main *model.Main) (bool, model.Page) {
if m.spotifox.CheckAuthSession() == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(EnterMenuCallback(main))
return false, page
}
res, err := m.spotifox.spotifyClient.GetAlbumTracks(context.Background(), m.album.ID, spotify.Limit(50))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), EnterMenuCallback(main)); catched {
return false, page
}
if err != nil {
return m.handleFetchErr(errors.Wrap(err, "get album's songs failed"))
}
var songs []spotify.FullTrack
for _, song := range res.Tracks {
songs = append(songs, spotify.FullTrack{
Album: m.album,
SimpleTrack: song,
})
}
m.songs = songs
m.menus = utils.MenuItemsFromSongs(m.songs)
return true, nil
}
}
func (m *AlbumDetailMenu) Songs() []spotify.FullTrack {
return m.songs
}