From fdd390a7312122646643693beb27e75a05388769 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Tue, 13 Sep 2022 22:04:39 +0530 Subject: [PATCH] Fixing Slow down of Playlists mentioned @ #20 As mentioned at https://github.com/aditya-K2/gomp/issues/20#issuecomment-1142443829 The Slow Down was caused due to constant calls to the MPD Server for playlist info. Using Watcher to handle playlist event changes. Also when In SearchView upon adding Artist/Album due to constant change in playlists there was a slow down. Hence using CommandList for that. --- client/client.go | 24 +++++++++++-------- main.go | 4 +--- views/playlistview.go | 55 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 66 insertions(+), 17 deletions(-) diff --git a/client/client.go b/client/client.go index 56ee8b4..adbdca2 100644 --- a/client/client.go +++ b/client/client.go @@ -111,27 +111,31 @@ func PrintArtistTree(a map[string]map[string]map[string]string) { // Adds All tracks from a specified album to a playlist func AddAlbum(a map[string]map[string]map[string]string, alb string, artist string) { + clist := Conn.BeginCommandList() for _, v := range a[artist][alb] { - err := Conn.Add(v) - if err != nil { - notify.Notify.Send("Could Not Add Song : " + v) - } + clist.Add(v) + } + if err := clist.End(); err != nil { + notify.Notify.Send("Could Not Add Album : " + alb) + } else { + notify.Notify.Send("Album Added: " + alb) } - notify.Notify.Send("Album Added : " + alb) } // Adds All tracks from a specified artist to a playlist func AddArtist(a map[string]map[string]map[string]string, artist string) { + clist := Conn.BeginCommandList() if val, ok := a[artist]; ok { for _, v := range val { for _, path := range v { - err := Conn.Add(path) - if err != nil { - notify.Notify.Send("Could Not Add Song : " + path) - } + clist.Add(path) } } - notify.Notify.Send("Artist Added : " + artist) + if err := clist.End(); err != nil { + notify.Notify.Send("Could Not Add Artist : " + artist) + } else { + notify.Notify.Send("Artist Added: " + artist) + } } } diff --git a/main.go b/main.go index bf8b76c..2147a95 100644 --- a/main.go +++ b/main.go @@ -60,9 +60,6 @@ func main() { // Generating the Directory Tree for File Navigation. client.DirTree = client.GenerateDirectoryTree(fileMap) - // Default View upon Opening is of Playlist. - views.PView.Update(ui.Ui.ExpandedView) - var Volume int64 var Random, Repeat bool var SeekOffset = viper.GetInt("SEEK_OFFSET") @@ -120,6 +117,7 @@ func main() { // This Function Is Responsible for Changing the Focus it uses the Focus Map and Based on it Chooses // the Draw Function + views.PView.StartWatcher() views.SetCurrentView(views.PView) ui.Ui.ExpandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) { views.GetCurrentView().Update(ui.Ui.ExpandedView) diff --git a/views/playlistview.go b/views/playlistview.go index 8166994..9af3cb1 100644 --- a/views/playlistview.go +++ b/views/playlistview.go @@ -6,10 +6,13 @@ import ( "github.com/aditya-K2/gomp/ui" "github.com/aditya-K2/gomp/utils" "github.com/aditya-K2/tview" + "github.com/fhs/gompd/v2/mpd" "github.com/gdamore/tcell/v2" + "github.com/spf13/viper" ) type PlaylistView struct { + Playlist []mpd.Attrs } func (s PlaylistView) GetViewName() string { @@ -57,11 +60,12 @@ func (p PlaylistView) DeleteSongFromPlaylist() { } func (p PlaylistView) Update(inputTable *tview.Table) { - CONN := client.Conn - _playlistAttr, _ := CONN.PlaylistInfo(-1, -1) - inputTable.Clear() - for i, j := range _playlistAttr { + cplaylist := make([]mpd.Attrs, len(p.Playlist)) + for k, v := range p.Playlist { + cplaylist[k] = v + } + for i, j := range cplaylist { _, _, w, _ := inputTable.GetInnerRect() if j["Title"] == "" || j["Artist"] == "" || j["Album"] == "" { inputTable.SetCell(i, 0, @@ -80,3 +84,46 @@ func (p PlaylistView) Update(inputTable *tview.Table) { } } } + +func (p *PlaylistView) StartWatcher() { + var err error + if p.Playlist == nil { + if p.Playlist, err = client.Conn.PlaylistInfo(-1, -1); err != nil { + utils.Print("RED", "Watcher couldn't get the current Playlist.\n") + panic(err) + } + } + del := "" + nt := viper.GetString("NETWORK_TYPE") + port := viper.GetString("MPD_PORT") + if nt == "tcp" { + del = ":" + } else if nt == "unix" && port != "" { + port = "" + } + + w, err := mpd.NewWatcher(nt, + viper.GetString("NETWORK_ADDRESS")+del+port, "", "playlist") + if err != nil { + utils.Print("RED", "Could Not Start Watcher.\n") + utils.Print("GREEN", "Please check your MPD Info in config File.\n") + panic(err) + } + + go func() { + for err := range w.Error { + notify.Notify.Send(err.Error()) + } + }() + + go func() { + for subsystem := range w.Event { + if subsystem == "playlist" { + if p.Playlist, err = client.Conn.PlaylistInfo(-1, -1); err != nil { + utils.Print("RED", "Watcher couldn't get the current Playlist.\n") + panic(err) + } + } + } + }() +}