2021-12-22 11:46:07 -07:00
|
|
|
package notify
|
2021-11-13 11:22:16 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2021-12-30 04:47:58 -07:00
|
|
|
"github.com/aditya-K2/gomp/ui"
|
|
|
|
|
2021-12-16 12:36:18 -07:00
|
|
|
"github.com/aditya-K2/gomp/utils"
|
2021-12-12 13:05:40 -07:00
|
|
|
|
2021-11-15 09:18:17 -07:00
|
|
|
"github.com/aditya-K2/tview"
|
2021-11-13 11:22:16 -07:00
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
)
|
|
|
|
|
2021-12-22 08:50:01 -07:00
|
|
|
var (
|
2021-12-22 11:46:07 -07:00
|
|
|
UI *ui.Application
|
2021-12-22 08:50:01 -07:00
|
|
|
)
|
|
|
|
|
2021-12-22 11:46:07 -07:00
|
|
|
func ConnectUI(a *ui.Application) {
|
2021-12-22 08:50:01 -07:00
|
|
|
UI = a
|
|
|
|
}
|
|
|
|
|
2021-11-13 11:22:16 -07:00
|
|
|
/* 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) {
|
2021-12-12 13:05:40 -07:00
|
|
|
termDetails := utils.GetWidth()
|
2021-11-13 11:22:16 -07:00
|
|
|
|
|
|
|
var (
|
|
|
|
COL int = int(termDetails.Col)
|
|
|
|
TEXTLENGTH int = len(self.Text)
|
|
|
|
HEIGHT int = 3
|
|
|
|
TEXTPOSITION int = 2
|
|
|
|
)
|
|
|
|
|
2021-12-30 04:47:58 -07:00
|
|
|
self.Box.SetBackgroundColor(tcell.ColorBlack)
|
2021-11-13 11:22:16 -07:00
|
|
|
self.SetRect(COL-(TEXTLENGTH+7), 1, TEXTLENGTH+4, HEIGHT)
|
|
|
|
self.DrawForSubclass(screen, self.Box)
|
|
|
|
tview.Print(screen, self.Text,
|
|
|
|
COL-(TEXTLENGTH+5), TEXTPOSITION, TEXTLENGTH,
|
2021-12-30 04:47:58 -07:00
|
|
|
tview.AlignCenter, tcell.ColorWhite)
|
2021-11-13 11:22:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
}
|