Removing Globals

Globals are no longer needed instead I am connecting each part with (
what were previously ) globals. Also Renaming SetRenderer to
ConnectRenderer which describes the name more precisely
This commit is contained in:
aditya-K2 2021-12-23 20:20:27 +05:30
parent b587f5acfd
commit f56eb1cf1f
2 changed files with 16 additions and 25 deletions

36
main.go
View File

@ -1,10 +1,11 @@
package main package main
import ( import (
"github.com/aditya-K2/gomp/ui/notify"
"strconv" "strconv"
"time" "time"
"github.com/aditya-K2/gomp/ui/notify"
"github.com/aditya-K2/gomp/render" "github.com/aditya-K2/gomp/render"
"github.com/aditya-K2/gomp/ui" "github.com/aditya-K2/gomp/ui"
@ -19,22 +20,11 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var (
CONN *mpd.Client
UI *ui.Application
Notify *notify.NotificationServer
RENDERER *render.Renderer
Volume int64
Random bool
Repeat bool
ArtistTree map[string]map[string]map[string]string
)
func main() { func main() {
config.ReadConfig() config.ReadConfig()
// Connect to MPD server // Connect to MPD server
var mpdConnectionError error var mpdConnectionError error
CONN, mpdConnectionError = mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT")) CONN, mpdConnectionError := mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT"))
if mpdConnectionError != nil { if mpdConnectionError != nil {
panic(mpdConnectionError) panic(mpdConnectionError)
} }
@ -47,16 +37,16 @@ func main() {
render.SetConnection(CONN) render.SetConnection(CONN)
cache.SetCacheDir(viper.GetString("CACHE_DIR")) cache.SetCacheDir(viper.GetString("CACHE_DIR"))
RENDERER = render.NewRenderer() Renderer := render.NewRenderer()
ui.SetRenderer(RENDERER) ui.ConnectRenderer(Renderer)
c, _ := CONN.CurrentSong() c, _ := CONN.CurrentSong()
if len(c) != 0 { if len(c) != 0 {
RENDERER.Start(c["file"]) Renderer.Start(c["file"])
} else { } else {
RENDERER.Start("stop") Renderer.Start("stop")
} }
UI = ui.NewApplication() UI := ui.NewApplication()
notify.ConnectUI(UI) notify.ConnectUI(UI)
fileMap, err := CONN.GetFiles() fileMap, err := CONN.GetFiles()
@ -65,13 +55,13 @@ func main() {
client.UpdatePlaylist(UI.ExpandedView) client.UpdatePlaylist(UI.ExpandedView)
_v, _ := CONN.Status() _v, _ := CONN.Status()
Volume, _ = strconv.ParseInt(_v["volume"], 10, 64) Volume, _ := strconv.ParseInt(_v["volume"], 10, 64)
Random, _ = strconv.ParseBool(_v["random"]) Random, _ := strconv.ParseBool(_v["random"])
Repeat, _ = strconv.ParseBool(_v["repeat"]) Repeat, _ := strconv.ParseBool(_v["repeat"])
ArtistTree, err = client.GenerateArtistTree() ArtistTree, err := client.GenerateArtistTree()
ArtistTreeContent := utils.ConvertToArray(ArtistTree) ArtistTreeContent := utils.ConvertToArray(ArtistTree)
Notify = notify.NewNotificationServer() Notify := notify.NewNotificationServer()
Notify.Start() Notify.Start()
client.SetNotificationServer(Notify) client.SetNotificationServer(Notify)
render.SetNotificationServer(Notify) render.SetNotificationServer(Notify)

View File

@ -2,9 +2,10 @@ package ui
import ( import (
"fmt" "fmt"
"github.com/fhs/gompd/mpd"
"strconv" "strconv"
"github.com/fhs/gompd/mpd"
"github.com/aditya-K2/gomp/utils" "github.com/aditya-K2/gomp/utils"
"github.com/aditya-K2/tview" "github.com/aditya-K2/tview"
@ -21,7 +22,7 @@ func SetConnection(c *mpd.Client) {
CONN = c CONN = c
} }
func SetRenderer(r interface{ Send(string) }) { func ConnectRenderer(r interface{ Send(string) }) {
RENDERER = r RENDERER = r
} }