Initial Implementation of Views

This commit is contained in:
aditya-K2
2022-08-31 23:42:15 +05:30
parent e86ceaca6a
commit ff74bf02ba
11 changed files with 488 additions and 333 deletions

103
views/buffsearchview.go Normal file
View File

@@ -0,0 +1,103 @@
package views
import (
"fmt"
"github.com/aditya-K2/gomp/globals"
"github.com/aditya-K2/gomp/utils"
"github.com/aditya-K2/tview"
"github.com/gdamore/tcell/v2"
)
type BuffSearchView struct {
}
func (s BuffSearchView) GetViewName() string {
return "BuffSearchView"
}
func (s BuffSearchView) ShowChildrenContent() {
UI := globals.Ui
CONN := globals.Conn
r, _ := UI.ExpandedView.GetSelection()
SetCurrentView(FView)
if len(globals.DirTree.Children[r].Children) == 0 {
if id, err := CONN.AddId(globals.DirTree.Children[globals.Matches[r].Index].AbsolutePath, -1); err != nil {
globals.Notify.Send(fmt.Sprintf("Could Not add the Song %s to the Playlist",
globals.DirTree.Children[globals.Matches[r].Index].AbsolutePath))
} else {
if err := CONN.PlayId(id); err != nil {
globals.Notify.Send("Could not Play the Song")
}
}
} else {
globals.DirTree = &globals.DirTree.Children[globals.Matches[r].Index]
FView.Update(UI.ExpandedView)
}
UI.SearchBar.SetText("")
// Resetting globals.Matches
globals.Matches = nil
}
func (s BuffSearchView) ShowParentContent() {
globals.Notify.Send("Not Allowed in this View")
return
}
func (s BuffSearchView) AddToPlaylist() {
UI := globals.Ui
CONN := globals.Conn
r, _ := UI.ExpandedView.GetSelection()
if err := CONN.Add(globals.DirTree.Children[globals.Matches[r].Index].AbsolutePath); err != nil {
globals.Notify.Send(fmt.Sprintf("Could Not Add URI %s to the Playlist",
globals.DirTree.Children[globals.Matches[r].Index].Path))
} else {
SetCurrentView(FView)
globals.Notify.Send(fmt.Sprintf("URI Added %s to the Playlist",
globals.DirTree.Children[globals.Matches[r].Index].Path))
SetCurrentView(BuffSView)
}
}
func (s BuffSearchView) Quit() {
UI := globals.Ui
SetCurrentView(FView)
UI.SearchBar.SetText("")
globals.Matches = nil
}
func (f BuffSearchView) FocusBuffSearchView() {
UI := globals.Ui
SetCurrentView(BuffSView)
UI.App.SetFocus(UI.SearchBar)
}
func (f BuffSearchView) DeleteSongFromPlaylist() {}
func (s BuffSearchView) Update(inputTable *tview.Table) {
m := globals.Matches
f := globals.DirTree.Children
inputTable.Clear()
if m == nil || len(m) == 0 {
FView.Update(inputTable)
} else {
for k, v := range m {
if len(f[v.Index].Children) != 0 {
inputTable.SetCell(k, 0,
GetCell(
utils.GetMatchedString(
utils.Unique(v.MatchedIndexes), f[v.Index].Path, "[blue:-:bi]"),
tcell.ColorYellow, true))
} else {
inputTable.SetCell(k, 0,
GetCell(
utils.GetMatchedString(
utils.Unique(v.MatchedIndexes), f[v.Index].Title, "[yellow:-:bi]"),
tcell.ColorGreen, true))
}
if k == 15 {
break
}
}
}
}

97
views/fileView.go Normal file
View File

