Add Mappings for SearchView

This commit is contained in:
aditya-K2
2023-04-16 06:26:16 +05:30
parent 20747b1a51
commit dee4a9d272
4 changed files with 66 additions and 5 deletions

View File

@@ -195,6 +195,8 @@ mappings:
play_entry
playlist_view:
open_entry
search_view:
open_entry
recently_played_view:
open_entry
top_tracks_view:
@@ -220,3 +222,4 @@ mappings:
- [ ] Wayland Support for Image rendering
- [ ] Queue Support
- [x] Key Mappings

View File

@@ -86,6 +86,9 @@ func GenerateMappings() map[string]map[Key]string {
"nav_menu": {
{K: tcell.KeyEnter}: "open_entry",
},
"search_view": {
{K: tcell.KeyEnter}: "open_entry",
},
"global": {
{R: 'd'}: "choose_device",
{R: '1'}: "focus_nav",

View File

@@ -167,7 +167,12 @@ func NewApplication() *Application {
return nil
}, pBar),
}))
searchView.SetActions(utils.MergeMaps(globalMaps, map[string]*Action{}))
searchView.SetActions(utils.MergeMaps(globalMaps, map[string]*Action{
"open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
searchView.SelectEntry()
return nil
}, nil),
}))
artistsView.SetActions(utils.MergeMaps(globalMaps, map[string]*Action{
"open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
artistsView.OpenArtist()
@@ -216,6 +221,7 @@ func NewApplication() *Application {
albumView.SetMappings(mappings["album_view"])
artistsView.SetMappings(mappings["artists_view"])
artistView.SetMappings(mappings["artist_view"])
searchView.SetMappings(mappings["search_view"])
searchNavFlex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(navMenu.Table, 6, 3, false).

View File

@@ -5,16 +5,24 @@ import (
"github.com/zmb3/spotify/v2"
)
type SearchContent struct {
URI spotify.URI
ID spotify.ID
Name string
Type string
}
type SearchView struct {
*DefaultViewNone
search string
results *spotify.SearchResult
search string
results *spotify.SearchResult
searchContent []SearchContent
}
func NewSearchView() *SearchView {
s := &SearchView{
&DefaultViewNone{&defView{}},
"", nil,
"", nil, []SearchContent{},
}
return s
}
@@ -23,9 +31,11 @@ func (a *SearchView) Content() func() [][]Content {
return func() [][]Content {
c := make([][]Content, 0)
if a.results != nil {
c = append(c, []Content{{"Tracks", NotSelectableStyle}})
if a.results.Tracks != nil {
c = append(c, []Content{{"Tracks", NotSelectableStyle}})
a.searchContent = append(a.searchContent, SearchContent{Type: "null"})
for _, v := range a.results.Tracks.Tracks {
a.searchContent = append(a.searchContent, SearchContent{v.URI, v.ID, v.Name, "track"})
c = append(c, []Content{
{Content: v.Name, Style: TrackStyle},
{Content: v.Artists[0].Name, Style: ArtistStyle},
@@ -35,7 +45,9 @@ func (a *SearchView) Content() func() [][]Content {
}
if a.results.Albums != nil {
c = append(c, []Content{{"Albums", NotSelectableStyle}})
a.searchContent = append(a.searchContent, SearchContent{Type: "null"})
for _, v := range a.results.Albums.Albums {
a.searchContent = append(a.searchContent, SearchContent{v.URI, v.ID, v.Name, "album"})
c = append(c, []Content{
{Content: v.Name, Style: AlbumStyle},
{Content: v.Artists[0].Name, Style: ArtistStyle},
@@ -45,7 +57,9 @@ func (a *SearchView) Content() func() [][]Content {
}
if a.results.Artists != nil {
c = append(c, []Content{{"Artists", NotSelectableStyle}})
a.searchContent = append(a.searchContent, SearchContent{Type: "null"})
for _, v := range a.results.Artists.Artists {
a.searchContent = append(a.searchContent, SearchContent{v.URI, v.ID, v.Name, "artist"})
c = append(c, []Content{
{Content: v.Name, Style: AlbumStyle},
})
@@ -53,7 +67,9 @@ func (a *SearchView) Content() func() [][]Content {
}
if a.results.Playlists != nil {
c = append(c, []Content{{"Playlists", NotSelectableStyle}})
a.searchContent = append(a.searchContent, SearchContent{Type: "null"})
for _, v := range a.results.Playlists.Playlists {
a.searchContent = append(a.searchContent, SearchContent{v.URI, v.ID, v.Name, "playlist"})
c = append(c, []Content{
{Content: v.Name, Style: PlaylistNavStyle},
{Content: v.Owner.DisplayName, Style: ArtistStyle},
@@ -65,6 +81,39 @@ func (a *SearchView) Content() func() [][]Content {
}
}
func (a *SearchView) SelectEntry() {
r, _ := Ui.Main.Table.GetSelection()
switch a.searchContent[r].Type {
case "track":
{
if err := spt.PlaySong(a.searchContent[r].URI); err != nil {
SendNotification(err.Error())
}
}
case "album":
{
albumView.SetAlbum(a.searchContent[r].Name, &a.searchContent[r].ID)
SetCurrentView(albumView)
}
case "artist":
{
artistView.SetArtist(&a.searchContent[r].ID)
artistView.RefreshState()
SetCurrentView(artistView)
}
case "playlist":
{
if p, err := spt.GetSimplePlaylist(&a.searchContent[r].ID); err != nil {
SendNotification("Error Opening the playlists: " + err.Error())
return
} else {
playlistView.SetPlaylist(&p.SimplePlaylist)
SetCurrentView(playlistView)
}
}
}
}
func (a *SearchView) RefreshState() {
if a.search != "" {
results, err := spt.Search(a.search)