Merge pull request #7 from aditya-K2/notfiy

Notification Service
This commit is contained in:
Aditya Kurdunkar 2021-11-14 11:48:31 +05:30 committed by GitHub
commit 5012b2832b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 122 additions and 22 deletions

19
App.go
View File

@ -9,10 +9,11 @@ var IMG_X, IMG_Y, IMG_W, IMG_H int
type Application struct { type Application struct {
App *tview.Application App *tview.Application
expandedView *tview.Table ExpandedView *tview.Table
Navbar *tview.Table Navbar *tview.Table
searchBar *tview.Table SearchBar *tview.Table
pBar *progressBar ProgressBar *progressBar
Pages *tview.Pages
} }
func newApplication(r *Renderer) *Application { func newApplication(r *Renderer) *Application {
@ -51,15 +52,19 @@ func newApplication(r *Renderer) *Application {
expandedView.SetBorderPadding(1, 1, 1, 1).SetBorder(true) expandedView.SetBorderPadding(1, 1, 1, 1).SetBorder(true)
expandedView.SetSelectable(true, false) expandedView.SetSelectable(true, false)
rootPages := tview.NewPages()
rootPages.AddPage("Main", mainFlex, true, true)
App := tview.NewApplication() App := tview.NewApplication()
App.SetRoot(mainFlex, true).SetFocus(expandedView) App.SetRoot(rootPages, true).SetFocus(expandedView)
return &Application{ return &Application{
App: App, App: App,
expandedView: expandedView, ExpandedView: expandedView,
Navbar: Navbar, Navbar: Navbar,
searchBar: searchBar, SearchBar: searchBar,
pBar: pBar, ProgressBar: pBar,
Pages: rootPages,
} }
} }

37
main.go
View File

@ -11,6 +11,7 @@ import (
) )
var CONN *mpd.Client var CONN *mpd.Client
var UI *Application
var Volume int64 var Volume int64
var Random bool var Random bool
var Repeat bool var Repeat bool
@ -34,36 +35,39 @@ func main() {
r.Start("stop") r.Start("stop")
} }
UI := newApplication(r) UI = newApplication(r)
fileMap, err := CONN.GetFiles() fileMap, err := CONN.GetFiles()
dirTree := generateDirectoryTree(fileMap) dirTree := generateDirectoryTree(fileMap)
UpdatePlaylist(UI.expandedView) 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"])
UI.expandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) { UI.ExpandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) {
if InsidePlaylist { if InsidePlaylist {
UpdatePlaylist(UI.expandedView) UpdatePlaylist(UI.ExpandedView)
} else { } else {
Update(dirTree.children, UI.expandedView) Update(dirTree.children, UI.ExpandedView)
} }
return UI.expandedView.GetInnerRect() return UI.ExpandedView.GetInnerRect()
}) })
notificationServer := NewNotificationServer()
notificationServer.Start()
var FUNC_MAP = map[string]func(){ var FUNC_MAP = map[string]func(){
"showChildrenContent": func() { "showChildrenContent": func() {
r, _ := UI.expandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
if !InsidePlaylist { if !InsidePlaylist {
if len(dirTree.children[r].children) == 0 { if len(dirTree.children[r].children) == 0 {
id, _ := CONN.AddId(dirTree.children[r].absolutePath, -1) id, _ := CONN.AddId(dirTree.children[r].absolutePath, -1)
CONN.PlayId(id) CONN.PlayId(id)
} else { } else {
Update(dirTree.children[r].children, UI.expandedView) Update(dirTree.children[r].children, UI.ExpandedView)
dirTree = &dirTree.children[r] dirTree = &dirTree.children[r]
} }
} else { } else {
@ -76,7 +80,7 @@ func main() {
"showParentContent": func() { "showParentContent": func() {
if !InsidePlaylist { if !InsidePlaylist {
if dirTree.parent != nil { if dirTree.parent != nil {
Update(dirTree.parent.children, UI.expandedView) Update(dirTree.parent.children, UI.ExpandedView)
dirTree = dirTree.parent dirTree = dirTree.parent
} }
} }
@ -87,15 +91,16 @@ func main() {
"clearPlaylist": func() { "clearPlaylist": func() {
CONN.Clear() CONN.Clear()
if InsidePlaylist { if InsidePlaylist {
UpdatePlaylist(UI.expandedView) UpdatePlaylist(UI.ExpandedView)
} }
notificationServer.Send("PlayList Cleared")
}, },
"previousSong": func() { "previousSong": func() {
CONN.Previous() CONN.Previous()
}, },
"addToPlaylist": func() { "addToPlaylist": func() {
if !InsidePlaylist { if !InsidePlaylist {
r, _ := UI.expandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
CONN.Add(dirTree.children[r].absolutePath) CONN.Add(dirTree.children[r].absolutePath)
} }
}, },
@ -130,12 +135,12 @@ func main() {
"navigateToFiles": func() { "navigateToFiles": func() {
InsidePlaylist = false InsidePlaylist = false
UI.Navbar.Select(1, 0) UI.Navbar.Select(1, 0)
Update(dirTree.children, UI.expandedView) Update(dirTree.children, UI.ExpandedView)
}, },
"navigateToPlaylist": func() { "navigateToPlaylist": func() {
InsidePlaylist = true InsidePlaylist = true
UI.Navbar.Select(0, 0) UI.Navbar.Select(0, 0)
UpdatePlaylist(UI.expandedView) UpdatePlaylist(UI.ExpandedView)
}, },
"navigateToMostPlayed": func() { "navigateToMostPlayed": func() {
InsidePlaylist = false InsidePlaylist = false
@ -146,16 +151,18 @@ func main() {
}, },
"stop": func() { "stop": func() {
CONN.Stop() CONN.Stop()
notificationServer.Send("Playback Stopped")
}, },
"updateDB": func() { "updateDB": func() {
_, err = CONN.Update("") _, err = CONN.Update("")
if err != nil { if err != nil {
panic(err) panic(err)
} }
notificationServer.Send("Database Updated")
}, },
"deleteSongFromPlaylist": func() { "deleteSongFromPlaylist": func() {
if InsidePlaylist { if InsidePlaylist {
r, _ := UI.expandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
CONN.Delete(r, -1) CONN.Delete(r, -1)
} }
}, },
@ -163,7 +170,7 @@ func main() {
config.GenerateKeyMap(FUNC_MAP) config.GenerateKeyMap(FUNC_MAP)
UI.expandedView.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey { UI.ExpandedView.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
if val, ok := config.KEY_MAP[int(e.Rune())]; ok { if val, ok := config.KEY_MAP[int(e.Rune())]; ok {
FUNC_MAP[val]() FUNC_MAP[val]()
return nil return nil

88
notification.go Normal file
View File

@ -0,0 +1,88 @@
package main
import (
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
/* Notification Primitive */
type Notification struct {
*tview.Box
Text string
}
/* Get A Pointer to A Notification Struct */
func NewNotification(s string) *Notification {
return &Notification{
Box: tview.NewBox(),
Text: s,
}
}
/* Draw Function for the Notification Primitive */
func (self *Notification) Draw(screen tcell.Screen) {
termDetails := getWidth()
var (
COL int = int(termDetails.Col)
TEXTLENGTH int = len(self.Text)
HEIGHT int = 3
TEXTPOSITION int = 2
)
self.Box.SetBackgroundColor(tcell.GetColor("#15191a"))
self.SetRect(COL-(TEXTLENGTH+7), 1, TEXTLENGTH+4, HEIGHT)
self.DrawForSubclass(screen, self.Box)
tview.Print(screen, self.Text,
COL-(TEXTLENGTH+5), TEXTPOSITION, TEXTLENGTH,
tview.AlignCenter, tcell.GetColor("#ffffff"))
}
/* Notification Server : Not an actual Server*/
type NotificationServer struct {
c chan string
}
/* Get A Pointer to a NotificationServer Struct */
func NewNotificationServer() *NotificationServer {
return &NotificationServer{
c: make(chan string),
}
}
/* This Method Starts the go routine for the NotificationServer */
func (self *NotificationServer) Start() {
go NotificationRoutine(self.c, "EMPTY NOTIFICATION")
}
/* The Notification Server is just a string channel and the NotificationRoutine
the channel is used to receive the Notification Data through the Send Function
The Channel keeps listening for the Notification when it receives a Notification it checks if it
is Empty or not if it is an empty Notification it calls the NotificationRoutine with the empty routine else
it will call the go routine that renders the Notification for the Notification Interval and agains start listening
for the notfications sort of works like a que */
func NotificationRoutine(c chan string, s string) {
if s != "EMPTY NOTIFICATION" {
go func() {
currentTime := time.Now().String()
UI.Pages.AddPage(currentTime, NewNotification(s), false, true)
UI.App.SetFocus(UI.ExpandedView)
time.Sleep(time.Second * 1)
UI.Pages.RemovePage(currentTime)
UI.App.SetFocus(UI.ExpandedView)
}()
}
NewNotification := <-c
if NewNotification == "EMPTY NOTIFICATION" {
NotificationRoutine(c, "EMPTY NOTIFICATION")
} else {
NotificationRoutine(c, NewNotification)
}
}
/* Sends the Notification to the Notification Server */
func (self NotificationServer) Send(text string) {
self.c <- text
}