@@ -0,0 +1,97 @@
package views
import (
"fmt"
"github.com/aditya-K2/gomp/globals"
"github.com/aditya-K2/gomp/utils"
"github.com/aditya-K2/tview"
"github.com/gdamore/tcell/v2"
)
type FileView struct {
}
func (f FileView) GetViewName() string {
return "FileView"
}
func (f FileView) ShowChildrenContent() {
UI := globals.Ui
CONN := globals.Conn
r, _ := UI.ExpandedView.GetSelection()
SetCurrentView(FView)
if len(globals.DirTree.Children[r].Children) == 0 {
if id, err := CONN.AddId(globals.DirTree.Children[r].AbsolutePath, -1); err != nil {
globals.Notify.Send(fmt.Sprintf("Could not Add Song %s",
globals.DirTree.Children[r].Path))
} else {
if err := CONN.PlayId(id); err != nil {
globals.Notify.Send(fmt.Sprintf("Could Not Play Song %s",
globals.DirTree.Children[r].Path))
}
}
} else {
globals.DirTree = &globals.DirTree.Children[r]
FView.Update(UI.ExpandedView)
UI.ExpandedView.Select(0, 0)
}
}
func (f FileView) ShowParentContent() {
UI := globals.Ui
if globals.DirTree.Parent != nil {
globals.DirTree = globals.DirTree.Parent
FView.Update(UI.ExpandedView)
}
}
func (f FileView) AddToPlaylist() {
UI := globals.Ui
CONN := globals.Conn
r, _ := UI.ExpandedView.GetSelection()
if err := CONN.Add(globals.DirTree.Children[r].AbsolutePath); err != nil {
globals.Notify.Send(fmt.Sprintf("Could not add %s to the Playlist",
globals.DirTree.Children[r].Path))
}
}
func (f FileView) Quit() {
globals.Ui.App.Stop()
}
func (f FileView) FocusBuffSearchView() {
UI := globals.Ui
SetCurrentView(BuffSView)
UI.App.SetFocus(UI.SearchBar)
}
func (f FileView) DeleteSongFromPlaylist() {}
func (f FileView) Update(inputTable *tview.Table) {
inputTable.Clear()
for i, j := range globals.DirTree.Children {
if len(j.Children) == 0 {
_songAttributes, err := globals.Conn.ListAllInfo(j.AbsolutePath)
if err == nil && _songAttributes[0]["Title"] != "" {
_, _, w, _ := inputTable.GetInnerRect()
inputTable.SetCell(i, 0,
GetCell(
utils.GetFormattedString(_songAttributes[0]["Title"], w/3), tcell.ColorGreen, false))
inputTable.SetCell(i, 1,
GetCell(
utils.GetFormattedString(_songAttributes[0]["Artist"], w/3), tcell.ColorPurple, false))
inputTable.SetCell(i, 2,
GetCell(_songAttributes[0]["Album"], tcell.ColorYellow, false))
} else if _songAttributes[0]["Title"] == "" {
inputTable.SetCell(i, 0,
GetCell(j.Path, tcell.ColorBlue, true))
}
} else {
inputTable.SetCell(i, 0,
GetCell(j.Path, tcell.ColorYellow, true))
}
}
}

80
views/playlistview.go Normal file
View File

@@ -0,0 +1,80 @@
package views
import (
"github.com/aditya-K2/gomp/globals"
"github.com/aditya-K2/gomp/utils"
"github.com/aditya-K2/tview"
"github.com/gdamore/tcell/v2"
)
type PlaylistView struct {
}
func (s PlaylistView) GetViewName() string {
return "PlaylistView"
}
func GetCell(text string, foreground tcell.Color, bold bool) *tview.TableCell {
return tview.NewTableCell(text).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(foreground).
Background(tcell.ColorBlack).
Bold(bold))
}
func (p PlaylistView) ShowChildrenContent() {
UI := globals.Ui
CONN := globals.Conn
r, _ := UI.ExpandedView.GetSelection()
if err := CONN.Play(r); err != nil {
globals.Notify.Send("Could Not Play the Song")
return
}
}
func (s PlaylistView) ShowParentContent() {
globals.Notify.Send("Not Allowed in this View")
return
}
func (p PlaylistView) AddToPlaylist() {}
func (p PlaylistView) Quit() {
globals.Ui.App.Stop()
}
func (p PlaylistView) FocusBuffSearchView() {}
func (p PlaylistView) DeleteSongFromPlaylist() {
UI := globals.Ui
CONN := globals.Conn
r, _ := UI.ExpandedView.GetSelection()
if err := CONN.Delete(r, -1); err != nil {
globals.Notify.Send("Could not Remove the Song from Playlist")
}
}
func (p PlaylistView) Update(inputTable *tview.Table) {
CONN := globals.Conn
_playlistAttr, _ := CONN.PlaylistInfo(-1, -1)
inputTable.Clear()
for i, j := range _playlistAttr {
_, _, w, _ := inputTable.GetInnerRect()
if j["Title"] == "" || j["Artist"] == "" || j["Album"] == "" {
inputTable.SetCell(i, 0,
GetCell(
utils.GetFormattedString(j["file"], w/3), tcell.ColorBlue, true))
} else {
inputTable.SetCell(i, 0,
GetCell(
utils.GetFormattedString(j["Title"], w/3), tcell.ColorGreen, false))
inputTable.SetCell(i, 1,
GetCell(
utils.GetFormattedString(j["Artist"], w/3), tcell.ColorPurple, false))
inputTable.SetCell(i, 2,
GetCell(j["Album"], tcell.ColorYellow, false))
}
}
}

