mirror of
https://github.com/aditya-K2/gspt.git
synced 2026-01-09 13:58:05 -05:00
initial implementation using views
This commit is contained in:
39
ui/app.go
39
ui/app.go
@@ -15,7 +15,7 @@ var (
|
||||
|
||||
type Application struct {
|
||||
App *tview.Application
|
||||
MainS *tview.Pages
|
||||
MainS *interactiveView
|
||||
Navbar *tview.Table
|
||||
SearchBar *tview.Box
|
||||
ProgressBar *tview.Box
|
||||
@@ -28,8 +28,19 @@ func NewApplication() *Application {
|
||||
App := tview.NewApplication()
|
||||
Main := NewMain()
|
||||
pBar := tview.NewBox().SetBorder(true).SetTitle("PROGRESS").SetBackgroundColor(tcell.ColorDefault)
|
||||
mainS := tview.NewPages()
|
||||
searchbar := tview.NewBox().SetBorder(true).SetTitle("SEARCH").SetBackgroundColor(tcell.ColorDefault)
|
||||
SetCurrentView(PView)
|
||||
mainS := NewInteractiveView()
|
||||
mainS.View.SetBorder(true)
|
||||
|
||||
mainS.SetContentFunc(GetCurrentView().Content)
|
||||
mainS.SetContextKey(GetCurrentView().ContextKey())
|
||||
f := func() {
|
||||
GetCurrentView().ContextOpener(Main, mainS.SelectionHandler)
|
||||
}
|
||||
mainS.SetContextOpener(f)
|
||||
mainS.SetContextHandler(GetCurrentView().ContextHandler)
|
||||
mainS.SetExternalCapture(GetCurrentView().ExternalInputCapture)
|
||||
|
||||
Navbar := tview.NewTable()
|
||||
imagePreviewer := tview.NewBox()
|
||||
@@ -47,36 +58,34 @@ func NewApplication() *Application {
|
||||
App.Draw()
|
||||
}
|
||||
}
|
||||
Playlists, err := NewPlaylistNav(done)
|
||||
playlistNav, err := NewPlaylistNav(done)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Playlists.Table.SetBackgroundColor(tcell.ColorDefault)
|
||||
playlistNav.Table.SetBackgroundColor(tcell.ColorDefault)
|
||||
PlaylistActions = map[string]*Action{
|
||||
"playEntry": NewAction(Playlists.PlaySelectEntry, nil),
|
||||
"playEntry": NewAction(playlistNav.PlaySelectEntry, nil),
|
||||
"openEntry": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
|
||||
Main.AfterContextClose(func() { App.SetFocus(mainS) })
|
||||
p := NewPlaylistView(Main)
|
||||
r, _ := Playlists.Table.GetSelection()
|
||||
p.CurrentPlaylist = &(*Playlists.Playlists)[r]
|
||||
mainS.AddPage("PLAYLIST", p.I.View, true, true)
|
||||
App.SetFocus(mainS)
|
||||
Main.AfterContextClose(func() { App.SetFocus(mainS.View) })
|
||||
r, _ := playlistNav.Table.GetSelection()
|
||||
PView.SetPlaylist(&(*playlistNav.Playlists)[r])
|
||||
App.SetFocus(mainS.View)
|
||||
return nil
|
||||
}, nil),
|
||||
}
|
||||
Playlists.MapActions(map[tcell.Key]string{
|
||||
playlistNav.MapActions(map[tcell.Key]string{
|
||||
tcell.KeyEnter: "openEntry",
|
||||
})
|
||||
|
||||
searchNavFlex := tview.NewFlex().SetDirection(tview.FlexRow).
|
||||
AddItem(Navbar, 6, 3, false).
|
||||
AddItem(Playlists.Table, 0, 6, false).
|
||||
AddItem(playlistNav.Table, 0, 6, false).
|
||||
AddItem(imagePreviewer, 9, 3, false)
|
||||
|
||||
sNavExpViewFlex := tview.NewFlex().
|
||||
AddItem(searchNavFlex, 17, 1, false).
|
||||
AddItem(mainS, 0, 4, false)
|
||||
AddItem(mainS.View, 0, 4, false)
|
||||
|
||||
searchBarFlex := tview.NewFlex().SetDirection(tview.FlexRow).
|
||||
AddItem(searchbar, 3, 1, false).
|
||||
@@ -91,7 +100,7 @@ func NewApplication() *Application {
|
||||
|
||||
Main.Primitive("root", rootPages)
|
||||
App.EnableMouse(true)
|
||||
App.SetRoot(Main.Root, true).SetFocus(Playlists.Table)
|
||||
App.SetRoot(Main.Root, true).SetFocus(playlistNav.Table)
|
||||
|
||||
return &Application{
|
||||
App: App,
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"github.com/aditya-K2/gspt/spt"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/zmb3/spotify/v2"
|
||||
)
|
||||
|
||||
type PlaylistView struct {
|
||||
CurrentPlaylist *spotify.SimplePlaylist
|
||||
CurrentUserFullPlaylist *spt.Playlist
|
||||
I *interactiveView
|
||||
}
|
||||
|
||||
func NewPlaylistView(m *Main) *PlaylistView {
|
||||
i := NewInteractiveView()
|
||||
|
||||
i.View.SetBorder(true)
|
||||
p := &PlaylistView{nil, nil, i}
|
||||
content := func() [][]Content {
|
||||
c := make([][]Content, 0)
|
||||
if p.CurrentPlaylist != nil {
|
||||
if p.CurrentUserFullPlaylist == nil {
|
||||
pf, err := spt.GetPlaylist(p.CurrentPlaylist.ID, func(bool, error) {})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
p.CurrentUserFullPlaylist = pf
|
||||
}
|
||||
for _, v := range *(*p.CurrentUserFullPlaylist).Tracks {
|
||||
c = append(c, []Content{
|
||||
{Content: v.Track.Name, Style: Defaultstyle.Foreground(tcell.ColorBlue)},
|
||||
{Content: v.Track.Artists[0].Name, Style: Defaultstyle.Foreground(tcell.ColorPink)},
|
||||
{Content: v.Track.Album.Name, Style: Defaultstyle.Foreground(tcell.ColorGreen)},
|
||||
})
|
||||
}
|
||||
} else {
|
||||
return [][]Content{{
|
||||
{"HElll", tcell.StyleDefault},
|
||||
{"HElll", tcell.StyleDefault},
|
||||
{"HElll", tcell.StyleDefault},
|
||||
{"HElll", tcell.StyleDefault},
|
||||
}}
|
||||
}
|
||||
return c
|
||||
}
|
||||
i.SetContentFunc(content)
|
||||
contextOpener := func() {
|
||||
c := NewMenu()
|
||||
cc := []string{}
|
||||
p, err := spt.CurrentUserPlaylists(func(s bool, err error) {})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, v := range *(p) {
|
||||
cc = append(cc, v.Name)
|
||||
}
|
||||
c.Content(cc)
|
||||
c.Title("Add to Playlist")
|
||||
c.SetSelectionHandler(i.SelectionHandler)
|
||||
m.AddCenteredWidget(c)
|
||||
}
|
||||
i.SetContextKey('a')
|
||||
i.SetContextOpener(contextOpener)
|
||||
|
||||
contextHandler := func(start, end, sel int) {
|
||||
// Assuming that there are no external effects on the user's playlists
|
||||
// (i.e Any Creation or Deletion of Playlists while the context Menu is
|
||||
// open
|
||||
ap, err := spt.CurrentUserPlaylists(func(s bool, err error) {})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
p.CurrentPlaylist = &(*ap)[sel]
|
||||
tracks := make([]spotify.ID, 0)
|
||||
for k := start; k <= end; k++ {
|
||||
tracks = append(tracks, (*(*p.CurrentUserFullPlaylist).Tracks)[k].Track.ID)
|
||||
}
|
||||
if err := spt.AddTracksToPlaylist((*ap)[sel].ID, tracks...); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
i.SetContextHandler(contextHandler)
|
||||
i.SetExternalCapture(func(e *tcell.EventKey) *tcell.EventKey {
|
||||
if e.Key() == tcell.KeyEnter {
|
||||
r, _ := i.View.GetSelection()
|
||||
if err := spt.PlaySongWithContext(&p.CurrentPlaylist.URI, r); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
return e
|
||||
})
|
||||
|
||||
return p
|
||||
}
|
||||
Reference in New Issue
Block a user