86
views/searchview.go Normal file
View File

@@ -0,0 +1,86 @@
package views
import (
"fmt"
"strings"
"github.com/aditya-K2/gomp/client"
"github.com/aditya-K2/gomp/globals"
"github.com/aditya-K2/gomp/utils"
"github.com/aditya-K2/tview"
"github.com/gdamore/tcell/v2"
)
type SearchView struct {
}
func (s SearchView) GetViewName() string {
return "SearchView"
}
func (s SearchView) ShowChildrenContent() {
UI := globals.Ui
SearchContentSlice := globals.SearchContentSlice
r, _ := UI.ExpandedView.GetSelection()
client.AddToPlaylist(SearchContentSlice[r], true)
}
func (s SearchView) ShowParentContent() {
fmt.Println(s)
globals.Notify.Send("Not Allowed in this View")
return
}
func (s SearchView) AddToPlaylist() {
UI := globals.Ui
SearchContentSlice := globals.SearchContentSlice
r, _ := UI.ExpandedView.GetSelection()
client.AddToPlaylist(SearchContentSlice[r], false)
}
func (p SearchView) Quit() {
globals.Ui.App.Stop()
}
func (s SearchView) FocusBuffSearchView() {}
func (s SearchView) DeleteSongFromPlaylist() {}
func (s SearchView) Update(inputTable *tview.Table) {
inputTable.Clear()
c := globals.SearchContentSlice
_, _, width, _ := inputTable.GetInnerRect()
for i, content := range c {
switch content.(type) {
case [3]string:
{
inputTable.SetCell(i, 0,
GetCell(
utils.GetFormattedString(content.([3]string)[0], width/3), tcell.ColorGreen, false))
inputTable.SetCell(i, 1,
GetCell(
utils.GetFormattedString(content.([3]string)[1], width/3), tcell.ColorPurple, false))
inputTable.SetCell(i, 2,
GetCell(content.([3]string)[2], tcell.ColorYellow, false))
}
case [2]string:
{
inputTable.SetCell(i, 0,
GetCell(
utils.GetFormattedString(content.([2]string)[0], width/3), tcell.ColorYellow, false))
inputTable.SetCell(i, 1,
GetCell(
utils.GetFormattedString(content.([2]string)[1], width/3), tcell.ColorPurple, false))
}
case string:
{
b := content.(string)
if !strings.HasPrefix(b, client.WHITE_AND_BOLD) {
inputTable.SetCell(i, 0,
GetCell(content.(string), tcell.ColorPurple, false))
} else {
inputTable.SetCell(i, 0,
GetCell(content.(string), tcell.ColorWhite, true).SetSelectable(false))
}
}
}
}
}

32
views/views.go Normal file
View File

@@ -0,0 +1,32 @@
package views
import (
"github.com/aditya-K2/tview"
)
var (
CurrentView View
BuffSView BuffSearchView
SView SearchView
FView FileView
PView PlaylistView
)
type View interface {
Update(inputTable *tview.Table)
ShowChildrenContent()
ShowParentContent()
AddToPlaylist()
Quit()
FocusBuffSearchView()
DeleteSongFromPlaylist()
GetViewName() string
}
func SetCurrentView(v View) {
CurrentView = v
}
func GetCurrentView() View {
return CurrentView
}